reactjs - 我有一个 JSX.Element 的道具。我如何在玩笑测试中模拟相同的东西
问题描述
我是我的反应组件,我有像下面这样的道具
interface Props {
errorMessage: JSX.Element;
addCommentCancel: () => void;
triggerAddComment: () => void;
showAddCommentForm: boolean;
}
在测试中我正在调用如下组件
const props = {
errorMessage: {},
addCommentCancel: () => jest.fn(),
triggerAddComment: () => jest.fn(),
showAddCommentForm: true
};
let wrapper = mount(<IncidentCommentList {...props} />);
我收到一个错误
Type '{}' is not assignable to type 'Element'.
如何模拟道具元素?
解决方案
const props = {
errorMessage: <div>errorMessage</div>,
addCommentCancel: () => jest.fn(),
triggerAddComment: () => jest.fn(),
showAddCommentForm: true
};
let wrapper = mount(<IncidentCommentList {...props} />);
上面的代码解决了这个问题。
推荐阅读
- java - 单击视频的缩略图并播放视频
- c++ - 从函数指针调用方法
- java - 下载 .csv 文件 Java6
- angular - 如何创建和更新可观察对象,这取决于 @Input
- c++ - 如何使用 sscanf 将字符串从文件解析为结构?
- asp.net-mvc - 在同一个域上运行 Asp.net MVC 和 React
- python-3.x - 数据集 API RecursionError:超出最大递归深度
- python - 根据关系模型值自动创建模型对象
- sql - 有没有办法提高在 select 子句中使用多个子查询的查询的性能
- google-apps-script - 如何用 mailto 替换变量:google doc apps 脚本中的链接