|
| 1 | +import tsx from "dedent"; |
| 2 | +import { expect } from "@playwright/test"; |
| 3 | + |
| 4 | +import { test } from "./helpers/fixtures"; |
| 5 | +import * as Stream from "./helpers/stream"; |
| 6 | +import getPort from "get-port"; |
| 7 | + |
| 8 | +test.use({ |
| 9 | + files: { |
| 10 | + "app/expect-type.ts": tsx` |
| 11 | + export type Expect<T extends true> = T |
| 12 | +
|
| 13 | + export type Equal<X, Y> = |
| 14 | + (<T>() => T extends X ? 1 : 2) extends |
| 15 | + (<T>() => T extends Y ? 1 : 2) ? true : false |
| 16 | + `, |
| 17 | + "app/routes.ts": tsx` |
| 18 | + import { type RouteConfig, route } from "@react-router/dev/routes" |
| 19 | +
|
| 20 | + export default [ |
| 21 | + route("parent", "routes/parent.tsx", [ |
| 22 | + route("current", "routes/current.tsx") |
| 23 | + ]), |
| 24 | + route("other", "routes/other.tsx"), |
| 25 | + ] satisfies RouteConfig |
| 26 | + `, |
| 27 | + "app/root.tsx": tsx` |
| 28 | + import { Outlet } from "react-router" |
| 29 | +
|
| 30 | + export const loader = () => ({ rootLoader: "root/loader" }) |
| 31 | + export const action = () => ({ rootAction: "root/action" }) |
| 32 | +
|
| 33 | + export default function Component() { |
| 34 | + return ( |
| 35 | + <> |
| 36 | + <h1>Root</h1> |
| 37 | + <Outlet /> |
| 38 | + </> |
| 39 | + ) |
| 40 | + } |
| 41 | + `, |
| 42 | + "app/routes/parent.tsx": tsx` |
| 43 | + import { Outlet } from "react-router" |
| 44 | +
|
| 45 | + export const loader = () => ({ parentLoader: "parent/loader" }) |
| 46 | + export const action = () => ({ parentAction: "parent/action" }) |
| 47 | +
|
| 48 | + export default function Component() { |
| 49 | + return ( |
| 50 | + <> |
| 51 | + <h2>Parent</h2> |
| 52 | + <Outlet /> |
| 53 | + </> |
| 54 | + ) |
| 55 | + } |
| 56 | + `, |
| 57 | + "app/routes/current.tsx": tsx` |
| 58 | + import { unstable_useRoute as useRoute } from "react-router" |
| 59 | +
|
| 60 | + import type { Expect, Equal } from "../expect-type" |
| 61 | +
|
| 62 | + export const loader = () => ({ currentLoader: "current/loader" }) |
| 63 | + export const action = () => ({ currentAction: "current/action" }) |
| 64 | +
|
| 65 | + export default function Component() { |
| 66 | + const current = useRoute() |
| 67 | + type Test1 = Expect<Equal<typeof current, { loaderData: unknown, actionData: unknown }>> |
| 68 | +
|
| 69 | + const root = useRoute("root") |
| 70 | + type Test2 = Expect<Equal<typeof root, { loaderData: { rootLoader: string } | undefined, actionData: { rootAction: string } | undefined }>> |
| 71 | +
|
| 72 | + const parent = useRoute("routes/parent") |
| 73 | + type Test3 = Expect<Equal<typeof parent, { loaderData: { parentLoader: string } | undefined, actionData: { parentAction: string } | undefined } | undefined>> |
| 74 | +
|
| 75 | + const other = useRoute("routes/other") |
| 76 | + type Test4 = Expect<Equal<typeof other, { loaderData: { otherLoader: string } | undefined, actionData: { otherAction: string } | undefined } | undefined>> |
| 77 | +
|
| 78 | + return ( |
| 79 | + <> |
| 80 | + <pre data-root>{root.loaderData?.rootLoader}</pre> |
| 81 | + <pre data-parent>{parent?.loaderData?.parentLoader}</pre> |
| 82 | + {/* @ts-expect-error */} |
| 83 | + <pre data-current>{current?.loaderData?.currentLoader}</pre> |
| 84 | + <pre data-other>{other === undefined ? "undefined" : "something else"}</pre> |
| 85 | + </> |
| 86 | + ) |
| 87 | + } |
| 88 | + `, |
| 89 | + "app/routes/other.tsx": tsx` |
| 90 | + export const loader = () => ({ otherLoader: "other/loader" }) |
| 91 | + export const action = () => ({ otherAction: "other/action" }) |
| 92 | +
|
| 93 | + export default function Component() { |
| 94 | + return <h2>Other</h2> |
| 95 | + } |
| 96 | + `, |
| 97 | + }, |
| 98 | +}); |
| 99 | + |
| 100 | +test("useRoute", async ({ $, page }) => { |
| 101 | + await $("pnpm typecheck"); |
| 102 | + |
| 103 | + const port = await getPort(); |
| 104 | + const url = `http://localhost:${port}`; |
| 105 | + |
| 106 | + const dev = $(`pnpm dev --port ${port}`); |
| 107 | + await Stream.match(dev.stdout, url); |
| 108 | + |
| 109 | + await page.goto(url + "/parent/current", { waitUntil: "networkidle" }); |
| 110 | + |
| 111 | + await expect(page.locator("[data-root]")).toHaveText("root/loader"); |
| 112 | + |
| 113 | + await expect(page.locator("[data-parent]")).toHaveText("parent/loader"); |
| 114 | + |
| 115 | + await expect(page.locator("[data-current]")).toHaveText("current/loader"); |
| 116 | + |
| 117 | + await expect(page.locator("[data-other]")).toHaveText("undefined"); |
| 118 | +}); |
0 commit comments