// @vitest-environment jsdom import gsap from "gsap"; import { afterEach, describe, expect, it } from "vitest"; import { pasteTimelineClips } from "../hooks/useClipboard"; const GOODBYE = '

Goodbye

'; const FILM = `

Title

${GOODBYE}
`; const goodbye = { html: GOODBYE, start: 1, duration: 3, track: 3 }; /** Runs the film's own script with real GSAP and says when each tween starts, by its target's id. */ function tweenStarts(html: string): Array<[string, number]> { const doc = new DOMParser().parseFromString(html, "text/html"); document.body.innerHTML = doc.body.innerHTML; const win = window as unknown as { __timelines: Record }; win.__timelines = {}; new Function("gsap", doc.querySelector("script")?.textContent ?? "")(gsap); return win.__timelines .main!.getChildren() .map((tween) => [(tween.targets()[0] as Element).id, tween.startTime()] as [string, number]) .sort(([a], [b]) => a.localeCompare(b)); } /** The declarations the film's styles give the rules for `selector`. */ function rulesFor(html: string, selector: string): string[] { const sheet = new CSSStyleSheet(); sheet.replaceSync( new DOMParser().parseFromString(html, "text/html").querySelector("style")!.textContent!, ); return Array.from(sheet.cssRules) .filter( (rule): rule is CSSStyleRule => rule instanceof CSSStyleRule && rule.selectorText === selector, ) .map((rule) => rule.style.cssText); } describe("a pasted clip takes its original's look and motion", () => { afterEach(() => { document.body.innerHTML = ""; }); it("copies the id's rules and tweens for the renamed copy, moved to where it lands", () => { const { content } = pasteTimelineClips(FILM, [goodbye], 4, [], true); expect(content).toContain('id="goodbye-2"'); expect(rulesFor(content, "#goodbye-2")).toEqual(rulesFor(FILM, "#goodbye")); expect(tweenStarts(content)).toEqual([ ["goodbye", 1], ["goodbye-2", 4], ["title", 1.5], ]); // The original's own motion is untouched: the copy's tween does not push the title's. expect(tweenStarts(FILM)).toEqual([ ["goodbye", 1], ["title", 1.5], ]); }); it("never moves the film's own tweens, whatever their timing, and runs a timeline declared in a block", () => { const film = FILM.replace( / `, ); const { content } = pasteTimelineClips(film, [goodbye], 4, [], true); const own = (html: string) => tweenStarts(html).filter(([id]) => id !== "goodbye-2"); expect(own(content)).toEqual(own(film)); // Only the tween at a number is copied: its copy starts 3 s after it (1.2 s with its delay), like the clip. expect(tweenStarts(content).filter(([id]) => id === "goodbye-2")).toEqual([["goodbye-2", 4.2]]); }); it("copies only the selectors naming the original, and leaves a style inside a script alone", () => { const styleInScript = `document.head.insertAdjacentHTML("beforeend", "");`; const film = FILM.replace( " #title { font-size: 72px; }", " #title, #goodbye { letter-spacing: 2px; }\n #title { letter-spacing: 9px; }", ).replace(" window.__timelines", ` ${styleInScript}\n window.__timelines`); const { content } = pasteTimelineClips(film, [goodbye], 4, [], true); expect(rulesFor(content, "#goodbye-2")).toEqual([ ...rulesFor(film, "#goodbye"), "letter-spacing: 2px;", ]); expect(content.match(/#title/g)?.length).toBe(film.match(/#title/g)?.length); expect(content).toContain(styleInScript); }); it("copies nothing for a clip from another file, whose id means something else here", () => { const { content } = pasteTimelineClips(FILM, [goodbye], 4, []); expect(rulesFor(content, "#goodbye-2")).toEqual([]); expect(tweenStarts(content).map(([id]) => id)).toEqual(["goodbye", "title"]); }); });