|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import { describe, it, expect, vi } from "vitest"; |
| 4 | +import { applySoftReload } from "./gsapSoftReload"; |
| 5 | + |
| 6 | +const SCRIPT_TEXT = ` |
| 7 | +window.__timelines = window.__timelines || {}; |
| 8 | +const tl = gsap.timeline({ paused: true }); |
| 9 | +tl.to("#box", { opacity: 0.8 }); |
| 10 | +window.__timelines["root"] = tl; |
| 11 | +`; |
| 12 | + |
| 13 | +function buildMockIframe(overrides: Record<string, unknown> = {}) { |
| 14 | + const scriptEl = document.createElement("script"); |
| 15 | + scriptEl.textContent = |
| 16 | + 'const tl = gsap.timeline({ paused: true }); tl.to("#box", { opacity: 0.5 });'; |
| 17 | + const container = document.createElement("div"); |
| 18 | + container.appendChild(scriptEl); |
| 19 | + |
| 20 | + const mockTimeline = { kill: vi.fn(), pause: vi.fn() }; |
| 21 | + const contentWindow = { |
| 22 | + gsap: { timeline: vi.fn() }, |
| 23 | + __hfForceTimelineRebind: vi.fn(), |
| 24 | + __timelines: { root: mockTimeline } as Record<string, typeof mockTimeline>, |
| 25 | + __player: { getTime: () => 2.0, seek: vi.fn() }, |
| 26 | + __hfStudioManualEditsApply: vi.fn(), |
| 27 | + __hfSuppressSceneMutations: undefined as undefined | (<T>(fn: () => T) => T), |
| 28 | + ...overrides, |
| 29 | + }; |
| 30 | + |
| 31 | + const contentDocument = { |
| 32 | + querySelectorAll: (sel: string) => (sel === "script:not([src])" ? [scriptEl] : []), |
| 33 | + createElement: (tag: string) => document.createElement(tag), |
| 34 | + body: container, |
| 35 | + }; |
| 36 | + |
| 37 | + return { |
| 38 | + iframe: { contentWindow, contentDocument } as unknown as HTMLIFrameElement, |
| 39 | + contentWindow, |
| 40 | + mockTimeline, |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +describe("applySoftReload", () => { |
| 45 | + it("returns false when iframe is null", () => { |
| 46 | + expect(applySoftReload(null, SCRIPT_TEXT)).toBe(false); |
| 47 | + }); |
| 48 | + |
| 49 | + it("returns false when scriptText is empty", () => { |
| 50 | + const { iframe } = buildMockIframe(); |
| 51 | + expect(applySoftReload(iframe, "")).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + it("returns false when gsap is not on iframe window", () => { |
| 55 | + const { iframe } = buildMockIframe({ gsap: undefined }); |
| 56 | + expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe(false); |
| 57 | + }); |
| 58 | + |
| 59 | + it("returns false when __hfForceTimelineRebind is missing", () => { |
| 60 | + const { iframe } = buildMockIframe({ __hfForceTimelineRebind: undefined }); |
| 61 | + expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it("kills existing timelines, rebinds, and re-seeks on success", () => { |
| 65 | + const { iframe, contentWindow, mockTimeline } = buildMockIframe(); |
| 66 | + const result = applySoftReload(iframe, SCRIPT_TEXT); |
| 67 | + expect(result).toBe(true); |
| 68 | + expect(mockTimeline.kill).toHaveBeenCalled(); |
| 69 | + expect(contentWindow.__hfForceTimelineRebind).toHaveBeenCalled(); |
| 70 | + expect(contentWindow.__player.seek).toHaveBeenCalledWith(2.0); |
| 71 | + expect(contentWindow.__hfStudioManualEditsApply).toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it("wraps execution in __hfSuppressSceneMutations when available", () => { |
| 75 | + let suppressionCalled = false; |
| 76 | + const { iframe } = buildMockIframe({ |
| 77 | + __hfSuppressSceneMutations: <T>(fn: () => T): T => { |
| 78 | + suppressionCalled = true; |
| 79 | + return fn(); |
| 80 | + }, |
| 81 | + }); |
| 82 | + const result = applySoftReload(iframe, SCRIPT_TEXT); |
| 83 | + expect(result).toBe(true); |
| 84 | + expect(suppressionCalled).toBe(true); |
| 85 | + }); |
| 86 | + |
| 87 | + it("returns false when multiple GSAP scripts exist (ambiguous)", () => { |
| 88 | + const script1 = document.createElement("script"); |
| 89 | + script1.textContent = "const tl = gsap.timeline({ paused: true });"; |
| 90 | + const script2 = document.createElement("script"); |
| 91 | + script2.textContent = 'tl.to("#other", { x: 10 });'; |
| 92 | + const container = document.createElement("div"); |
| 93 | + container.appendChild(script1); |
| 94 | + container.appendChild(script2); |
| 95 | + |
| 96 | + const { iframe } = buildMockIframe(); |
| 97 | + (iframe as unknown as { contentDocument: unknown }).contentDocument = { |
| 98 | + querySelectorAll: (sel: string) => (sel === "script:not([src])" ? [script1, script2] : []), |
| 99 | + createElement: (tag: string) => document.createElement(tag), |
| 100 | + body: container, |
| 101 | + }; |
| 102 | + expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe(false); |
| 103 | + }); |
| 104 | +}); |
0 commit comments