|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { h, render } from "preact"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | + |
| 5 | +import { configureClient, Form } from "../src/index.ts"; |
| 6 | +import { fetchPrachtRouteState } from "../src/runtime.ts"; |
| 7 | + |
| 8 | +function createJsonResponse(body: unknown, init?: ResponseInit): Response { |
| 9 | + return new Response(JSON.stringify(body), { |
| 10 | + headers: { |
| 11 | + "content-type": "application/json", |
| 12 | + }, |
| 13 | + ...init, |
| 14 | + }); |
| 15 | +} |
| 16 | + |
| 17 | +describe("configureClient", () => { |
| 18 | + afterEach(() => { |
| 19 | + // Reset to default (global fetch) between tests. |
| 20 | + configureClient({ fetch: undefined }); |
| 21 | + vi.unstubAllGlobals(); |
| 22 | + vi.restoreAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + it("routes fetchPrachtRouteState through the configured fetch", async () => { |
| 26 | + const customFetch = vi.fn(async () => createJsonResponse({ data: { hello: "world" } })); |
| 27 | + |
| 28 | + configureClient({ fetch: customFetch as unknown as typeof fetch }); |
| 29 | + |
| 30 | + const result = await fetchPrachtRouteState("/foo"); |
| 31 | + |
| 32 | + expect(customFetch).toHaveBeenCalledTimes(1); |
| 33 | + expect(customFetch).toHaveBeenCalledWith( |
| 34 | + "/foo", |
| 35 | + expect.objectContaining({ |
| 36 | + headers: expect.objectContaining({ |
| 37 | + "Cache-Control": "no-cache", |
| 38 | + "x-pracht-route-state-request": "1", |
| 39 | + }), |
| 40 | + redirect: "manual", |
| 41 | + }), |
| 42 | + ); |
| 43 | + expect(result).toEqual({ type: "data", data: { hello: "world" } }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("falls back to the global fetch when no configuration is set", async () => { |
| 47 | + const globalFetch = vi.fn(async () => createJsonResponse({ data: 1 })); |
| 48 | + vi.stubGlobal("fetch", globalFetch); |
| 49 | + |
| 50 | + await fetchPrachtRouteState("/bar"); |
| 51 | + |
| 52 | + expect(globalFetch).toHaveBeenCalledTimes(1); |
| 53 | + }); |
| 54 | + |
| 55 | + it("resets to the global fetch when fetch: undefined is passed", async () => { |
| 56 | + const customFetch = vi.fn(async () => createJsonResponse({ data: 1 })); |
| 57 | + const globalFetch = vi.fn(async () => createJsonResponse({ data: 2 })); |
| 58 | + vi.stubGlobal("fetch", globalFetch); |
| 59 | + |
| 60 | + configureClient({ fetch: customFetch as unknown as typeof fetch }); |
| 61 | + await fetchPrachtRouteState("/a"); |
| 62 | + expect(customFetch).toHaveBeenCalledTimes(1); |
| 63 | + expect(globalFetch).not.toHaveBeenCalled(); |
| 64 | + |
| 65 | + configureClient({ fetch: undefined }); |
| 66 | + await fetchPrachtRouteState("/b"); |
| 67 | + expect(customFetch).toHaveBeenCalledTimes(1); |
| 68 | + expect(globalFetch).toHaveBeenCalledTimes(1); |
| 69 | + }); |
| 70 | + |
| 71 | + it("forwards an Authorization header injected by the configured fetch to client navigation fetches", async () => { |
| 72 | + const seenHeaders: Array<Record<string, string>> = []; |
| 73 | + const customFetch = vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => { |
| 74 | + seenHeaders.push((init?.headers as Record<string, string>) ?? {}); |
| 75 | + return createJsonResponse({ data: null }); |
| 76 | + }); |
| 77 | + |
| 78 | + configureClient({ |
| 79 | + fetch: (input, init) => { |
| 80 | + const headers = { |
| 81 | + ...(init?.headers as Record<string, string> | undefined), |
| 82 | + Authorization: "Bearer token-123", |
| 83 | + }; |
| 84 | + return customFetch(input, { ...init, headers }); |
| 85 | + }, |
| 86 | + }); |
| 87 | + |
| 88 | + await fetchPrachtRouteState("/baz"); |
| 89 | + |
| 90 | + expect(seenHeaders[0]).toMatchObject({ |
| 91 | + Authorization: "Bearer token-123", |
| 92 | + "x-pracht-route-state-request": "1", |
| 93 | + "Cache-Control": "no-cache", |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + it("routes <Form> submissions through the configured fetch", async () => { |
| 98 | + const customFetch = vi.fn<typeof fetch>(async () => new Response(null, { status: 204 })); |
| 99 | + configureClient({ fetch: customFetch }); |
| 100 | + |
| 101 | + const root = document.createElement("div"); |
| 102 | + document.body.appendChild(root); |
| 103 | + |
| 104 | + try { |
| 105 | + render( |
| 106 | + h( |
| 107 | + Form, |
| 108 | + { action: "/api/do", method: "post" }, |
| 109 | + h("input", { name: "x", defaultValue: "y" }), |
| 110 | + h("button", { type: "submit" }, "Go"), |
| 111 | + ), |
| 112 | + root, |
| 113 | + ); |
| 114 | + |
| 115 | + const form = root.querySelector("form")!; |
| 116 | + form.dispatchEvent(new Event("submit", { bubbles: true, cancelable: true })); |
| 117 | + // let the microtask queue settle |
| 118 | + await Promise.resolve(); |
| 119 | + await Promise.resolve(); |
| 120 | + |
| 121 | + expect(customFetch).toHaveBeenCalledTimes(1); |
| 122 | + const [url, init] = customFetch.mock.calls[0]!; |
| 123 | + expect(url).toBe("/api/do"); |
| 124 | + expect(init?.method).toBe("POST"); |
| 125 | + expect(init?.redirect).toBe("manual"); |
| 126 | + } finally { |
| 127 | + render(null, root); |
| 128 | + root.remove(); |
| 129 | + } |
| 130 | + }); |
| 131 | +}); |
0 commit comments