@@ -3,12 +3,12 @@ id: example-react-router
33title : React Router
44---
55
6- The example below now supports React Router v6.
6+ This example demonstrates React Router v6. For previous versions see below .
77
88``` jsx
99// app.js
1010import React from ' react'
11- import {Link , Route , Switch , useLocation } from ' react-router-dom'
11+ import {Link , Route , Routes , useLocation } from ' react-router-dom'
1212
1313const About = () => < div> You are on the about page< / div>
1414const Home = () => < div> You are home< / div>
@@ -26,19 +26,13 @@ export const App = () => (
2626
2727 < Link to= " /about" > About< / Link>
2828
29- < Switch>
30- < Route exact path= " /" >
31- < Home / >
32- < / Route>
29+ < Routes>
30+ < Route exact path= " /" element= {< Home / > } / >
3331
34- < Route path= " /about" >
35- < About / >
36- < / Route>
32+ < Route path= " /about" element= {< About / > } / >
3733
38- < Route>
39- < NoMatch / >
40- < / Route>
41- < / Switch>
34+ < Route path= " *" element= {< NoMatch / > } / >
35+ < / Routes>
4236
4337 < LocationDisplay / >
4438 < / div>
@@ -49,79 +43,55 @@ export const App = () => (
4943// app.test.js
5044import {render , screen } from ' @testing-library/react'
5145import userEvent from ' @testing-library/user-event'
52- import {createMemoryHistory } from ' history'
5346import React from ' react'
54- import {Router } from ' react-router-dom'
55-
5647import ' @testing-library/jest-dom'
57-
5848import {App , LocationDisplay } from ' ./app'
49+ import {BrowserRouter , MemoryRouter } from ' react-router-dom'
5950
6051test (' full app rendering/navigating' , async () => {
61- const history = createMemoryHistory ()
62- render (
63- < Router location= {history .location } navigator = {history}>
64- < App / >
65- < / Router> ,
66- )
52+ render (< App / > , {wrapper: BrowserRouter})
6753 const user = userEvent .setup ()
68- // verify page content for expected route
69- // often you'd use a data-testid or role query, but this is also possible
54+
55+ // verify page content for default route
7056 expect (screen .getByText (/ you are home/ i )).toBeInTheDocument ()
7157
58+ // verify page content for expected route after navigating
7259 await user .click (screen .getByText (/ about/ i ))
73-
74- // check that the content changed to the new page
7560 expect (screen .getByText (/ you are on the about page/ i )).toBeInTheDocument ()
7661})
7762
7863test (' landing on a bad page' , () => {
79- const history = createMemoryHistory ()
80- history .push (' /some/bad/route' )
64+ const badRoute = ' /some/bad/route'
65+
66+ // use <MemoryRouter> when you want to manually control the history
8167 render (
82- < Router location = { history . location } navigator = {history }>
68+ < MemoryRouter initialEntries = {[badRoute] }>
8369 < App / >
84- < / Router > ,
70+ < / MemoryRouter > ,
8571 )
8672
73+ // verify navigation to "no match" route
8774 expect (screen .getByText (/ no match/ i )).toBeInTheDocument ()
8875})
8976
9077test (' rendering a component that uses useLocation' , () => {
91- const history = createMemoryHistory ()
9278 const route = ' /some-route'
93- history .push (route)
79+
80+ // use <MemoryRouter> when you want to manually control the history
9481 render (
95- < Router location = { history . location } navigator = {history }>
82+ < MemoryRouter initialEntries = {[route] }>
9683 < LocationDisplay / >
97- < / Router > ,
84+ < / MemoryRouter > ,
9885 )
9986
87+ // verify location display is rendered
10088 expect (screen .getByTestId (' location-display' )).toHaveTextContent (route)
10189})
10290```
10391
10492## Reducing boilerplate
10593
106- 1 . You can use the ` wrapper ` option to wrap a ` MemoryRouter ` around the
107- component you want to render.
108- ` MemoryRouter ` works when you don't need access to the history object itself
109- in the test, but just need the components to be able to render and
110- navigate.
111- If you _ do_ need to change the history, you could use ` BrowserRouter ` .
112-
113- ``` jsx
114- import {MemoryRouter } from ' react-router-dom'
115-
116- test (' full app rendering/navigating' , () => {
117- render (< App / > , {wrapper: MemoryRouter})
118-
119- // verify page content for expected route
120- expect (screen .getByText (/ you are home/ i )).toBeInTheDocument ()
121- })
122- ```
123-
124- 2 . If you find yourself adding Router components to your tests a lot, you may
94+ 1 . If you find yourself adding Router components to your tests a lot, you may
12595 want to create a helper function that wraps around ` render ` .
12696
12797``` jsx
@@ -161,16 +131,50 @@ test('rendering a component that uses useLocation', () => {
161131})
162132```
163133
164- ## Testing Library and React Router v5
134+ ## Testing Library and React Router v5
165135
166- In React Router v5, you need to pass the history object as a whole to the Route component.
136+ In React Router v5, you need to pass the history object as a whole to the Route
137+ component.
167138
168139``` jsx
169- test (' full app rendering/navigating' , () => {
140+ // app.test.js
141+ import {render , screen } from ' @testing-library/react'
142+ import userEvent from ' @testing-library/user-event'
143+ import {createMemoryHistory } from ' history'
144+ import React from ' react'
145+ import {Router } from ' react-router-dom'
146+ import ' @testing-library/jest-dom'
147+ import {App } from ' ./app'
148+
149+ // React Router v5
150+
151+ test (' full app rendering/navigating' , async () => {
170152 const history = createMemoryHistory ()
171153 render (
172- < Router history= {history}>
154+ < Router location = { history . location } navigator = {history}>
173155 < App / >
174156 < / Router> ,
175157 )
176- ` ` `
158+ const user = userEvent .setup ()
159+ // verify page content for expected route
160+ // often you'd use a data-testid or role query, but this is also possible
161+ expect (screen .getByText (/ you are home/ i )).toBeInTheDocument ()
162+
163+ await user .click (screen .getByText (/ about/ i ))
164+
165+ // check that the content changed to the new page
166+ expect (screen .getByText (/ you are on the about page/ i )).toBeInTheDocument ()
167+ })
168+
169+ test (' landing on a bad page' , () => {
170+ const history = createMemoryHistory ()
171+ history .push (' /some/bad/route' )
172+ render (
173+ < Router location= {history .location } navigator = {history}>
174+ < App / >
175+ < / Router> ,
176+ )
177+
178+ expect (screen .getByText (/ no match/ i )).toBeInTheDocument ()
179+ })
180+ ```
0 commit comments