Skip to content

Commit 6f0012b

Browse files
committed
Update useLocation unit tests
1 parent 61952ea commit 6f0012b

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

.changeset/hungry-vans-ring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
"react-router-native": patch
66
---
77

8-
Fix for `useLocation` returning the wrong `Location` when passing a `locationArg` into a `Routes` component.
8+
fix: update `useLocation` to return the scoped `Location` when inside a `<Routes location>` component

packages/react-router/__tests__/useLocation-test.tsx

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as React from "react";
22
import * as TestRenderer from "react-test-renderer";
33
import { MemoryRouter, Routes, Route, useLocation } from "react-router";
44

5-
function ShowPath() {
6-
let { pathname, search, hash } = useLocation();
7-
return <pre>{JSON.stringify({ pathname, search, hash })}</pre>;
5+
function ShowLocation() {
6+
let location = useLocation();
7+
return <pre>{JSON.stringify(location)}</pre>;
88
}
99

1010
describe("useLocation", () => {
@@ -14,16 +14,55 @@ describe("useLocation", () => {
1414
renderer = TestRenderer.create(
1515
<MemoryRouter initialEntries={["/home?the=search#the-hash"]}>
1616
<Routes>
17-
<Route path="/home" element={<ShowPath />} />
17+
<Route path="/home" element={<ShowLocation />} />
1818
</Routes>
1919
</MemoryRouter>
2020
);
2121
});
2222

2323
expect(renderer.toJSON()).toMatchInlineSnapshot(`
2424
<pre>
25-
{"pathname":"/home","search":"?the=search","hash":"#the-hash"}
25+
{"pathname":"/home","search":"?the=search","hash":"#the-hash","state":null,"key":"default"}
2626
</pre>
2727
`);
2828
});
29+
30+
it("returns the scoped location object when nested in <Routes location>", () => {
31+
let renderer: TestRenderer.ReactTestRenderer;
32+
TestRenderer.act(() => {
33+
renderer = TestRenderer.create(
34+
<MemoryRouter initialEntries={["/home?the=search#the-hash"]}>
35+
<App />
36+
</MemoryRouter>
37+
);
38+
});
39+
40+
function App() {
41+
return (
42+
<div>
43+
<h1>App</h1>
44+
<Routes>
45+
<Route path="/home" element={<ShowLocation />} />
46+
</Routes>
47+
<Routes location="/scoped?scoped=search#scoped-hash">
48+
<Route path="/scoped" element={<ShowLocation />} />
49+
</Routes>
50+
</div>
51+
);
52+
}
53+
54+
expect(renderer.toJSON()).toMatchInlineSnapshot(`
55+
<div>
56+
<h1>
57+
App
58+
</h1>
59+
<pre>
60+
{"pathname":"/home","search":"?the=search","hash":"#the-hash","state":null,"key":"default"}
61+
</pre>
62+
<pre>
63+
{"pathname":"/scoped","search":"?scoped=search","hash":"#scoped-hash","state":null,"key":"default"}
64+
</pre>
65+
</div>
66+
`);
67+
});
2968
});

packages/react-router/__tests__/useRoutes-test.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from "react";
22
import * as TestRenderer from "react-test-renderer";
33
import type { RouteObject } from "react-router";
4-
import { MemoryRouter, useRoutes, useLocation } from "react-router";
4+
import { MemoryRouter, useRoutes } from "react-router";
55

66
describe("useRoutes", () => {
77
it("returns the matching element from a route config", () => {
@@ -54,15 +54,11 @@ describe("useRoutes", () => {
5454
});
5555

5656
it("Uses the `location` prop instead of context location`", () => {
57-
let TwoComponent = () => {
58-
const location = useLocation();
59-
return <h1>two path:{location.pathname}</h1>;
60-
};
6157
let routes = [
6258
{ path: "one", element: <h1>one</h1> },
6359
{
6460
path: "two",
65-
element: <TwoComponent />,
61+
element: <h1>two</h1>,
6662
},
6763
];
6864

@@ -75,11 +71,9 @@ describe("useRoutes", () => {
7571
);
7672
});
7773

78-
// console.log(renderer.toTree());
7974
expect(renderer.toJSON()).toMatchInlineSnapshot(`
8075
<h1>
81-
two path:
82-
/two
76+
two
8377
</h1>
8478
`);
8579
});

packages/react-router/lib/hooks.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,11 +449,11 @@ export function useRoutes(
449449
<LocationContext.Provider
450450
value={{
451451
location: {
452-
state: undefined,
453-
key: "default",
454452
pathname: "/",
455453
search: "",
456454
hash: "",
455+
state: null,
456+
key: "default",
457457
...location,
458458
},
459459
navigationType: NavigationType.Pop,

0 commit comments

Comments
 (0)