如何在酶测试案例中通过重定向的道具

时间:2019-09-09 07:31:02

标签: reactjs jestjs enzyme

我已经编写了一个测试案例,用于对一组组件(PasswordReset)进行快照测试。在我的代码中,我正在重定向中传递一个道具,像这样-

<Redirect to={{pathname: '/forgotPassword', state: this.state.email}} />

我想在测试用例中传递相同的Redirect标签道具。问题在于PasswordReset组件使用此道具在构造函数中初始化状态下的值。因此,我的测试用例未能说出-

Cannot read property 'state' of undefined

我已经搜索并找到了一些有关如何为Routes编写测试用例的答案,但是我没有针对此问题的任何解决方案。我的测试用例是这样-

it ("Check if rendering properly", () => {
    const wrapper = mount (
        <MemoryRouter initialEntries={['/forgotPassword']}>
            <PasswordReset />
        </MemoryRouter>
    );

    expect(wrapper.find(PasswordReset)).toMatchSnapshot();
});

对不起,我是React新手。我想知道如何通过这个道具。

0 个答案:

没有答案