|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { quietPreloads } from "~/lib/route-preloads" |
| 3 | + |
| 4 | +// A file route as SolidStart builds it: a lazy component whose preload is the reloading loader, and |
| 5 | +// the route's own import of its code. |
| 6 | +function route(code: () => Promise<unknown>) { |
| 7 | + const calls = { preload: 0, import: 0 } |
| 8 | + return { |
| 9 | + calls, |
| 10 | + route: { |
| 11 | + component: { preload: () => void calls.preload++ }, |
| 12 | + $component: { |
| 13 | + import: () => { |
| 14 | + calls.import++ |
| 15 | + return code() |
| 16 | + }, |
| 17 | + }, |
| 18 | + }, |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +describe("quietPreloads", () => { |
| 23 | + test("hands a route its code once the code has loaded", async () => { |
| 24 | + const page = route(() => Promise.resolve({})) |
| 25 | + quietPreloads([page.route]) |
| 26 | + |
| 27 | + await page.route.component.preload() |
| 28 | + |
| 29 | + expect(page.calls).toEqual({ preload: 1, import: 1 }) |
| 30 | + }) |
| 31 | + |
| 32 | + test("leaves the route as it was when its code fails to load", async () => { |
| 33 | + const page = route(() => Promise.reject(new TypeError("Failed to fetch dynamically imported module"))) |
| 34 | + quietPreloads([page.route]) |
| 35 | + |
| 36 | + expect(await page.route.component.preload()).toBeUndefined() |
| 37 | + expect(page.calls).toEqual({ preload: 0, import: 1 }) |
| 38 | + }) |
| 39 | + |
| 40 | + // quietPreloads relies on the shape SolidStart gives each file route, and skips any route without |
| 41 | + // it, so a SolidStart or Solid upgrade that changes it would turn the fix off without a sound. If |
| 42 | + // one of these fails, check that preloads still can't reload the page (src/lib/route-preloads.ts), |
| 43 | + // then update the expectations. |
| 44 | + test("SolidStart's file routes still have the shape it relies on", async () => { |
| 45 | + const start = (file: string) => |
| 46 | + Bun.file(new URL(`../node_modules/@solidjs/start/dist/${file}`, import.meta.url)).text() |
| 47 | + // The route's own import of its code, unwrapped: `$component: { src, build, import }`. |
| 48 | + expect(await start("config/fs-routes/index.js")).toContain("import: `_$() => import('${buildId}')$_`") |
| 49 | + const router = await start("router.jsx") |
| 50 | + // Each route keeps its `$component`, and its component is a lazy one over the reloading loader. |
| 51 | + expect(router).toContain("...route,") |
| 52 | + expect(router).toContain("lazy(loadComponent(route))") |
| 53 | + expect(router).toContain("withRouteChunkReload(route.$component.import)") |
| 54 | + // The router preloads a route through its component's `preload`, which a lazy component has. |
| 55 | + expect(await Bun.file(new URL("../node_modules/@solidjs/router/dist/index.js", import.meta.url)).text()).toContain( |
| 56 | + "route.component && route.component.preload && route.component.preload()", |
| 57 | + ) |
| 58 | + expect(await Bun.file(new URL("../node_modules/solid-js/dist/solid.js", import.meta.url)).text()).toContain( |
| 59 | + "wrap.preload = () =>", |
| 60 | + ) |
| 61 | + }) |
| 62 | + |
| 63 | + test("reaches nested routes and wraps each preload once", async () => { |
| 64 | + const page = route(() => Promise.resolve({})) |
| 65 | + const routes = [{ children: [page.route] }] |
| 66 | + quietPreloads(routes) |
| 67 | + quietPreloads(routes) |
| 68 | + |
| 69 | + await page.route.component.preload() |
| 70 | + |
| 71 | + expect(page.calls).toEqual({ preload: 1, import: 1 }) |
| 72 | + }) |
| 73 | +}) |
0 commit comments