Skip to content

Testing does not work properly with nested route #308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chipueatfast opened this issue Feb 25, 2019 · 2 comments
Closed

Testing does not work properly with nested route #308

chipueatfast opened this issue Feb 25, 2019 · 2 comments

Comments

@chipueatfast
Copy link

  • react-testing-library version: 5.9.0
  • react version: 16.8.1
  • node version: 8.10.0
  • yarn version: 1.7.0

Relevant code or config:

...
const Home2 = () => <div>You are home again</div>
...
// In App component
<Switch>
        <Route exact path="/" component={Home} />
        <Route path="/about">
          <Route exact path="/home2" component={Home2} />
        </Route>
        <Route component={NoMatch} />
</Switch>
...
test('react-router landing on a suppose-to-be Home2 page', () => {
  const { container } = renderWithRouter(<App />, {
    route: '/about/home',
  })
  // normally I'd use a data-testid, but just wanted to show this is also possible
  expect(container.innerHTML).toMatch('You are home again')
})

What you did:

I copied the example on 'react-router' and run, it went smoothly. But when I add a nested route, the routing seem to stop at the parent route.

What happened:

Link to Example: https://testing-library.com/docs/example-react-router

The test result is shown like this:

  expect(received).toMatch(expected)

    Expected value to match:
      "You are home again"
    Received:
      "<div><a href=\"/\">Home</a><a href=\"/about\">About</a><div data-testid=\"location-display\">/about/home2</div></div>"

Problem description:

Can someone tell me what I did wrong or this is a limitation at the moment

@bcarroll22
Copy link
Contributor

👋 howdy. Pretty sure the issue you’re running in to is because in React Router you aren’t supposed to nest routes like this. Hard to tell without the whole example, but I’m fairly confident that’s the issue.

Try nesting your route inside the about page component instead of under the about route and you should be good to go 👍

@kentcdodds
Copy link
Member

Closing this for inactivity. If someone wants to work on it in the future feel free to open a new issue. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants