-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
/
Copy pathfile-path-config-test.ts
192 lines (177 loc) · 5.68 KB
/
file-path-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
import { expect } from "@playwright/test";
import type { Files } from "./helpers/vite.js";
import { test, viteConfig, build, createProject } from "./helpers/vite.js";
const js = String.raw;
const simpleFiles: Files = async ({ port }) => ({
"vite.config.ts": await viteConfig.basic({ port }),
"react-router.config.ts": js`
export default {
rootRouteFile: "custom/root.tsx",
routesFile: "custom/app-routes.ts",
clientEntryFile: "custom/entry.client.tsx",
serverEntryFile: "custom/entry.server.tsx",
};
`,
"app/custom/root.tsx": js`
import { Links, Meta, Outlet, Scripts } from "react-router";
export default function Root() {
return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<body>
<div id="content">
<h1>Custom Root</h1>
<Outlet />
</div>
<Scripts />
</body>
</html>
);
}
`,
"app/custom/app-routes.ts": js`
import { type RouteConfig, index } from "@react-router/dev/routes";
export default [
index("index.tsx"),
] satisfies RouteConfig;
`,
"app/index.tsx": js`
export default function IndexRoute() {
return <div id="hydrated" onClick={() => {}}>Custom IndexRoute</div>
}
`,
"app/custom/entry.client.tsx": js`
import { HydratedRouter } from "react-router/dom";
import { startTransition, StrictMode } from "react";
import { hydrateRoot } from "react-dom/client";
window.__customClientEntryExecuted = true;
startTransition(() => {
hydrateRoot(
document,
<StrictMode>
<HydratedRouter discover={"none"} />
</StrictMode>
);
});
`,
"app/custom/entry.server.tsx": js`
import * as React from "react";
import { ServerRouter } from "react-router";
import { renderToString } from "react-dom/server";
export default function handleRequest(
request,
responseStatusCode,
responseHeaders,
remixContext
) {
let markup = renderToString(
<ServerRouter context={remixContext} url={request.url} />
);
responseHeaders.set("Content-Type", "text/html");
responseHeaders.set("X-Custom-Server-Entry", "true");
return new Response('<!DOCTYPE html>' + markup, {
headers: responseHeaders,
status: responseStatusCode,
});
}
`,
});
test.describe("File path configuration", () => {
test("uses custom file paths", async ({ page, dev, request }) => {
let { port } = await dev(simpleFiles);
const response = await page.goto(`http://localhost:${port}/`);
// Verify custom root.tsx and app-routes.ts is being used.
await expect(page.locator("h1")).toHaveText("Custom Root");
await expect(page.locator("#content div")).toHaveText("Custom IndexRoute");
// Verify client entry is being used.
expect(
await page.evaluate(() => (window as any).__customClientEntryExecuted)
).toBe(true);
// Verify server entry is used by checking for the custom header.
expect(response?.headers()["x-custom-server-entry"]).toBe("true");
});
test("fails build when custom rootRouteFile doesn't exist", async () => {
let cwd = await createProject({
"react-router.config.ts": js`
export default {
rootRouteFile: "custom/nonexistent-root.tsx"
};
`,
});
let buildResult = build({ cwd });
expect(buildResult.status).toBe(1);
expect(buildResult.stderr.toString()).toContain(
'Could not find "root" entry file at'
);
expect(buildResult.stderr.toString()).toContain("nonexistent-root.tsx");
});
test("fails build when custom routesFile doesn't exist", async () => {
let cwd = await createProject({
"app/root.tsx": js`
export default function Root() {
return <div>Root</div>;
}
`,
"react-router.config.ts": js`
export default {
routesFile: "custom/nonexistent-routes.ts"
};
`,
});
let buildResult = build({ cwd });
expect(buildResult.status).toBe(1);
expect(buildResult.stderr.toString()).toContain(
'Could not find "routes" entry file at'
);
expect(buildResult.stderr.toString()).toContain("nonexistent-routes.ts");
});
test("fails build when custom clientEntryFile doesn't exist", async () => {
let cwd = await createProject({
"app/root.tsx": js`
export default function Root() {
return <div>Root</div>;
}
`,
"app/routes.ts": js`
export default [];
`,
"react-router.config.ts": js`
export default {
clientEntryFile: "custom/nonexistent-entry.client.tsx"
};
`,
});
let buildResult = build({ cwd });
expect(buildResult.status).toBe(1);
expect(buildResult.stderr.toString()).toContain(
'Could not find "entry.client" entry file at'
);
expect(buildResult.stderr.toString()).toContain("nonexistent-entry.client.tsx");
});
test("fails build when custom serverEntryFile doesn't exist", async () => {
let cwd = await createProject({
"app/root.tsx": js`
export default function Root() {
return <div>Root</div>;
}
`,
"app/routes.ts": js`
export default [];
`,
"react-router.config.ts": js`
export default {
serverEntryFile: "custom/nonexistent-entry.server.tsx"
};
`,
});
let buildResult = build({ cwd });
expect(buildResult.status).toBe(1);
expect(buildResult.stderr.toString()).toContain(
'Could not find "entry.server" entry file at'
);
expect(buildResult.stderr.toString()).toContain("nonexistent-entry.server.tsx");
});
});