|
1 | 1 | import * as React from "react";
|
2 | 2 | import * as TestRenderer from "react-test-renderer";
|
3 |
| -import type { PathMatch } from "react-router"; |
4 | 3 | import { MemoryRouter, Routes, Route, useMatch } from "react-router";
|
5 | 4 |
|
| 5 | +function ShowMatch({ pattern }: { pattern: string }) { |
| 6 | + return <pre>{JSON.stringify(useMatch(pattern), null, 2)}</pre>; |
| 7 | +} |
| 8 | + |
6 | 9 | describe("useMatch", () => {
|
7 | 10 | describe("when the path matches the current URL", () => {
|
8 | 11 | it("returns the match", () => {
|
9 |
| - let match: PathMatch | null = null; |
10 |
| - function Layout() { |
11 |
| - match = useMatch("home"); |
12 |
| - return null; |
13 |
| - } |
14 |
| - |
| 12 | + let renderer: TestRenderer.ReactTestRenderer; |
15 | 13 | TestRenderer.act(() => {
|
16 |
| - TestRenderer.create( |
| 14 | + renderer = TestRenderer.create( |
17 | 15 | <MemoryRouter initialEntries={["/home"]}>
|
18 | 16 | <Routes>
|
19 |
| - <Route path="/" element={<Layout />}> |
| 17 | + <Route path="/" element={<ShowMatch pattern="home" />}> |
20 | 18 | <Route path="/home" element={<h1>Home</h1>} />
|
21 | 19 | </Route>
|
22 | 20 | </Routes>
|
23 | 21 | </MemoryRouter>
|
24 | 22 | );
|
25 | 23 | });
|
26 | 24 |
|
27 |
| - expect(match).toMatchObject({ |
28 |
| - params: {}, |
29 |
| - pathname: "/home", |
30 |
| - pattern: { path: "home" } |
31 |
| - }); |
| 25 | + expect(renderer.toJSON()).toMatchInlineSnapshot(` |
| 26 | + <pre> |
| 27 | + { |
| 28 | + "params": {}, |
| 29 | + "pathname": "/home", |
| 30 | + "pathnameBase": "/home", |
| 31 | + "pattern": { |
| 32 | + "path": "home", |
| 33 | + "caseSensitive": false, |
| 34 | + "end": true |
| 35 | + } |
| 36 | + } |
| 37 | + </pre> |
| 38 | + `); |
32 | 39 | });
|
33 | 40 | });
|
34 | 41 |
|
35 | 42 | describe("when the current URL ends with a slash", () => {
|
36 | 43 | it("returns the match.pathname with the trailing slash", () => {
|
37 |
| - let match = null; |
38 |
| - function Layout() { |
39 |
| - match = useMatch("home"); |
40 |
| - return null; |
41 |
| - } |
42 |
| - |
| 44 | + let renderer: TestRenderer.ReactTestRenderer; |
43 | 45 | TestRenderer.act(() => {
|
44 |
| - TestRenderer.create( |
| 46 | + renderer = TestRenderer.create( |
45 | 47 | <MemoryRouter initialEntries={["/home/"]}>
|
46 | 48 | <Routes>
|
47 |
| - <Route path="/" element={<Layout />}> |
| 49 | + <Route path="/" element={<ShowMatch pattern="home" />}> |
48 | 50 | <Route path="/home" element={<h1>Home</h1>} />
|
49 | 51 | </Route>
|
50 | 52 | </Routes>
|
51 | 53 | </MemoryRouter>
|
52 | 54 | );
|
53 | 55 | });
|
54 | 56 |
|
55 |
| - expect(match).toMatchObject({ |
56 |
| - params: {}, |
57 |
| - pathname: "/home/", |
58 |
| - pattern: { path: "home" } |
59 |
| - }); |
| 57 | + expect(renderer.toJSON()).toMatchInlineSnapshot(` |
| 58 | + <pre> |
| 59 | + { |
| 60 | + "params": {}, |
| 61 | + "pathname": "/home/", |
| 62 | + "pathnameBase": "/home", |
| 63 | + "pattern": { |
| 64 | + "path": "home", |
| 65 | + "caseSensitive": false, |
| 66 | + "end": true |
| 67 | + } |
| 68 | + } |
| 69 | + </pre> |
| 70 | + `); |
60 | 71 | });
|
61 | 72 | });
|
62 | 73 |
|
63 | 74 | describe("when the path does not match the current URL", () => {
|
64 | 75 | it("returns null", () => {
|
65 |
| - let match = null; |
66 |
| - function Layout() { |
67 |
| - match = useMatch("about"); |
68 |
| - return null; |
69 |
| - } |
70 |
| - |
| 76 | + let renderer: TestRenderer.ReactTestRenderer; |
71 | 77 | TestRenderer.act(() => {
|
72 |
| - TestRenderer.create( |
| 78 | + renderer = TestRenderer.create( |
73 | 79 | <MemoryRouter initialEntries={["/home"]}>
|
74 | 80 | <Routes>
|
75 |
| - <Route path="/" element={<Layout />}> |
| 81 | + <Route path="/" element={<ShowMatch pattern="about" />}> |
76 | 82 | <Route path="/home" element={<h1>Home</h1>} />
|
77 | 83 | </Route>
|
78 | 84 | </Routes>
|
79 | 85 | </MemoryRouter>
|
80 | 86 | );
|
81 | 87 | });
|
82 | 88 |
|
83 |
| - expect(match).toBeNull(); |
| 89 | + expect(renderer.toJSON()).toMatchInlineSnapshot(` |
| 90 | + <pre> |
| 91 | + null |
| 92 | + </pre> |
| 93 | + `); |
84 | 94 | });
|
85 | 95 | });
|
86 | 96 | });
|
0 commit comments