-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathcatch-boundary-data-test.ts
243 lines (214 loc) · 7.47 KB
/
catch-boundary-data-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
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";
let fixture: Fixture;
let appFixture: AppFixture;
let ROOT_BOUNDARY_TEXT = "ROOT_TEXT" as const;
let LAYOUT_BOUNDARY_TEXT = "LAYOUT_BOUNDARY_TEXT" as const;
let OWN_BOUNDARY_TEXT = "OWN_BOUNDARY_TEXT" as const;
let NO_BOUNDARY_LOADER_FILE = "/no.loader" as const;
let NO_BOUNDARY_LOADER = "/no/loader" as const;
let HAS_BOUNDARY_LAYOUT_NESTED_LOADER_FILE =
"/yes.loader-layout-boundary" as const;
let HAS_BOUNDARY_LAYOUT_NESTED_LOADER = "/yes/loader-layout-boundary" as const;
let HAS_BOUNDARY_NESTED_LOADER_FILE = "/yes.loader-self-boundary" as const;
let HAS_BOUNDARY_NESTED_LOADER = "/yes/loader-self-boundary" as const;
let ROOT_DATA = "root data";
let LAYOUT_DATA = "root data";
test.describe("ErrorBoundary (thrown responses)", () => {
test.beforeEach(async ({ context }) => {
await context.route(/.data/, async (route) => {
await new Promise((resolve) => setTimeout(resolve, 50));
route.continue();
});
});
test.beforeAll(async () => {
fixture = await createFixture({
files: {
"app/root.tsx": js`
import {
Links,
Meta,
Outlet,
Scripts,
useLoaderData,
useMatches,
} from "react-router";
export const loader = () => "${ROOT_DATA}";
export default function Root() {
const data = useLoaderData();
return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<body>
<div id="root-data">{data}</div>
<Outlet />
<Scripts />
</body>
</html>
);
}
export function ErrorBoundary() {
let matches = useMatches();
let { data } = matches.find(match => match.id === "root");
return (
<html>
<head />
<body>
<div id="root-boundary">${ROOT_BOUNDARY_TEXT}</div>
<div id="root-boundary-data">{data}</div>
<Scripts />
</body>
</html>
);
}
`,
"app/routes/_index.tsx": js`
import { Link } from "react-router";
export default function Index() {
return (
<div>
<Link to="${NO_BOUNDARY_LOADER}">${NO_BOUNDARY_LOADER}</Link>
<Link to="${HAS_BOUNDARY_LAYOUT_NESTED_LOADER}">${HAS_BOUNDARY_LAYOUT_NESTED_LOADER}</Link>
<Link to="${HAS_BOUNDARY_NESTED_LOADER}">${HAS_BOUNDARY_NESTED_LOADER}</Link>
</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${HAS_BOUNDARY_LAYOUT_NESTED_LOADER_FILE}.jsx`]: js`
import { useMatches } from "react-router";
export function loader() {
return "${LAYOUT_DATA}";
}
export default function Layout() {
return <div/>;
}
export function ErrorBoundary() {
let matches = useMatches();
let { data } = matches.find(match => match.id === "routes${HAS_BOUNDARY_LAYOUT_NESTED_LOADER_FILE}");
return (
<div>
<div id="layout-boundary">${LAYOUT_BOUNDARY_TEXT}</div>
<div id="layout-boundary-data">{data}</div>
</div>
);
}
`,
[`app/routes${HAS_BOUNDARY_LAYOUT_NESTED_LOADER_FILE}._index.jsx`]: js`
export function loader() {
throw new Response("", { status: 401 });
}
export default function Index() {
return <div/>;
}
`,
[`app/routes${HAS_BOUNDARY_NESTED_LOADER_FILE}.jsx`]: js`
import { Outlet, useLoaderData } from "react-router";
export function loader() {
return "${LAYOUT_DATA}";
}
export default function Layout() {
let data = useLoaderData();
return (
<div>
<div id="layout-data">{data}</div>
<Outlet/>
</div>
);
}
`,
[`app/routes${HAS_BOUNDARY_NESTED_LOADER_FILE}._index.jsx`]: js`
export function loader() {
throw new Response("", { status: 401 });
}
export default function Index() {
return <div/>;
}
export function ErrorBoundary() {
return (
<div id="own-boundary">${OWN_BOUNDARY_TEXT}</div>
);
}
`,
},
});
appFixture = await createAppFixture(fixture);
});
test.afterAll(() => {
appFixture.close();
});
test("renders root boundary with data available", async () => {
let res = await fixture.requestDocument(NO_BOUNDARY_LOADER);
expect(res.status).toBe(401);
let html = await res.text();
expect(html).toMatch(ROOT_BOUNDARY_TEXT);
expect(html).toMatch(ROOT_DATA);
});
test("renders root boundary with data available on transition", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink(NO_BOUNDARY_LOADER);
await page.waitForSelector("#root-boundary");
await page.waitForSelector(`#root-boundary-data:has-text("${ROOT_DATA}")`);
});
test("renders layout boundary with data available", async () => {
let res = await fixture.requestDocument(HAS_BOUNDARY_LAYOUT_NESTED_LOADER);
expect(res.status).toBe(401);
let html = await res.text();
expect(html).toMatch(ROOT_DATA);
expect(html).toMatch(LAYOUT_BOUNDARY_TEXT);
expect(html).toMatch(LAYOUT_DATA);
});
test("renders layout boundary with data available on transition", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink(HAS_BOUNDARY_LAYOUT_NESTED_LOADER);
await page.waitForSelector(`#root-data:has-text("${ROOT_DATA}")`);
await page.waitForSelector(
`#layout-boundary:has-text("${LAYOUT_BOUNDARY_TEXT}")`
);
await page.waitForSelector(
`#layout-boundary-data:has-text("${LAYOUT_DATA}")`
);
});
test("renders self boundary with layout data available", async () => {
let res = await fixture.requestDocument(HAS_BOUNDARY_NESTED_LOADER);
expect(res.status).toBe(401);
let html = await res.text();
expect(html).toMatch(ROOT_DATA);
expect(html).toMatch(LAYOUT_DATA);
expect(html).toMatch(OWN_BOUNDARY_TEXT);
});
test("renders self boundary with layout data available on transition", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink(HAS_BOUNDARY_NESTED_LOADER);
await page.waitForSelector(`#root-data:has-text("${ROOT_DATA}")`);
await page.waitForSelector(`#layout-data:has-text("${LAYOUT_DATA}")`);
await page.waitForSelector(
`#own-boundary:has-text("${OWN_BOUNDARY_TEXT}")`
);
});
});