|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be |
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | | -import {animate, animateChild, AnimationEvent, AnimationMetadata, AnimationOptions, AUTO_STYLE, group, keyframes, query, state, style, transition, trigger, ɵPRE_STYLE as PRE_STYLE} from '@angular/animations'; |
| 8 | +import {animate, animateChild, animation, AnimationEvent, AnimationMetadata, AnimationOptions, AUTO_STYLE, group, keyframes, query, state, style, transition, trigger, useAnimation, ɵPRE_STYLE as PRE_STYLE} from '@angular/animations'; |
9 | 9 | import {AnimationDriver, ɵAnimationEngine, ɵNoopAnimationDriver as NoopAnimationDriver} from '@angular/animations/browser'; |
10 | 10 | import {MockAnimationDriver, MockAnimationPlayer} from '@angular/animations/browser/testing'; |
11 | 11 | import {ChangeDetectorRef, Component, HostBinding, HostListener, Inject, RendererFactory2, ViewChild} from '@angular/core'; |
@@ -3830,6 +3830,143 @@ describe('animation tests', function() { |
3830 | 3830 | /only state\(\) and transition\(\) definitions can sit inside of a trigger\(\)/); |
3831 | 3831 | }); |
3832 | 3832 |
|
| 3833 | + describe('animation and useAnimation functions', () => { |
| 3834 | + it('should apply the delay specified in the animation', () => { |
| 3835 | + const animationMetaData = animation( |
| 3836 | + [ |
| 3837 | + style({color: 'red'}), |
| 3838 | + animate(1000, style({color: 'green'})), |
| 3839 | + ], |
| 3840 | + {delay: 3000}); |
| 3841 | + |
| 3842 | + @Component({ |
| 3843 | + selector: 'cmp', |
| 3844 | + template: ` |
| 3845 | + <div @anim *ngIf="exp"> |
| 3846 | + </div> |
| 3847 | + `, |
| 3848 | + animations: [ |
| 3849 | + trigger('anim', [transition( |
| 3850 | + ':enter', |
| 3851 | + useAnimation(animationMetaData), |
| 3852 | + )]), |
| 3853 | + ] |
| 3854 | + }) |
| 3855 | + class Cmp { |
| 3856 | + exp: boolean = false; |
| 3857 | + } |
| 3858 | + |
| 3859 | + TestBed.configureTestingModule({declarations: [Cmp]}); |
| 3860 | + |
| 3861 | + const engine = TestBed.inject(ɵAnimationEngine); |
| 3862 | + const fixture = TestBed.createComponent(Cmp); |
| 3863 | + const cmp = fixture.componentInstance; |
| 3864 | + cmp.exp = true; |
| 3865 | + |
| 3866 | + fixture.detectChanges(); |
| 3867 | + engine.flush(); |
| 3868 | + |
| 3869 | + const players = getLog(); |
| 3870 | + expect(players.length).toEqual(1); |
| 3871 | + const [player] = players; |
| 3872 | + expect(player.delay).toEqual(3000); |
| 3873 | + expect(player.duration).toEqual(1000); |
| 3874 | + expect(player.keyframes).toEqual([ |
| 3875 | + new Map<string, string|number>([['color', 'red'], ['offset', 0]]), |
| 3876 | + new Map<string, string|number>([['color', 'green'], ['offset', 1]]), |
| 3877 | + ]); |
| 3878 | + }); |
| 3879 | + |
| 3880 | + it('should apply the delay specified in the animation using params', () => { |
| 3881 | + const animationMetaData = animation( |
| 3882 | + [ |
| 3883 | + style({color: 'red'}), |
| 3884 | + animate(500, style({color: 'green'})), |
| 3885 | + ], |
| 3886 | + {delay: '{{animationDelay}}ms', params: {animationDelay: 5500}}); |
| 3887 | + |
| 3888 | + @Component({ |
| 3889 | + selector: 'cmp', |
| 3890 | + template: ` |
| 3891 | + <div @anim *ngIf="exp"> |
| 3892 | + </div> |
| 3893 | + `, |
| 3894 | + animations: [ |
| 3895 | + trigger('anim', [transition( |
| 3896 | + ':enter', |
| 3897 | + useAnimation(animationMetaData), |
| 3898 | + )]), |
| 3899 | + ] |
| 3900 | + }) |
| 3901 | + class Cmp { |
| 3902 | + exp: boolean = false; |
| 3903 | + } |
| 3904 | + |
| 3905 | + TestBed.configureTestingModule({declarations: [Cmp]}); |
| 3906 | + |
| 3907 | + const engine = TestBed.inject(ɵAnimationEngine); |
| 3908 | + const fixture = TestBed.createComponent(Cmp); |
| 3909 | + const cmp = fixture.componentInstance; |
| 3910 | + cmp.exp = true; |
| 3911 | + |
| 3912 | + fixture.detectChanges(); |
| 3913 | + engine.flush(); |
| 3914 | + |
| 3915 | + const players = getLog(); |
| 3916 | + expect(players.length).toEqual(1); |
| 3917 | + const [player] = players; |
| 3918 | + expect(player.delay).toEqual(5500); |
| 3919 | + expect(player.duration).toEqual(500); |
| 3920 | + expect(player.keyframes).toEqual([ |
| 3921 | + new Map<string, string|number>([['color', 'red'], ['offset', 0]]), |
| 3922 | + new Map<string, string|number>([['color', 'green'], ['offset', 1]]), |
| 3923 | + ]); |
| 3924 | + }); |
| 3925 | + |
| 3926 | + it('should combine the delay specified in the animation with that of the caller', () => { |
| 3927 | + const animationMetaData = animation( |
| 3928 | + [ |
| 3929 | + style({color: 'red'}), |
| 3930 | + animate(500, style({color: 'green'})), |
| 3931 | + ], |
| 3932 | + {delay: 2000}); |
| 3933 | + |
| 3934 | + @Component({ |
| 3935 | + selector: 'cmp', |
| 3936 | + template: ` |
| 3937 | + <div @anim *ngIf="exp"> |
| 3938 | + </div> |
| 3939 | + `, |
| 3940 | + animations: [ |
| 3941 | + trigger('anim', [transition(':enter', useAnimation(animationMetaData), {delay: 750})]), |
| 3942 | + ] |
| 3943 | + }) |
| 3944 | + class Cmp { |
| 3945 | + exp: boolean = false; |
| 3946 | + } |
| 3947 | + |
| 3948 | + TestBed.configureTestingModule({declarations: [Cmp]}); |
| 3949 | + |
| 3950 | + const engine = TestBed.inject(ɵAnimationEngine); |
| 3951 | + const fixture = TestBed.createComponent(Cmp); |
| 3952 | + const cmp = fixture.componentInstance; |
| 3953 | + cmp.exp = true; |
| 3954 | + |
| 3955 | + fixture.detectChanges(); |
| 3956 | + engine.flush(); |
| 3957 | + |
| 3958 | + const players = getLog(); |
| 3959 | + expect(players.length).toEqual(1); |
| 3960 | + const [player] = players; |
| 3961 | + expect(player.delay).toEqual(2750); |
| 3962 | + expect(player.duration).toEqual(500); |
| 3963 | + expect(player.keyframes).toEqual([ |
| 3964 | + new Map<string, string|number>([['color', 'red'], ['offset', 0]]), |
| 3965 | + new Map<string, string|number>([['color', 'green'], ['offset', 1]]), |
| 3966 | + ]); |
| 3967 | + }); |
| 3968 | + }); |
| 3969 | + |
3833 | 3970 | it('should combine multiple errors together into one exception when an animation fails to be built', |
3834 | 3971 | () => { |
3835 | 3972 | @Component({ |
|
0 commit comments