testing-library / testing-library/testing-library-docs

The first test of example-react-router fails

Open
#897 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
479
Forks
740
PR merge metrics
No merged PRs in 30d

Description

the first example of example-react-router fails
to be more precise

test('full app rendering/navigating', () => {
  const history = createMemoryHistory()
  render(
    <Router history={history}>
      <App />
    </Router>,
  )
  // verify page content for expected route
  // often you'd use a data-testid or role query, but this is also possible
  expect(screen.getByText(/you are home/i)).toBeInTheDocument()

  const leftClick = {button: 0}
  userEvent.click(screen.getByText(/about/i), leftClick)

  // check that the content changed to the new page
  expect(screen.getByText(/you are on the about page/i)).toBeInTheDocument()
})

To Reproduce Steps to reproduce the behavior:

  1. copy paste the app.test.js from here
  2. run tests

Expected behavior All three test cases should pass

Screenshots
Screenshot 2021-07-24 at 5 50 00 PM

However I could resolve the error by rendering the App component like this

test('full app rendering/navigating', () => {
	// const history = createMemoryHistory();
	// history.push('/about');
	render(<App />, { wrapper: MemoryRouter });
	// verify page content for expected route
	// often you'd use a data-testid or role query, but this is also possible
	// verify page content for expected route
	// often you'd use a data-testid or role query, but this is also possible
	expect(screen.getByText(/you are home/i)).toBeInTheDocument();

	const leftClick = { button: 0 };
	userEvent.click(screen.getByText(/about/i), leftClick);

	// check that the content changed to the new page
	expect(screen.getByText(/you are on the about page/i)).toBeInTheDocument();
});
Screenshot 2021-07-24 at 5 53 18 PM

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the example-react-router page and the app.test.js copied from its linked documentation example. Run the example's tests to reproduce the first failure, then compare the Router and MemoryRouter variants shown in the issue. Done means all three test cases pass using the documented example.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
documentation, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.