| 
									
										
										
										
											2018-02-16 16:23:27 +01: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-10-30 17:03:01 -07:00
										 |  |  | import {Directive as _Directive, InjectionToken, OnChanges, OnDestroy, Pipe as _Pipe, PipeTransform, createInjector, defineInjectable, defineInjector, ɵNgModuleDef as NgModuleDef, ɵdefineComponent as defineComponent, ɵdirectiveInject as directiveInject} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  | import {expect} from '@angular/platform-browser/testing/src/matchers'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  | import {defineDirective, definePipe} from '../../src/render3/definition'; | 
					
						
							| 
									
										
										
										
											2018-10-05 21:23:41 -07:00
										 |  |  | import {bind, container, containerRefreshEnd, containerRefreshStart, elementEnd, elementProperty, elementStart, embeddedViewEnd, embeddedViewStart, interpolation1, load, text, textBinding} from '../../src/render3/instructions'; | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  | import {RenderFlags} from '../../src/render3/interfaces/definition'; | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  | import {pipe, pipeBind1, pipeBind3, pipeBind4, pipeBindV} from '../../src/render3/pipe'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {RenderLog, getRendererFactory2, patchLoggingRenderer2} from './imported_renderer2'; | 
					
						
							| 
									
										
										
										
											2018-10-05 21:23:41 -07:00
										 |  |  | import {ComponentFixture, createComponent, getDirectiveOnNode, renderToHtml} from './render_util'; | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-30 17:03:01 -07:00
										 |  |  | const Directive: typeof _Directive = function(...args: any[]): any { | 
					
						
							|  |  |  |   // In test we use @Directive for documentation only so it's safe to mock out the implementation.
 | 
					
						
							|  |  |  |   return () => undefined; | 
					
						
							|  |  |  | } as any; | 
					
						
							|  |  |  | const Pipe: typeof _Pipe = function(...args: any[]): any { | 
					
						
							|  |  |  |   // In test we use @Pipe for documentation only so it's safe to mock out the implementation.
 | 
					
						
							|  |  |  |   return () => undefined; | 
					
						
							|  |  |  | } as any; | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | let log: string[] = []; | 
					
						
							|  |  |  | let person: Person; | 
					
						
							|  |  |  | let renderLog: RenderLog = new RenderLog(); | 
					
						
							|  |  |  | const rendererFactory2 = getRendererFactory2(document); | 
					
						
							|  |  |  | patchLoggingRenderer2(rendererFactory2, renderLog); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('pipe', () => { | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     log = []; | 
					
						
							|  |  |  |     renderLog.clear(); | 
					
						
							|  |  |  |     person = new Person(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-29 12:58:41 -07:00
										 |  |  |   const pipes = () => [CountingPipe, MultiArgPipe, CountingImpurePipe]; | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |   it('should support interpolation', () => { | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     function Template(rf: RenderFlags, person: Person) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |         text(0); | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |         pipe(1, 'countingPipe'); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       if (rf & RenderFlags.Update) { | 
					
						
							| 
									
										
										
										
											2018-08-15 16:32:08 -07:00
										 |  |  |         textBinding(0, interpolation1('', pipeBind1(1, 1, person.name), '')); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     person.init('bob', null); | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     expect(renderToHtml(Template, person, 2, 3, null, pipes)).toEqual('bob state:0'); | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should throw if pipe is not found', () => { | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     const App = createComponent('app', function(rf: RenderFlags, ctx: any) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |         text(0); | 
					
						
							|  |  |  |         pipe(1, 'randomPipeName'); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       if (rf & RenderFlags.Update) { | 
					
						
							| 
									
										
										
										
											2018-08-15 16:32:08 -07:00
										 |  |  |         textBinding(0, interpolation1('', pipeBind1(1, 1, ctx.value), '')); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     }, 2, 3, [], pipes); | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(() => { | 
					
						
							|  |  |  |       const fixture = new ComponentFixture(App); | 
					
						
							|  |  |  |     }).toThrowError(/Pipe with name 'randomPipeName' not found!/); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should support bindings', () => { | 
					
						
							|  |  |  |     let directive: any = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @Directive({selector: '[my-dir]', inputs: ['dirProp: elprop'], exportAs: 'mydir'}) | 
					
						
							|  |  |  |     class MyDir { | 
					
						
							|  |  |  |       dirProp: string; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       constructor() { this.dirProp = ''; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |       static ngDirectiveDef = defineDirective({ | 
					
						
							|  |  |  |         type: MyDir, | 
					
						
							| 
									
										
										
										
											2018-03-29 16:41:45 -07:00
										 |  |  |         selectors: [['', 'myDir', '']], | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |         factory: () => new MyDir(), | 
					
						
							|  |  |  |         inputs: {dirProp: 'elprop'} | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @Pipe({name: 'double'}) | 
					
						
							|  |  |  |     class DoublePipe implements PipeTransform { | 
					
						
							|  |  |  |       transform(value: any) { return `${value}${value}`; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       static ngPipeDef = definePipe({ | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |         name: 'double', | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |         type: DoublePipe, | 
					
						
							|  |  |  |         factory: function DoublePipe_Factory() { return new DoublePipe(); }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     function Template(rf: RenderFlags, ctx: string) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |         elementStart(0, 'div', ['myDir', '']); | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |         pipe(1, 'double'); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |         elementEnd(); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       if (rf & RenderFlags.Update) { | 
					
						
							| 
									
										
										
										
											2018-08-15 16:32:08 -07:00
										 |  |  |         elementProperty(0, 'elprop', bind(pipeBind1(1, 1, ctx))); | 
					
						
							| 
									
										
										
										
											2018-10-05 21:23:41 -07:00
										 |  |  |         directive = getDirectiveOnNode(0); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, 'a', 2, 3, [MyDir], [DoublePipe]); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     expect(directive !.dirProp).toEqual('aa'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should support arguments in pipes', () => { | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     function Template(rf: RenderFlags, person: Person) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |         text(0); | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |         pipe(1, 'multiArgPipe'); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       if (rf & RenderFlags.Update) { | 
					
						
							|  |  |  |         textBinding( | 
					
						
							| 
									
										
										
										
											2018-08-15 16:32:08 -07:00
										 |  |  |             0, interpolation1('', pipeBind3(1, 1, person.name, 'one', person.address !.city), '')); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     person.init('value', new Address('two')); | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     expect(renderToHtml(Template, person, 2, 5, null, pipes)).toEqual('value one two default'); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should support calling pipes with different number of arguments', () => { | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     function Template(rf: RenderFlags, person: Person) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |         text(0); | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |         pipe(1, 'multiArgPipe'); | 
					
						
							|  |  |  |         pipe(2, 'multiArgPipe'); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       if (rf & RenderFlags.Update) { | 
					
						
							|  |  |  |         textBinding( | 
					
						
							| 
									
										
										
										
											2018-05-21 15:59:25 -07:00
										 |  |  |             0, interpolation1( | 
					
						
							| 
									
										
										
										
											2018-08-15 16:32:08 -07:00
										 |  |  |                    '', pipeBind4(2, 5, pipeBindV(1, 1, [person.name, 'a', 'b']), 0, 1, 2), '')); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     person.init('value', null); | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     expect(renderToHtml(Template, person, 3, 10, null, pipes)).toEqual('value a b default 0 1 2'); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should do nothing when no change', () => { | 
					
						
							|  |  |  |     @Pipe({name: 'identityPipe'}) | 
					
						
							|  |  |  |     class IdentityPipe implements PipeTransform { | 
					
						
							|  |  |  |       transform(value: any) { return value; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       static ngPipeDef = definePipe({ | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |         name: 'identityPipe', | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |         type: IdentityPipe, | 
					
						
							|  |  |  |         factory: function IdentityPipe_Factory() { return new IdentityPipe(); }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     function Template(rf: RenderFlags, person: Person) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |         elementStart(0, 'div'); | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |         pipe(1, 'identityPipe'); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |         elementEnd(); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       if (rf & RenderFlags.Update) { | 
					
						
							| 
									
										
										
										
											2018-08-15 16:32:08 -07:00
										 |  |  |         elementProperty(0, 'someProp', bind(pipeBind1(1, 1, 'Megatron'))); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, person, 2, 3, null, [IdentityPipe], rendererFactory2); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     expect(renderLog.log).toEqual(['someProp=Megatron']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     renderLog.clear(); | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, person, 2, 3, null, pipes, rendererFactory2); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     expect(renderLog.log).toEqual([]); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('pure', () => { | 
					
						
							|  |  |  |     it('should call pure pipes only if the arguments change', () => { | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       function Template(rf: RenderFlags, person: Person) { | 
					
						
							|  |  |  |         if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |           text(0); | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |           pipe(1, 'countingPipe'); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |         if (rf & RenderFlags.Update) { | 
					
						
							| 
									
										
										
										
											2018-08-15 16:32:08 -07:00
										 |  |  |           textBinding(0, interpolation1('', pipeBind1(1, 1, person.name), '')); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // change from undefined -> null
 | 
					
						
							|  |  |  |       person.name = null; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |       expect(renderToHtml(Template, person, 2, 3, null, pipes)).toEqual('null state:0'); | 
					
						
							|  |  |  |       expect(renderToHtml(Template, person, 2, 3, null, pipes)).toEqual('null state:0'); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // change from null -> some value
 | 
					
						
							|  |  |  |       person.name = 'bob'; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |       expect(renderToHtml(Template, person, 2, 3, null, pipes)).toEqual('bob state:1'); | 
					
						
							|  |  |  |       expect(renderToHtml(Template, person, 2, 3, null, pipes)).toEqual('bob state:1'); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // change from some value -> some other value
 | 
					
						
							|  |  |  |       person.name = 'bart'; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |       expect(renderToHtml(Template, person, 2, 3, null, pipes)).toEqual('bart state:2'); | 
					
						
							|  |  |  |       expect(renderToHtml(Template, person, 2, 3, null, pipes)).toEqual('bart state:2'); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('impure', () => { | 
					
						
							|  |  |  |     it('should call impure pipes on each change detection run', () => { | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       function Template(rf: RenderFlags, person: Person) { | 
					
						
							|  |  |  |         if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |           text(0); | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |           pipe(1, 'countingImpurePipe'); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |         if (rf & RenderFlags.Update) { | 
					
						
							| 
									
										
										
										
											2018-08-15 16:32:08 -07:00
										 |  |  |           textBinding(0, interpolation1('', pipeBind1(1, 1, person.name), '')); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       person.name = 'bob'; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |       expect(renderToHtml(Template, person, 2, 3, null, pipes)).toEqual('bob state:0'); | 
					
						
							|  |  |  |       expect(renderToHtml(Template, person, 2, 3, null, pipes)).toEqual('bob state:1'); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should not cache impure pipes', () => { | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       function Template(rf: RenderFlags, ctx: any) { | 
					
						
							|  |  |  |         if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |           elementStart(0, 'div'); | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |           pipe(1, 'countingImpurePipe'); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |           elementEnd(); | 
					
						
							|  |  |  |           elementStart(2, 'div'); | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |           pipe(3, 'countingImpurePipe'); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |           elementEnd(); | 
					
						
							|  |  |  |           container(4); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |         if (rf & RenderFlags.Update) { | 
					
						
							| 
									
										
										
										
											2018-05-21 15:59:25 -07:00
										 |  |  |           elementProperty(0, 'someProp', bind(pipeBind1(1, 2, true))); | 
					
						
							|  |  |  |           elementProperty(2, 'someProp', bind(pipeBind1(3, 4, true))); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |           pipeInstances.push(load<CountingImpurePipe>(1), load(3)); | 
					
						
							|  |  |  |           containerRefreshStart(4); | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             for (let i of [1, 2]) { | 
					
						
							| 
									
										
										
										
											2018-08-21 00:03:21 -07:00
										 |  |  |               let rf1 = embeddedViewStart(1, 2, 3); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |               { | 
					
						
							|  |  |  |                 if (rf1 & RenderFlags.Create) { | 
					
						
							|  |  |  |                   elementStart(0, 'div'); | 
					
						
							|  |  |  |                   pipe(1, 'countingImpurePipe'); | 
					
						
							|  |  |  |                   elementEnd(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (rf1 & RenderFlags.Update) { | 
					
						
							| 
									
										
										
										
											2018-08-15 16:32:08 -07:00
										 |  |  |                   elementProperty(0, 'someProp', bind(pipeBind1(1, 1, true))); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |                   pipeInstances.push(load<CountingImpurePipe>(1)); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |               } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |               embeddedViewEnd(); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |           containerRefreshEnd(); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |       const pipeInstances: CountingImpurePipe[] = []; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |       renderToHtml(Template, {}, 5, 6, null, pipes, rendererFactory2); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |       expect(pipeInstances.length).toEqual(4); | 
					
						
							|  |  |  |       expect(pipeInstances[0]).toBeAnInstanceOf(CountingImpurePipe); | 
					
						
							|  |  |  |       expect(pipeInstances[1]).toBeAnInstanceOf(CountingImpurePipe); | 
					
						
							|  |  |  |       expect(pipeInstances[1]).not.toBe(pipeInstances[0]); | 
					
						
							|  |  |  |       expect(pipeInstances[2]).toBeAnInstanceOf(CountingImpurePipe); | 
					
						
							|  |  |  |       expect(pipeInstances[2]).not.toBe(pipeInstances[0]); | 
					
						
							|  |  |  |       expect(pipeInstances[3]).toBeAnInstanceOf(CountingImpurePipe); | 
					
						
							|  |  |  |       expect(pipeInstances[3]).not.toBe(pipeInstances[0]); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('lifecycles', () => { | 
					
						
							|  |  |  |     @Pipe({name: 'pipeWithOnDestroy'}) | 
					
						
							|  |  |  |     class PipeWithOnDestroy implements PipeTransform, OnDestroy { | 
					
						
							|  |  |  |       ngOnDestroy() { log.push('pipeWithOnDestroy - ngOnDestroy'); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       transform(value: any): any { return null; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       static ngPipeDef = definePipe({ | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |         name: 'pipeWithOnDestroy', | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |         type: PipeWithOnDestroy, | 
					
						
							|  |  |  |         factory: function PipeWithOnDestroy_Factory() { return new PipeWithOnDestroy(); }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should call ngOnDestroy on pipes', () => { | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       function Template(rf: RenderFlags, person: Person) { | 
					
						
							|  |  |  |         if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |           container(0); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |         if (rf & RenderFlags.Update) { | 
					
						
							|  |  |  |           containerRefreshStart(0); | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             if (person.age > 20) { | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |               let rf1 = embeddedViewStart(1, 2, 3); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |               { | 
					
						
							|  |  |  |                 if (rf1 & RenderFlags.Create) { | 
					
						
							|  |  |  |                   text(0); | 
					
						
							|  |  |  |                   pipe(1, 'pipeWithOnDestroy'); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (rf & RenderFlags.Update) { | 
					
						
							| 
									
										
										
										
											2018-08-15 16:32:08 -07:00
										 |  |  |                   textBinding(0, interpolation1('', pipeBind1(1, 1, person.age), '')); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |               } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |               embeddedViewEnd(); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |           containerRefreshEnd(); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-03-29 12:58:41 -07:00
										 |  |  |       const pipes = [PipeWithOnDestroy]; | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |       person.age = 25; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |       renderToHtml(Template, person, 1, 0, null, pipes); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       person.age = 15; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |       renderToHtml(Template, person, 1, 0, null, pipes); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |       expect(log).toEqual(['pipeWithOnDestroy - ngOnDestroy']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       log = []; | 
					
						
							|  |  |  |       person.age = 30; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |       renderToHtml(Template, person, 1, 0, null, pipes); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |       expect(log).toEqual([]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       log = []; | 
					
						
							|  |  |  |       person.age = 10; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |       renderToHtml(Template, person, 1, 0, null, pipes); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |       expect(log).toEqual(['pipeWithOnDestroy - ngOnDestroy']); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-05 17:46:58 -07:00
										 |  |  |   describe('injection mechanism', () => { | 
					
						
							|  |  |  |     class ServiceA { | 
					
						
							|  |  |  |       title = 'ServiceA Title'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class ServiceB { | 
					
						
							|  |  |  |       title = 'ServiceB Title'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       static ngInjectableDef = | 
					
						
							|  |  |  |           defineInjectable({providedIn: 'root', factory: () => new ServiceB()}); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class ModuleA { | 
					
						
							|  |  |  |       static ngInjectorDef = defineInjector({factory: () => new ModuleA(), providers: [ServiceA]}); | 
					
						
							|  |  |  |       static ngModuleDef: NgModuleDef<any> = { bootstrap: [] } as any; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const generatePipe = (InjectionType: any) => { | 
					
						
							|  |  |  |       return class MyConcatPipe implements PipeTransform { | 
					
						
							|  |  |  |         constructor(public obj: any) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         transform(value: string): string { return `${value} - ${this.obj.title}`; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         static ngPipeDef = definePipe({ | 
					
						
							|  |  |  |           name: 'myConcatPipe', | 
					
						
							|  |  |  |           type: MyConcatPipe, | 
					
						
							|  |  |  |           factory: () => new MyConcatPipe(directiveInject(InjectionType)), | 
					
						
							|  |  |  |           pure: false | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const generateComponent = (overrides: any) => { | 
					
						
							|  |  |  |       return class MyComponent { | 
					
						
							|  |  |  |         title = 'MyComponent Title'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         static ngComponentDef = defineComponent({ | 
					
						
							|  |  |  |           type: MyComponent, | 
					
						
							|  |  |  |           selectors: [['my-app']], | 
					
						
							|  |  |  |           factory: function MyComponent_Factory() { return new MyComponent(); }, | 
					
						
							|  |  |  |           consts: 2, | 
					
						
							|  |  |  |           vars: 3, | 
					
						
							|  |  |  |           // '{{ title | myConcatPipe }}'
 | 
					
						
							|  |  |  |           template: (rf: RenderFlags, ctx: MyComponent) => { | 
					
						
							|  |  |  |             if (rf & 1) { | 
					
						
							|  |  |  |               text(0); | 
					
						
							|  |  |  |               pipe(1, 'myConcatPipe'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (rf & 2) { | 
					
						
							|  |  |  |               textBinding(0, interpolation1('', pipeBind1(1, 1, ctx.title), '')); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           ...overrides | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should be able to handle Service injection', () => { | 
					
						
							|  |  |  |       const Comp = generateComponent({providers: [ServiceB], pipes: [generatePipe(ServiceB)]}); | 
					
						
							|  |  |  |       const fixture = new ComponentFixture(Comp); | 
					
						
							|  |  |  |       expect(fixture.html).toEqual('MyComponent Title - ServiceB Title'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should be able to handle Token injections', () => { | 
					
						
							|  |  |  |       const provider = new InjectionToken<ServiceA>( | 
					
						
							|  |  |  |           'token', {providedIn: 'root', factory: () => new ServiceB()}); | 
					
						
							|  |  |  |       const Comp = generateComponent({providers: [provider], pipes: [generatePipe(provider)]}); | 
					
						
							|  |  |  |       const fixture = new ComponentFixture(Comp); | 
					
						
							|  |  |  |       expect(fixture.html).toEqual('MyComponent Title - ServiceB Title'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should be able to handle Module injection', () => { | 
					
						
							|  |  |  |       const injector = createInjector(ModuleA); | 
					
						
							|  |  |  |       const Comp = generateComponent({providers: [], pipes: [generatePipe(ServiceA)]}); | 
					
						
							|  |  |  |       const fixture = new ComponentFixture(Comp, {injector}); | 
					
						
							|  |  |  |       expect(fixture.html).toEqual('MyComponent Title - ServiceA Title'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Pipe({name: 'countingPipe'}) | 
					
						
							|  |  |  | class CountingPipe implements PipeTransform { | 
					
						
							|  |  |  |   state: number = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   transform(value: any) { return `${value} state:${this.state++}`; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static ngPipeDef = definePipe({ | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |     name: 'countingPipe', | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     type: CountingPipe, | 
					
						
							|  |  |  |     factory: function CountingPipe_Factory() { return new CountingPipe(); }, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Pipe({name: 'countingImpurePipe', pure: false}) | 
					
						
							|  |  |  | class CountingImpurePipe implements PipeTransform { | 
					
						
							|  |  |  |   state: number = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   transform(value: any) { return `${value} state:${this.state++}`; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static ngPipeDef = definePipe({ | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |     name: 'countingImpurePipe', | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     type: CountingImpurePipe, | 
					
						
							|  |  |  |     factory: function CountingImpurePipe_Factory() { return new CountingImpurePipe(); }, | 
					
						
							|  |  |  |     pure: false, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Pipe({name: 'multiArgPipe'}) | 
					
						
							|  |  |  | class MultiArgPipe implements PipeTransform { | 
					
						
							|  |  |  |   transform(value: any, arg1: any, arg2: any, arg3 = 'default') { | 
					
						
							|  |  |  |     return `${value} ${arg1} ${arg2} ${arg3}`; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static ngPipeDef = definePipe({ | 
					
						
							| 
									
										
										
										
											2018-03-27 15:53:48 -07:00
										 |  |  |     name: 'multiArgPipe', | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |     type: MultiArgPipe, | 
					
						
							|  |  |  |     factory: function MultiArgPipe_Factory() { return new MultiArgPipe(); }, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Person { | 
					
						
							| 
									
										
										
										
											2018-06-18 16:38:33 -07:00
										 |  |  |   // TODO(issue/24571): remove '!'.
 | 
					
						
							|  |  |  |   age !: number; | 
					
						
							|  |  |  |   // TODO(issue/24571): remove '!'.
 | 
					
						
							|  |  |  |   name !: string | null; | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  |   address: Address|null = null; | 
					
						
							| 
									
										
										
										
											2018-06-18 16:38:33 -07:00
										 |  |  |   // TODO(issue/24571): remove '!'.
 | 
					
						
							|  |  |  |   phones !: number[]; | 
					
						
							| 
									
										
										
										
											2018-02-16 16:23:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   init(name: string|null, address: Address|null = null) { | 
					
						
							|  |  |  |     this.name = name; | 
					
						
							|  |  |  |     this.address = address; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   sayHi(m: any): string { return `Hi, ${m}`; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   passThrough(val: any): any { return val; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   toString(): string { | 
					
						
							|  |  |  |     const address = this.address == null ? '' : ' address=' + this.address.toString(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 'name=' + this.name + address; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Address { | 
					
						
							|  |  |  |   cityGetterCalls: number = 0; | 
					
						
							|  |  |  |   zipCodeGetterCalls: number = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor(public _city: string, public _zipcode: any = null) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   get city() { | 
					
						
							|  |  |  |     this.cityGetterCalls++; | 
					
						
							|  |  |  |     return this._city; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   get zipcode() { | 
					
						
							|  |  |  |     this.zipCodeGetterCalls++; | 
					
						
							|  |  |  |     return this._zipcode; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   set city(v) { this._city = v; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   set zipcode(v) { this._zipcode = v; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   toString(): string { return this.city || '-'; } | 
					
						
							|  |  |  | } |