forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror-boundary-test.ts
480 lines (418 loc) · 14.7 KB
/
error-boundary-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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
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("ErrorBoundary", () => {
let fixture: Fixture;
let appFixture: AppFixture;
let _consoleError: any;
let ROOT_BOUNDARY_TEXT = "ROOT_BOUNDARY_TEXT";
let OWN_BOUNDARY_TEXT = "OWN_BOUNDARY_TEXT";
let HAS_BOUNDARY_LOADER = "/yes/loader";
let HAS_BOUNDARY_ACTION = "/yes/action";
let HAS_BOUNDARY_RENDER = "/yes/render";
let NO_BOUNDARY_ACTION = "/no/action";
let NO_BOUNDARY_LOADER = "/no/loader";
let NO_BOUNDARY_RENDER = "/no/render";
let NOT_FOUND_HREF = "/not/found";
// packages/remix-react/errorBoundaries.tsx
let INTERNAL_ERROR_BOUNDARY_HEADING = "Application Error";
test.beforeAll(async () => {
_consoleError = console.error;
console.error = () => {};
fixture = await createFixture({
files: {
"app/root.jsx": js`
import { Links, Meta, Outlet, Scripts } from "@remix-run/react";
export default function Root() {
return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<body>
<main>
<Outlet />
</main>
<Scripts />
</body>
</html>
);
}
export function ErrorBoundary() {
return (
<html>
<head />
<body>
<main>
<div>${ROOT_BOUNDARY_TEXT}</div>
</main>
<Scripts />
</body>
</html>
)
}
`,
"app/routes/index.jsx": js`
import { Link, Form } from "@remix-run/react";
export default function () {
return (
<div>
<Link to="${NOT_FOUND_HREF}">${NOT_FOUND_HREF}</Link>
<Form method="post">
<button formAction="${HAS_BOUNDARY_ACTION}" type="submit">
Own Boundary
</button>
<button formAction="${NO_BOUNDARY_ACTION}" type="submit">
No Boundary
</button>
</Form>
<Link to="${HAS_BOUNDARY_LOADER}">
${HAS_BOUNDARY_LOADER}
</Link>
<Link to="${NO_BOUNDARY_LOADER}">
${NO_BOUNDARY_LOADER}
</Link>
<Link to="${HAS_BOUNDARY_RENDER}">
${HAS_BOUNDARY_RENDER}
</Link>
<Link to="${NO_BOUNDARY_RENDER}">
${NO_BOUNDARY_RENDER}
</Link>
</div>
)
}
`,
[`app/routes${HAS_BOUNDARY_ACTION}.jsx`]: js`
import { Form } from "@remix-run/react";
export async function action() {
throw new Error("Kaboom!")
}
export function ErrorBoundary() {
return <p>${OWN_BOUNDARY_TEXT}</p>
}
export default function () {
return (
<Form method="post">
<button type="submit" formAction="${HAS_BOUNDARY_ACTION}">
Go
</button>
</Form>
);
}
`,
[`app/routes${NO_BOUNDARY_ACTION}.jsx`]: js`
import { Form } from "@remix-run/react";
export function action() {
throw new Error("Kaboom!")
}
export default function () {
return (
<Form method="post">
<button type="submit" formAction="${NO_BOUNDARY_ACTION}">
Go
</button>
</Form>
)
}
`,
[`app/routes${HAS_BOUNDARY_LOADER}.jsx`]: js`
export function loader() {
throw new Error("Kaboom!")
}
export function ErrorBoundary() {
return <div>${OWN_BOUNDARY_TEXT}</div>
}
export default function () {
return <div/>
}
`,
[`app/routes${NO_BOUNDARY_LOADER}.jsx`]: js`
export function loader() {
throw new Error("Kaboom!")
}
export default function () {
return <div/>
}
`,
[`app/routes${NO_BOUNDARY_RENDER}.jsx`]: js`
export default function () {
throw new Error("Kaboom!")
return <div/>
}
`,
[`app/routes${HAS_BOUNDARY_RENDER}.jsx`]: js`
export default function () {
throw new Error("Kaboom!")
return <div/>
}
export function ErrorBoundary() {
return <div>${OWN_BOUNDARY_TEXT}</div>
}
`,
"app/routes/action.jsx": js`
import { Outlet, useLoaderData } from "@remix-run/react";
export function loader() {
return "PARENT";
}
export default function () {
return (
<div>
<p id="parent-data">{useLoaderData()}</p>
<Outlet />
</div>
)
}
`,
"app/routes/action/child-error.jsx": js`
import { Form, useLoaderData } from "@remix-run/react";
export function loader() {
return "CHILD";
}
export function action() {
throw new Error("Broken!");
}
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({ error }) {
return <p id="child-error">{error.message}</p>;
}
`,
},
});
appFixture = await createAppFixture(fixture);
});
test.afterAll(async () => {
console.error = _consoleError;
await appFixture.close();
});
test("own boundary, action, document request", async () => {
let params = new URLSearchParams();
let res = await fixture.postDocument(HAS_BOUNDARY_ACTION, params);
expect(res.status).toBe(500);
expect(await res.text()).toMatch(OWN_BOUNDARY_TEXT);
});
// FIXME: this is broken, test renders the root boundary logging in `RemixRoute`
// test's because the route module hasn't been loaded, my gut tells me that we
// didn't load the route module but tried to render test's boundary, we need the
// module for that! this will probably fix the twin test over in
// catch-boundary-test
test.skip("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);
expect(await app.getHtml("main")).toMatch(OWN_BOUNDARY_TEXT);
});
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);
expect(await app.getHtml("main")).toMatch(OWN_BOUNDARY_TEXT);
});
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(500);
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);
expect(await app.getHtml("main")).toMatch(ROOT_BOUNDARY_TEXT);
});
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);
expect(await app.getHtml("main")).toMatch(ROOT_BOUNDARY_TEXT);
});
test("own boundary, loader, document request", async () => {
let res = await fixture.requestDocument(HAS_BOUNDARY_LOADER);
expect(res.status).toBe(500);
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);
expect(await app.getHtml("main")).toMatch(OWN_BOUNDARY_TEXT);
});
test("bubbles to parent in loader document requests", async () => {
let res = await fixture.requestDocument(NO_BOUNDARY_LOADER);
expect(res.status).toBe(500);
expect(await res.text()).toMatch(ROOT_BOUNDARY_TEXT);
});
test("bubbles to parent in loader script transitions from other routes", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink(NO_BOUNDARY_LOADER);
expect(await app.getHtml("main")).toMatch(ROOT_BOUNDARY_TEXT);
});
test("ssr rendering errors with no boundary", async () => {
let res = await fixture.requestDocument(NO_BOUNDARY_RENDER);
expect(res.status).toBe(500);
expect(await res.text()).toMatch(ROOT_BOUNDARY_TEXT);
});
test("script transition rendering errors with no boundary", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink(NO_BOUNDARY_RENDER);
expect(await app.getHtml("main")).toMatch(ROOT_BOUNDARY_TEXT);
});
test("ssr rendering errors with boundary", async () => {
let res = await fixture.requestDocument(HAS_BOUNDARY_RENDER);
expect(res.status).toBe(500);
expect(await res.text()).toMatch(OWN_BOUNDARY_TEXT);
});
test("script transition rendering errors with boundary", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink(HAS_BOUNDARY_RENDER);
expect(await app.getHtml("main")).toMatch(OWN_BOUNDARY_TEXT);
});
test("uses correct error boundary on server action errors in nested routes", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto(`/action/child-error`);
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-error");
// Preserves parent loader data
expect(await app.getHtml("#parent-data")).toMatch("PARENT");
expect(await app.getHtml("#child-error")).toMatch("Broken!");
});
test.describe("if no error boundary exists in the app", () => {
let NO_ROOT_BOUNDARY_LOADER = "/loader-bad";
let NO_ROOT_BOUNDARY_ACTION = "/action-bad";
let NO_ROOT_BOUNDARY_ACTION_RETURN = "/action-no-return";
test.beforeAll(async () => {
fixture = await createFixture({
files: {
"app/root.jsx": js`
import { Links, Meta, Outlet, Scripts } from "@remix-run/react";
export default function Root() {
return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<body>
<Outlet />
<Scripts />
</body>
</html>
);
}
`,
"app/routes/index.jsx": js`
import { Link, Form } from "@remix-run/react";
export default function () {
return (
<div>
<h1>Home</h1>
<Form method="post">
<button formAction="${NO_ROOT_BOUNDARY_ACTION}" type="submit">
Action go boom
</button>
<button formAction="${NO_ROOT_BOUNDARY_ACTION_RETURN}" type="submit">
Action no return
</button>
</Form>
</div>
)
}
`,
[`app/routes${NO_ROOT_BOUNDARY_LOADER}.jsx`]: js`
import { Link, Form } from "@remix-run/react";
export async function loader() {
throw Error("BLARGH");
}
export default function () {
return (
<div>
<h1>Hello</h1>
</div>
)
}
`,
[`app/routes${NO_ROOT_BOUNDARY_ACTION}.jsx`]: js`
import { Link, Form } from "@remix-run/react";
export async function action() {
throw Error("YOOOOOOOO WHAT ARE YOU DOING");
}
export default function () {
return (
<div>
<h1>Goodbye</h1>
</div>
)
}
`,
[`app/routes${NO_ROOT_BOUNDARY_ACTION_RETURN}.jsx`]: js`
import { Link, Form, useActionData } from "@remix-run/react";
export async function action() {}
export default function () {
let data = useActionData();
return (
<div>
<h1>{data}</h1>
</div>
)
}
`,
},
});
appFixture = await createAppFixture(fixture);
});
test("bubbles to internal boundary in loader document requests", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto(NO_ROOT_BOUNDARY_LOADER);
expect(await app.getHtml("h1")).toMatch(INTERNAL_ERROR_BOUNDARY_HEADING);
});
test("bubbles to internal boundary in action script transitions from other routes", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickSubmitButton(NO_ROOT_BOUNDARY_ACTION);
expect(await app.getHtml("h1")).toMatch(INTERNAL_ERROR_BOUNDARY_HEADING);
});
test("bubbles to internal boundary if action doesn't return", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickSubmitButton(NO_ROOT_BOUNDARY_ACTION_RETURN);
expect(await app.getHtml("h1")).toMatch(INTERNAL_ERROR_BOUNDARY_HEADING);
});
});
});