|
| 1 | +// fallow-ignore-file code-duplication |
1 | 2 | // @vitest-environment happy-dom |
2 | 3 |
|
| 4 | +import { patchRuntimeTweenInPlace } from "../hooks/gsapRuntimePatch"; |
3 | 5 | import { afterEach, describe, it, expect, vi } from "vitest"; |
4 | 6 | import { |
5 | 7 | readNestedFiles, |
@@ -70,6 +72,29 @@ function buildMockIframe(overrides: Record<string, unknown> = {}) { |
70 | 72 | }; |
71 | 73 | } |
72 | 74 |
|
| 75 | +/** A mock iframe whose document holds one GSAP script and one composition root; `byId` answers id selectors. */ |
| 76 | +function iframeWithComposition(script: string, root: Element, byId: Record<string, Element> = {}) { |
| 77 | + const scriptEl = document.createElement("script"); |
| 78 | + scriptEl.textContent = script; |
| 79 | + const container = document.createElement("div"); |
| 80 | + container.appendChild(scriptEl); |
| 81 | + const { iframe } = buildMockIframe({ gsap: { timeline: vi.fn(), set: vi.fn() } }); |
| 82 | + (iframe as unknown as { contentDocument: unknown }).contentDocument = { |
| 83 | + querySelectorAll: (sel: string) => |
| 84 | + sel === "script:not([src])" |
| 85 | + ? [scriptEl] |
| 86 | + : sel.includes("composition-id") |
| 87 | + ? [root] |
| 88 | + : byId[sel] |
| 89 | + ? [byId[sel]] |
| 90 | + : [], |
| 91 | + createElement: (tag: string) => document.createElement(tag), |
| 92 | + body: container, |
| 93 | + head: document.createElement("div"), |
| 94 | + }; |
| 95 | + return iframe; |
| 96 | +} |
| 97 | + |
73 | 98 | describe("applySoftReload", () => { |
74 | 99 | it('returns "cannot-soft-reload" when iframe is null', () => { |
75 | 100 | expect(applySoftReload(null, SCRIPT_TEXT)).toBe("cannot-soft-reload"); |
@@ -127,30 +152,51 @@ describe("applySoftReload", () => { |
127 | 152 | orphan.style.cssText = "left: 1240px; top: 200px; transform: translate(449px, 0px)"; |
128 | 153 | Object.assign(orphan, { _gsap: {} }); // GSAP cache marker (set by gsap.set) |
129 | 154 |
|
130 | | - const scriptEl = document.createElement("script"); |
131 | | - scriptEl.textContent = 'const tl = gsap.timeline({ paused: true }); tl.to("#x", { x: 1 });'; |
132 | | - const container = document.createElement("div"); |
133 | | - container.appendChild(scriptEl); |
134 | | - |
135 | 155 | const root = document.createElement("div"); |
136 | 156 | root.setAttribute("data-composition-id", "root"); |
137 | 157 | root.appendChild(orphan); |
138 | | - |
139 | | - const { iframe } = buildMockIframe({ gsap: { timeline: vi.fn(), set: vi.fn() } }); |
140 | | - (iframe as unknown as { contentDocument: unknown }).contentDocument = { |
141 | | - querySelectorAll: (sel: string) => |
142 | | - sel === "script:not([src])" ? [scriptEl] : sel.includes("composition-id") ? [root] : [], |
143 | | - createElement: (tag: string) => document.createElement(tag), |
144 | | - body: container, |
145 | | - head: document.createElement("div"), |
146 | | - }; |
| 158 | + const iframe = iframeWithComposition( |
| 159 | + 'const tl = gsap.timeline({ paused: true }); tl.to("#x", { x: 1 });', |
| 160 | + root, |
| 161 | + ); |
147 | 162 |
|
148 | 163 | applySoftReload(iframe, SCRIPT_TEXT); |
149 | 164 |
|
150 | 165 | expect(orphan.style.transform).toBe(""); // stale GSAP transform stripped |
151 | 166 | expect(orphan.style.left).toBe("1240px"); // authored CSS base preserved |
152 | 167 | }); |
153 | 168 |
|
| 169 | + it("clears what a standalone gsap.set wrote once the new script no longer sets it", () => { |
| 170 | + // An undo of a Design-panel W edit on an animated box removes its gsap.set width. |
| 171 | + const target = document.createElement("div"); |
| 172 | + target.id = "target"; |
| 173 | + target.style.cssText = "left: 10px; width: 300px"; |
| 174 | + Object.assign(target, { _gsap: {} }); |
| 175 | + const root = document.createElement("div"); |
| 176 | + root.setAttribute("data-composition-id", "root"); |
| 177 | + root.appendChild(target); |
| 178 | + const iframe = iframeWithComposition( |
| 179 | + `window.__timelines = window.__timelines || {}; |
| 180 | +const tl = gsap.timeline({ paused: true }); |
| 181 | +tl.to("#target", { x: 100 }); |
| 182 | +gsap.set("#target", { width: 300 }); |
| 183 | +window.__timelines["root"] = tl;`, |
| 184 | + root, |
| 185 | + { "#target": target }, |
| 186 | + ); |
| 187 | + const restored = `window.__timelines = window.__timelines || {}; |
| 188 | +const tl = gsap.timeline({ paused: true }); |
| 189 | +tl.to("#target", { x: 100 }); |
| 190 | +window.__timelines["root"] = tl;`; |
| 191 | + |
| 192 | + applySoftReload(iframe, restored, { |
| 193 | + authoredHtml: `<html><body><div data-composition-id="root"><div id="target" style="left: 10px"></div></div><script>${restored}</script></body></html>`, |
| 194 | + }); |
| 195 | + |
| 196 | + expect(target.style.width).toBe(""); |
| 197 | + expect(target.style.left).toBe("10px"); |
| 198 | + }); |
| 199 | + |
154 | 200 | it("wraps execution in __hfSuppressSceneMutations when available", () => { |
155 | 201 | let suppressionCalled = false; |
156 | 202 | const { iframe } = buildMockIframe({ |
@@ -917,3 +963,41 @@ describe("applySoftReload over a composition's own markup", () => { |
917 | 963 | expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe("cannot-soft-reload"); |
918 | 964 | }); |
919 | 965 | }); |
| 966 | + |
| 967 | +describe("a gsap.set a live patch applied", () => { |
| 968 | + it("is cleared by the next soft reload once the new script no longer sets it, and only once", () => { |
| 969 | + const markup = `<div data-composition-id="root"><div id="a" style="left: 10px"></div></div>`; |
| 970 | + const doc = document.implementation.createHTMLDocument(""); |
| 971 | + doc.body.innerHTML = `${markup}<script>window.__timelines["root"]=gsap.timeline();</script>`; |
| 972 | + const set = (target: HTMLElement | HTMLElement[], vars: Record<string, unknown>) => { |
| 973 | + for (const el of [target].flat()) { |
| 974 | + if (vars.clearProps) el.removeAttribute("style"); |
| 975 | + else for (const [k, v] of Object.entries(vars)) el.style.setProperty(k, `${v}px`); |
| 976 | + } |
| 977 | + }; |
| 978 | + const iframe = { |
| 979 | + contentDocument: doc, |
| 980 | + contentWindow: { |
| 981 | + gsap: { timeline: () => {}, set }, |
| 982 | + __hfForceTimelineRebind: () => {}, |
| 983 | + __timelines: {}, |
| 984 | + __player: { getTime: () => 0, seek: () => {} }, |
| 985 | + }, |
| 986 | + } as unknown as HTMLIFrameElement; |
| 987 | + const el = doc.getElementById("a")!; |
| 988 | + const reload = () => { |
| 989 | + const script = `window.__timelines["root"]=gsap.timeline();gsap.set("#a",{width:300});`; |
| 990 | + applySoftReload(iframe, script, { authoredHtml: `${markup}<script>${script}</script>` }); |
| 991 | + }; |
| 992 | + |
| 993 | + // W 300, then H 200, then a commit that removes height from the set. |
| 994 | + patchRuntimeTweenInPlace(iframe, "#a", { kind: "global-set", props: { width: 300 } }); |
| 995 | + patchRuntimeTweenInPlace(iframe, "#a", { kind: "global-set", props: { height: 200 } }); |
| 996 | + reload(); |
| 997 | + expect([el.style.height, el.style.left]).toEqual(["", "10px"]); |
| 998 | + |
| 999 | + el.style.height = "50px"; |
| 1000 | + reload(); |
| 1001 | + expect(el.style.height).toBe("50px"); |
| 1002 | + }); |
| 1003 | +}); |
0 commit comments