首页 > 解决方案 > 在已安装的酶测试中未调用 componentDidUpdate

问题描述

我正在尝试测试以下组件:

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);
});  

标签: reactjsjestjsenzyme

解决方案


推荐阅读