|
| 1 | +import { injectHtmlRenderBootstrap } from "@t3tools/shared/htmlRender"; |
1 | 2 | import { describe, expect, it, vi } from "vite-plus/test"; |
2 | 3 |
|
3 | 4 | import "~/index.css"; |
@@ -34,3 +35,109 @@ describe("HtmlRenderDocument", () => { |
34 | 35 | expect(onContentHeight).toHaveBeenCalledTimes(1); |
35 | 36 | }); |
36 | 37 | }); |
| 38 | + |
| 39 | +it("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 | +}); |
0 commit comments