我正在尝试测试以下组件:
componentDidUpdate(prevProps, prevState, snapshot){
if (this.props.location.pathname.split('/')[1] !== prevProps.location.pathname.split('/')[1]) {
for(var i in this.props.moduleList) {
if(this.props.moduleList[i].Name.replace(/\ /g,'') === this.props.location.pathname.split('/')[1]) {
this.props.setModule(this.props.moduleList[i], null);
}
}
}
}
在对stackoverflow进行了一些研究之后,遇到了这个答案,但是出现了以下错误:
expect(received)[.not].toHaveBeenCalled()
这是测试文件
beforeEach(() => wrapper = mount(<BrowserRouter><Component {...baseProps} /></BrowserRouter>));
it(`should call the 'setModule' function when 'moduleList.Name' prop changes`, () => {
// test that the setModule wasn't called yet
expect(setModule).not.toHaveBeenCalled();
// now update the prop
wrapper.setProps({ location: { ...baseProps.moduleList, Name: "/otherpath" } });
// now check that the setModule was called
expect(setModule).toHaveBeenCalled(baseProps.moduleList);
});