|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { Asset } from "./assets/render.tsx"; |
| 3 | +import type { FetchEvent } from "./types.ts"; |
| 4 | + |
| 5 | +const writeEarlyHints = vi.fn(); |
| 6 | +vi.mock("h3", () => ({ writeEarlyHints })); |
| 7 | + |
| 8 | +const { sendEarlyHints, toLinkValues } = await import("./early-hints.ts"); |
| 9 | + |
| 10 | +const assets: Asset[] = [ |
| 11 | + { tag: "link", attrs: { href: "/_build/assets/entry-client.css", rel: "stylesheet" } }, |
| 12 | + { tag: "link", attrs: { href: "/_build/assets/entry-client.js", rel: "modulepreload" } }, |
| 13 | + { tag: "link", attrs: { href: "/_build/assets/漢字-abc.js", rel: "modulepreload" } }, |
| 14 | + { tag: "style", attrs: {}, children: "body {}" }, |
| 15 | + { tag: "link", attrs: { href: "/favicon.ico", rel: "icon" } }, |
| 16 | +]; |
| 17 | + |
| 18 | +function createEvent(headersSent = false) { |
| 19 | + return { nativeEvent: { runtime: { node: { res: { headersSent } } } } } as unknown as FetchEvent; |
| 20 | +} |
| 21 | + |
| 22 | +describe("toLinkValues", () => { |
| 23 | + it("preloads stylesheets and modulepreloads scripts, like the page does", () => { |
| 24 | + expect(toLinkValues(assets)).toEqual([ |
| 25 | + "</_build/assets/entry-client.css>; rel=preload; as=style", |
| 26 | + "</_build/assets/entry-client.js>; rel=modulepreload", |
| 27 | + "</_build/assets/%E6%BC%A2%E5%AD%97-abc.js>; rel=modulepreload", |
| 28 | + ]); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +describe("sendEarlyHints", () => { |
| 33 | + beforeEach(() => { |
| 34 | + vi.stubEnv("PROD", true); |
| 35 | + // A build inlines a boolean here. Vitest stores env values as strings, so a |
| 36 | + // test can only set it (truthy) or leave it unset (falsy). |
| 37 | + import.meta.env.START_EARLY_HINTS = true; |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + vi.unstubAllEnvs(); |
| 42 | + delete (import.meta.env as Partial<ImportMetaEnv>).START_EARLY_HINTS; |
| 43 | + writeEarlyHints.mockReset(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("sends the assets as early hints", () => { |
| 47 | + const event = createEvent(); |
| 48 | + sendEarlyHints(event, assets); |
| 49 | + |
| 50 | + expect(writeEarlyHints).toHaveBeenCalledWith(event.nativeEvent, { |
| 51 | + link: toLinkValues(assets), |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it("does nothing when the earlyHints option is off", () => { |
| 56 | + delete (import.meta.env as Partial<ImportMetaEnv>).START_EARLY_HINTS; |
| 57 | + sendEarlyHints(createEvent(), assets); |
| 58 | + expect(writeEarlyHints).not.toHaveBeenCalled(); |
| 59 | + }); |
| 60 | + |
| 61 | + it("does nothing outside production", () => { |
| 62 | + vi.stubEnv("PROD", false); |
| 63 | + sendEarlyHints(createEvent(), assets); |
| 64 | + expect(writeEarlyHints).not.toHaveBeenCalled(); |
| 65 | + }); |
| 66 | + |
| 67 | + it("does nothing once the response headers are sent", () => { |
| 68 | + sendEarlyHints(createEvent(true), assets); |
| 69 | + expect(writeEarlyHints).not.toHaveBeenCalled(); |
| 70 | + }); |
| 71 | + |
| 72 | + it("does nothing when there is nothing to hint", () => { |
| 73 | + sendEarlyHints(createEvent(), [{ tag: "style", attrs: {}, children: "body {}" }]); |
| 74 | + expect(writeEarlyHints).not.toHaveBeenCalled(); |
| 75 | + }); |
| 76 | +}); |
0 commit comments