Jest测试react-router-dom链接点击

问题描述 投票:0回答:1

我正试图抓住链接的点击

<Link to="/exchange">Exchange</Link>

const comp = mount(
      <Router>
        <ComponentWithLink {...state} />
      </Router>
    );

const link = comp.find("Link");
    link.simulate("click");

// What do I test ?

我试图弄清楚如何添加模拟而又一事无成。我感觉我需要检查位置或模拟react-router-dom模块,但是我没有任何运气。

任何人都可以指出正确的方向吗?

谢谢

reactjs hyperlink react-router-dom jest
1个回答
0
投票

您不应该开玩笑地测试dom操作。如果您尝试测试单击链接,则该测试已在库中准备就绪。

如果您想在某些链接中测试道具,请执行以下操作:

const wrapper = shallow(<MemoryRouter><MyComponent/></MemoryRouter>);
expect(wrapper.find(Link).at(0).props().to).toBe('/my-route');
© www.soinside.com 2019 - 2024. All rights reserved.