在组件中,我正在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;
};
该测试为何失败?在React Native中窥探componentWillMount
的正确方法是什么?
答案 0 :(得分:2)
const newProps: ComponentProps = {
...defaultProps,
foo,
};
foo
应该倒在其他道具上