Skip to content

Commit 3d6a417

Browse files
authored
Merge branch 'molefrog:v3' into ostapch/patch-26
2 parents 5cbdd3a + 2b1e5eb commit 3d6a417

File tree

10 files changed

+159
-255
lines changed

10 files changed

+159
-255
lines changed

bun.lock

Lines changed: 27 additions & 138 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@
138138
"@rollup/plugin-node-resolve": "^15.0.2",
139139
"@rollup/plugin-replace": "^5.0.7",
140140
"@size-limit/preset-small-lib": "^11.2.0",
141+
"@testing-library/dom": "^10.4.0",
141142
"@testing-library/jest-dom": "^6.1.4",
142-
"@testing-library/react": "^14.0.0",
143+
"@testing-library/react": "^16.3.0",
143144
"@types/babel__core": "^7.20.5",
145+
"@types/bun": "^1.2.14",
144146
"@types/react": "^18.2.0",
145147
"@types/react-test-renderer": "^18.0.0",
146148
"@vitejs/plugin-react": "^4.0.4",
@@ -149,15 +151,14 @@
149151
"copyfiles": "^2.4.1",
150152
"eslint": "^7.19.0",
151153
"eslint-plugin-react-hooks": "^4.6.2",
154+
"happy-dom": "^17.4.7",
152155
"husky": "^4.3.0",
153-
"jsdom": "^22.1.0",
154156
"path-to-regexp": "^6.2.1",
155157
"preact": "^10.23.2",
156158
"preact-render-to-string": "^6.5.9",
157159
"prettier": "^2.4.1",
158160
"react": "^18.2.0",
159161
"react-dom": "^18.2.0",
160-
"react-test-renderer": "^18.2.0",
161162
"rimraf": "^3.0.2",
162163
"rollup": "^3.29.5",
163164
"size-limit": "^10.0.1",

packages/wouter-preact/vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ export default defineProject({
55
plugins: [preact()],
66
test: {
77
name: "wouter-preact",
8-
environment: "jsdom",
8+
environment: "happy-dom",
99
},
1010
});

packages/wouter/test/link.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ describe("<Link />", () => {
163163

164164
fireEvent.click(getByTestId("link"));
165165
expect(location.pathname).toBe("/goo-baz");
166-
expect(history.state).toBe(testState);
166+
expect(history.state).toStrictEqual(testState);
167167
});
168168

169169
it("can be configured to use custom href formatting", () => {

packages/wouter/test/redirect.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ it("supports history state", () => {
6666
const { unmount } = render(<Redirect to="/users" state={testState} />);
6767

6868
expect(location.pathname).toBe("/users");
69-
expect(history.state).toBe(testState);
69+
expect(history.state).toStrictEqual(testState);
7070
unmount();
7171
});
7272

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,89 @@
1-
import { it, expect } from "vitest";
2-
import { render, act } from "@testing-library/react";
3-
import * as TestRenderer from "react-test-renderer";
1+
import { it, expect, afterEach } from "vitest";
2+
import { render, act, cleanup } from "@testing-library/react";
43

54
import { Router, Route } from "wouter";
65
import { memoryLocation } from "wouter/memory-location";
76
import { ReactElement } from "react";
87

8+
// Clean up after each test to avoid DOM pollution
9+
afterEach(cleanup);
10+
911
const testRouteRender = (initialPath: string, jsx: ReactElement) => {
10-
const instance = TestRenderer.create(
12+
return render(
1113
<Router hook={memoryLocation({ path: initialPath }).hook}>{jsx}</Router>
12-
).root;
13-
14-
return instance;
14+
);
1515
};
1616

1717
it("always renders its content when `path` is empty", () => {
18-
const result = testRouteRender(
18+
const { container } = testRouteRender(
1919
"/nothing",
2020
<Route>
2121
<h1>Hello!</h1>
2222
</Route>
2323
);
2424

25-
expect(result.findByType("h1").props.children).toBe("Hello!");
25+
const heading = container.querySelector("h1");
26+
expect(heading).toBeInTheDocument();
27+
expect(heading).toHaveTextContent("Hello!");
2628
});
2729

2830
it("accepts plain children", () => {
29-
const result = testRouteRender(
31+
const { container } = testRouteRender(
3032
"/foo",
3133
<Route path="/foo">
3234
<h1>Hello!</h1>
3335
</Route>
3436
);
3537

36-
expect(result.findByType("h1").props.children).toBe("Hello!");
38+
const heading = container.querySelector("h1");
39+
expect(heading).toBeInTheDocument();
40+
expect(heading).toHaveTextContent("Hello!");
3741
});
3842

3943
it("works with render props", () => {
40-
const result = testRouteRender(
44+
const { container } = testRouteRender(
4145
"/foo",
4246
<Route path="/foo">{() => <h1>Hello!</h1>}</Route>
4347
);
4448

45-
expect(result.findByType("h1").props.children).toBe("Hello!");
49+
const heading = container.querySelector("h1");
50+
expect(heading).toBeInTheDocument();
51+
expect(heading).toHaveTextContent("Hello!");
4652
});
4753

4854
it("passes a match param object to the render function", () => {
49-
const result = testRouteRender(
55+
const { container } = testRouteRender(
5056
"/users/alex",
5157
<Route path="/users/:name">{(params) => <h1>{params.name}</h1>}</Route>
5258
);
5359

54-
expect(result.findByType("h1").props.children).toBe("alex");
60+
const heading = container.querySelector("h1");
61+
expect(heading).toBeInTheDocument();
62+
expect(heading).toHaveTextContent("alex");
5563
});
5664

5765
it("renders nothing when there is not match", () => {
58-
const result = testRouteRender(
66+
const { container } = testRouteRender(
5967
"/bar",
6068
<Route path="/foo">
6169
<div>Hi!</div>
6270
</Route>
6371
);
6472

65-
expect(() => result.findByType("div")).toThrow();
73+
expect(container.querySelector("div")).not.toBeInTheDocument();
6674
});
6775

6876
it("supports `component` prop similar to React-Router", () => {
6977
const Users = () => <h2>All users</h2>;
7078

71-
const result = testRouteRender(
79+
const { container } = testRouteRender(
7280
"/foo",
7381
<Route path="/foo" component={Users} />
7482
);
7583

76-
expect(result.findByType("h2").props.children).toBe("All users");
84+
const heading = container.querySelector("h2");
85+
expect(heading).toBeInTheDocument();
86+
expect(heading).toHaveTextContent("All users");
7787
});
7888

7989
it("supports `base` routers with relative path", () => {
@@ -90,52 +100,58 @@ it("supports `base` routers with relative path", () => {
90100

91101
act(() => history.replaceState(null, "", "/app/nested"));
92102

93-
expect(container.childNodes.length).toBe(1);
94-
expect((container.firstChild as HTMLElement).tagName).toBe("H1");
103+
expect(container.children).toHaveLength(1);
104+
expect(container.firstChild).toHaveProperty("tagName", "H1");
95105

96106
unmount();
97107
});
98108

99109
it("supports `path` prop with regex", () => {
100-
const result = testRouteRender(
110+
const { container } = testRouteRender(
101111
"/foo",
102112
<Route path={/[/]foo/}>
103113
<h1>Hello!</h1>
104114
</Route>
105115
);
106116

107-
expect(result.findByType("h1").props.children).toBe("Hello!");
117+
const heading = container.querySelector("h1");
118+
expect(heading).toBeInTheDocument();
119+
expect(heading).toHaveTextContent("Hello!");
108120
});
109121

110122
it("supports regex path named params", () => {
111-
const result = testRouteRender(
123+
const { container } = testRouteRender(
112124
"/users/alex",
113125
<Route path={/[/]users[/](?<name>[a-z]+)/}>
114126
{(params) => <h1>{params.name}</h1>}
115127
</Route>
116128
);
117129

118-
expect(result.findByType("h1").props.children).toBe("alex");
130+
const heading = container.querySelector("h1");
131+
expect(heading).toBeInTheDocument();
132+
expect(heading).toHaveTextContent("alex");
119133
});
120134

121135
it("supports regex path anonymous params", () => {
122-
const result = testRouteRender(
136+
const { container } = testRouteRender(
123137
"/users/alex",
124138
<Route path={/[/]users[/]([a-z]+)/}>
125139
{(params) => <h1>{params[0]}</h1>}
126140
</Route>
127141
);
128142

129-
expect(result.findByType("h1").props.children).toBe("alex");
143+
const heading = container.querySelector("h1");
144+
expect(heading).toBeInTheDocument();
145+
expect(heading).toHaveTextContent("alex");
130146
});
131147

132148
it("rejects when a path does not match the regex", () => {
133-
const result = testRouteRender(
149+
const { container } = testRouteRender(
134150
"/users/1234",
135151
<Route path={/[/]users[/](?<name>[a-z]+)/}>
136152
{(params) => <h1>{params.name}</h1>}
137153
</Route>
138154
);
139155

140-
expect(() => result.findByType("h1")).toThrow();
156+
expect(container.querySelector("h1")).not.toBeInTheDocument();
141157
});

packages/wouter/test/router.test.tsx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { memo, ReactElement, cloneElement, ComponentProps } from "react";
22
import { renderHook, render } from "@testing-library/react";
3-
import * as TestRenderer from "react-test-renderer";
43
import { it, expect, describe } from "vitest";
54
import {
65
Router,
@@ -91,25 +90,24 @@ it("can extract `ssrSearch` from `ssrPath` after the '?' symbol", () => {
9190
});
9291

9392
it("shares one router instance between components", () => {
94-
const RouterGetter = ({ el }: { el: ReactElement }) => {
93+
const routers: any[] = [];
94+
95+
const RouterGetter = ({ index }: { index: number }) => {
9596
const router = useRouter();
96-
return cloneElement(el, { router: router });
97+
routers[index] = router;
98+
return <div data-testid={`router-${index}`} />;
9799
};
98100

99-
const { root } = TestRenderer.create(
101+
render(
100102
<>
101-
<RouterGetter el={<div />} />
102-
<RouterGetter el={<div />} />
103-
<RouterGetter el={<div />} />
104-
<RouterGetter el={<div />} />
103+
<RouterGetter index={0} />
104+
<RouterGetter index={1} />
105+
<RouterGetter index={2} />
106+
<RouterGetter index={3} />
105107
</>
106108
);
107109

108-
const uniqRouters = [
109-
...new Set<DefaultParams>(
110-
root.findAllByType("div").map((x) => x.props.router)
111-
),
112-
];
110+
const uniqRouters = [...new Set<DefaultParams>(routers)];
113111
expect(uniqRouters.length).toBe(1);
114112
});
115113

0 commit comments

Comments
 (0)