Skip to content

Commit 58d5667

Browse files
committed
Update useMatch tests to assert based on renderer output
1 parent abbcf56 commit 58d5667

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed
Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,96 @@
11
import * as React from "react";
22
import * as TestRenderer from "react-test-renderer";
3-
import type { PathMatch } from "react-router";
43
import { MemoryRouter, Routes, Route, useMatch } from "react-router";
54

5+
function ShowMatch({ pattern }: { pattern: string }) {
6+
return <pre>{JSON.stringify(useMatch(pattern), null, 2)}</pre>;
7+
}
8+
69
describe("useMatch", () => {
710
describe("when the path matches the current URL", () => {
811
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;
1513
TestRenderer.act(() => {
16-
TestRenderer.create(
14+
renderer = TestRenderer.create(
1715
<MemoryRouter initialEntries={["/home"]}>
1816
<Routes>
19-
<Route path="/" element={<Layout />}>
17+
<Route path="/" element={<ShowMatch pattern="home" />}>
2018
<Route path="/home" element={<h1>Home</h1>} />
2119
</Route>
2220
</Routes>
2321
</MemoryRouter>
2422
);
2523
});
2624

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+
`);
3239
});
3340
});
3441

3542
describe("when the current URL ends with a slash", () => {
3643
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;
4345
TestRenderer.act(() => {
44-
TestRenderer.create(
46+
renderer = TestRenderer.create(
4547
<MemoryRouter initialEntries={["/home/"]}>
4648
<Routes>
47-
<Route path="/" element={<Layout />}>
49+
<Route path="/" element={<ShowMatch pattern="home" />}>
4850
<Route path="/home" element={<h1>Home</h1>} />
4951
</Route>
5052
</Routes>
5153
</MemoryRouter>
5254
);
5355
});
5456

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+
`);
6071
});
6172
});
6273

6374
describe("when the path does not match the current URL", () => {
6475
it("returns null", () => {
65-
let match = null;
66-
function Layout() {
67-
match = useMatch("about");
68-
return null;
69-
}
70-
76+
let renderer: TestRenderer.ReactTestRenderer;
7177
TestRenderer.act(() => {
72-
TestRenderer.create(
78+
renderer = TestRenderer.create(
7379
<MemoryRouter initialEntries={["/home"]}>
7480
<Routes>
75-
<Route path="/" element={<Layout />}>
81+
<Route path="/" element={<ShowMatch pattern="about" />}>
7682
<Route path="/home" element={<h1>Home</h1>} />
7783
</Route>
7884
</Routes>
7985
</MemoryRouter>
8086
);
8187
});
8288

83-
expect(match).toBeNull();
89+
expect(renderer.toJSON()).toMatchInlineSnapshot(`
90+
<pre>
91+
null
92+
</pre>
93+
`);
8494
});
8595
});
8696
});

0 commit comments

Comments
 (0)