score:2

From the Switch docs:

Renders the first child <Route> or <Redirect> that matches the location.


In this case <Route path="/" component={Home} /> matches when the path is both / and /foo so Home is always rendered.

You can fix this by using either exact so it only matches if the path is exactly /, or moving it to the end of the Route list so other routes match first:

test('Clicking link will render component associated with path', () => {
  const wrapper = mount(
    <MemoryRouter>
      <div>
        <Link to="/foo" />
        <Switch>
          <Route path="/foo" component={MockComp} />
          <Route path="/" component={Home} />
        </Switch>
      </div>
    </MemoryRouter>
  );
  wrapper.find('a').simulate('click', { button: 0 });
  expect(wrapper.find('.protected')).toHaveLength(1);  // SUCCESS
});

Related Query

More Query from same tag