22
33import React , { act } from "react" ;
44import { createRoot } from "react-dom/client" ;
5+ import type { GsapAnimation } from "@hyperframes/core/gsap-parser" ;
56import { afterEach , describe , expect , it , vi } from "vitest" ;
67import { AnimationCard } from "./AnimationCard" ;
7- import type { GsapAnimation } from "@hyperframes/core/gsap-parser" ;
88import { EASE_PRESETS } from "./easePresetLibrary" ;
9+ import type { AnimationKeyframeTarget } from "../../hooks/gsapTweenSynth" ;
910
1011const trackStudioSegmentEaseEdit = vi . hoisted ( ( ) => vi . fn ( ) ) ;
1112vi . mock ( "../../telemetry/events" , ( ) => ( { trackStudioSegmentEaseEdit } ) ) ;
1213
1314( globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT : boolean } ) . IS_REACT_ACT_ENVIRONMENT = true ;
1415
16+ const ANIMATION : GsapAnimation = {
17+ id : "position-tween" ,
18+ targetSelector : "#clip-1" ,
19+ method : "to" ,
20+ position : 0 ,
21+ duration : 2 ,
22+ ease : "power1.out" ,
23+ properties : { x : 200 } ,
24+ keyframes : {
25+ format : "percentage" ,
26+ keyframes : [
27+ { percentage : 0 , properties : { x : 0 } } ,
28+ { percentage : 50 , properties : { x : 100 } } ,
29+ { percentage : 100 , properties : { x : 200 } } ,
30+ ] ,
31+ } ,
32+ } ;
33+
34+ const FLAT_ANIMATION : GsapAnimation = {
35+ ...ANIMATION ,
36+ id : "flat-position-tween" ,
37+ keyframes : undefined ,
38+ } ;
39+
1540afterEach ( ( ) => {
1641 document . body . innerHTML = "" ;
1742 trackStudioSegmentEaseEdit . mockClear ( ) ;
1843} ) ;
1944
20- function baseAnimation ( overrides : Partial < GsapAnimation > = { } ) : GsapAnimation {
21- return {
22- id : "anim-1" ,
23- method : "to" ,
24- position : 0.8 ,
25- duration : 1.2 ,
26- ease : "power2.out" ,
27- properties : { opacity : 1 } ,
28- ...overrides ,
29- } as GsapAnimation ;
45+ function renderFocusCard (
46+ focusedSegment : {
47+ tweenPercentage : number ;
48+ collidingAnimationTargets ?: AnimationKeyframeTarget [ ] ;
49+ } | null ,
50+ onEaseCommit = vi . fn ( ) ,
51+ defaultExpanded = false ,
52+ animation = ANIMATION ,
53+ onUpdateMeta = vi . fn ( ) ,
54+ onUpdateSegmentEase = vi . fn ( ) ,
55+ ) {
56+ const host = document . createElement ( "div" ) ;
57+ document . body . append ( host ) ;
58+ const root = createRoot ( host ) ;
59+ const render = ( nextFocusedSegment : { tweenPercentage : number } | null ) => {
60+ act ( ( ) => {
61+ root . render (
62+ < AnimationCard
63+ animation = { animation }
64+ defaultExpanded = { defaultExpanded }
65+ focusedSegment = { nextFocusedSegment }
66+ onFocusSegmentConsumed = { vi . fn ( ) }
67+ onUpdateProperty = { vi . fn ( ) }
68+ onUpdateMeta = { onUpdateMeta }
69+ onDeleteAnimation = { vi . fn ( ) }
70+ onAddProperty = { vi . fn ( ) }
71+ onRemoveProperty = { vi . fn ( ) }
72+ onUpdateKeyframeEase = { onEaseCommit }
73+ onUpdateSegmentEase = { onUpdateSegmentEase }
74+ /> ,
75+ ) ;
76+ } ) ;
77+ } ;
78+ render ( focusedSegment ) ;
79+ return { host, root, render } ;
3080}
3181
32- const noop = ( ) => { } ;
82+ function findButton ( host : HTMLElement , text : string ) : HTMLButtonElement | undefined {
83+ return Array . from ( host . querySelectorAll ( "button" ) ) . find ( ( button ) =>
84+ button . textContent ?. includes ( text ) ,
85+ ) ;
86+ }
3387
3488function selectPreset ( host : HTMLElement , presetId : string ) : string {
3589 const presetConfig = EASE_PRESETS . find ( ( candidate ) => candidate . id === presetId ) ;
@@ -45,6 +99,8 @@ function selectPreset(host: HTMLElement, presetId: string): string {
4599 return presetConfig . ease ;
46100}
47101
102+ const noop = ( ) => { } ;
103+
48104/** Every test mounts the same card; only expansion, flat mode, and the spies differ. */
49105function renderCard ( {
50106 animation = baseAnimation ( ) ,
@@ -82,6 +138,126 @@ function renderCard({
82138 return { host, root } ;
83139}
84140
141+ function restoreScrollIntoView ( descriptor : PropertyDescriptor | undefined ) : void {
142+ if ( descriptor ) Object . defineProperty ( HTMLElement . prototype , "scrollIntoView" , descriptor ) ;
143+ else Reflect . deleteProperty ( HTMLElement . prototype , "scrollIntoView" ) ;
144+ }
145+
146+ describe ( "AnimationCard" , ( ) => {
147+ it ( "scrolls a focused segment into view but not a manually toggled segment" , ( ) => {
148+ const originalScrollIntoView = Object . getOwnPropertyDescriptor (
149+ HTMLElement . prototype ,
150+ "scrollIntoView" ,
151+ ) ;
152+ const scrollIntoView = vi . fn ( ) ;
153+ Object . defineProperty ( HTMLElement . prototype , "scrollIntoView" , {
154+ configurable : true ,
155+ value : scrollIntoView ,
156+ } ) ;
157+
158+ const view = renderFocusCard ( { tweenPercentage : 50 } ) ;
159+ try {
160+ expect ( scrollIntoView ) . toHaveBeenCalledOnce ( ) ;
161+ expect ( scrollIntoView ) . toHaveBeenCalledWith ( { block : "nearest" , behavior : "smooth" } ) ;
162+
163+ view . render ( null ) ;
164+ const manualToggle = findButton ( view . host , "50% → 100%" ) ;
165+ expect ( manualToggle ) . toBeDefined ( ) ;
166+ act ( ( ) => manualToggle ?. click ( ) ) ;
167+ expect ( scrollIntoView ) . toHaveBeenCalledOnce ( ) ;
168+ } finally {
169+ act ( ( ) => view . root . unmount ( ) ) ;
170+ restoreScrollIntoView ( originalScrollIntoView ) ;
171+ }
172+ } ) ;
173+
174+ it ( "tracks a committed segment ease alongside the existing update" , ( ) => {
175+ const onEaseCommit = vi . fn ( ) ;
176+ const view = renderFocusCard ( null , onEaseCommit , true ) ;
177+ const segment = findButton ( view . host , "0% → 50%" ) ;
178+ expect ( segment ) . toBeDefined ( ) ;
179+ act ( ( ) => segment ?. click ( ) ) ;
180+ const ease = selectPreset ( view . host , "quad-out" ) ;
181+
182+ expect ( onEaseCommit ) . toHaveBeenCalledWith ( ANIMATION . id , 50 , ease ) ;
183+ expect ( trackStudioSegmentEaseEdit ) . toHaveBeenCalledWith ( { action : "commit" , ease } ) ;
184+ act ( ( ) => view . root . unmount ( ) ) ;
185+ } ) ;
186+
187+ it ( "commits a focused multi-id segment ease through the bulk callback" , ( ) => {
188+ const onUpdateKeyframeEase = vi . fn ( ) ;
189+ const onUpdateSegmentEase = vi . fn ( ) ;
190+ const collidingAnimationTargets = [
191+ { animationId : ANIMATION . id , tweenPercentage : 50 } ,
192+ { animationId : "scale-tween" , tweenPercentage : 75 } ,
193+ { animationId : "opacity-tween" , tweenPercentage : 25 } ,
194+ ] ;
195+ const view = renderFocusCard (
196+ { tweenPercentage : 50 , collidingAnimationTargets } ,
197+ onUpdateKeyframeEase ,
198+ false ,
199+ ANIMATION ,
200+ vi . fn ( ) ,
201+ onUpdateSegmentEase ,
202+ ) ;
203+ const ease = selectPreset ( view . host , "quad-out" ) ;
204+
205+ expect ( onUpdateSegmentEase ) . toHaveBeenCalledExactlyOnceWith ( collidingAnimationTargets , ease ) ;
206+ expect ( onUpdateKeyframeEase ) . not . toHaveBeenCalled ( ) ;
207+ act ( ( ) => view . root . unmount ( ) ) ;
208+ } ) ;
209+
210+ it ( "keeps a focused single-id segment ease on the single callback" , ( ) => {
211+ const onUpdateKeyframeEase = vi . fn ( ) ;
212+ const onUpdateSegmentEase = vi . fn ( ) ;
213+ const view = renderFocusCard (
214+ {
215+ tweenPercentage : 50 ,
216+ collidingAnimationTargets : [ { animationId : ANIMATION . id , tweenPercentage : 50 } ] ,
217+ } ,
218+ onUpdateKeyframeEase ,
219+ false ,
220+ ANIMATION ,
221+ vi . fn ( ) ,
222+ onUpdateSegmentEase ,
223+ ) ;
224+
225+ const ease = selectPreset ( view . host , "quad-out" ) ;
226+
227+ expect ( onUpdateKeyframeEase ) . toHaveBeenCalledExactlyOnceWith ( ANIMATION . id , 50 , ease ) ;
228+ expect ( onUpdateSegmentEase ) . not . toHaveBeenCalled ( ) ;
229+ act ( ( ) => view . root . unmount ( ) ) ;
230+ } ) ;
231+
232+ it ( "commits a focused flat tween segment ease through tween metadata" , ( ) => {
233+ const onUpdateMeta = vi . fn ( ) ;
234+ const onUpdateKeyframeEase = vi . fn ( ) ;
235+ const view = renderFocusCard (
236+ { tweenPercentage : 100 } ,
237+ onUpdateKeyframeEase ,
238+ false ,
239+ FLAT_ANIMATION ,
240+ onUpdateMeta ,
241+ ) ;
242+ const ease = selectPreset ( view . host , "quad-out" ) ;
243+
244+ expect ( onUpdateMeta ) . toHaveBeenCalledExactlyOnceWith ( FLAT_ANIMATION . id , { ease } ) ;
245+ expect ( onUpdateKeyframeEase ) . not . toHaveBeenCalled ( ) ;
246+ act ( ( ) => view . root . unmount ( ) ) ;
247+ } ) ;
248+ } ) ;
249+
250+ function baseAnimation ( overrides : Partial < GsapAnimation > = { } ) : GsapAnimation {
251+ return {
252+ id : "anim-1" ,
253+ method : "to" ,
254+ position : 0.8 ,
255+ duration : 1.2 ,
256+ ease : "power2.out" ,
257+ properties : { opacity : 1 } ,
258+ ...overrides ,
259+ } as GsapAnimation ;
260+ }
85261describe ( "AnimationCard ease editing" , ( ) => {
86262 it ( "commits one preset change to the selected keyframe segment" , ( ) => {
87263 const onUpdateKeyframeEase = vi . fn ( ) ;
0 commit comments