reactjs - 如何在单元测试的componentwillreceiveprops中触发if(this.props!==nextProps)?
问题描述
我是 React 单元测试的新手。我对 if 语句有疑问。我需要测试 componentwillreceiveprops 的功能,我知道我可以使用 spy = sinon.stub(component.props,"componentwillreceiveprops") 方法让 spy 被调用。
但是在我的组件中,有一个 if 语句,比如
componentwillreceiveprops(nextProps){
if(this.props.name!==nextProps.name) {
this.setState({
oldstate: newstate
})
}
}
我可以componentWillReceiveProps
调用函数。如果我删除 if 语句,也可以调用 setState 函数。但是我的组件中包含一个 if 语句,当我运行单元测试时它不会被调用。有什么方法可以调用这个 if 语句?先感谢您。
解决方案
componentwillreceiveprops
应该componentWillReceiveProps
推荐阅读
- laravel - Laravel Eloquent:动态地从一种方法转移到另一种方法
- python - ValueError: max() arg 是 NLP 中的空序列
- flutter - Flutter Web 如何将站点发布和托管到根文件夹以外的位置
- php - 无论我把它放在代码中的什么位置,password_hash 都不起作用,我无法弄清楚?
- ffmpeg - 使用 ffmeg 将 file.ts 转换为 file.aac
- sql-server - 如何使用 Azure 数据工厂从 SQL Server 读取 XML 列数据
- sql - SQL查询从同一个表中获取不同的值
- c++ - 如果整个 cpp 文件被命名空间包围,有什么方法可以显示 C++ 函数列表?
- swift - 解决多个数据库问题
- dialogflow-cx - DialogFlow CX 与 SQL Server 集成