Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

HtmlRenderDialog.browser.test.tsx

74 lines2.8 KB

1import { HTML_RENDER_DEFAULT_FONTS, htmlRenderTheme } from "@t3tools/shared/htmlRender";
2import { T3_CODE_DARK_THEME_COLORS } from "@t3tools/shared/themePalettes";
3import { expect, it, vi } from "vite-plus/test";
4import { page, userEvent } from "vite-plus/test/browser";
5​
6import "~/index.css";
7import { act, render } from "../../../test/render";
8​
9vi.mock(import("~/hooks/useHtmlRenderTheme"), async (original) => ({
10 ...(await original()),
11 useHtmlRenderTheme: () =>
12 htmlRenderTheme(T3_CODE_DARK_THEME_COLORS, "dark", HTML_RENDER_DEFAULT_FONTS),
13}));
14​
15import { HtmlRenderDialog } from "./HtmlRenderDialog";
16​
17const dialog = (
18 <HtmlRenderDialog
19 src="https://environment.test/page.html"
20 htmlRender={{ attachmentId: "html", title: "Diagram", height: 400 }}
21 onClose={() => {}}
22 />
23);
24​
25function frameAndCard() {
26 const frame = document.querySelector<HTMLIFrameElement>('[role="dialog"] iframe')!;
27 let card = frame.parentElement!;
28 while (getComputedStyle(card).overflowY !== "auto") card = card.parentElement!;
29 return { frame, card };
31​
32async function reportHeight(frame: HTMLIFrameElement, height: number) {
33 await act(() => {
34 window.dispatchEvent(
35 new MessageEvent("message", {
36 source: frame.contentWindow,
37 data: { jsonrpc: "2.0", method: "ui/notifications/size-changed", params: { height } },
38 }),
39 );
40 });
42​
43it("fits the frame to the page's height and scrolls only the card", async () => {
44 await page.viewport(1280, 800);
45 await using _screen = await render(dialog);
46 const { frame, card } = frameAndCard();
47 await reportHeight(frame, 300);
48 expect(frame.getBoundingClientRect().height).toBe(300);
49 expect(card.scrollHeight).toBe(card.clientHeight);
50 await reportHeight(frame, 5000);
51 expect(frame.getBoundingClientRect().height).toBe(5000);
52 expect(card.clientHeight).toBeLessThan(800);
53 expect(card.scrollHeight).toBeGreaterThan(card.clientHeight);
54});
55​
56it("zooms by reflowing the page to the card's width instead of overflowing it", async () => {
57 await page.viewport(1280, 800);
58 await using _screen = await render(dialog);
59 const { frame, card } = frameAndCard();
60 await reportHeight(frame, 300);
61 const contentWidth = frame.getBoundingClientRect().width;
62 for (let step = 0; step < 4; step += 1) {
63 await userEvent.click(page.getByRole("button", { name: "Zoom in" }));
64 }
65 expect(frame.getBoundingClientRect().width).toBeCloseTo(contentWidth, 0);
66 expect(frame.getBoundingClientRect().height).toBe(600);
67 expect(card.scrollWidth).toBe(card.clientWidth);
68 await userEvent.click(page.getByRole("button", { name: "Reset zoom" }));
69 for (let step = 0; step < 2; step += 1) {
70 await userEvent.click(page.getByRole("button", { name: "Zoom out" }));
71 }
72 expect(frame.getBoundingClientRect().width).toBeCloseTo(contentWidth, 0);
73 expect(frame.getBoundingClientRect().height).toBe(150);
74});