|
| 1 | +// @vitest-environment happy-dom |
| 2 | +// @vitest-environment-options {"happyDOM":{"settings":{"disableIframePageLoading":true}}} |
| 3 | + |
| 4 | +import React, { act, createRef } from "react"; |
| 5 | +import { createRoot } from "react-dom/client"; |
| 6 | +import { afterEach, expect, it, vi } from "vitest"; |
| 7 | +import { NLEPreview } from "./NLEPreview"; |
| 8 | + |
| 9 | +globalThis.IS_REACT_ACT_ENVIRONMENT = true; |
| 10 | + |
| 11 | +afterEach(() => { |
| 12 | + vi.useRealTimers(); |
| 13 | + document.body.innerHTML = ""; |
| 14 | +}); |
| 15 | + |
| 16 | +it("keeps a shadow reload whose runtime boots after 8 s, and its player comes ready", async () => { |
| 17 | + await import("@hyperframes/player"); |
| 18 | + vi.useFakeTimers(); |
| 19 | + const onShadowError = vi.fn(); |
| 20 | + const host = document.createElement("div"); |
| 21 | + document.body.append(host); |
| 22 | + const root = createRoot(host); |
| 23 | + await act(async () => { |
| 24 | + root.render( |
| 25 | + React.createElement(NLEPreview, { |
| 26 | + projectId: "p", |
| 27 | + iframeRef: createRef<HTMLIFrameElement>(), |
| 28 | + onIframeLoad: () => {}, |
| 29 | + previewSlots: [ |
| 30 | + { gen: 0, role: "live" }, |
| 31 | + { gen: 1, role: "shadow", url: "/api/projects/p/preview?_t=1" }, |
| 32 | + ], |
| 33 | + onShadowIframeLoad: () => {}, |
| 34 | + onShadowReadyChange: () => {}, |
| 35 | + onShadowError, |
| 36 | + setShadowIframeNode: () => {}, |
| 37 | + resetPreviewSlots: () => {}, |
| 38 | + }), |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + const shadow = host.querySelectorAll< |
| 43 | + HTMLElement & { iframeElement: HTMLIFrameElement; ready: boolean } |
| 44 | + >("hyperframes-player")[1]; |
| 45 | + const iframe = shadow.iframeElement; |
| 46 | + act(() => { |
| 47 | + iframe.dispatchEvent(new Event("load")); |
| 48 | + }); |
| 49 | + act(() => { |
| 50 | + vi.advanceTimersByTime(10_000); |
| 51 | + }); |
| 52 | + expect(onShadowError).not.toHaveBeenCalled(); |
| 53 | + |
| 54 | + Object.assign(iframe.contentWindow!, { __player: { getDuration: () => 9 } }); |
| 55 | + act(() => { |
| 56 | + vi.advanceTimersByTime(1_000); |
| 57 | + }); |
| 58 | + expect(onShadowError).not.toHaveBeenCalled(); |
| 59 | + expect(shadow.ready).toBe(true); |
| 60 | + // The live slot keeps the player's own 8 s deadline. |
| 61 | + expect(host.querySelector("hyperframes-player")?.hasAttribute("no-load-timeout")).toBe(false); |
| 62 | + act(() => root.unmount()); |
| 63 | +}); |
0 commit comments