|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import { act, useRef } from "react"; |
| 4 | +import { createRoot } from "react-dom/client"; |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | +import { usePreviewIframeStore } from "../../player/store/previewIframeStore"; |
| 7 | +import { |
| 8 | + usePreviewCompositionRect, |
| 9 | + type PreviewCompositionRect, |
| 10 | +} from "./usePreviewCompositionRect"; |
| 11 | + |
| 12 | +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 13 | + |
| 14 | +afterEach(() => { |
| 15 | + usePreviewIframeStore.getState().setIframe(null); |
| 16 | + document.body.innerHTML = ""; |
| 17 | +}); |
| 18 | + |
| 19 | +function boxed<T extends HTMLElement>(el: T, box: [number, number, number, number]): T { |
| 20 | + const [left, top, width, height] = box; |
| 21 | + el.getBoundingClientRect = () => new DOMRect(left, top, width, height); |
| 22 | + return el; |
| 23 | +} |
| 24 | + |
| 25 | +function mountProbe() { |
| 26 | + let rect: PreviewCompositionRect | undefined; |
| 27 | + function Probe() { |
| 28 | + const ref = useRef<HTMLDivElement>(null); |
| 29 | + rect = usePreviewCompositionRect(ref); |
| 30 | + return ( |
| 31 | + <div |
| 32 | + ref={(el) => { |
| 33 | + ref.current = el; |
| 34 | + if (el) boxed(el, [10, 20, 1000, 600]); |
| 35 | + }} |
| 36 | + /> |
| 37 | + ); |
| 38 | + } |
| 39 | + const host = document.createElement("div"); |
| 40 | + document.body.append(host); |
| 41 | + act(() => createRoot(host).render(<Probe />)); |
| 42 | + return () => rect; |
| 43 | +} |
| 44 | + |
| 45 | +function compositionFrame(box: [number, number, number, number], width: number, height: number) { |
| 46 | + const iframe = boxed(document.createElement("iframe"), box); |
| 47 | + document.body.append(iframe); |
| 48 | + const root = iframe.contentDocument?.createElement("div"); |
| 49 | + root?.setAttribute("data-composition-id", "main"); |
| 50 | + root?.setAttribute("data-width", String(width)); |
| 51 | + root?.setAttribute("data-height", String(height)); |
| 52 | + if (root) iframe.contentDocument?.body.append(root); |
| 53 | + return iframe; |
| 54 | +} |
| 55 | + |
| 56 | +const settle = () => new Promise((r) => setTimeout(r, 400)); |
| 57 | + |
| 58 | +describe("usePreviewCompositionRect", () => { |
| 59 | + it("reports the live preview iframe's box relative to the overlay, scaled to the composition", async () => { |
| 60 | + const read = mountProbe(); |
| 61 | + const iframe = compositionFrame([110, 70, 960, 540], 1920, 1080); |
| 62 | + await act(async () => { |
| 63 | + usePreviewIframeStore.getState().setIframe(iframe); |
| 64 | + await settle(); |
| 65 | + }); |
| 66 | + expect(read()).toEqual({ |
| 67 | + left: 100, |
| 68 | + top: 50, |
| 69 | + width: 960, |
| 70 | + height: 540, |
| 71 | + scaleX: 0.5, |
| 72 | + scaleY: 0.5, |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it("follows the iframe when a reload replaces it", async () => { |
| 77 | + const read = mountProbe(); |
| 78 | + await act(async () => { |
| 79 | + usePreviewIframeStore.getState().setIframe(compositionFrame([110, 70, 960, 540], 1920, 1080)); |
| 80 | + await settle(); |
| 81 | + }); |
| 82 | + await act(async () => { |
| 83 | + usePreviewIframeStore.getState().setIframe(compositionFrame([60, 40, 500, 500], 1000, 1000)); |
| 84 | + await settle(); |
| 85 | + }); |
| 86 | + expect(read()).toEqual({ |
| 87 | + left: 50, |
| 88 | + top: 20, |
| 89 | + width: 500, |
| 90 | + height: 500, |
| 91 | + scaleX: 0.5, |
| 92 | + scaleY: 0.5, |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments