Skip to content

Commit 9fc19c5

Browse files
authored
Update Router props based on react-router-dom v6 (#981)
* Update Router props based on react-router-dom v6 Since react-router-dom v6, there's no history object on Router anymore. Instead, the objects navigator and location needs to be used. Reference: https://stackoverflow.com/questions/69859509/cannot-read-properties-of-undefined-reading-pathname-when-testing-pages-in * Add info text and * Notify about React Router v6 in example and have a separate short example for v5
1 parent 2434656 commit 9fc19c5

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

docs/example-react-router.mdx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ id: example-react-router
33
title: React Router
44
---
55

6+
The example below now supports React Router v6.
7+
68
```jsx
79
// app.js
810
import React from 'react'
@@ -58,7 +60,7 @@ import {App, LocationDisplay} from './app'
5860
test('full app rendering/navigating', async () => {
5961
const history = createMemoryHistory()
6062
render(
61-
<Router history={history}>
63+
<Router location={history.location} navigator={history}>
6264
<App />
6365
</Router>,
6466
)
@@ -77,7 +79,7 @@ test('landing on a bad page', () => {
7779
const history = createMemoryHistory()
7880
history.push('/some/bad/route')
7981
render(
80-
<Router history={history}>
82+
<Router location={history.location} navigator={history}>
8183
<App />
8284
</Router>,
8385
)
@@ -90,7 +92,7 @@ test('rendering a component that uses useLocation', () => {
9092
const route = '/some-route'
9193
history.push(route)
9294
render(
93-
<Router history={history}>
95+
<Router location={history.location} navigator={history}>
9496
<LocationDisplay />
9597
</Router>,
9698
)
@@ -158,3 +160,17 @@ test('rendering a component that uses useLocation', () => {
158160
expect(screen.getByTestId('location-display')).toHaveTextContent(route)
159161
})
160162
```
163+
164+
## Testing Library and React Router v5
165+
166+
In React Router v5, you need to pass the history object as a whole to the Route component.
167+
168+
```jsx
169+
test('full app rendering/navigating', () => {
170+
const history = createMemoryHistory()
171+
render(
172+
<Router history={history} />
173+
<App />
174+
</Router>,
175+
)
176+
```

0 commit comments

Comments
 (0)