javascript - 在 react js 中进行单元测试时,有没有办法改变 props 值?
问题描述
我正在为我的反应代码编写开玩笑的单元测试。firstName 是我从 PersonInfo.mock.js 获得的数据
import { firstName } from './PersonInfo.mock.js'
const PrintName = () => {
const FirstName = () => {
if ( firstName.length === 0 ) {
return <h1>No Name Found</h1>
}else{
return <h2>{firstName}</h2>
}
};
return <>{FirstName()}</>
};
我尝试使用 setProps,但似乎无法更改为 firstName 的值。
describe('<PrintName/>', () => {
const testContainer = shallow(<PrintName />);
testContainer.setProps( {firstName : []} );
it('should print No Name Found', () => {
expect(testContainer.find('h1').exists()).toBeTruthy();
});
知道如何更改/模拟我的名字的值吗?
解决方案
推荐阅读
- reactjs - prevProps 和 props 是一样的
- prolog - squares/2的递归实现
- mysql - 在 MySQL 中返回每组的前 N 行,但效率很高
- python - @commands.has permissions(administrator=true) 命令没有属性 has_permisions 错误
- python - Pandas:通过迭代现有列中的值来填充数据框中的特定列以计算重复值的数量
- c# - C# 中按 2 列和 Max() 其他列分组
- java - Jnius Android 蓝牙低功耗
- create-react-app - 为 create-react-app 开发禁用 CSP
- c# - 使用 IRequestExecutor 在 .Net 中模拟身份验证
- c# - 如何使用 HTML Agility Pack 在 href 中添加数据