|
| 1 | +import { test, expect } from "@playwright/test"; |
| 2 | + |
| 3 | +import { |
| 4 | + createFixture, |
| 5 | + createAppFixture, |
| 6 | + js, |
| 7 | +} from "./helpers/create-fixture.js"; |
| 8 | +import type { Fixture, AppFixture } from "./helpers/create-fixture.js"; |
| 9 | +import { PlaywrightFixture } from "./helpers/playwright-fixture.js"; |
| 10 | +import { |
| 11 | + type TemplateName, |
| 12 | + reactRouterConfig, |
| 13 | + viteConfig, |
| 14 | +} from "./helpers/vite.js"; |
| 15 | + |
| 16 | +const templateNames = [ |
| 17 | + "vite-5-template", |
| 18 | + "rsc-vite-framework", |
| 19 | +] as const satisfies TemplateName[]; |
| 20 | + |
| 21 | +test.describe("MDX", () => { |
| 22 | + for (const templateName of templateNames) { |
| 23 | + test.describe(`template: ${templateName}`, () => { |
| 24 | + let fixture: Fixture; |
| 25 | + let appFixture: AppFixture; |
| 26 | + |
| 27 | + test.beforeAll(async () => { |
| 28 | + fixture = await createFixture({ |
| 29 | + templateName, |
| 30 | + files: { |
| 31 | + "vite.config.js": await viteConfig.basic({ |
| 32 | + templateName, |
| 33 | + mdx: true, |
| 34 | + }), |
| 35 | + "react-router.config.ts": reactRouterConfig({ |
| 36 | + viteEnvironmentApi: templateName.includes("rsc"), |
| 37 | + }), |
| 38 | + "app/root.tsx": js` |
| 39 | + import { Outlet, Scripts } from "react-router" |
| 40 | + |
| 41 | + export default function Root() { |
| 42 | + return ( |
| 43 | + <html> |
| 44 | + <head></head> |
| 45 | + <body> |
| 46 | + <main> |
| 47 | + <Outlet /> |
| 48 | + </main> |
| 49 | + <Scripts /> |
| 50 | + </body> |
| 51 | + </html> |
| 52 | + ); |
| 53 | + } |
| 54 | + `, |
| 55 | + "app/routes/_index.tsx": js` |
| 56 | + import { Link } from "react-router" |
| 57 | + export default function Component() { |
| 58 | + return <Link to="/mdx">Go to MDX route</Link> |
| 59 | + } |
| 60 | + `, |
| 61 | + "app/routes/mdx.mdx": js` |
| 62 | + import { MdxComponent } from "../components/mdx-components"; |
| 63 | +
|
| 64 | + export const loader = () => { |
| 65 | + return { |
| 66 | + content: "MDX route content from loader", |
| 67 | + } |
| 68 | + } |
| 69 | +
|
| 70 | + ## MDX Route |
| 71 | +
|
| 72 | + <MdxComponent /> |
| 73 | + `, |
| 74 | + // This needs to be a separate file to support RSC since |
| 75 | + // `useLoaderData` is not available in RSC environments, and |
| 76 | + // components defined within an MDX file must be exported. This |
| 77 | + // means they're not removed in the RSC build. |
| 78 | + "app/components/mdx-components.tsx": js` |
| 79 | + import { useState, useEffect } from "react"; |
| 80 | + import { useLoaderData } from "react-router"; |
| 81 | +
|
| 82 | + export function MdxComponent() { |
| 83 | + const { content } = useLoaderData(); |
| 84 | + const [mounted, setMounted] = useState(false); |
| 85 | + useEffect(() => { |
| 86 | + setMounted(true); |
| 87 | + }, []); |
| 88 | +
|
| 89 | + return ( |
| 90 | + <> |
| 91 | + <h3>Loader data</h3> |
| 92 | + <div data-loader-data>{content}</div> |
| 93 | + <h3>Mounted</h3> |
| 94 | + <div data-mounted>{mounted ? "true" : "false"}</div> |
| 95 | + </> |
| 96 | + ); |
| 97 | + } |
| 98 | + `, |
| 99 | + }, |
| 100 | + }); |
| 101 | + |
| 102 | + appFixture = await createAppFixture(fixture); |
| 103 | + }); |
| 104 | + |
| 105 | + test.afterAll(() => { |
| 106 | + appFixture.close(); |
| 107 | + }); |
| 108 | + |
| 109 | + test("handles MDX routes", async ({ page }) => { |
| 110 | + let app = new PlaywrightFixture(appFixture, page); |
| 111 | + await app.goto("/mdx"); |
| 112 | + |
| 113 | + let loaderData = page.locator("[data-loader-data]"); |
| 114 | + await expect(loaderData).toHaveText("MDX route content from loader"); |
| 115 | + |
| 116 | + let mounted = page.locator("[data-mounted]"); |
| 117 | + await expect(mounted).toHaveText("true"); |
| 118 | + }); |
| 119 | + }); |
| 120 | + } |
| 121 | +}); |
0 commit comments