|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { parseGsapScript } from "@hyperframes/core/gsap-parser"; |
| 4 | +import { addAnimationWithKeyframesToScript } from "@hyperframes/parsers/gsap-writer-acorn"; |
| 5 | +import type { DomEditSelection } from "../components/editor/domEditingTypes"; |
| 6 | +import { buildStableSelector, getSelectorIndex } from "../components/editor/domEditingDom"; |
| 7 | +import { resolveSelectorElementIds, writeTargetSelector } from "./gsapShared"; |
| 8 | +import { commitKeyframeAtTimeImpl } from "./gsapKeyframeCommit"; |
| 9 | + |
| 10 | +afterEach(() => { |
| 11 | + document.body.innerHTML = ""; |
| 12 | +}); |
| 13 | + |
| 14 | +/** |
| 15 | + * A selection built the way production builds it (getDomLayerPatchTarget), so |
| 16 | + * the class-only case under test is the real one: buildStableSelector hands back |
| 17 | + * a BARE class for an element with no id / hf-id / composition id. |
| 18 | + */ |
| 19 | +function selectionFor(el: HTMLElement): DomEditSelection { |
| 20 | + const selector = buildStableSelector(el); |
| 21 | + return { |
| 22 | + element: el, |
| 23 | + id: el.id || undefined, |
| 24 | + hfId: el.getAttribute("data-hf-id") || undefined, |
| 25 | + selector, |
| 26 | + selectorIndex: getSelectorIndex(document, el, selector, "index.html", null), |
| 27 | + sourceFile: "index.html", |
| 28 | + dataAttributes: { start: "0", duration: "2" }, |
| 29 | + } as unknown as DomEditSelection; |
| 30 | +} |
| 31 | + |
| 32 | +/** Five class-only siblings — the shape that made one add collapse the timeline. */ |
| 33 | +function mountGroupSiblings(): HTMLElement[] { |
| 34 | + document.body.innerHTML = ` |
| 35 | + <div id="scene" class="clip" data-start="0" data-duration="2"> |
| 36 | + <div class="group"></div> |
| 37 | + <div class="group"></div> |
| 38 | + <div class="group"></div> |
| 39 | + <div class="group"></div> |
| 40 | + <div class="group"></div> |
| 41 | + </div> |
| 42 | + `; |
| 43 | + return Array.from(document.querySelectorAll<HTMLElement>(".group")); |
| 44 | +} |
| 45 | + |
| 46 | +const BASE_SCRIPT = ` |
| 47 | +const tl = gsap.timeline({ paused: true }); |
| 48 | +tl.to("#scene", { opacity: 1, duration: 0.5 }, 0); |
| 49 | +`.trim(); |
| 50 | + |
| 51 | +const KEYFRAMES = [ |
| 52 | + { percentage: 0, properties: { x: 0 } }, |
| 53 | + { percentage: 100, properties: { x: 40 } }, |
| 54 | +]; |
| 55 | + |
| 56 | +/** Author the selector into a real script and read the tween's targets back. */ |
| 57 | +function roundTripTargets(selector: string): { script: string; targets: string[] } { |
| 58 | + const { script } = addAnimationWithKeyframesToScript(BASE_SCRIPT, selector, 0, 1, KEYFRAMES); |
| 59 | + const added = parseGsapScript(script).animations.find((a) => a.targetSelector === selector); |
| 60 | + if (!added) throw new Error(`written selector did not re-parse: ${selector}`); |
| 61 | + return { script, targets: resolveSelectorElementIds(added.targetSelector, document) }; |
| 62 | +} |
| 63 | + |
| 64 | +describe("writeTargetSelector", () => { |
| 65 | + it("addresses exactly one element when the only identity is a shared class", () => { |
| 66 | + const groups = mountGroupSiblings(); |
| 67 | + const selection = selectionFor(groups[2]); |
| 68 | + |
| 69 | + // Precondition: this is the defect's input — a bare class hitting all five. |
| 70 | + expect(selection.selector).toBe(".group"); |
| 71 | + expect(document.querySelectorAll(".group")).toHaveLength(5); |
| 72 | + |
| 73 | + const written = writeTargetSelector(selection); |
| 74 | + |
| 75 | + expect(written).toBeTruthy(); |
| 76 | + expect(document.querySelectorAll(written!)).toHaveLength(1); |
| 77 | + expect(document.querySelector(written!)).toBe(groups[2]); |
| 78 | + }); |
| 79 | + |
| 80 | + it("keeps a unique #id target", () => { |
| 81 | + document.body.innerHTML = `<div id="box" class="card"></div>`; |
| 82 | + const el = document.querySelector<HTMLElement>("#box")!; |
| 83 | + |
| 84 | + expect(writeTargetSelector(selectionFor(el))).toBe("#box"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("prefers data-hf-id over a generated structural selector", () => { |
| 88 | + document.body.innerHTML = ` |
| 89 | + <div id="scene"><div class="group"></div><div class="group" data-hf-id="hf-42"></div></div> |
| 90 | + `; |
| 91 | + const el = document.querySelectorAll<HTMLElement>(".group")[1]!; |
| 92 | + |
| 93 | + const written = writeTargetSelector(selectionFor(el)); |
| 94 | + |
| 95 | + expect(written).toBe('[data-hf-id="hf-42"]'); |
| 96 | + expect(document.querySelector(written!)).toBe(el); |
| 97 | + }); |
| 98 | + |
| 99 | + it("keeps an already-unique class selector as authored", () => { |
| 100 | + document.body.innerHTML = `<div id="scene"><div class="header"></div></div>`; |
| 101 | + const el = document.querySelector<HTMLElement>(".header")!; |
| 102 | + |
| 103 | + expect(writeTargetSelector(selectionFor(el))).toBe(".header"); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +describe("writeTargetSelector — write/read round trip", () => { |
| 108 | + it("re-parses and attributes the new tween to the one element it targeted", () => { |
| 109 | + const groups = mountGroupSiblings(); |
| 110 | + // Ids let resolveSelectorElementIds name the attributed elements; the |
| 111 | + // SELECTION still has none, so the write path still faces the bare class. |
| 112 | + groups.forEach((el, i) => el.setAttribute("id", `group-${i}`)); |
| 113 | + const selection = { ...selectionFor(groups[2]), id: undefined } as DomEditSelection; |
| 114 | + |
| 115 | + const written = writeTargetSelector(selection); |
| 116 | + const { targets } = roundTripTargets(written!); |
| 117 | + |
| 118 | + expect(targets).toEqual(["group-2"]); |
| 119 | + }); |
| 120 | + |
| 121 | + it("does not widen the add to the element's four class siblings", () => { |
| 122 | + const groups = mountGroupSiblings(); |
| 123 | + groups.forEach((el, i) => el.setAttribute("id", `group-${i}`)); |
| 124 | + const selection = { ...selectionFor(groups[0]), id: undefined } as DomEditSelection; |
| 125 | + |
| 126 | + // The old bare-class write attributed the one add to every sibling — the |
| 127 | + // attribution blow-up behind "one add collapsed the timeline to a single row". |
| 128 | + expect(roundTripTargets(".group").targets).toHaveLength(5); |
| 129 | + |
| 130 | + expect(roundTripTargets(writeTargetSelector(selection)!).targets).toHaveLength(1); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +describe("commitKeyframeAtTimeImpl — new-tween target", () => { |
| 135 | + it("authors a one-element selector when no tween exists at the playhead", async () => { |
| 136 | + const groups = mountGroupSiblings(); |
| 137 | + const selection = selectionFor(groups[3]); |
| 138 | + const commitMutation = vi.fn(async () => undefined); |
| 139 | + |
| 140 | + await commitKeyframeAtTimeImpl(selection, 1, [], { x: 12 }, commitMutation); |
| 141 | + |
| 142 | + const mutation = commitMutation.mock.calls[0]?.[1] as { targetSelector: string }; |
| 143 | + expect(mutation.targetSelector).not.toBe(".group"); |
| 144 | + expect(document.querySelectorAll(mutation.targetSelector)).toHaveLength(1); |
| 145 | + expect(document.querySelector(mutation.targetSelector)).toBe(groups[3]); |
| 146 | + }); |
| 147 | +}); |
0 commit comments