我的组件有:
class Search extends Component {
constructor(props) {
super(props);
this.state = {
searchTerm:
typeof this.props.match.params.searchTerm !== "undefined"
? this.props.match.params.searchTerm
: ""
};
}
测试是:
test("Search should render correct amount of shows", () => {
const component = shallow(<Search shows={preload.shows} />);
expect(component.find(ShowCard).length).toEqual(preload.shows.length);
});
我懂了
TypeError:无法读取未定义的属性“ params”
如何解决该问题或如何在测试中设置查询参数?
似乎在测试之外时,Search
组件会match
正确接收道具。
在测试中进行浅层渲染时,可以将其作为道具传递:
test("Search should render correct amount of shows", () => {
const match = { params: { searchTerm: 'foo' } }
const component = shallow(<Search shows={preload.shows} match={match}/>);
expect(component.find(ShowCard).length).toEqual(preload.shows.length);
});
在那种情况下,您并没有以不好的方式更改被测组件,您的测试用例只是发现了一个错误,这很好,应该针对测试,并且您通过实现默认的道具来改进了该组件,更强大。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句