|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import type { GsapAnimation } from "@hyperframes/core/gsap-parser"; |
| 3 | +import { editableAnimationId } from "./motionPathSelection"; |
| 4 | +import { |
| 5 | + commitNode, |
| 6 | + commitAddWaypoint, |
| 7 | + commitAddKeyframe, |
| 8 | + commitRemoveWaypoint, |
| 9 | + commitCreatePath, |
| 10 | +} from "./motionPathCommit"; |
| 11 | + |
| 12 | +const anim = (over: Partial<GsapAnimation>): GsapAnimation => |
| 13 | + ({ |
| 14 | + id: "a1", |
| 15 | + targetSelector: "#el", |
| 16 | + method: "to", |
| 17 | + position: 0, |
| 18 | + properties: {}, |
| 19 | + ...over, |
| 20 | + }) as GsapAnimation; |
| 21 | + |
| 22 | +describe("editableAnimationId", () => { |
| 23 | + it("picks the arc animation for an arc path", () => { |
| 24 | + const arc = anim({ id: "arc1", arcPath: { enabled: true, autoRotate: false, segments: [] } }); |
| 25 | + expect(editableAnimationId([anim({ id: "other" }), arc], "arc")).toBe("arc1"); |
| 26 | + }); |
| 27 | + |
| 28 | + it("picks a position-keyframe animation for a linear path", () => { |
| 29 | + const kf = anim({ |
| 30 | + id: "kf1", |
| 31 | + propertyGroup: "position", |
| 32 | + keyframes: { |
| 33 | + format: "percentage", |
| 34 | + keyframes: [{ percentage: 0, properties: { x: 0, y: 0 } }], |
| 35 | + } as never, |
| 36 | + }); |
| 37 | + expect(editableAnimationId([kf], "linear")).toBe("kf1"); |
| 38 | + }); |
| 39 | + |
| 40 | + it("returns null for dynamic (unresolved) tweens — read-only", () => { |
| 41 | + const dyn = anim({ |
| 42 | + id: "dyn", |
| 43 | + arcPath: { enabled: true, autoRotate: false, segments: [] }, |
| 44 | + hasUnresolvedKeyframes: true, |
| 45 | + }); |
| 46 | + expect(editableAnimationId([dyn], "arc")).toBeNull(); |
| 47 | + }); |
| 48 | + |
| 49 | + it("returns null for non-literal (helper) provenance — read-only", () => { |
| 50 | + const helper = anim({ |
| 51 | + id: "h", |
| 52 | + arcPath: { enabled: true, autoRotate: false, segments: [] }, |
| 53 | + provenance: { kind: "helper" } as never, |
| 54 | + }); |
| 55 | + expect(editableAnimationId([helper], "arc")).toBeNull(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("returns null when nothing matches", () => { |
| 59 | + expect(editableAnimationId([anim({ id: "x" })], "linear")).toBeNull(); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe("commitNode", () => { |
| 64 | + it("routes a keyframe node to update-keyframe by percentage", async () => { |
| 65 | + const commit = vi.fn().mockResolvedValue(undefined); |
| 66 | + await commitNode({ type: "keyframe", pct: 50 }, 120, 30, "a1", commit); |
| 67 | + expect(commit).toHaveBeenCalledWith( |
| 68 | + { type: "update-keyframe", animationId: "a1", percentage: 50, properties: { x: 120, y: 30 } }, |
| 69 | + expect.objectContaining({ softReload: true }), |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it("routes a waypoint node to update-motion-path-point by index", async () => { |
| 74 | + const commit = vi.fn().mockResolvedValue(undefined); |
| 75 | + await commitNode({ type: "waypoint", index: 2 }, 80, 40, "a1", commit); |
| 76 | + expect(commit).toHaveBeenCalledWith( |
| 77 | + { type: "update-motion-path-point", animationId: "a1", pointIndex: 2, x: 80, y: 40 }, |
| 78 | + expect.objectContaining({ softReload: true }), |
| 79 | + ); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe("commitAddWaypoint / commitRemoveWaypoint", () => { |
| 84 | + it("adds a waypoint at an index with coordinates", async () => { |
| 85 | + const commit = vi.fn().mockResolvedValue(undefined); |
| 86 | + await commitAddWaypoint("a1", 1, 120, -40, commit); |
| 87 | + expect(commit).toHaveBeenCalledWith( |
| 88 | + { type: "add-motion-path-point", animationId: "a1", index: 1, x: 120, y: -40 }, |
| 89 | + expect.objectContaining({ softReload: true }), |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + it("removes a waypoint by index", async () => { |
| 94 | + const commit = vi.fn().mockResolvedValue(undefined); |
| 95 | + await commitRemoveWaypoint("a1", 2, commit); |
| 96 | + expect(commit).toHaveBeenCalledWith( |
| 97 | + { type: "remove-motion-path-point", animationId: "a1", index: 2 }, |
| 98 | + expect.objectContaining({ softReload: true }), |
| 99 | + ); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe("commitAddKeyframe", () => { |
| 104 | + it("inserts an x/y keyframe at a tween-relative percentage", async () => { |
| 105 | + const commit = vi.fn().mockResolvedValue(undefined); |
| 106 | + await commitAddKeyframe("a1", 42.5, 80, -20, commit); |
| 107 | + expect(commit).toHaveBeenCalledWith( |
| 108 | + { type: "add-keyframe", animationId: "a1", percentage: 42.5, properties: { x: 80, y: -20 } }, |
| 109 | + expect.objectContaining({ softReload: true }), |
| 110 | + ); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +describe("commitCreatePath", () => { |
| 115 | + it("authors a new motionPath to a destination at a given time", async () => { |
| 116 | + const commit = vi.fn().mockResolvedValue(undefined); |
| 117 | + await commitCreatePath("#title", 2.0, 300, -120, commit); |
| 118 | + expect(commit).toHaveBeenCalledWith( |
| 119 | + { |
| 120 | + type: "add-motion-path", |
| 121 | + targetSelector: "#title", |
| 122 | + position: 2.0, |
| 123 | + duration: 1.5, |
| 124 | + x: 300, |
| 125 | + y: -120, |
| 126 | + }, |
| 127 | + expect.objectContaining({ softReload: true }), |
| 128 | + ); |
| 129 | + }); |
| 130 | +}); |
0 commit comments