reactjs - componentWillMount 内部方法调用的 Jest 测试失败
问题描述
在我的组件中,我在this.props.foo()
里面调用componentWillMount
:
public componentWillMount() {
this.props.foo();
}
现在我想测试一下这个方法是否被调用,开玩笑:
it("should call startCountdown when mounted.", () => {
const foo= jest.fn();
const newProps: ComponentProps = {
foo,
...defaultProps,
};
renderComponent(newProps);
expect(foo).toHaveBeenCalled();
});
renderComponent
做这个:
const renderComponent= (props: ComponentProps = defaultProps) => {
const rendered = TestRenderer.create(
<Component {...props}/>);
return rendered.root;
};
这个测试怎么会失败?componentWillMount
在 React Native中监视的正确方法是什么?
解决方案
const newProps: ComponentProps = {
...defaultProps,
foo,
};
foo
应该最后覆盖其他道具
推荐阅读
- html - 如何使用 Jsoup 从 HTML 中获取“img src 链接”?
- reactjs - 我将 value 和 onChange 都传递给受控输入元素,为什么会出现 Failed Prop Type 错误?
- python - 如何使用函数更改数据?
- regex - SQLite 正则表达式查找字符串不在预定义集中的行
- c# - 在 Microsoft Unity DI 中注册所有类
- c# - 如何避免对订阅方法的责任
- angular - 无法读取未定义 Ionic 3 的属性“推送”
- c# - C# 循环 x 毫秒
- android - Firebase 数据库通知发送到一台设备 Android Studio
- java - 是否有运行 docker 应用程序的 maven 命令?