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