| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. 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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 15:14:49 -08:00
										 |  |  | import {animate, state, style, transition, trigger} from '@angular/animations'; | 
					
						
							|  |  |  | import {buildTrigger} from '../../src/dsl/animation_trigger'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function makeTrigger(name: string, steps: any) { | 
					
						
							|  |  |  |   const triggerData = trigger(name, steps); | 
					
						
							|  |  |  |   const triggerInstance = buildTrigger(triggerData.name, triggerData.definitions); | 
					
						
							|  |  |  |   return triggerInstance; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('AnimationTrigger', () => { | 
					
						
							|  |  |  |     describe('trigger validation', () => { | 
					
						
							|  |  |  |       it('should group errors together for an animation trigger', () => { | 
					
						
							|  |  |  |         expect(() => { | 
					
						
							| 
									
										
										
										
											2017-02-22 15:14:49 -08:00
										 |  |  |           makeTrigger('myTrigger', [transition('12345', animate(3333))]); | 
					
						
							| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  |         }).toThrowError(/Animation parsing for the myTrigger trigger have failed/); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should throw an error when a transition within a trigger contains an invalid expression', | 
					
						
							|  |  |  |          () => { | 
					
						
							| 
									
										
										
										
											2017-02-22 15:14:49 -08:00
										 |  |  |            expect( | 
					
						
							|  |  |  |                () => { makeTrigger('name', [transition('somethingThatIsWrong', animate(3333))]); }) | 
					
						
							| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  |                .toThrowError( | 
					
						
							|  |  |  |                    /- The provided transition expression "somethingThatIsWrong" is not supported/); | 
					
						
							|  |  |  |          }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should throw an error if an animation alias is used that is not yet supported', () => { | 
					
						
							|  |  |  |         expect(() => { | 
					
						
							| 
									
										
										
										
											2017-02-22 15:14:49 -08:00
										 |  |  |           makeTrigger('name', [transition(':angular', animate(3333))]); | 
					
						
							| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  |         }).toThrowError(/- The transition alias value ":angular" is not supported/); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('trigger usage', () => { | 
					
						
							|  |  |  |       it('should construct a trigger based on the states and transition data', () => { | 
					
						
							| 
									
										
										
										
											2017-02-22 15:14:49 -08:00
										 |  |  |         const result = makeTrigger('name', [ | 
					
						
							| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  |           state('on', style({width: 0})), state('off', style({width: 100})), | 
					
						
							|  |  |  |           transition('on => off', animate(1000)), transition('off => on', animate(1000)) | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(result.states).toEqual({'on': {width: 0}, 'off': {width: 100}}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(result.transitionFactories.length).toEqual(2); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-15 09:24:09 -07:00
										 |  |  |       it('should allow multiple state values to use the same styles', () => { | 
					
						
							|  |  |  |         const result = makeTrigger('name', [ | 
					
						
							|  |  |  |           state('on, off', style({width: 50})), transition('on => off', animate(1000)), | 
					
						
							|  |  |  |           transition('off => on', animate(1000)) | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(result.states).toEqual({'on': {width: 50}, 'off': {width: 50}}); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  |       it('should find the first transition that matches', () => { | 
					
						
							| 
									
										
										
										
											2017-02-22 15:14:49 -08:00
										 |  |  |         const result = makeTrigger( | 
					
						
							| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  |             'name', [transition('a => b', animate(1234)), transition('b => c', animate(5678))]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const trans = result.matchTransition('b', 'c'); | 
					
						
							|  |  |  |         expect(trans.timelines.length).toEqual(1); | 
					
						
							|  |  |  |         const timeline = trans.timelines[0]; | 
					
						
							|  |  |  |         expect(timeline.duration).toEqual(5678); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should find a transition with a `*` value', () => { | 
					
						
							| 
									
										
										
										
											2017-02-22 15:14:49 -08:00
										 |  |  |         const result = makeTrigger('name', [ | 
					
						
							| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  |           transition('* => b', animate(1234)), transition('b => *', animate(5678)), | 
					
						
							|  |  |  |           transition('* => *', animate(9999)) | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let trans = result.matchTransition('b', 'c'); | 
					
						
							|  |  |  |         expect(trans.timelines[0].duration).toEqual(5678); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         trans = result.matchTransition('a', 'b'); | 
					
						
							|  |  |  |         expect(trans.timelines[0].duration).toEqual(1234); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         trans = result.matchTransition('c', 'c'); | 
					
						
							|  |  |  |         expect(trans.timelines[0].duration).toEqual(9999); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should null when no results are found', () => { | 
					
						
							| 
									
										
										
										
											2017-02-22 15:14:49 -08:00
										 |  |  |         const result = makeTrigger('name', [transition('a => b', animate(1111))]); | 
					
						
							| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const trans = result.matchTransition('b', 'a'); | 
					
						
							|  |  |  |         expect(trans).toBeFalsy(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should allow a function to be used as a predicate for the transition', () => { | 
					
						
							|  |  |  |         let returnValue = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 15:14:49 -08:00
										 |  |  |         const result = makeTrigger('name', [transition((from, to) => returnValue, animate(1111))]); | 
					
						
							| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(result.matchTransition('a', 'b')).toBeFalsy(); | 
					
						
							|  |  |  |         expect(result.matchTransition('1', 2)).toBeFalsy(); | 
					
						
							|  |  |  |         expect(result.matchTransition(false, true)).toBeFalsy(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         returnValue = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(result.matchTransition('a', 'b')).toBeTruthy(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should call each transition predicate function until the first one that returns true', | 
					
						
							|  |  |  |          () => { | 
					
						
							|  |  |  |            let count = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            function countAndReturn(value: boolean) { | 
					
						
							|  |  |  |              return (fromState: any, toState: any) => { | 
					
						
							|  |  |  |                count++; | 
					
						
							|  |  |  |                return value; | 
					
						
							|  |  |  |              }; | 
					
						
							|  |  |  |            } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 15:14:49 -08:00
										 |  |  |            const result = makeTrigger('name', [ | 
					
						
							| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  |              transition(countAndReturn(false), animate(1111)), | 
					
						
							|  |  |  |              transition(countAndReturn(false), animate(2222)), | 
					
						
							|  |  |  |              transition(countAndReturn(true), animate(3333)), | 
					
						
							|  |  |  |              transition(countAndReturn(true), animate(3333)) | 
					
						
							|  |  |  |            ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            const trans = result.matchTransition('a', 'b'); | 
					
						
							|  |  |  |            expect(trans.timelines[0].duration).toEqual(3333); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            expect(count).toEqual(3); | 
					
						
							|  |  |  |          }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should support bi-directional transition expressions', () => { | 
					
						
							| 
									
										
										
										
											2017-02-22 15:14:49 -08:00
										 |  |  |         const result = makeTrigger('name', [transition('a <=> b', animate(2222))]); | 
					
						
							| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const t1 = result.matchTransition('a', 'b'); | 
					
						
							|  |  |  |         expect(t1.timelines[0].duration).toEqual(2222); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const t2 = result.matchTransition('b', 'a'); | 
					
						
							|  |  |  |         expect(t2.timelines[0].duration).toEqual(2222); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should support multiple transition statements in one string', () => { | 
					
						
							| 
									
										
										
										
											2017-02-22 15:14:49 -08:00
										 |  |  |         const result = makeTrigger('name', [transition('a => b, b => a, c => *', animate(1234))]); | 
					
						
							| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const t1 = result.matchTransition('a', 'b'); | 
					
						
							|  |  |  |         expect(t1.timelines[0].duration).toEqual(1234); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const t2 = result.matchTransition('b', 'a'); | 
					
						
							|  |  |  |         expect(t2.timelines[0].duration).toEqual(1234); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const t3 = result.matchTransition('c', 'a'); | 
					
						
							|  |  |  |         expect(t3.timelines[0].duration).toEqual(1234); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       describe('aliases', () => { | 
					
						
							|  |  |  |         it('should alias the :enter transition as void => *', () => { | 
					
						
							| 
									
										
										
										
											2017-02-22 15:14:49 -08:00
										 |  |  |           const result = makeTrigger('name', [transition(':enter', animate(3333))]); | 
					
						
							| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           const trans = result.matchTransition('void', 'something'); | 
					
						
							|  |  |  |           expect(trans.timelines[0].duration).toEqual(3333); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should alias the :leave transition as * => void', () => { | 
					
						
							| 
									
										
										
										
											2017-02-22 15:14:49 -08:00
										 |  |  |           const result = makeTrigger('name', [transition(':leave', animate(3333))]); | 
					
						
							| 
									
										
										
										
											2017-01-26 11:16:51 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           const trans = result.matchTransition('something', 'void'); | 
					
						
							|  |  |  |           expect(trans.timelines[0].duration).toEqual(3333); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |