-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Expand file tree
/
Copy pathcatch-boundary-test.ts
More file actions
379 lines (333 loc) · 12.2 KB
/
Copy pathcatch-boundary-test.ts
File metadata and controls
379 lines (333 loc) · 12.2 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { test, expect } from "@playwright/test";
import {
createAppFixture,
createFixture,
js,
} from "./helpers/create-fixture.js";
import type { Fixture, AppFixture } from "./helpers/create-fixture.js";
import { PlaywrightFixture } from "./helpers/playwright-fixture.js";
test.describe("ErrorBoundary (thrown responses)", () => {
let fixture: Fixture;
let appFixture: AppFixture;
let originalConsoleError: typeof console.error;
let originalConsoleWarn: typeof console.warn;
let ROOT_BOUNDARY_TEXT = "ROOT_TEXT" as const;
let OWN_BOUNDARY_TEXT = "OWN_BOUNDARY_TEXT" as const;
let HAS_BOUNDARY_LOADER = "/yes/loader" as const;
let HAS_BOUNDARY_LOADER_FILE = "/yes.loader" as const;
let HAS_BOUNDARY_ACTION = "/yes/action" as const;
let HAS_BOUNDARY_ACTION_FILE = "/yes.action" as const;
let NO_BOUNDARY_ACTION = "/no/action" as const;
let NO_BOUNDARY_ACTION_FILE = "/no.action" as const;
let NO_BOUNDARY_LOADER = "/no/loader" as const;
let NO_BOUNDARY_LOADER_FILE = "/no.loader" as const;
let NOT_FOUND_HREF = "/not/found";
test.beforeAll(async () => {
fixture = await createFixture({
files: {
"app/root.tsx": js`
import { Links, Meta, Outlet, Scripts, useMatches } from "react-router";
export function loader() {
return { data: "ROOT LOADER" };
}
export default function Root() {
return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<body>
<Outlet />
<Scripts />
</body>
</html>
);
}
export function ErrorBoundary() {
let matches = useMatches()
return (
<html>
<head />
<body>
<div id="root-boundary">${ROOT_BOUNDARY_TEXT}</div>
<pre id="matches">{JSON.stringify(matches)}</pre>
<Scripts />
</body>
</html>
)
}
`,
"app/routes/_index.tsx": js`
import { Link, Form } from "react-router";
export default function() {
return (
<div>
<Link to="${NOT_FOUND_HREF}">${NOT_FOUND_HREF}</Link>
<Form method="post">
<button formAction="${HAS_BOUNDARY_ACTION}" type="submit" />
<button formAction="${NO_BOUNDARY_ACTION}" type="submit" />
</Form>
<Link to="${HAS_BOUNDARY_LOADER}">
${HAS_BOUNDARY_LOADER}
</Link>
<Link to="${HAS_BOUNDARY_LOADER}/child">
${HAS_BOUNDARY_LOADER}/child
</Link>
<Link to="${NO_BOUNDARY_LOADER}">
${NO_BOUNDARY_LOADER}
</Link>
</div>
)
}
`,
[`app/routes${HAS_BOUNDARY_ACTION_FILE}.jsx`]: js`
import { Form } from "react-router";
export async function action() {
throw new Response("", { status: 401 })
}
export function ErrorBoundary() {
return <p id="action-boundary">${OWN_BOUNDARY_TEXT}</p>
}
export default function Index() {
return (
<Form method="post">
<button type="submit" formAction="${HAS_BOUNDARY_ACTION}">
Go
</button>
</Form>
);
}
`,
[`app/routes${NO_BOUNDARY_ACTION_FILE}.jsx`]: js`
import { Form } from "react-router";
export function action() {
throw new Response("", { status: 401 })
}
export default function Index() {
return (
<Form method="post">
<button type="submit" formAction="${NO_BOUNDARY_ACTION}">
Go
</button>
</Form>
)
}
`,
[`app/routes${HAS_BOUNDARY_LOADER_FILE}.jsx`]: js`
import { useRouteError } from "react-router";
export function loader() {
throw new Response("", { status: 401 })
}
export function ErrorBoundary() {
let error = useRouteError();
return (
<>
<div id="boundary-loader">${OWN_BOUNDARY_TEXT}</div>
<pre id="status">{error.status}</pre>
</>
);
}
export default function Index() {
return <div/>
}
`,
[`app/routes${HAS_BOUNDARY_LOADER_FILE}.child.jsx`]: js`
export function loader() {
throw new Response("", { status: 404 })
}
export default function Index() {
return <div/>
}
`,
[`app/routes${NO_BOUNDARY_LOADER_FILE}.jsx`]: js`
export function loader() {
throw new Response("", { status: 401 })
}
export default function Index() {
return <div/>
}
`,
"app/routes/action.tsx": js`
import { Outlet, useLoaderData } from "react-router";
export function loader() {
return "PARENT";
}
export default function () {
return (
<div>
<p id="parent-data">{useLoaderData()}</p>
<Outlet />
</div>
)
}
`,
"app/routes/action.child-catch.tsx": js`
import { Form, useLoaderData, useRouteError } from "react-router";
export function loader() {
return "CHILD";
}
export function action() {
throw new Response("Caught!", { status: 400 });
}
export default function () {
return (
<>
<p id="child-data">{useLoaderData()}</p>
<Form method="post" reloadDocument={true}>
<button type="submit" name="key" value="value">
Submit
</button>
</Form>
</>
)
}
export function ErrorBoundary() {
let error = useRouteError()
return <p id="child-catch">{error.status} {error.data}</p>;
}
`,
},
});
appFixture = await createAppFixture(fixture);
originalConsoleError = console.error;
console.error = () => {};
originalConsoleWarn = console.warn;
console.warn = () => {};
});
test.afterAll(() => {
appFixture.close();
console.error = originalConsoleError;
console.warn = originalConsoleWarn;
});
test("non-matching urls on document requests", async () => {
let oldConsoleError;
oldConsoleError = console.error;
console.error = () => {};
let res = await fixture.requestDocument(NOT_FOUND_HREF);
expect(res.status).toBe(404);
let html = await res.text();
expect(html).toMatch(ROOT_BOUNDARY_TEXT);
// There should be no loader data on the root route
let expected = JSON.stringify([
{ id: "root", pathname: "", params: {} },
]).replace(/"/g, """);
expect(html).toContain(`<pre id="matches">${expected}</pre>`);
console.error = oldConsoleError;
});
test("non-matching urls on client transitions", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink(NOT_FOUND_HREF, { wait: false });
await page.waitForSelector("#root-boundary");
// Root loader data sticks around from previous load
let expected = JSON.stringify([
{
id: "root",
pathname: "",
params: {},
loaderData: { data: "ROOT LOADER" },
},
]);
expect(await app.getHtml("#matches")).toContain(expected);
});
test("own boundary, action, document request", async () => {
let params = new URLSearchParams();
let res = await fixture.postDocument(HAS_BOUNDARY_ACTION, params);
expect(res.status).toBe(401);
expect(await res.text()).toMatch(OWN_BOUNDARY_TEXT);
});
test("own boundary, action, client transition from other route", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickSubmitButton(HAS_BOUNDARY_ACTION);
await page.waitForSelector("#action-boundary");
});
test("own boundary, action, client transition from itself", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto(HAS_BOUNDARY_ACTION);
await app.clickSubmitButton(HAS_BOUNDARY_ACTION);
await page.waitForSelector("#action-boundary");
});
test("bubbles to parent in action document requests", async () => {
let params = new URLSearchParams();
let res = await fixture.postDocument(NO_BOUNDARY_ACTION, params);
expect(res.status).toBe(401);
expect(await res.text()).toMatch(ROOT_BOUNDARY_TEXT);
});
test("bubbles to parent in action script transitions from other routes", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickSubmitButton(NO_BOUNDARY_ACTION);
await page.waitForSelector("#root-boundary");
});
test("bubbles to parent in action script transitions from self", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto(NO_BOUNDARY_ACTION);
await app.clickSubmitButton(NO_BOUNDARY_ACTION);
await page.waitForSelector("#root-boundary");
});
test("own boundary, loader, document request", async () => {
let res = await fixture.requestDocument(HAS_BOUNDARY_LOADER);
expect(res.status).toBe(401);
expect(await res.text()).toMatch(OWN_BOUNDARY_TEXT);
});
test("own boundary, loader, client transition", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink(HAS_BOUNDARY_LOADER);
await page.waitForSelector("#boundary-loader");
});
test("bubbles to parent in loader document requests", async () => {
let res = await fixture.requestDocument(NO_BOUNDARY_LOADER);
expect(res.status).toBe(401);
expect(await res.text()).toMatch(ROOT_BOUNDARY_TEXT);
});
test("bubbles to parent in loader transitions from other routes", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink(NO_BOUNDARY_LOADER);
await page.waitForSelector("#root-boundary");
});
test("uses correct catch boundary on server action errors", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto(`/action/child-catch`);
expect(await app.getHtml("#parent-data")).toMatch("PARENT");
expect(await app.getHtml("#child-data")).toMatch("CHILD");
await page.click("button[type=submit]");
await page.waitForSelector("#child-catch");
// Preserves parent loader data
expect(await app.getHtml("#parent-data")).toMatch("PARENT");
expect(await app.getHtml("#child-catch")).toMatch("400");
expect(await app.getHtml("#child-catch")).toMatch("Caught!");
});
test("prefers parent catch when child loader also bubbles, document request", async () => {
let res = await fixture.requestDocument(`${HAS_BOUNDARY_LOADER}/child`);
expect(res.status).toBe(401);
let text = await res.text();
expect(text).toMatch(OWN_BOUNDARY_TEXT);
expect(text).toMatch('<pre id="status">401</pre>');
});
test("prefers parent catch when child loader also bubbles, client transition", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink(`${HAS_BOUNDARY_LOADER}/child`);
await page.waitForSelector("#boundary-loader");
expect(await app.getHtml("#boundary-loader")).toMatch(OWN_BOUNDARY_TEXT);
expect(await app.getHtml("#status")).toMatch("401");
});
});