Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在点击按钮后与MemoryRouter反应中测试导航(笑话,酶)_Reactjs_Testing_Jestjs_Router_Enzyme - Fatal编程技术网

Reactjs 在点击按钮后与MemoryRouter反应中测试导航(笑话,酶)

Reactjs 在点击按钮后与MemoryRouter反应中测试导航(笑话,酶),reactjs,testing,jestjs,router,enzyme,Reactjs,Testing,Jestjs,Router,Enzyme,因此,我尝试使用MemoryRouter进行测试,如果我单击一个按钮,该按钮应该将我带到一个新的路线,它实际上会打开该路线 因此,“ConversationView”是会话中的react组件。我-希望能够测试,当我点击位于“对话”中的按钮时,我将能够到达新路线“对话视图”,并在其中找到“h2”标记 这是我的代码: const wrapper = mount( <MemoryRouter initialEntries={[ "/" ]}> <Conversatio

因此,我尝试使用MemoryRouter进行测试,如果我单击一个按钮,该按钮应该将我带到一个新的路线,它实际上会打开该路线

因此,“ConversationView”是会话中的react组件。我-希望能够测试,当我点击位于“对话”中的按钮时,我将能够到达新路线“对话视图”,并在其中找到“h2”标记

这是我的代码:

const wrapper = mount(
   <MemoryRouter initialEntries={[ "/" ]}>
     <Conversations.wrappedComponent store={store}>
       <ConversationView>
       </ConversationView>
     </Conversations.wrappedComponent>
   </MemoryRouter>);

   const button = wrapper.find("button").first();
   button.simulate("click");
   expect(wrapper.find("h2")).toHaveLength(1);
const wrapper=mount(
);
const button=wrapper.find(“button”).first();
按钮。模拟(“单击”);
expect(wrapper.find(“h2”).toHaveLength(1);
我做错了什么?我应该如何测试这样的场景

另外,我正在给对话注入一个MobX商店。但是我模拟了存储,因此没有额外的异步工作。

尝试更改expact()结果:

或者,您可以尝试将道具设置为包装器,如下所示:

const wrapper = mount(
            // remove initialEntrie from here
            <MemoryRouter>
                <Conversations >
                    <ConversationView>
                    </ConversationView>
                </Conversations>
            </MemoryRouter>);

        // set props (initialEntries) to the component 
        wrapper.setProps({ initialEntries: '/' })

        const button = wrapper.find("button").first();
        button.simulate("click");
        expect(wrapper.find("h2")).toHaveLength(1);
const wrapper=mount(
//从这里移除initialEntrie
);
//为组件设置道具(初始条目)
wrapper.setProps({initialEntries:'/'})
const button=wrapper.find(“button”).first();
按钮。模拟(“单击”);
expect(wrapper.find(“h2”).toHaveLength(1);

您的
单击处理程序是否执行异步工作?不,它只是应该呈现不同的页面。不,这并没有真正改变任何事情。
const wrapper = mount(
            // remove initialEntrie from here
            <MemoryRouter>
                <Conversations >
                    <ConversationView>
                    </ConversationView>
                </Conversations>
            </MemoryRouter>);

        // set props (initialEntries) to the component 
        wrapper.setProps({ initialEntries: '/' })

        const button = wrapper.find("button").first();
        button.simulate("click");
        expect(wrapper.find("h2")).toHaveLength(1);