Skip to content

Commit c960275

Browse files
committed
Swap snapshots for more specific tests
1 parent bf08537 commit c960275

File tree

3 files changed

+50
-44
lines changed

3 files changed

+50
-44
lines changed

packages/react-router/__tests__/__snapshots__/nested-params-test.tsx.snap

Lines changed: 0 additions & 28 deletions
This file was deleted.

packages/react-router/__tests__/nested-params-test.tsx

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from "react";
2-
import { create as createTestRenderer } from "react-test-renderer";
2+
import * as ReactDOM from "react-dom";
3+
import { act } from "react-dom/test-utils";
34
import {
45
MemoryRouter as Router,
56
Outlet,
@@ -9,6 +10,17 @@ import {
910
} from "react-router";
1011

1112
describe("nested routes", () => {
13+
let node: HTMLDivElement;
14+
beforeEach(() => {
15+
node = document.createElement("div");
16+
document.body.appendChild(node);
17+
});
18+
19+
afterEach(() => {
20+
document.body.removeChild(node);
21+
node = null!;
22+
});
23+
1224
it("gets all params from parent routes", () => {
1325
function Users() {
1426
return (
@@ -43,30 +55,32 @@ describe("nested routes", () => {
4355
}
4456

4557
function Course() {
46-
// We should be able to access the username param here
47-
// even though it was defined in a parent route from
48-
// another set of <Routes>
58+
// We should be able to access the username param here even though it was
59+
// defined in a parent route from another set of <Routes>
4960
let { username, courseId } = useParams();
5061
return (
5162
<div>
52-
<h1>
63+
<h1 id="course">
5364
User: {username}, course {courseId}
5465
</h1>
5566
</div>
5667
);
5768
}
5869

59-
let renderer = createTestRenderer(
60-
<Router initialEntries={["/users/michael/courses/routing"]}>
61-
<Routes>
62-
<Route path="users" element={<Users />}>
63-
<Route path=":username/*" element={<User />} />
64-
</Route>
65-
</Routes>
66-
</Router>
67-
);
70+
act(() => {
71+
ReactDOM.render(
72+
<Router initialEntries={["/users/michael/courses/routing"]}>
73+
<Routes>
74+
<Route path="users" element={<Users />}>
75+
<Route path=":username/*" element={<User />} />
76+
</Route>
77+
</Routes>
78+
</Router>,
79+
node
80+
);
81+
});
6882

69-
expect(renderer.toJSON()).not.toBeNull();
70-
expect(renderer.toJSON()).toMatchSnapshot();
83+
let elem = document.querySelector<HTMLHeadingElement>("#course")!;
84+
expect(elem.innerHTML).toBe("User: michael, course routing");
7185
});
7286
});

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,26 @@ describe("useHref", () => {
195195
});
196196
});
197197

198+
describe("to an absolute route", () => {
199+
it("returns the correct href", () => {
200+
let href = "";
201+
function AdvancedReact() {
202+
href = useHref("/users");
203+
return <h1>Advanced React</h1>;
204+
}
205+
206+
createTestRenderer(
207+
<Router initialEntries={["/courses/advanced-react"]}>
208+
<Routes>
209+
<Route path="courses/advanced-react" element={<AdvancedReact />} />
210+
</Routes>
211+
</Router>
212+
);
213+
214+
expect(href).toBe("/users");
215+
});
216+
});
217+
198218
describe("with a to value that has more .. segments than are in the URL", () => {
199219
it("returns the correct href", () => {
200220
function Courses() {

0 commit comments

Comments
 (0)