|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import { act } from "react"; |
| 4 | +import { createRoot, type Root } from "react-dom/client"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from "vitest"; |
| 6 | +import { GlobalAssetsView } from "./GlobalAssetsView"; |
| 7 | + |
| 8 | +Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true }); |
| 9 | + |
| 10 | +const EMPTY_CACHE = "No assets in the global cache yet"; |
| 11 | +const FAILURE = "Couldn't load the global asset cache"; |
| 12 | +const LOADING = "Loading global assets"; |
| 13 | +const LOGO = { id: "logo", type: "image", description: "Brand logo" }; |
| 14 | + |
| 15 | +let fetchMock: Mock<typeof fetch>; |
| 16 | +let root: Root | null = null; |
| 17 | +let host: HTMLDivElement; |
| 18 | + |
| 19 | +beforeEach(() => { |
| 20 | + fetchMock = vi.fn<typeof fetch>(); |
| 21 | + vi.stubGlobal("fetch", fetchMock); |
| 22 | +}); |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | + if (root) act(() => root?.unmount()); |
| 26 | + root = null; |
| 27 | + document.body.innerHTML = ""; |
| 28 | + vi.unstubAllGlobals(); |
| 29 | +}); |
| 30 | + |
| 31 | +function respond(status: number, body: unknown): Response { |
| 32 | + return new Response(JSON.stringify(body), { status }); |
| 33 | +} |
| 34 | + |
| 35 | +async function settle(): Promise<void> { |
| 36 | + await act(async () => { |
| 37 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +async function show(searchQuery: string): Promise<void> { |
| 42 | + await act(async () => root?.render(<GlobalAssetsView searchQuery={searchQuery} />)); |
| 43 | + await settle(); |
| 44 | +} |
| 45 | + |
| 46 | +async function mount(searchQuery = ""): Promise<void> { |
| 47 | + host = document.createElement("div"); |
| 48 | + document.body.append(host); |
| 49 | + root = createRoot(host); |
| 50 | + await show(searchQuery); |
| 51 | +} |
| 52 | + |
| 53 | +function tryAgainButton(): HTMLButtonElement | undefined { |
| 54 | + return [...host.querySelectorAll("button")].find((b) => b.textContent === "Try again"); |
| 55 | +} |
| 56 | + |
| 57 | +describe("GlobalAssetsView", () => { |
| 58 | + it.each([ |
| 59 | + ["an error status", () => fetchMock.mockResolvedValueOnce(respond(500, { error: "boom" }))], |
| 60 | + ["a rejected request", () => fetchMock.mockRejectedValueOnce(new TypeError("Failed to fetch"))], |
| 61 | + ])("reports %s as a failure instead of an empty cache", async (_case, failOnce) => { |
| 62 | + failOnce(); |
| 63 | + await mount(); |
| 64 | + |
| 65 | + expect(host.textContent).toContain(FAILURE); |
| 66 | + expect(host.textContent).not.toContain(EMPTY_CACHE); |
| 67 | + expect(tryAgainButton()).toBeDefined(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("keeps the empty-cache guidance for a successful empty response", async () => { |
| 71 | + fetchMock.mockResolvedValueOnce(respond(200, { assets: [] })); |
| 72 | + await mount(); |
| 73 | + |
| 74 | + expect(host.textContent).toContain(EMPTY_CACHE); |
| 75 | + expect(host.textContent).not.toContain(FAILURE); |
| 76 | + }); |
| 77 | + |
| 78 | + it("says a search matched nothing, and restores the rows without refetching when it clears", async () => { |
| 79 | + fetchMock.mockResolvedValueOnce(respond(200, { assets: [LOGO] })); |
| 80 | + await mount("zzz"); |
| 81 | + |
| 82 | + expect(host.textContent).toContain("No global assets match"); |
| 83 | + expect(host.textContent).not.toContain(EMPTY_CACHE); |
| 84 | + |
| 85 | + await show(""); |
| 86 | + |
| 87 | + expect(host.textContent).toContain("Brand logo"); |
| 88 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 89 | + }); |
| 90 | + |
| 91 | + it.each([ |
| 92 | + ["the cached assets", { assets: [LOGO] }, "Brand logo"], |
| 93 | + ["the empty-cache guidance", { assets: [] }, EMPTY_CACHE], |
| 94 | + ])("retries a failed request only on request, then shows %s", async (_case, body, expected) => { |
| 95 | + let answerRetry: (response: Response) => void = () => {}; |
| 96 | + fetchMock.mockResolvedValueOnce(respond(503, {})).mockReturnValueOnce( |
| 97 | + new Promise((resolve) => { |
| 98 | + answerRetry = resolve; |
| 99 | + }), |
| 100 | + ); |
| 101 | + await mount(); |
| 102 | + await settle(); |
| 103 | + |
| 104 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 105 | + |
| 106 | + await act(async () => tryAgainButton()?.click()); |
| 107 | + |
| 108 | + expect(host.textContent).toContain(LOADING); |
| 109 | + |
| 110 | + await act(async () => answerRetry(respond(200, body))); |
| 111 | + await settle(); |
| 112 | + |
| 113 | + expect(host.textContent).toContain(expected); |
| 114 | + expect(tryAgainButton()).toBeUndefined(); |
| 115 | + expect(fetchMock).toHaveBeenCalledTimes(2); |
| 116 | + }); |
| 117 | +}); |
0 commit comments