Skip to content

Commit 2984188

Browse files
committed
wip: tests
1 parent 3035559 commit 2984188

File tree

2 files changed

+128
-4
lines changed

2 files changed

+128
-4
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import type { Page } from "@playwright/test";
2+
import { expect } from "@playwright/test";
3+
import dedent from "dedent";
4+
5+
import type { Files } from "./helpers/vite.js";
6+
import { test, viteConfig } from "./helpers/vite.js";
7+
8+
const tsx = dedent;
9+
10+
const files: Files = async ({ port }) => ({
11+
"vite.config.ts": tsx`
12+
import { reactRouter } from "@react-router/dev/vite";
13+
14+
export default {
15+
${await viteConfig.server({ port })}
16+
plugins: [reactRouter()],
17+
}
18+
`,
19+
"app/lib/components.tsx": tsx`
20+
function Component({ loaderData }: any) {
21+
return <h1 data-title>{loaderData.title}</h1>;
22+
}
23+
export const ComponentAlias = Component;
24+
export default Component;
25+
26+
export function HydrateFallback() {
27+
return <div>Loading...</div>;
28+
}
29+
export const HydrateFallbackAlias = HydrateFallback;
30+
31+
export function ErrorBoundary() {
32+
return <div>Error</div>;
33+
}
34+
export const ErrorBoundaryAlias = ErrorBoundary;
35+
`,
36+
"app/routes.ts": tsx`
37+
import { type RouteConfig, index, route } from "@react-router/dev/routes";
38+
39+
export default [
40+
route("named-reexport-with-source", "routes/named-reexport-with-source.tsx"),
41+
route("alias-reexport-with-source", "routes/alias-reexport-with-source.tsx"),
42+
route("named-reexport-without-source", "routes/named-reexport-without-source.tsx"),
43+
route("alias-reexport-without-source", "routes/alias-reexport-without-source.tsx"),
44+
] satisfies RouteConfig;
45+
`,
46+
"app/routes/named-reexport-with-source.tsx": tsx`
47+
export const loader = () => ({ title: "named-reexport-with-source" })
48+
49+
export {
50+
default,
51+
HydrateFallback,
52+
ErrorBoundary,
53+
} from "../lib/components"
54+
`,
55+
"app/routes/alias-reexport-with-source.tsx": tsx`
56+
export const loader = () => ({ title: "alias-reexport-with-source" })
57+
58+
export {
59+
ComponentAlias as default,
60+
HydrateFallbackAlias as HydrateFallback,
61+
ErrorBoundaryAlias as ErrorBoundary,
62+
} from "../lib/components"
63+
`,
64+
"app/routes/named-reexport-without-source.tsx": tsx`
65+
import { ComponentAlias, HydrateFallbackAlias, ErrorBoundaryAlias } from "../lib/components"
66+
67+
export const loader = () => ({ title: "named-reexport-without-source" })
68+
69+
export default ComponentAlias
70+
const HydrateFallback = HydrateFallbackAlias
71+
const ErrorBoundary = ErrorBoundaryAlias
72+
73+
export {
74+
// note: it would be invalid syntax to use 'default' keyword here,
75+
// so instead we 'export default' separately
76+
HydrateFallback,
77+
ErrorBoundary,
78+
}
79+
`,
80+
"app/routes/alias-reexport-without-source.tsx": tsx`
81+
import { ComponentAlias, HydrateFallbackAlias, ErrorBoundaryAlias } from "../lib/components"
82+
83+
export const loader = () => ({ title: "alias-reexport-without-source" })
84+
85+
export {
86+
ComponentAlias as default,
87+
HydrateFallbackAlias as HydrateFallback,
88+
ErrorBoundaryAlias as ErrorBoundary,
89+
}
90+
`,
91+
});
92+
93+
test("dev", async ({ page, dev }) => {
94+
let { cwd, port } = await dev(files);
95+
console.log({ cwd });
96+
await workflow({ page, port });
97+
});
98+
99+
test("build", async ({ page, reactRouterServe }) => {
100+
let { port } = await reactRouterServe(files);
101+
await workflow({ page, port });
102+
});
103+
104+
async function workflow({ page, port }: { page: Page; port: number }) {
105+
const routes = [
106+
"named-reexport-with-source",
107+
"alias-reexport-with-source",
108+
"named-reexport-without-source",
109+
"alias-reexport-without-source",
110+
];
111+
for (const route of routes) {
112+
await page.goto(`http://localhost:${port}/${route}`, {
113+
waitUntil: "networkidle",
114+
});
115+
await expect(page.locator("[data-title]")).toHaveText(route);
116+
expect(page.errors).toEqual([]);
117+
}
118+
}

packages/react-router-dev/vite/with-props.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ export const decorateComponentExportsWithProps = (
4040
const { source } = path.node;
4141

4242
const exports: Array<{
43-
uid: Babel.Identifier;
43+
specifier: NodePath;
4444
local: Babel.Identifier;
45+
uid: Babel.Identifier;
4546
exported: Babel.Identifier;
4647
}> = [];
4748
for (const specifier of path.get("specifiers")) {
@@ -51,8 +52,7 @@ export const decorateComponentExportsWithProps = (
5152
if (!t.isIdentifier(exported)) continue;
5253
const uid = path.scope.generateUidIdentifier(`_${name}`);
5354
if (exported.name === "default" || isNamedComponentExport(name)) {
54-
exports.push({ uid, local, exported });
55-
specifier.remove();
55+
exports.push({ specifier, local, uid, exported });
5656
}
5757
}
5858
}
@@ -68,7 +68,9 @@ export const decorateComponentExportsWithProps = (
6868
]);
6969
} else {
7070
for (const { local, uid } of exports) {
71-
path.scope.getBinding(local.name)?.scope.rename(uid.name);
71+
console.log(`Renaming '${local.name}' to '${uid.name}'`);
72+
// path.scope.getBinding(local.name)?.scope.rename(uid.name);
73+
path.scope.getProgramParent().rename(local.name, uid.name);
7274
}
7375
}
7476

@@ -85,6 +87,10 @@ export const decorateComponentExportsWithProps = (
8587
);
8688
}),
8789
);
90+
91+
for (const { specifier } of exports) {
92+
specifier.remove();
93+
}
8894
},
8995
});
9096

0 commit comments

Comments
 (0)