| 
									
										
										
										
											2018-01-17 10:09:05 -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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-27 14:19:58 +02:00
										 |  |  | import {DoCheck, OnChanges, SimpleChange, SimpleChanges} from '../../src/core'; | 
					
						
							| 
									
										
										
										
											2018-04-14 09:18:38 -07:00
										 |  |  | import {DirectiveDef, NgOnChangesFeature, defineDirective} from '../../src/render3/index'; | 
					
						
							| 
									
										
										
										
											2018-01-17 10:09:05 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('define', () => { | 
					
						
							|  |  |  |   describe('component', () => { | 
					
						
							|  |  |  |     describe('NgOnChangesFeature', () => { | 
					
						
							|  |  |  |       it('should patch class', () => { | 
					
						
							|  |  |  |         class MyDirective implements OnChanges, DoCheck { | 
					
						
							| 
									
										
										
										
											2018-05-27 14:19:58 +02:00
										 |  |  |           public log: Array<string|SimpleChange> = []; | 
					
						
							| 
									
										
										
										
											2018-01-17 10:09:05 -08:00
										 |  |  |           public valA: string = 'initValue'; | 
					
						
							|  |  |  |           public set valB(value: string) { this.log.push(value); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           public get valB() { return 'works'; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           ngDoCheck(): void { this.log.push('ngDoCheck'); } | 
					
						
							|  |  |  |           ngOnChanges(changes: SimpleChanges): void { | 
					
						
							|  |  |  |             this.log.push('ngOnChanges'); | 
					
						
							| 
									
										
										
										
											2018-05-27 14:19:58 +02:00
										 |  |  |             this.log.push('valA', changes['valA']); | 
					
						
							|  |  |  |             this.log.push('valB', changes['valB']); | 
					
						
							| 
									
										
										
										
											2018-01-17 10:09:05 -08:00
										 |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           static ngDirectiveDef = defineDirective({ | 
					
						
							| 
									
										
										
										
											2018-01-23 10:57:48 -08:00
										 |  |  |             type: MyDirective, | 
					
						
							| 
									
										
										
										
											2018-03-29 16:41:45 -07:00
										 |  |  |             selectors: [['', 'myDir', '']], | 
					
						
							| 
									
										
										
										
											2018-01-17 10:09:05 -08:00
										 |  |  |             factory: () => new MyDirective(), | 
					
						
							| 
									
										
										
										
											2018-03-12 11:18:50 -07:00
										 |  |  |             features: [NgOnChangesFeature()], | 
					
						
							| 
									
										
										
										
											2018-01-17 10:09:05 -08:00
										 |  |  |             inputs: {valA: 'valA', valB: 'valB'} | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-14 09:18:38 -07:00
										 |  |  |         const myDir = | 
					
						
							|  |  |  |             (MyDirective.ngDirectiveDef as DirectiveDef<MyDirective>).factory() as MyDirective; | 
					
						
							| 
									
										
										
										
											2018-01-17 10:09:05 -08:00
										 |  |  |         myDir.valA = 'first'; | 
					
						
							|  |  |  |         expect(myDir.valA).toEqual('first'); | 
					
						
							|  |  |  |         myDir.valB = 'second'; | 
					
						
							|  |  |  |         expect(myDir.log).toEqual(['second']); | 
					
						
							|  |  |  |         expect(myDir.valB).toEqual('works'); | 
					
						
							|  |  |  |         myDir.log.length = 0; | 
					
						
							| 
									
										
										
										
											2018-04-14 09:18:38 -07:00
										 |  |  |         (MyDirective.ngDirectiveDef as DirectiveDef<MyDirective>).doCheck !.call(myDir); | 
					
						
							| 
									
										
										
										
											2018-05-27 15:26:41 +02:00
										 |  |  |         const changeA = new SimpleChange(undefined, 'first', true); | 
					
						
							| 
									
										
										
										
											2018-05-27 14:19:58 +02:00
										 |  |  |         const changeB = new SimpleChange(undefined, 'second', true); | 
					
						
							|  |  |  |         expect(myDir.log).toEqual(['ngOnChanges', 'valA', changeA, 'valB', changeB, 'ngDoCheck']); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('correctly computes firstChange', () => { | 
					
						
							|  |  |  |         class MyDirective implements OnChanges { | 
					
						
							|  |  |  |           public log: Array<string|SimpleChange> = []; | 
					
						
							|  |  |  |           public valA: string = 'initValue'; | 
					
						
							|  |  |  |           public valB: string; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           ngOnChanges(changes: SimpleChanges): void { | 
					
						
							|  |  |  |             this.log.push('valA', changes['valA']); | 
					
						
							|  |  |  |             this.log.push('valB', changes['valB']); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           static ngDirectiveDef = defineDirective({ | 
					
						
							|  |  |  |             type: MyDirective, | 
					
						
							|  |  |  |             selectors: [['', 'myDir', '']], | 
					
						
							|  |  |  |             factory: () => new MyDirective(), | 
					
						
							|  |  |  |             features: [NgOnChangesFeature()], | 
					
						
							|  |  |  |             inputs: {valA: 'valA', valB: 'valB'} | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const myDir = | 
					
						
							|  |  |  |             (MyDirective.ngDirectiveDef as DirectiveDef<MyDirective>).factory() as MyDirective; | 
					
						
							|  |  |  |         myDir.valA = 'first'; | 
					
						
							|  |  |  |         myDir.valB = 'second'; | 
					
						
							|  |  |  |         (MyDirective.ngDirectiveDef as DirectiveDef<MyDirective>).doCheck !.call(myDir); | 
					
						
							| 
									
										
										
										
											2018-05-27 15:26:41 +02:00
										 |  |  |         const changeA1 = new SimpleChange(undefined, 'first', true); | 
					
						
							| 
									
										
										
										
											2018-05-27 14:19:58 +02:00
										 |  |  |         const changeB1 = new SimpleChange(undefined, 'second', true); | 
					
						
							|  |  |  |         expect(myDir.log).toEqual(['valA', changeA1, 'valB', changeB1]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         myDir.log.length = 0; | 
					
						
							|  |  |  |         myDir.valA = 'third'; | 
					
						
							|  |  |  |         (MyDirective.ngDirectiveDef as DirectiveDef<MyDirective>).doCheck !.call(myDir); | 
					
						
							|  |  |  |         const changeA2 = new SimpleChange('first', 'third', false); | 
					
						
							|  |  |  |         expect(myDir.log).toEqual(['valA', changeA2, 'valB', undefined]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should not create a getter when only a setter is originally defined', () => { | 
					
						
							|  |  |  |         class MyDirective implements OnChanges { | 
					
						
							|  |  |  |           public log: Array<string|SimpleChange> = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           public set onlySetter(value: string) { this.log.push(value); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           ngOnChanges(changes: SimpleChanges): void { | 
					
						
							|  |  |  |             this.log.push('ngOnChanges'); | 
					
						
							|  |  |  |             this.log.push('onlySetter', changes['onlySetter']); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           static ngDirectiveDef = defineDirective({ | 
					
						
							|  |  |  |             type: MyDirective, | 
					
						
							|  |  |  |             selectors: [['', 'myDir', '']], | 
					
						
							|  |  |  |             factory: () => new MyDirective(), | 
					
						
							|  |  |  |             features: [NgOnChangesFeature()], | 
					
						
							|  |  |  |             inputs: {onlySetter: 'onlySetter'} | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const myDir = | 
					
						
							|  |  |  |             (MyDirective.ngDirectiveDef as DirectiveDef<MyDirective>).factory() as MyDirective; | 
					
						
							|  |  |  |         myDir.onlySetter = 'someValue'; | 
					
						
							|  |  |  |         expect(myDir.onlySetter).toBeUndefined(); | 
					
						
							|  |  |  |         (MyDirective.ngDirectiveDef as DirectiveDef<MyDirective>).doCheck !.call(myDir); | 
					
						
							|  |  |  |         const changeSetter = new SimpleChange(undefined, 'someValue', true); | 
					
						
							|  |  |  |         expect(myDir.log).toEqual(['someValue', 'ngOnChanges', 'onlySetter', changeSetter]); | 
					
						
							| 
									
										
										
										
											2018-01-17 10:09:05 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2018-01-23 18:39:09 -08:00
										 |  |  | }); |