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
});
Source: stackoverflow.com
Related Query
- testing click behavior on React Router Links with Enzyme
- Testing react router v4 with jest enzyme
- Testing React Router with Jest and Enzyme
- Testing components that contain react router Link with Enzyme
- Why do I get a blank page when I click on links that point to '/' with my react router setup?
- Nested components testing with Enzyme inside of React & Redux
- Accessing the State of a Functional Component with React Hooks when Testing with Enzyme
- Testing React portals with enzyme
- Testing functions inside stateless React component with Enzyme
- Testing React Component with React Router V6
- React testing component prop change with enzyme
- Testing debounced function in React component with Jest and Enzyme
- _this.store.getState is not a function when testing react component with enzyme and mocha
- React Router with params navigatin back needs double click
- Unit testing route matching with React Router 2
- Testing React Router with Link
- Testing React Component className with enzyme
- How to test Material-UI's Responsive UI (e.g. Hidden, Grid, Breakpoints) in React with Enzyme or React Testing Library
- How to test React Router params with Redux and enzyme
- Testing debounced method in React with Enzyme
- React testing with Jest and Enzyme (in Symfony) got "Syntax Error: Unexpected token import"
- How to test a click event with enzyme in a React component's dangerouslySetInnerHTML content
- React testing state of component with Jest and Enzyme
- Getting started testing React components with Enzyme and Jest
- Testing React component with Enzyme Jest finding HTML element
- Simulating a click on React with Enzyme not doing anything
- Cannot test redirecting in react router with enzyme
- Links don't redirect with React DOM Router
- Testing navigation in react with MemoryRouter after a button click (jest, enzyme)
- testing custom react methods with jest and enzyme
More Query from same tag
- Using useEffect like ComponentDidUpdate
- React pdf viewer - using worker from local server
- React CRA: Preload fonts from fonts folder
- How to fix Failed to set the 'value' property on 'HTMLInputElement' for react?
- One component overlapping on other in react.js app
- Error when using CanvasJS in a NextJS app
- How to fit or zoom out a React Vis.JS Network Graph?
- Spread Operator Dynamic Properties Update
- Cannot get user session from Amazon Cognito User Pools
- What is the way to pass object short-hand as props into React component?
- How React recognize clicking items?
- Async/await call action from React component
- Should I organize my react redux reducer composition based on the calls made OR based on the state a reducer manages?
- unit test a react component- jest, enzyme
- ReactJS - How to add in a removeEventListener when my mouse enters div element (onMouseEnter)
- How to merge object properties inside an object belonging to a Reducer
- gatsby HTML file size too large (i.e. due to bootstrap-CSS)
- Can't format moment object to date and time
- React onChange is not a function when passing method as props to child component
- How to trigger popup notification on submit of a form in React using React Router navigate hook?
- Referencing state from within state - React js
- Clear input text on button click
- Animate when scrollY is equal to 0.52 - Next.js ( with framer-motion )
- Handling data stream with Redux
- Animate width of button when text changes
- Module not found when import .jsx file
- Isomorphic react redux sending Dynamic Id
- Socket.io - io.in(room).emit() not working on rooms joined in callbacks
- Default today's date in date picker using react js without using DatePicker component
- How to setState in reducer - React