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

BrowserDocumentFrame.browser.test.tsx

143 lines5.6 KB

1import { injectHtmlRenderBootstrap } from "@t3tools/shared/htmlRender";
2import { describe, expect, it, vi } from "vite-plus/test";
3​
4import "~/index.css";
5import { render } from "../../../test/render";
6​
7import { HtmlRenderDocument } from "./BrowserDocumentFrame";
8​
9const sizeChanged = (height: number) => ({
10 jsonrpc: "2.0",
11 method: "ui/notifications/size-changed",
12 params: { height },
13});
14​
15describe("HtmlRenderDocument", () => {
16 it("reports the page's first height and ignores messages from other windows", async () => {
17 using pageUrl = (() => {
18 const page = `<script>parent.postMessage(${JSON.stringify(sizeChanged(321))}, "*");</script>`;
19 const url = URL.createObjectURL(new Blob([page], { type: "text/html" }));
20 return { url, [Symbol.dispose]: () => URL.revokeObjectURL(url) };
21 })();
22 const onContentHeight = vi.fn();
23 await using _screen = await render(
24 <HtmlRenderDocument src={pageUrl.url} title="Report" onContentHeight={onContentHeight} />,
25 );
26​
27 await vi.waitFor(() => expect(onContentHeight).toHaveBeenCalledWith(321));
28​
29 // Listeners run in registration order, so once this one sees the message the frame's has too.
30 const delivered = new Promise<void>((resolve) =>
31 window.addEventListener("message", () => resolve(), { once: true }),
32 );
33 window.postMessage(sizeChanged(999), "*");
34 await delivered;
35 expect(onContentHeight).toHaveBeenCalledTimes(1);
36 });
37});
38​
39it("keeps the first and last selected card rings inside the fitted renderer frame", async () => {
40 const html = injectHtmlRenderBootstrap(`<!doctype html><html><head><style>
41 main { display: grid; gap: 12px; }
42 button { box-sizing: border-box; width: 100%; height: 72px; border: 0; }
43 button[aria-pressed="true"] { outline: 3px solid blue; outline-offset: 2px; }
44 </style></head><body><main>
45 <button id="first" aria-pressed="false">Meadow</button>
46 <button id="middle" aria-pressed="false">Harbor</button>
47 <button id="last" aria-pressed="false">Orchid</button>
48 </main><script>
49 document.querySelectorAll("button").forEach(function(button) {
50 button.addEventListener("click", function() {
51 document.querySelectorAll("button").forEach(function(card) {
52 card.setAttribute("aria-pressed", String(card === button));
53 });
54 });
55 });
56 window.addEventListener("message", function(event) {
57 if (event.source !== window.parent || event.data.type !== "select-card") return;
58 var button = document.getElementById(event.data.id);
59 button.click();
60 var rect = button.getBoundingClientRect();
61 var style = getComputedStyle(button);
62 var ring = parseFloat(style.outlineWidth) + parseFloat(style.outlineOffset);
63 window.parent.postMessage({
64 type: "selected-card-bounds",
65 id: button.id,
66 selected: button.getAttribute("aria-pressed") === "true",
67 ring: ring,
68 left: rect.left - ring,
69 right: rect.right + ring,
70 top: rect.top - ring,
71 bottom: rect.bottom + ring,
72 pageHeight: document.documentElement.getBoundingClientRect().height,
73 viewportHeight: window.innerHeight,
74 viewportWidth: window.innerWidth,
75 scrollWidth: document.documentElement.scrollWidth
76 }, "*");
77 });
78 </script></body></html>`);
79 const src = URL.createObjectURL(new Blob([html], { type: "text/html" }));
80 using _url = { [Symbol.dispose]: () => URL.revokeObjectURL(src) };
81 const container = document.createElement("div");
82 container.style.width = "728px";
83 const reports = new Map<
84 string,
85 {
86 selected: boolean;
87 ring: number;
88 left: number;
89 right: number;
90 top: number;
91 bottom: number;
92 pageHeight: number;
93 viewportHeight: number;
94 viewportWidth: number;
95 scrollWidth: number;
96 }
97 >();
98 const messages = new AbortController();
99 using _messages = { [Symbol.dispose]: () => messages.abort() };
100 window.addEventListener(
101 "message",
102 (event) => {
103 const frame = container.querySelector("iframe");
104 if (event.source === frame?.contentWindow && event.data?.type === "selected-card-bounds") {
105 reports.set(event.data.id, event.data);
106 }
107 },
108 { signal: messages.signal },
109 );
110 const contentHeight = vi.fn((height: number) => {
111 container.style.setProperty("--content-height", `${height}px`);
112 });
113 await using screen = await render(
114 <>
115 <style>{`iframe[title="Edge cards"] {
116 display: block; width: 100%; height: var(--content-height, 120px); border: 0;
117 }`}</style>
118 <HtmlRenderDocument src={src} title="Edge cards" onContentHeight={contentHeight} />
119 </>,
120 { container },
121 );
122​
123 await vi.waitFor(() => expect(contentHeight).toHaveBeenCalled());
124 const frame = screen.getByTitle("Edge cards").element() as HTMLIFrameElement;
125 await vi.waitFor(() => {
126 expect(frame.getBoundingClientRect().height).toBe(contentHeight.mock.lastCall?.[0]);
127 });
128​
129 for (const id of ["first", "last"]) {
130 frame.contentWindow?.postMessage({ type: "select-card", id }, "*");
131 await vi.waitFor(() => expect(reports.get(id)?.selected).toBe(true));
132 const bounds = reports.get(id)!;
133 expect(bounds.ring).toBe(5);
134 expect(bounds.viewportWidth).toBe(728);
135 expect(bounds.pageHeight).toBe(contentHeight.mock.lastCall?.[0]);
136 expect(bounds.viewportHeight).toBe(bounds.pageHeight);
137 expect(bounds.left).toBeGreaterThanOrEqual(0);
138 expect(bounds.top).toBeGreaterThanOrEqual(0);
139 expect(bounds.right).toBeLessThanOrEqual(bounds.viewportWidth);
140 expect(bounds.bottom).toBeLessThanOrEqual(bounds.viewportHeight);
141 expect(bounds.scrollWidth).toBeLessThanOrEqual(bounds.viewportWidth);
142 }
143});