|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import { act } from "react"; |
| 3 | +import { createRoot, type Root } from "react-dom/client"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { MediaPreview } from "./MediaPreview"; |
| 6 | + |
| 7 | +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 8 | + |
| 9 | +let root: Root | null = null; |
| 10 | +let host: HTMLDivElement | null = null; |
| 11 | + |
| 12 | +afterEach(() => { |
| 13 | + if (root) act(() => root?.unmount()); |
| 14 | + root = null; |
| 15 | + host = null; |
| 16 | + document.body.replaceChildren(); |
| 17 | +}); |
| 18 | + |
| 19 | +async function renderPreview(projectId: string, filePath: string): Promise<HTMLDivElement> { |
| 20 | + if (!root) { |
| 21 | + host = document.createElement("div"); |
| 22 | + document.body.append(host); |
| 23 | + root = createRoot(host); |
| 24 | + } |
| 25 | + await act(async () => { |
| 26 | + root?.render(<MediaPreview projectId={projectId} filePath={filePath} />); |
| 27 | + }); |
| 28 | + return host as HTMLDivElement; |
| 29 | +} |
| 30 | + |
| 31 | +describe("MediaPreview", () => { |
| 32 | + it("retries a repaired file after visiting another media source", async () => { |
| 33 | + const host = await renderPreview("test", "broken.png"); |
| 34 | + const failedImage = host.querySelector("img"); |
| 35 | + expect(failedImage).not.toBeNull(); |
| 36 | + |
| 37 | + await act(async () => { |
| 38 | + failedImage?.dispatchEvent(new Event("error")); |
| 39 | + }); |
| 40 | + expect(host.textContent).toContain("Couldn't load this file"); |
| 41 | + |
| 42 | + await renderPreview("test", "healthy.png"); |
| 43 | + expect(host.querySelector("img")?.getAttribute("src")).toContain("healthy.png"); |
| 44 | + |
| 45 | + await renderPreview("test", "broken.png"); |
| 46 | + expect(host.querySelector("img")?.getAttribute("src")).toContain("broken.png"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("resets failure state when the project changes, but preserves a current failure", async () => { |
| 50 | + const host = await renderPreview("first", "broken.png"); |
| 51 | + const image = host.querySelector("img"); |
| 52 | + await act(async () => { |
| 53 | + image?.dispatchEvent(new Event("error")); |
| 54 | + }); |
| 55 | + expect(host.textContent).toContain("Couldn't load this file"); |
| 56 | + |
| 57 | + await renderPreview("second", "broken.png"); |
| 58 | + expect(host.querySelector("img")?.getAttribute("src")).toContain("/second/preview/broken.png"); |
| 59 | + |
| 60 | + const secondImage = host.querySelector("img"); |
| 61 | + await act(async () => { |
| 62 | + secondImage?.dispatchEvent(new Event("error")); |
| 63 | + }); |
| 64 | + expect(host.textContent).toContain("Couldn't load this file"); |
| 65 | + |
| 66 | + await renderPreview("second", "broken.png"); |
| 67 | + expect(host.querySelector("img")).toBeNull(); |
| 68 | + }); |
| 69 | +}); |
0 commit comments