|
| 1 | +// @vitest-environment jsdom |
| 2 | + |
| 3 | +import { act } from "react"; |
| 4 | +import { createRoot, type Root } from "react-dom/client"; |
| 5 | +import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; |
| 6 | +import { htmlRenderTheme, HTML_RENDER_DEFAULT_FONTS } from "@t3tools/shared/htmlRender"; |
| 7 | +import { T3_CODE_DARK_THEME_COLORS } from "@t3tools/shared/themePalettes"; |
| 8 | +import { EnvironmentId } from "@t3tools/contracts"; |
| 9 | + |
| 10 | +import { HtmlRenderDialog } from "./HtmlRenderDialog"; |
| 11 | +import { HtmlRenderFrame } from "./HtmlRenderFrame"; |
| 12 | + |
| 13 | +const { refresh, showError } = vi.hoisted(() => ({ refresh: vi.fn(), showError: vi.fn() })); |
| 14 | +vi.mock("~/assets/assetUrls", () => ({ |
| 15 | + useAssetUrlState: () => ({ |
| 16 | + _tag: "Success", |
| 17 | + url: "https://environment.test/original.html", |
| 18 | + expiresAt: Date.now() + 60 * 60_000, |
| 19 | + }), |
| 20 | + useAssetUrlRefresh: () => refresh, |
| 21 | +})); |
| 22 | +vi.mock("../ui/toast", () => ({ toastManager: { add: showError } })); |
| 23 | + |
| 24 | +vi.mock("~/hooks/useHtmlRenderTheme", () => ({ |
| 25 | + useHtmlRenderTheme: () => |
| 26 | + htmlRenderTheme(T3_CODE_DARK_THEME_COLORS, "dark", HTML_RENDER_DEFAULT_FONTS), |
| 27 | +})); |
| 28 | + |
| 29 | +let root: Root; |
| 30 | +let container: HTMLDivElement; |
| 31 | +const onClose = vi.fn(); |
| 32 | + |
| 33 | +beforeEach(async () => { |
| 34 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 35 | + vi.stubGlobal( |
| 36 | + "ResizeObserver", |
| 37 | + class { |
| 38 | + observe() {} |
| 39 | + disconnect() {} |
| 40 | + unobserve() {} |
| 41 | + }, |
| 42 | + ); |
| 43 | + refresh.mockReset(); |
| 44 | + showError.mockClear(); |
| 45 | + onClose.mockClear(); |
| 46 | + container = document.createElement("div"); |
| 47 | + document.body.append(container); |
| 48 | + root = createRoot(container); |
| 49 | + await act(() => |
| 50 | + root.render( |
| 51 | + <HtmlRenderDialog |
| 52 | + src="https://environment.test/page.html" |
| 53 | + title="Diagram" |
| 54 | + onClose={onClose} |
| 55 | + />, |
| 56 | + ), |
| 57 | + ); |
| 58 | +}); |
| 59 | + |
| 60 | +afterEach(async () => { |
| 61 | + await act(() => root.unmount()); |
| 62 | + container.remove(); |
| 63 | + vi.unstubAllGlobals(); |
| 64 | +}); |
| 65 | + |
| 66 | +async function click(label: string) { |
| 67 | + const button = document.querySelector<HTMLButtonElement>(`button[aria-label="${label}"]`); |
| 68 | + expect(button).not.toBeNull(); |
| 69 | + await act(() => button!.click()); |
| 70 | +} |
| 71 | + |
| 72 | +it("zooms the loaded page within bounds and resets without reloading it", async () => { |
| 73 | + const frame = document.querySelector("iframe")!; |
| 74 | + const src = frame.src; |
| 75 | + for (let step = 0; step < 6; step += 1) await click("Zoom in"); |
| 76 | + expect(document.querySelector<HTMLButtonElement>('[aria-label="Zoom in"]')!.disabled).toBe(true); |
| 77 | + expect(frame.parentElement!.style.getPropertyValue("zoom")).toBe("2"); |
| 78 | + for (let step = 0; step < 8; step += 1) await click("Zoom out"); |
| 79 | + expect(document.querySelector<HTMLButtonElement>('[aria-label="Zoom out"]')!.disabled).toBe(true); |
| 80 | + expect(frame.parentElement!.style.getPropertyValue("zoom")).toBe("0.5"); |
| 81 | + await click("Reset zoom"); |
| 82 | + expect(frame.parentElement!.style.getPropertyValue("zoom")).toBe("1"); |
| 83 | + expect(document.querySelector("iframe")).toBe(frame); |
| 84 | + expect(frame.src).toBe(src); |
| 85 | + expect(frame.getAttribute("sandbox")).toBe("allow-scripts allow-forms"); |
| 86 | +}); |
| 87 | + |
| 88 | +it("closes with the close button or Escape", async () => { |
| 89 | + await click("Close"); |
| 90 | + expect(onClose).toHaveBeenCalledOnce(); |
| 91 | + onClose.mockClear(); |
| 92 | + await act(() => { |
| 93 | + document.activeElement?.dispatchEvent( |
| 94 | + new KeyboardEvent("keydown", { key: "Escape", bubbles: true }), |
| 95 | + ); |
| 96 | + }); |
| 97 | + expect(onClose).toHaveBeenCalledOnce(); |
| 98 | +}); |
| 99 | + |
| 100 | +async function renderInline() { |
| 101 | + await act(() => |
| 102 | + root.render( |
| 103 | + <HtmlRenderFrame |
| 104 | + environmentId={EnvironmentId.make("environment")} |
| 105 | + htmlRender={{ attachmentId: "html", title: "Diagram", height: 400 }} |
| 106 | + onOpen={() => undefined} |
| 107 | + />, |
| 108 | + ), |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +it("opens with a fresh URL while keeping the inline page loaded", async () => { |
| 113 | + await renderInline(); |
| 114 | + const inlineFrame = document.querySelector("iframe")!; |
| 115 | + const originalSrc = inlineFrame.src; |
| 116 | + refresh.mockResolvedValue("https://environment.test/renewed.html"); |
| 117 | + await click("Open full screen"); |
| 118 | + expect(refresh).toHaveBeenCalledOnce(); |
| 119 | + expect(document.querySelector('[role="dialog"] iframe')?.getAttribute("src")).toContain( |
| 120 | + "https://environment.test/renewed.html#", |
| 121 | + ); |
| 122 | + expect(inlineFrame.src).toBe(originalSrc); |
| 123 | + await click("Close"); |
| 124 | + expect(document.querySelector('[role="dialog"]')).toBeNull(); |
| 125 | + expect(document.querySelector("iframe")).toBe(inlineFrame); |
| 126 | +}); |
| 127 | + |
| 128 | +it.each([null, new Error("Disconnected")])( |
| 129 | + "reports an unavailable URL instead of opening a blank page: %s", |
| 130 | + async (failure) => { |
| 131 | + await renderInline(); |
| 132 | + if (failure === null) refresh.mockResolvedValue(null); |
| 133 | + else refresh.mockRejectedValue(failure); |
| 134 | + await click("Open full screen"); |
| 135 | + expect(document.querySelector('[role="dialog"]')).toBeNull(); |
| 136 | + expect(showError).toHaveBeenCalledWith( |
| 137 | + expect.objectContaining({ type: "error", title: "Page unavailable" }), |
| 138 | + ); |
| 139 | + }, |
| 140 | +); |
0 commit comments