React js-使用MemoryRouter进行测试时遇到问题

时间:2019-01-26 19:26:49

标签: reactjs jestjs enzyme react-router-v4 react-router-dom

这是我第一次使用React Router 4,在确定如何进行测试时遇到了一些问题。我想验证每条路线都指向正确的组件。

“我的路线”组件如下所示:

const Routes = () => {
  return (
    <Switch>
      <Route exact path="/" component={HomePage} />
      <Route path="/register" component={Registration} />
      <Route path="/login" component={Login} />
      <Route component={PageNotFound} />
    </Switch>
  );
};

我的测试如下:

  it('should show Login component when sent to login route', () => {
    const wrapper = mount(
      <MemoryRouter initalEntires={[{ pathname: '/login' }]} initialIndex={0}>
        <Routes />
      </MemoryRouter>
    );
    expect(wrapper.find('Login').length).toBe(1);
  });

我想验证是否在使用/ login路由时使用了Login组件,但这始终呈现HomePage组件。有什么我想念的吗?

谢谢, 詹姆斯

0 个答案:

没有答案