我的组件有:
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);
});
I get
类型错误:无法读取未定义的属性“params”
我该如何解决这个问题或如何在我的测试中设置查询参数?
好像在考试之外,Search
组件接收到match
道具正确。
在测试中进行浅层渲染时,您可以将其作为 props 传递:
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);
});
在这种情况下,您不会以不好的方式更改被测组件,您的测试用例只是发现了一个错误,这是好的并且应该针对测试,并且您通过实现默认道具来改进组件,使其成为可能更坚固。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)