@@ -27,6 +27,7 @@ import {
2727 setArcPathInScript as setArcAcorn ,
2828 updateArcSegmentInScript as updateArcSegmentAcorn ,
2929 removeArcPathFromScript as removeArcAcorn ,
30+ unrollDynamicAnimations as unrollAcorn ,
3031} from "./gsapWriterAcorn.js" ;
3132function acornId ( script : string ) : string {
3233 const parsed = parseGsapScriptAcornForWrite ( script ) as ParsedGsapAcornForWrite ;
@@ -460,3 +461,71 @@ describe("removeArcPathFromScript: acorn output correctness", () => {
460461 expect ( arcShapeOf ( removeArcAcorn ( enabled , id ) ) . arcPath ) . toBeUndefined ( ) ;
461462 } ) ;
462463} ) ;
464+
465+ // ─── unrollDynamicAnimations correctness ──────────────────────────────────────
466+
467+ const UNROLL_LOOP_SCRIPT = `
468+ const tl = gsap.timeline({ paused: true });
469+ const items = ["#a", "#b"];
470+ for (let i = 0; i < items.length; i++) {
471+ tl.to(items[i], { opacity: 1, duration: 1 }, 0);
472+ }
473+ ` ;
474+
475+ const UNROLL_FOREACH_SCRIPT = `
476+ const tl = gsap.timeline({ paused: true });
477+ ["#a", "#b"].forEach(function(sel) {
478+ tl.to(sel, { opacity: 1, duration: 2 }, 1);
479+ });
480+ ` ;
481+
482+ const UNROLL_ELEMENTS = [
483+ {
484+ selector : "#hero" ,
485+ keyframes : [
486+ { percentage : 0 , properties : { opacity : 0 } } ,
487+ { percentage : 100 , properties : { opacity : 1 } } ,
488+ ] ,
489+ } ,
490+ {
491+ selector : "#sub" ,
492+ keyframes : [
493+ { percentage : 0 , properties : { x : 0 } } ,
494+ { percentage : 100 , properties : { x : 200 } } ,
495+ ] ,
496+ } ,
497+ ] ;
498+
499+ function unrollId ( script : string ) : string {
500+ const p = acornId ( script ) ;
501+ return p ;
502+ }
503+
504+ describe ( "unrollDynamicAnimations: acorn output correctness" , ( ) => {
505+ it ( "for-loop: loop replaced with individual tl.to() calls" , ( ) => {
506+ const id = unrollId ( UNROLL_LOOP_SCRIPT ) ;
507+ const out = unrollAcorn ( UNROLL_LOOP_SCRIPT , id , UNROLL_ELEMENTS ) ;
508+ expect ( out ) . not . toBe ( UNROLL_LOOP_SCRIPT ) ;
509+ expect ( out ) . toContain ( 'tl.to("#hero"' ) ;
510+ expect ( out ) . toContain ( 'tl.to("#sub"' ) ;
511+ expect ( out ) . not . toContain ( "for (" ) ;
512+ } ) ;
513+
514+ it ( "forEach: loop replaced with individual tl.to() calls" , ( ) => {
515+ const id = unrollId ( UNROLL_FOREACH_SCRIPT ) ;
516+ const out = unrollAcorn ( UNROLL_FOREACH_SCRIPT , id , UNROLL_ELEMENTS ) ;
517+ expect ( out ) . toContain ( 'tl.to("#hero"' ) ;
518+ expect ( out ) . not . toContain ( "forEach" ) ;
519+ } ) ;
520+
521+ it ( "preserves duration and position from original tween" , ( ) => {
522+ const id = unrollId ( UNROLL_LOOP_SCRIPT ) ;
523+ const out = unrollAcorn ( UNROLL_LOOP_SCRIPT , id , UNROLL_ELEMENTS ) ;
524+ expect ( out ) . toContain ( "duration: 1" ) ;
525+ expect ( out ) . toContain ( "}, 0)" ) ;
526+ } ) ;
527+
528+ it ( "no-op when animationId not found" , ( ) => {
529+ expect ( unrollAcorn ( UNROLL_LOOP_SCRIPT , "nope" , UNROLL_ELEMENTS ) ) . toBe ( UNROLL_LOOP_SCRIPT ) ;
530+ } ) ;
531+ } ) ;
0 commit comments