Skip to content

Commit 43045de

Browse files
committed
add tests for useResolvedPath
1 parent 3a26104 commit 43045de

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import * as React from "react";
2+
import { create as createTestRenderer } from "react-test-renderer";
3+
import {
4+
MemoryRouter as Router,
5+
Routes,
6+
Route,
7+
useResolvedPath
8+
} from "react-router";
9+
import type { Path } from "history";
10+
11+
describe("useResolvedPath", () => {
12+
it("path string resolves to Path object", () => {
13+
let path!: Path;
14+
function Home() {
15+
path = useResolvedPath("/home?user=mj#welcome");
16+
return <h1>Home</h1>;
17+
}
18+
19+
createTestRenderer(
20+
<Router initialEntries={["/home"]}>
21+
<Routes>
22+
<Route path="/home" element={<Home />} />
23+
</Routes>
24+
</Router>
25+
);
26+
27+
expect(typeof path).toBe("object");
28+
expect(path).toMatchObject({
29+
pathname: "/home",
30+
search: "?user=mj",
31+
hash: "#welcome"
32+
});
33+
});
34+
35+
describe("given a hash with a ? character", () => {
36+
it("hash is not parsed as a search string", () => {
37+
let path!: Path;
38+
function Home() {
39+
path = useResolvedPath("/home#welcome?user=mj");
40+
return <h1>Home</h1>;
41+
}
42+
43+
createTestRenderer(
44+
<Router initialEntries={["/home"]}>
45+
<Routes>
46+
<Route path="/home" element={<Home />} />
47+
</Routes>
48+
</Router>
49+
);
50+
51+
expect(typeof path).toBe("object");
52+
expect(path).toMatchObject({
53+
pathname: "/home",
54+
search: "",
55+
hash: "#welcome?user=mj"
56+
});
57+
});
58+
});
59+
});

0 commit comments

Comments
 (0)