forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatches-test.ts
196 lines (182 loc) · 5.82 KB
/
matches-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { test, expect } from "@playwright/test";
import { createAppFixture, createFixture, js } from "./helpers/create-fixture";
import type { Fixture, AppFixture } from "./helpers/create-fixture";
import { PlaywrightFixture } from "./helpers/playwright-fixture";
test.describe("useMatches", () => {
let fixture: Fixture;
let appFixture: AppFixture;
test.beforeAll(async () => {
fixture = await createFixture({
files: {
"app/root.jsx": js`
import * as React from 'react';
import { json } from "@remix-run/node";
import { Link, Links, Meta, Outlet, Scripts, useMatches } from "@remix-run/react";
export const handle = { stuff: "root handle"};
export const loader = () => json("ROOT");
export default function Root() {
let matches = useMatches();
let [matchesCount, setMatchesCount] = React.useState(0);
React.useEffect(() => setMatchesCount(matchesCount + 1), [matches]);
return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<body>
<Link to="/about">About</Link>
<pre id="matches">
{JSON.stringify(matches, null, 2)}
</pre>
{matchesCount > 0 ? <pre id="matches-count-root">{matchesCount}</pre> : null}
<Outlet />
<Scripts />
</body>
</html>
);
}
`,
"app/routes/index.jsx": js`
import { json } from "@remix-run/node";
export const handle = { stuff: "index handle"};
export const loader = () => json("INDEX");
export default function Index() {
return <h1 id="index">Index Page</h1>
}
`,
"app/routes/about.jsx": js`
import { json } from "@remix-run/node";
export const handle = { stuff: "about handle"};
export const loader = async () => {
await new Promise(r => setTimeout(r, 100));
return json("ABOUT");
}
export default function About() {
return <h1 id="about">About Page</h1>
}
`,
"app/routes/count.jsx": js`
import * as React from 'react';
import { useMatches } from "@remix-run/react";
export default function Count() {
let matches = useMatches();
let [count, setCount] = React.useState(0);
let [matchesCount, setMatchesCount] = React.useState(0);
React.useEffect(() => setMatchesCount(matchesCount + 1), [matches]);
return (
<>
<h1>Count Page</h1>
<button id="increment" onClick={() => setCount(count + 1)}>
Increment
</button>
<pre id="count">{count}</pre>
{matchesCount > 0 ? <pre id="matches-count-child">{matchesCount}</pre> : null}
</>
);
}
`,
},
});
appFixture = await createAppFixture(fixture);
});
test.afterAll(() => {
appFixture.close();
});
test("grabs the handle from the route module cache", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
// Wait for effect
await page.waitForSelector("#matches-count-root");
expect(await app.getHtml("#matches-count-root")).toMatch(">1<");
expect(await app.getHtml()).toMatch("Index Page");
expect(await app.getHtml("#matches")).toEqual(`<pre id="matches">
[
{
"id": "root",
"pathname": "/",
"params": {},
"data": "ROOT",
"handle": {
"stuff": "root handle"
}
},
{
"id": "routes/index",
"pathname": "/",
"params": {},
"data": "INDEX",
"handle": {
"stuff": "index handle"
}
}
]</pre
>`);
// Click and don't wait so we can assert _during_ the navigation that we're
// still showing the index matches and we haven't triggered a new effect
await app.clickLink("/about", { wait: false });
expect(await app.getHtml("#matches")).toEqual(`<pre id="matches">
[
{
"id": "root",
"pathname": "/",
"params": {},
"data": "ROOT",
"handle": {
"stuff": "root handle"
}
},
{
"id": "routes/index",
"pathname": "/",
"params": {},
"data": "INDEX",
"handle": {
"stuff": "index handle"
}
}
]</pre
>`);
expect(await app.getHtml("#matches-count-root")).toMatch(">1<");
// Once the new page shows up we should get update dmatches and a single
// new effect execution
await page.waitForSelector("#about");
expect(await app.getHtml()).toMatch("About Page");
expect(await app.getHtml("#matches-count-root")).toMatch(">2<");
expect(await app.getHtml("#matches")).toEqual(`<pre id="matches">
[
{
"id": "root",
"pathname": "/",
"params": {},
"data": "ROOT",
"handle": {
"stuff": "root handle"
}
},
{
"id": "routes/about",
"pathname": "/about",
"params": {},
"data": "ABOUT",
"handle": {
"stuff": "about handle"
}
}
]</pre
>`);
});
test("memoizes matches from react router", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/count");
await page.waitForSelector("#matches-count-child");
expect(await app.getHtml("#count")).toMatch(">0<");
expect(await app.getHtml("#matches-count-child")).toMatch(">1<");
await app.clickElement("#increment");
expect(await app.getHtml("#count")).toMatch(">1<");
expect(await app.getHtml("#matches-count-child")).toMatch(">1<");
await app.clickElement("#increment");
expect(await app.getHtml("#count")).toMatch(">2<");
expect(await app.getHtml("#matches-count-child")).toMatch(">1<");
});
});