File tree Expand file tree Collapse file tree 2 files changed +77
-3
lines changed Expand file tree Collapse file tree 2 files changed +77
-3
lines changed Original file line number Diff line number Diff line change 1+ import * as React from "react" ;
2+ import { create as createTestRenderer } from "react-test-renderer" ;
3+ import {
4+ MemoryRouter as Router ,
5+ Route ,
6+ Routes ,
7+ useParams
8+ } from "react-router" ;
9+
10+ describe ( "<Routes> with a location" , ( ) => {
11+
12+ function Home ( ) {
13+ return < h1 > Home</ h1 > ;
14+ }
15+
16+ function User ( ) {
17+ let { userId } = useParams ( ) ;
18+ return (
19+ < div >
20+ < h1 > User: { userId } </ h1 >
21+ </ div >
22+ ) ;
23+ }
24+
25+ it ( "matches when the location is overridden" , ( ) => {
26+
27+ const location = {
28+ pathname : '/home' ,
29+ search : '' ,
30+ hash : '' ,
31+ state : null ,
32+ key : 'r9qntrej'
33+ } ;
34+ const renderer = createTestRenderer (
35+ < Router initialEntries = { [ "/users/michael" ] } >
36+ < Routes location = { location } >
37+ < Route path = "home" element = { < Home /> } />
38+ < Route path = "users/:userId" element = { < User /> } />
39+ </ Routes >
40+ </ Router >
41+ ) ;
42+
43+ expect ( renderer . toJSON ( ) ) . not . toBeNull ( ) ;
44+ expect ( renderer . toJSON ( ) ) . toMatchInlineSnapshot ( `
45+ <h1>
46+ Home
47+ </h1>
48+ ` ) ;
49+ } ) ;
50+
51+
52+ it ( "matches when the location is not overridden" , ( ) => {
53+ const renderer = createTestRenderer (
54+ < Router initialEntries = { [ "/users/michael" ] } >
55+ < Routes >
56+ < Route path = "home" element = { < Home /> } />
57+ < Route path = "users/:userId" element = { < User /> } />
58+ </ Routes >
59+ </ Router >
60+ ) ;
61+
62+ expect ( renderer . toJSON ( ) ) . not . toBeNull ( ) ;
63+ expect ( renderer . toJSON ( ) ) . toMatchInlineSnapshot ( `
64+ <div>
65+ <h1>
66+ User:
67+ michael
68+ </h1>
69+ </div>
70+ ` ) ;
71+ } ) ;
72+ } ) ;
Original file line number Diff line number Diff line change @@ -253,6 +253,7 @@ export function Router({
253253export interface RoutesProps {
254254 basename ?: string ;
255255 children ?: React . ReactNode ;
256+ location ?: Location ;
256257}
257258
258259/**
@@ -263,11 +264,12 @@ export interface RoutesProps {
263264 */
264265export function Routes ( {
265266 basename = "" ,
266- children
267+ children,
268+ location
267269} : RoutesProps ) : React . ReactElement | null {
268270 let routes = createRoutesFromChildren ( children ) ;
269- let location = useLocation ( ) ;
270- return useRoutes_ ( routes , location , basename ) ;
271+ let location_ = useLocation ( ) ;
272+ return useRoutes_ ( routes , location ?? location_ , basename ) ;
271273}
272274
273275///////////////////////////////////////////////////////////////////////////////
You can’t perform that action at this time.
0 commit comments