-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathroute-config-test.ts
272 lines (238 loc) · 8.56 KB
/
route-config-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
import fs from "node:fs/promises";
import path from "node:path";
import { expect } from "@playwright/test";
import {
type Files,
createProject,
build,
test,
viteConfig,
createEditor,
viteMajorTemplates,
} from "./helpers/vite.js";
const js = String.raw;
test.describe("route config", () => {
viteMajorTemplates.forEach(({ templateName, templateDisplayName }) => {
test.describe(templateDisplayName, () => {
test("fails the build if route config is missing", async () => {
let cwd = await createProject({}, templateName);
await fs.rm(path.join(cwd, "app/routes.ts"));
let buildResult = build({ cwd });
expect(buildResult.status).toBe(1);
expect(buildResult.stderr.toString()).toContain(
'Route config file not found at "app/routes.ts"'
);
});
test("fails the build if route config is invalid", async () => {
let cwd = await createProject(
{ "app/routes.ts": `export default INVALID(` },
templateName
);
let buildResult = build({ cwd });
expect(buildResult.status).toBe(1);
expect(buildResult.stderr.toString()).toContain(
'Route config in "routes.ts" is invalid.'
);
});
test("fails the dev process if route config is initially invalid", async ({
dev,
}) => {
let files: Files = async ({ port }) => ({
"vite.config.js": await viteConfig.basic({ port }),
"app/routes.ts": `export default INVALID(`,
});
let devError: Error | undefined;
try {
await dev(files);
} catch (error: any) {
devError = error;
}
expect(devError?.toString()).toContain(
'Route config in "routes.ts" is invalid.'
);
});
test("supports correcting an invalid route config", async ({
page,
dev,
}) => {
let files: Files = async ({ port }) => ({
"vite.config.js": await viteConfig.basic({ port }),
"app/routes.ts": js`
import { type RouteConfig, index } from "@react-router/dev/routes";
export default [
index("test-route-1.tsx"),
] satisfies RouteConfig;
`,
"app/test-route-1.tsx": `
export default () => <div data-test-route>Test route 1</div>
`,
"app/test-route-2.tsx": `
export default () => <div data-test-route>Test route 2</div>
`,
});
let { cwd, port } = await dev(files);
await page.goto(`http://localhost:${port}/`, {
waitUntil: "networkidle",
});
await expect(page.locator("[data-test-route]")).toHaveText(
"Test route 1"
);
let edit = createEditor(cwd);
// Make config invalid
await edit("app/routes.ts", (contents) => contents + "INVALID");
// Ensure dev server is still running with old config + HMR
await edit("app/test-route-1.tsx", (contents) =>
contents.replace("Test route 1", "Test route 1 updated")
);
await expect(page.locator("[data-test-route]")).toHaveText(
"Test route 1 updated"
);
// Fix config with new route
await edit("app/routes.ts", (contents) =>
contents
.replace("INVALID", "")
.replace("test-route-1", "test-route-2")
);
await expect(async () => {
// Reload to pick up new route for current path
await page.reload();
await expect(page.locator("[data-test-route]")).toHaveText(
"Test route 2"
);
}).toPass();
});
test("supports correcting an invalid route config module graph", async ({
page,
dev,
}) => {
let files: Files = async ({ port }) => ({
"vite.config.js": await viteConfig.basic({ port }),
"app/routes.ts": js`
export { default } from "./actual-routes";
`,
"app/actual-routes.ts": js`
import { type RouteConfig, index } from "@react-router/dev/routes";
export default [
index("test-route-1.tsx"),
] satisfies RouteConfig;
`,
"app/test-route-1.tsx": `
export default () => <div data-test-route>Test route 1</div>
`,
"app/test-route-2.tsx": `
export default () => <div data-test-route>Test route 2</div>
`,
});
let { cwd, port } = await dev(files);
await page.goto(`http://localhost:${port}/`, {
waitUntil: "networkidle",
});
await expect(page.locator("[data-test-route]")).toHaveText(
"Test route 1"
);
let edit = createEditor(cwd);
// Make config invalid
await edit("app/actual-routes.ts", (contents) => contents + "INVALID");
// Ensure dev server is still running with old config + HMR
await edit("app/test-route-1.tsx", (contents) =>
contents.replace("Test route 1", "Test route 1 updated")
);
await expect(page.locator("[data-test-route]")).toHaveText(
"Test route 1 updated"
);
// Fix config with new route
await edit("app/actual-routes.ts", (contents) =>
contents
.replace("INVALID", "")
.replace("test-route-1", "test-route-2")
);
await expect(async () => {
// Reload to pick up new route for current path
await page.reload();
await expect(page.locator("[data-test-route]")).toHaveText(
"Test route 2"
);
}).toPass();
});
test("supports correcting a missing route config", async ({
page,
dev,
}) => {
let files: Files = async ({ port }) => ({
"vite.config.js": await viteConfig.basic({ port }),
"app/routes.ts": js`
import { type RouteConfig, index } from "@react-router/dev/routes";
export default [
index("test-route-1.tsx"),
] satisfies RouteConfig;
`,
"app/test-route-1.tsx": js`
export default () => <div data-test-route>Test route 1</div>
`,
"app/test-route-2.tsx": js`
export default () => <div data-test-route>Test route 2</div>
`,
});
let { cwd, port } = await dev(files);
await page.goto(`http://localhost:${port}/`, {
waitUntil: "networkidle",
});
await expect(page.locator("[data-test-route]")).toHaveText(
"Test route 1"
);
let edit = createEditor(cwd);
let INVALID_FILENAME = "app/routes.ts.oops";
// Rename config to make it missing
await fs.rename(
path.join(cwd, "app/routes.ts"),
path.join(cwd, INVALID_FILENAME)
);
// Ensure dev server is still running with old config + HMR
await edit("app/test-route-1.tsx", (contents) =>
contents.replace("Test route 1", "Test route 1 updated")
);
await expect(page.locator("[data-test-route]")).toHaveText(
"Test route 1 updated"
);
// Add new route
await edit(INVALID_FILENAME, (contents) =>
contents.replace("test-route-1", "test-route-2")
);
// Rename config to bring it back
await fs.rename(
path.join(cwd, INVALID_FILENAME),
path.join(cwd, "app/routes.ts")
);
await expect(async () => {
// Reload to pick up new route for current path
await page.reload();
await expect(page.locator("[data-test-route]")).toHaveText(
"Test route 2"
);
}).toPass();
});
test("supports absolute route file paths", async ({ page, dev }) => {
let files: Files = async ({ port }) => ({
"vite.config.js": await viteConfig.basic({ port }),
"app/routes.ts": js`
import path from "node:path";
import { type RouteConfig, index } from "@react-router/dev/routes";
export default [
index(path.resolve(import.meta.dirname, "test-route.tsx")),
] satisfies RouteConfig;
`,
"app/test-route.tsx": `
export default () => <div data-test-route>Test route</div>
`,
});
let { port } = await dev(files);
await page.goto(`http://localhost:${port}/`, {
waitUntil: "networkidle",
});
await expect(page.locator("[data-test-route]")).toHaveText(
"Test route"
);
});
});
});
});