在componentWillMount内部进行方法调用的笑话测试失败

时间:2019-01-31 20:48:10

标签: reactjs unit-testing react-native jestjs

在组件中,我正在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的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

        const newProps: ComponentProps = {
            ...defaultProps,
            foo,
        };

foo应该倒在其他道具上

相关问题