-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
catch-boundary-test.ts
333 lines (292 loc) · 10.4 KB
/
catch-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
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("CatchBoundary", () => {
let fixture: Fixture;
let appFixture: AppFixture;
let ROOT_BOUNDARY_TEXT = "ROOT_TEXT";
let OWN_BOUNDARY_TEXT = "OWN_BOUNDARY_TEXT";
let HAS_BOUNDARY_LOADER = "/yes/loader";
let HAS_BOUNDARY_ACTION = "/yes/action";
let NO_BOUNDARY_ACTION = "/no/action";
let NO_BOUNDARY_LOADER = "/no/loader";
let NOT_FOUND_HREF = "/not/found";
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>
);
}
export function CatchBoundary() {
return (
<html>
<head />
<body>
<div id="root-boundary">${ROOT_BOUNDARY_TEXT}</div>
<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" />
<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}.jsx`]: js`
import { Form } from "@remix-run/react";
export async function action() {
throw new Response("", { status: 401 })
}
export function CatchBoundary() {
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}.jsx`]: js`
import { Form } from "@remix-run/react";
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}.jsx`]: js`
import { useCatch } from '@remix-run/react';
export function loader() {
throw new Response("", { status: 401 })
}
export function CatchBoundary() {
let caught = useCatch();
return (
<>
<div id="boundary-loader">${OWN_BOUNDARY_TEXT}</div>
<pre id="status">{caught.status}</pre>
</>
);
}
export default function Index() {
return <div/>
}
`,
[`app/routes${HAS_BOUNDARY_LOADER}/child.jsx`]: js`
export function loader() {
throw new Response("", { status: 404 })
}
export default function Index() {
return <div/>
}
`,
[`app/routes${NO_BOUNDARY_LOADER}.jsx`]: js`
export function loader() {
throw new Response("", { status: 401 })
}
export default function Index() {
return <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-catch.jsx": js`
import { Form, useCatch, useLoaderData } from "@remix-run/react";
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 CatchBoundary() {
let caught = useCatch()
return <p id="child-catch">{caught.status} {caught.data}</p>;
}
`,
},
});
appFixture = await createAppFixture(fixture);
});
test.afterAll(() => {
appFixture.close();
});
test("non-matching urls on document requests", async () => {
let res = await fixture.requestDocument(NOT_FOUND_HREF);
expect(res.status).toBe(404);
expect(await res.text()).toMatch(ROOT_BOUNDARY_TEXT);
});
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");
});
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");
});
});