-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
animation_spec.ts
1484 lines (1307 loc) Β· 48.9 KB
/
animation_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
animate,
animation,
AnimationMetadata,
AnimationMetadataType,
AnimationOptions,
AUTO_STYLE,
group,
keyframes,
query,
sequence,
state,
style,
transition,
trigger,
useAnimation,
Ι΅StyleDataMap,
} from '@angular/animations';
import {Animation} from '../../src/dsl/animation';
import {buildAnimationAst} from '../../src/dsl/animation_ast_builder';
import {AnimationTimelineInstruction} from '../../src/dsl/animation_timeline_instruction';
import {ElementInstructionMap} from '../../src/dsl/element_instruction_map';
import {MockAnimationDriver} from '../../testing';
function createDiv() {
return document.createElement('div');
}
describe('Animation', () => {
// these tests are only meant to be run within the DOM (for now)
if (isNode) {
// Jasmine will throw if there are no tests.
it('should pass', () => {});
return;
}
let rootElement: any;
let subElement1: any;
let subElement2: any;
beforeEach(() => {
rootElement = createDiv();
subElement1 = createDiv();
subElement2 = createDiv();
document.body.appendChild(rootElement);
rootElement.appendChild(subElement1);
rootElement.appendChild(subElement2);
});
afterEach(() => {
document.body.removeChild(rootElement);
});
describe('validation', () => {
it('should throw an error if one or more but not all keyframes() styles contain offsets', () => {
const steps = animate(1000, keyframes([style({opacity: 0}), style({opacity: 1, offset: 1})]));
expect(() => {
validateAndThrowAnimationSequence(steps);
}).toThrowError(/Not all style\(\) steps within the declared keyframes\(\) contain offsets/);
});
it('should throw an error if not all offsets are between 0 and 1', () => {
let steps = animate(
1000,
keyframes([style({opacity: 0, offset: -1}), style({opacity: 1, offset: 1})]),
);
expect(() => {
validateAndThrowAnimationSequence(steps);
}).toThrowError(/Please ensure that all keyframe offsets are between 0 and 1/);
steps = animate(
1000,
keyframes([style({opacity: 0, offset: 0}), style({opacity: 1, offset: 1.1})]),
);
expect(() => {
validateAndThrowAnimationSequence(steps);
}).toThrowError(/Please ensure that all keyframe offsets are between 0 and 1/);
});
it('should throw an error if a smaller offset shows up after a bigger one', () => {
let steps = animate(
1000,
keyframes([style({opacity: 0, offset: 1}), style({opacity: 1, offset: 0})]),
);
expect(() => {
validateAndThrowAnimationSequence(steps);
}).toThrowError(/Please ensure that all keyframe offsets are in order/);
});
it('should throw an error if any styles overlap during parallel animations', () => {
const steps = group([
sequence([
// 0 -> 2000ms
style({opacity: 0}),
animate('500ms', style({opacity: 0.25})),
animate('500ms', style({opacity: 0.5})),
animate('500ms', style({opacity: 0.75})),
animate('500ms', style({opacity: 1})),
]),
animate(
'1s 500ms',
keyframes([
// 0 -> 1500ms
style({width: 0}),
style({opacity: 1, width: 1000}),
]),
),
]);
expect(() => {
validateAndThrowAnimationSequence(steps);
}).toThrowError(
/The CSS property "opacity" that exists between the times of "0ms" and "2000ms" is also being animated in a parallel animation between the times of "0ms" and "1500ms"/,
);
});
it('should not throw an error if animations overlap in different query levels within different transitions', () => {
const steps = trigger('myAnimation', [
transition(
'a => b',
group([
query('h1', animate('1s', style({opacity: 0}))),
query('h2', animate('1s', style({opacity: 1}))),
]),
),
transition(
'b => a',
group([
query('h1', animate('1s', style({opacity: 0}))),
query('h2', animate('1s', style({opacity: 1}))),
]),
),
]);
expect(() => validateAndThrowAnimationSequence(steps)).not.toThrow();
});
it('should not allow triggers to be defined with a prefixed `@` symbol', () => {
const steps = trigger('@foo', []);
expect(() => validateAndThrowAnimationSequence(steps)).toThrowError(
/animation triggers cannot be prefixed with an `@` sign \(e\.g\. trigger\('@foo', \[...\]\)\)/,
);
});
it('should throw an error if an animation time is invalid', () => {
const steps = [animate('500xs', style({opacity: 1}))];
expect(() => {
validateAndThrowAnimationSequence(steps);
}).toThrowError(/The provided timing value "500xs" is invalid/);
const steps2 = [animate('500ms 500ms 500ms ease-out', style({opacity: 1}))];
expect(() => {
validateAndThrowAnimationSequence(steps2);
}).toThrowError(/The provided timing value "500ms 500ms 500ms ease-out" is invalid/);
});
it('should throw if negative durations are used', () => {
const steps = [animate(-1000, style({opacity: 1}))];
expect(() => {
validateAndThrowAnimationSequence(steps);
}).toThrowError(/Duration values below 0 are not allowed for this animation step/);
const steps2 = [animate('-1s', style({opacity: 1}))];
expect(() => {
validateAndThrowAnimationSequence(steps2);
}).toThrowError(/Duration values below 0 are not allowed for this animation step/);
});
it('should throw if negative delays are used', () => {
const steps = [animate('1s -500ms', style({opacity: 1}))];
expect(() => {
validateAndThrowAnimationSequence(steps);
}).toThrowError(/Delay values below 0 are not allowed for this animation step/);
const steps2 = [animate('1s -0.5s', style({opacity: 1}))];
expect(() => {
validateAndThrowAnimationSequence(steps2);
}).toThrowError(/Delay values below 0 are not allowed for this animation step/);
});
it('should throw if keyframes() is not used inside of animate()', () => {
const steps = [keyframes([])];
expect(() => {
validateAndThrowAnimationSequence(steps);
}).toThrowError(/keyframes\(\) must be placed inside of a call to animate\(\)/);
const steps2 = [group([keyframes([])])];
expect(() => {
validateAndThrowAnimationSequence(steps2);
}).toThrowError(/keyframes\(\) must be placed inside of a call to animate\(\)/);
});
it('should throw if dynamic style substitutions are used without defaults within state() definitions', () => {
const steps = [
state(
'final',
style({
'width': '{{ one }}px',
'borderRadius': '{{ two }}px {{ three }}px',
}),
),
];
expect(() => {
validateAndThrowAnimationSequence(steps);
}).toThrowError(
/state\("final", ...\) must define default values for all the following style substitutions: one, two, three/,
);
const steps2 = [
state(
'panfinal',
style({
'color': '{{ greyColor }}',
'borderColor': '1px solid {{ greyColor }}',
'backgroundColor': '{{ redColor }}',
}),
{params: {redColor: 'maroon'}},
),
];
expect(() => {
validateAndThrowAnimationSequence(steps2);
}).toThrowError(
/state\("panfinal", ...\) must define default values for all the following style substitutions: greyColor/,
);
});
it('should provide a warning if an invalid CSS property is used in the animation', () => {
const steps = [animate(1000, style({abc: '500px'}))];
expect(getValidationWarningsForAnimationSequence(steps)).toEqual([
'The following provided properties are not recognized: abc',
]);
});
it('should provide a warning if multiple invalid CSS properties are used in the animation', () => {
const steps = [
state(
'state',
style({
'123': '100px',
}),
),
style({abc: '200px'}),
animate(1000, style({xyz: '300px'})),
];
expect(getValidationWarningsForAnimationSequence(steps)).toEqual([
'The following provided properties are not recognized: 123, abc, xyz',
]);
});
it('should allow a vendor-prefixed property to be used in an animation sequence without throwing an error', () => {
const steps = [
style({webkitTransform: 'translateX(0px)'}),
animate(1000, style({webkitTransform: 'translateX(100px)'})),
];
expect(() => validateAndThrowAnimationSequence(steps)).not.toThrow();
});
it('should allow for old CSS properties (like transform) to be auto-prefixed by webkit', () => {
const steps = [
style({transform: 'translateX(-100px)'}),
animate(1000, style({transform: 'translateX(500px)'})),
];
expect(() => validateAndThrowAnimationSequence(steps)).not.toThrow();
});
});
describe('keyframe building', () => {
describe('style() / animate()', () => {
it('should produce a balanced series of keyframes given a sequence of animate steps', () => {
const steps = [
style({width: 0}),
animate(1000, style({height: 50})),
animate(1000, style({width: 100})),
animate(1000, style({height: 150})),
animate(1000, style({width: 200})),
];
const players = invokeAnimationSequence(rootElement, steps);
expect(players[0].keyframes).toEqual([
new Map<string, string | number>([
['height', AUTO_STYLE],
['width', 0],
['offset', 0],
]),
new Map<string, string | number>([
['height', 50],
['width', 0],
['offset', 0.25],
]),
new Map<string, string | number>([
['height', 50],
['width', 100],
['offset', 0.5],
]),
new Map<string, string | number>([
['height', 150],
['width', 100],
['offset', 0.75],
]),
new Map<string, string | number>([
['height', 150],
['width', 200],
['offset', 1],
]),
]);
});
it('should fill in missing starting steps when a starting `style()` value is not used', () => {
const steps = [animate(1000, style({width: 999}))];
const players = invokeAnimationSequence(rootElement, steps);
expect(players[0].keyframes).toEqual([
new Map<string, string | number>([
['width', AUTO_STYLE],
['offset', 0],
]),
new Map<string, string | number>([
['width', 999],
['offset', 1],
]),
]);
});
it('should merge successive style() calls together before an animate() call', () => {
const steps = [
style({width: 0}),
style({height: 0}),
style({width: 200}),
style({opacity: 0}),
animate(1000, style({width: 100, height: 400, opacity: 1})),
];
const players = invokeAnimationSequence(rootElement, steps);
expect(players[0].keyframes).toEqual([
new Map<string, string | number>([
['width', 200],
['height', 0],
['opacity', 0],
['offset', 0],
]),
new Map<string, string | number>([
['width', 100],
['height', 400],
['opacity', 1],
['offset', 1],
]),
]);
});
it('should not merge in successive style() calls to the previous animate() keyframe', () => {
const steps = [
style({opacity: 0}),
animate(1000, style({opacity: 0.5})),
style({opacity: 0.6}),
animate(1000, style({opacity: 1})),
];
const players = invokeAnimationSequence(rootElement, steps);
const keyframes = humanizeOffsets(players[0].keyframes, 4);
expect(keyframes).toEqual([
new Map<string, string | number>([
['opacity', 0],
['offset', 0],
]),
new Map<string, string | number>([
['opacity', 0.5],
['offset', 0.4998],
]),
new Map<string, string | number>([
['opacity', 0.6],
['offset', 0.5002],
]),
new Map<string, string | number>([
['opacity', 1],
['offset', 1],
]),
]);
});
it('should support an easing value that uses cubic-bezier(...)', () => {
const steps = [
style({opacity: 0}),
animate('1s cubic-bezier(.29, .55 ,.53 ,1.53)', style({opacity: 1})),
];
const player = invokeAnimationSequence(rootElement, steps)[0];
const firstKeyframe = player.keyframes[0];
const firstKeyframeEasing = firstKeyframe.get('easing') as string;
expect(firstKeyframeEasing.replace(/\s+/g, '')).toEqual('cubic-bezier(.29,.55,.53,1.53)');
});
});
describe('sequence()', () => {
it('should not produce extra timelines when multiple sequences are used within each other', () => {
const steps = [
style({width: 0}),
animate(1000, style({width: 100})),
sequence([
animate(1000, style({width: 200})),
sequence([animate(1000, style({width: 300}))]),
]),
animate(1000, style({width: 400})),
sequence([animate(1000, style({width: 500}))]),
];
const players = invokeAnimationSequence(rootElement, steps);
expect(players.length).toEqual(1);
const player = players[0];
expect(player.keyframes).toEqual([
new Map<string, string | number>([
['width', 0],
['offset', 0],
]),
new Map<string, string | number>([
['width', 100],
['offset', 0.2],
]),
new Map<string, string | number>([
['width', 200],
['offset', 0.4],
]),
new Map<string, string | number>([
['width', 300],
['offset', 0.6],
]),
new Map<string, string | number>([
['width', 400],
['offset', 0.8],
]),
new Map<string, string | number>([
['width', 500],
['offset', 1],
]),
]);
});
it('should create a new timeline after a sequence if group() or keyframe() commands are used within', () => {
const steps = [
style({width: 100, height: 100}),
animate(1000, style({width: 150, height: 150})),
sequence([
group([animate(1000, style({height: 200}))]),
animate(1000, keyframes([style({width: 180}), style({width: 200})])),
]),
animate(1000, style({width: 500, height: 500})),
];
const players = invokeAnimationSequence(rootElement, steps);
expect(players.length).toEqual(4);
const finalPlayer = players[players.length - 1];
expect(finalPlayer.keyframes).toEqual([
new Map<string, string | number>([
['width', 200],
['height', 200],
['offset', 0],
]),
new Map<string, string | number>([
['width', 500],
['height', 500],
['offset', 1],
]),
]);
});
it('should push the start of a sequence if a delay option is provided', () => {
const steps = [
style({width: '0px'}),
animate(1000, style({width: '100px'})),
sequence([animate(1000, style({width: '200px'}))], {delay: 500}),
];
const players = invokeAnimationSequence(rootElement, steps);
const finalPlayer = players[players.length - 1];
expect(finalPlayer.keyframes).toEqual([
new Map<string, string | number>([
['width', '100px'],
['offset', 0],
]),
new Map<string, string | number>([
['width', '200px'],
['offset', 1],
]),
]);
expect(finalPlayer.delay).toEqual(1500);
});
it('should allow a float-based delay value to be used', () => {
let steps: any[] = [animate('.75s 0.75s', style({width: '300px'}))];
let players = invokeAnimationSequence(rootElement, steps);
expect(players.length).toEqual(1);
let p1 = players.pop()!;
expect(p1.duration).toEqual(1500);
expect(p1.keyframes).toEqual([
new Map<string, string | number>([
['width', '*'],
['offset', 0],
]),
new Map<string, string | number>([
['width', '*'],
['offset', 0.5],
]),
new Map<string, string | number>([
['width', '300px'],
['offset', 1],
]),
]);
steps = [style({width: '100px'}), animate('.5s .5s', style({width: '200px'}))];
players = invokeAnimationSequence(rootElement, steps);
expect(players.length).toEqual(1);
p1 = players.pop()!;
expect(p1.duration).toEqual(1000);
expect(p1.keyframes).toEqual([
new Map<string, string | number>([
['width', '100px'],
['offset', 0],
]),
new Map<string, string | number>([
['width', '100px'],
['offset', 0.5],
]),
new Map<string, string | number>([
['width', '200px'],
['offset', 1],
]),
]);
});
});
describe('substitutions', () => {
it('should allow params to be substituted even if they are not defaulted in a reusable animation', () => {
const myAnimation = animation([
style({left: '{{ start }}'}),
animate(1000, style({left: '{{ end }}'})),
]);
const steps = [useAnimation(myAnimation, {params: {start: '0px', end: '100px'}})];
const players = invokeAnimationSequence(rootElement, steps, {});
expect(players.length).toEqual(1);
const player = players[0];
expect(player.keyframes).toEqual([
new Map<string, string | number>([
['left', '0px'],
['offset', 0],
]),
new Map<string, string | number>([
['left', '100px'],
['offset', 1],
]),
]);
});
it('should substitute in timing values', () => {
function makeAnimation(exp: string, options: {[key: string]: any}) {
const steps = [style({opacity: 0}), animate(exp, style({opacity: 1}))];
return invokeAnimationSequence(rootElement, steps, options);
}
let players = makeAnimation('{{ duration }}', buildParams({duration: '1234ms'}));
expect(players[0].duration).toEqual(1234);
players = makeAnimation('{{ duration }}', buildParams({duration: '9s 2s'}));
expect(players[0].duration).toEqual(11000);
players = makeAnimation('{{ duration }} 1s', buildParams({duration: '1.5s'}));
expect(players[0].duration).toEqual(2500);
players = makeAnimation(
'{{ duration }} {{ delay }}',
buildParams({duration: '1s', delay: '2s'}),
);
expect(players[0].duration).toEqual(3000);
});
it('should allow multiple substitutions to occur within the same style value', () => {
const steps = [
style({borderRadius: '100px 100px'}),
animate(1000, style({borderRadius: '{{ one }}px {{ two }}'})),
];
const players = invokeAnimationSequence(
rootElement,
steps,
buildParams({one: '200', two: '400px'}),
);
expect(players[0].keyframes).toEqual([
new Map<string, string | number>([
['offset', 0],
['borderRadius', '100px 100px'],
]),
new Map<string, string | number>([
['offset', 1],
['borderRadius', '200px 400px'],
]),
]);
});
it('should substitute in values that are defined as parameters for inner areas of a sequence', () => {
const steps = sequence(
[
sequence(
[
sequence(
[style({height: '{{ x0 }}px'}), animate(1000, style({height: '{{ x2 }}px'}))],
buildParams({x2: '{{ x1 }}3'}),
),
],
buildParams({x1: '{{ x0 }}2'}),
),
],
buildParams({x0: '1'}),
);
const players = invokeAnimationSequence(rootElement, steps);
expect(players.length).toEqual(1);
const [player] = players;
expect(player.keyframes).toEqual([
new Map<string, string | number>([
['offset', 0],
['height', '1px'],
]),
new Map<string, string | number>([
['offset', 1],
['height', '123px'],
]),
]);
});
it('should substitute in values that are defined as parameters for reusable animations', () => {
const anim = animation([
style({height: '{{ start }}'}),
animate(1000, style({height: '{{ end }}'})),
]);
const steps = sequence(
[
sequence(
[useAnimation(anim, buildParams({start: '{{ a }}', end: '{{ b }}'}))],
buildParams({a: '100px', b: '200px'}),
),
],
buildParams({a: '0px'}),
);
const players = invokeAnimationSequence(rootElement, steps);
expect(players.length).toEqual(1);
const [player] = players;
expect(player.keyframes).toEqual([
new Map<string, string | number>([
['offset', 0],
['height', '100px'],
]),
new Map<string, string | number>([
['offset', 1],
['height', '200px'],
]),
]);
});
it('should throw an error when an input variable is not provided when invoked and is not a default value', () => {
expect(() =>
invokeAnimationSequence(rootElement, [style({color: '{{ color }}'})]),
).toThrowError(/Please provide a value for the animation param color/);
expect(() =>
invokeAnimationSequence(
rootElement,
[style({color: '{{ start }}'}), animate('{{ time }}', style({color: '{{ end }}'}))],
buildParams({start: 'blue', end: 'red'}),
),
).toThrowError(/Please provide a value for the animation param time/);
expect(() =>
invokeAnimationSequence(
rootElement,
[style({color: '{{ color }}'})],
buildParams({color: undefined}),
),
).toThrowError(/Please provide a value for the animation param color/);
expect(() =>
invokeAnimationSequence(
rootElement,
[style({color: '{{ color }}'})],
buildParams({color: null}),
),
).toThrowError(/Please provide a value for the animation param color/);
});
});
describe('keyframes()', () => {
it('should produce a sub timeline when `keyframes()` is used within a sequence', () => {
const steps = [
animate(1000, style({opacity: 0.5})),
animate(1000, style({opacity: 1})),
animate(1000, keyframes([style({height: 0}), style({height: 100}), style({height: 50})])),
animate(1000, style({height: 0, opacity: 0})),
];
const players = invokeAnimationSequence(rootElement, steps);
expect(players.length).toEqual(3);
const player0 = players[0];
expect(player0.delay).toEqual(0);
expect(player0.keyframes).toEqual([
new Map<string, string | number>([
['opacity', AUTO_STYLE],
['offset', 0],
]),
new Map<string, string | number>([
['opacity', 0.5],
['offset', 0.5],
]),
new Map<string, string | number>([
['opacity', 1],
['offset', 1],
]),
]);
const subPlayer = players[1];
expect(subPlayer.delay).toEqual(2000);
expect(subPlayer.keyframes).toEqual([
new Map<string, string | number>([
['height', 0],
['offset', 0],
]),
new Map<string, string | number>([
['height', 100],
['offset', 0.5],
]),
new Map<string, string | number>([
['height', 50],
['offset', 1],
]),
]);
const player1 = players[2];
expect(player1.delay).toEqual(3000);
expect(player1.keyframes).toEqual([
new Map<string, string | number>([
['opacity', 1],
['height', 50],
['offset', 0],
]),
new Map<string, string | number>([
['opacity', 0],
['height', 0],
['offset', 1],
]),
]);
});
it('should propagate inner keyframe style data to the parent timeline if used afterwards', () => {
const steps = [
style({opacity: 0}),
animate(1000, style({opacity: 0.5})),
animate(1000, style({opacity: 1})),
animate(1000, keyframes([style({color: 'red'}), style({color: 'blue'})])),
animate(1000, style({color: 'green', opacity: 0})),
];
const players = invokeAnimationSequence(rootElement, steps);
const finalPlayer = players[players.length - 1];
expect(finalPlayer.keyframes).toEqual([
new Map<string, string | number>([
['opacity', 1],
['color', 'blue'],
['offset', 0],
]),
new Map<string, string | number>([
['opacity', 0],
['color', 'green'],
['offset', 1],
]),
]);
});
it('should feed in starting data into inner keyframes if used in an style step beforehand', () => {
const steps = [
animate(1000, style({opacity: 0.5})),
animate(
1000,
keyframes([style({opacity: 0.8, offset: 0.5}), style({opacity: 1, offset: 1})]),
),
];
const players = invokeAnimationSequence(rootElement, steps);
expect(players.length).toEqual(2);
const topPlayer = players[0];
expect(topPlayer.keyframes).toEqual([
new Map<string, string | number>([
['opacity', AUTO_STYLE],
['offset', 0],
]),
new Map<string, string | number>([
['opacity', 0.5],
['offset', 1],
]),
]);
const subPlayer = players[1];
expect(subPlayer.keyframes).toEqual([
new Map<string, string | number>([
['opacity', 0.5],
['offset', 0],
]),
new Map<string, string | number>([
['opacity', 0.8],
['offset', 0.5],
]),
new Map<string, string | number>([
['opacity', 1],
['offset', 1],
]),
]);
});
it('should set the easing value as an easing value for the entire timeline', () => {
const steps = [
style({opacity: 0}),
animate(1000, style({opacity: 0.5})),
animate(
'1s ease-out',
keyframes([style({opacity: 0.8, offset: 0.5}), style({opacity: 1, offset: 1})]),
),
];
const player = invokeAnimationSequence(rootElement, steps)[1];
expect(player.easing).toEqual('ease-out');
});
it('should combine the starting time + the given delay as the delay value for the animated keyframes', () => {
const steps = [
style({opacity: 0}),
animate(500, style({opacity: 0.5})),
animate(
'1s 2s ease-out',
keyframes([style({opacity: 0.8, offset: 0.5}), style({opacity: 1, offset: 1})]),
),
];
const player = invokeAnimationSequence(rootElement, steps)[1];
expect(player.delay).toEqual(2500);
});
it('should not leak in additional styles used later on after keyframe styles have already been declared', () => {
const steps = [
animate(1000, style({height: '50px'})),
animate(
2000,
keyframes([
style({left: '0', top: '0', offset: 0}),
style({left: '40%', top: '50%', offset: 0.33}),
style({left: '60%', top: '80%', offset: 0.66}),
style({left: 'calc(100% - 100px)', top: '100%', offset: 1}),
]),
),
group([animate('2s', style({width: '200px'}))]),
animate('2s', style({height: '300px'})),
group([animate('2s', style({height: '500px', width: '500px'}))]),
];
const players = invokeAnimationSequence(rootElement, steps);
expect(players.length).toEqual(5);
const firstPlayerKeyframes = players[0].keyframes;
expect(firstPlayerKeyframes[0].get('width')).toBeFalsy();
expect(firstPlayerKeyframes[1].get('width')).toBeFalsy();
expect(firstPlayerKeyframes[0].get('height')).toEqual(AUTO_STYLE);
expect(firstPlayerKeyframes[1].get('height')).toEqual('50px');
const keyframePlayerKeyframes = players[1].keyframes;
expect(keyframePlayerKeyframes[0].get('width')).toBeFalsy();
expect(keyframePlayerKeyframes[0].get('height')).toBeFalsy();
const groupPlayerKeyframes = players[2].keyframes;
expect(groupPlayerKeyframes[0].get('width')).toEqual(AUTO_STYLE);
expect(groupPlayerKeyframes[1].get('width')).toEqual('200px');
expect(groupPlayerKeyframes[0].get('height')).toBeFalsy();
expect(groupPlayerKeyframes[1].get('height')).toBeFalsy();
const secondToFinalAnimatePlayerKeyframes = players[3].keyframes;
expect(secondToFinalAnimatePlayerKeyframes[0].get('width')).toBeFalsy();
expect(secondToFinalAnimatePlayerKeyframes[1].get('width')).toBeFalsy();
expect(secondToFinalAnimatePlayerKeyframes[0].get('height')).toEqual('50px');
expect(secondToFinalAnimatePlayerKeyframes[1].get('height')).toEqual('300px');
const finalAnimatePlayerKeyframes = players[4].keyframes;
expect(finalAnimatePlayerKeyframes[0].get('width')).toEqual('200px');
expect(finalAnimatePlayerKeyframes[1].get('width')).toEqual('500px');
expect(finalAnimatePlayerKeyframes[0].get('height')).toEqual('300px');
expect(finalAnimatePlayerKeyframes[1].get('height')).toEqual('500px');
});
it('should respect offsets if provided directly within the style data', () => {
const steps = animate(
1000,
keyframes([
style({opacity: 0, offset: 0}),
style({opacity: 0.6, offset: 0.6}),
style({opacity: 1, offset: 1}),
]),
);
const players = invokeAnimationSequence(rootElement, steps);
expect(players.length).toEqual(1);
const player = players[0];
expect(player.keyframes).toEqual([
new Map<string, string | number>([
['opacity', 0],
['offset', 0],
]),
new Map<string, string | number>([
['opacity', 0.6],
['offset', 0.6],
]),
new Map<string, string | number>([
['opacity', 1],
['offset', 1],
]),
]);
});
it('should respect offsets if provided directly within the style metadata type', () => {
const steps = animate(
1000,
keyframes([
{type: AnimationMetadataType.Style, offset: 0, styles: {opacity: 0}},
{type: AnimationMetadataType.Style, offset: 0.4, styles: {opacity: 0.4}},
{type: AnimationMetadataType.Style, offset: 1, styles: {opacity: 1}},
]),
);
const players = invokeAnimationSequence(rootElement, steps);
expect(players.length).toEqual(1);
const player = players[0];
expect(player.keyframes).toEqual([
new Map<string, string | number>([
['opacity', 0],
['offset', 0],
]),
new Map<string, string | number>([
['opacity', 0.4],
['offset', 0.4],
]),
new Map<string, string | number>([
['opacity', 1],
['offset', 1],
]),
]);
});
});
describe('group()', () => {
it('should properly tally style data within a group() for use in a follow-up animate() step', () => {
const steps = [
style({width: 0, height: 0}),
animate(1000, style({width: 20, height: 50})),
group([animate('1s 1s', style({width: 200})), animate('1s', style({height: 500}))]),
animate(1000, style({width: 1000, height: 1000})),
];