diff --git a/packages/react-router/__tests__/Routes-location-test.tsx b/packages/react-router/__tests__/Routes-location-test.tsx new file mode 100644 index 0000000000..da4047eed3 --- /dev/null +++ b/packages/react-router/__tests__/Routes-location-test.tsx @@ -0,0 +1,72 @@ +import * as React from "react"; +import { create as createTestRenderer } from "react-test-renderer"; +import { + MemoryRouter as Router, + Route, + Routes, + useParams +} from "react-router"; + +describe(" with a location", () => { + + function Home() { + return

Home

; + } + + function User() { + let { userId } = useParams(); + return ( +
+

User: {userId}

+
+ ); + } + + it("matches when the location is overridden", () => { + + const location = { + pathname: '/home', + search: '', + hash: '', + state: null, + key: 'r9qntrej' + }; + const renderer = createTestRenderer( + + + } /> + } /> + + + ); + + expect(renderer.toJSON()).not.toBeNull(); + expect(renderer.toJSON()).toMatchInlineSnapshot(` +

+ Home +

+ `); + }); + + + it("matches when the location is not overridden", () => { + const renderer = createTestRenderer( + + + } /> + } /> + + + ); + + expect(renderer.toJSON()).not.toBeNull(); + expect(renderer.toJSON()).toMatchInlineSnapshot(` +
+

+ User: + michael +

+
+ `); + }); +}); diff --git a/packages/react-router/index.tsx b/packages/react-router/index.tsx index dcfa8e2f6c..e90572cae9 100644 --- a/packages/react-router/index.tsx +++ b/packages/react-router/index.tsx @@ -255,6 +255,7 @@ export function Router({ export interface RoutesProps { basename?: string; children?: React.ReactNode; + location?: Location; } /** @@ -265,11 +266,12 @@ export interface RoutesProps { */ export function Routes({ basename = "", - children + children, + location }: RoutesProps): React.ReactElement | null { let routes = createRoutesFromChildren(children); - let location = useLocation(); - return useRoutes_(routes, location, basename); + let location_ = useLocation(); + return useRoutes_(routes, location ?? location_, basename); } ///////////////////////////////////////////////////////////////////////////////