reactjs - 在已安装的酶测试中未调用 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);
});
解决方案
推荐阅读
- java - 如何验证每个 json 数组对象?
- node.js - 如何跨日期列从 postgreSQL 数据库中删除 gmt?
- javascript - Scriptable (iOS) - 在 Safari 中获取网站的 HTML
- lua - Splash render.html 不可读,render.png 为空白
- angular - 使用 Angular 12 实现可连接插件
- javascript - 只运行一次通知脚本,即使脚本在多个选项卡上运行
- wordpress - 属性的下拉功能 // Woocommerce
- jquery - 博主下拉菜单消失 20
- python - Databricks - 如何将 accessToken 传递给 spark._sc._gateway.jvm.java.sql.DriverManager?
- ios - 如果 iPhone 支持 NFC,如何签入 Swift?