模拟传递给子组件的道具

时间:2018-10-12 11:55:13

标签: reactjs redux jestjs redux-form

我是开玩笑的新手。我的组件看起来像这样。

class ComponentToTest extends React.Component {
    searchName = () => {
        return axios.get(...)
    }
    render() {
        return(
            <Form onSubmit={handleSubmit(save)}>
                <SomeChildComponent searchName={ (searchTerm: string) => this.searchName(searchTerm)} />
            </Form>
        )
    }
}


`ComponentToTest.test.js`
it('should call `handleSubmit` with `save`', () => {
    const store = createStore((state) => state, { app: {} });
    const onSave = jest.fn().mockReturnValue(() => {});
    const props: any = { handleSubmit: onSave };
    const tree = mount(
        <Provider store={store}>
            <ComponentToTest {...props} />
        </Provider>,
    );

    tree.find('button').simulate('submit');
    expect(onSave).toHaveBeenCalled();
});

运行此测试后出现错误。有什么方法可以模拟对searchName的调用并返回promise?

0 个答案:

没有答案