| 
									
										
										
										
											2017-12-01 14:23:03 -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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {EventEmitter} from '@angular/core'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-06 16:11:20 -08:00
										 |  |  | import {defineComponent, defineDirective} from '../../src/render3/index'; | 
					
						
							| 
									
										
										
										
											2018-07-27 14:28:22 -07:00
										 |  |  | import {bind, container, containerRefreshEnd, containerRefreshStart, element, elementEnd, elementProperty, elementStart, embeddedViewEnd, embeddedViewStart, listener, text} from '../../src/render3/instructions'; | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  | import {RenderFlags} from '../../src/render3/interfaces/definition'; | 
					
						
							| 
									
										
										
										
											2018-07-27 14:28:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | import {containerEl, renderToHtml} from './render_util'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('outputs', () => { | 
					
						
							|  |  |  |   let buttonToggle: ButtonToggle; | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |   let destroyComp: DestroyComp; | 
					
						
							|  |  |  |   let buttonDir: MyButton; | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   class ButtonToggle { | 
					
						
							|  |  |  |     change = new EventEmitter(); | 
					
						
							|  |  |  |     resetStream = new EventEmitter(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static ngComponentDef = defineComponent({ | 
					
						
							| 
									
										
										
										
											2018-01-22 15:27:21 -08:00
										 |  |  |       type: ButtonToggle, | 
					
						
							| 
									
										
										
										
											2018-03-29 16:41:45 -07:00
										 |  |  |       selectors: [['button-toggle']], | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       template: function(rf: RenderFlags, ctx: any) {}, | 
					
						
							| 
									
										
										
										
											2018-08-16 18:53:21 -07:00
										 |  |  |       consts: 0, | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |       vars: 0, | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       factory: () => buttonToggle = new ButtonToggle(), | 
					
						
							|  |  |  |       outputs: {change: 'change', resetStream: 'reset'} | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 11:48:54 -08:00
										 |  |  |   let otherDir: OtherDir; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   class OtherDir { | 
					
						
							|  |  |  |     changeStream = new EventEmitter(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-22 15:27:21 -08:00
										 |  |  |     static ngDirectiveDef = defineDirective({ | 
					
						
							|  |  |  |       type: OtherDir, | 
					
						
							| 
									
										
										
										
											2018-03-29 16:41:45 -07:00
										 |  |  |       selectors: [['', 'otherDir', '']], | 
					
						
							| 
									
										
										
										
											2018-01-22 15:27:21 -08:00
										 |  |  |       factory: () => otherDir = new OtherDir, | 
					
						
							|  |  |  |       outputs: {changeStream: 'change'} | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-12-08 11:48:54 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |   class DestroyComp { | 
					
						
							|  |  |  |     events: string[] = []; | 
					
						
							|  |  |  |     ngOnDestroy() { this.events.push('destroy'); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static ngComponentDef = defineComponent({ | 
					
						
							|  |  |  |       type: DestroyComp, | 
					
						
							| 
									
										
										
										
											2018-03-29 16:41:45 -07:00
										 |  |  |       selectors: [['destroy-comp']], | 
					
						
							| 
									
										
										
										
											2018-08-16 18:53:21 -07:00
										 |  |  |       consts: 0, | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |       vars: 0, | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       template: function(rf: RenderFlags, ctx: any) {}, | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |       factory: () => destroyComp = new DestroyComp() | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** <button myButton (click)="onClick()">Click me</button> */ | 
					
						
							|  |  |  |   class MyButton { | 
					
						
							|  |  |  |     click = new EventEmitter(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static ngDirectiveDef = defineDirective({ | 
					
						
							|  |  |  |       type: MyButton, | 
					
						
							| 
									
										
										
										
											2018-03-29 16:41:45 -07:00
										 |  |  |       selectors: [['', 'myButton', '']], | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |       factory: () => buttonDir = new MyButton, | 
					
						
							|  |  |  |       outputs: {click: 'click'} | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-29 12:58:41 -07:00
										 |  |  |   const deps = [ButtonToggle, OtherDir, DestroyComp, MyButton]; | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |   it('should call component output function when event is emitted', () => { | 
					
						
							|  |  |  |     /** <button-toggle (change)="onChange()"></button-toggle> */ | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     function Template(rf: RenderFlags, ctx: any) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |         elementStart(0, 'button-toggle'); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:36:50 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-03-01 09:46:39 -08:00
										 |  |  |           listener('change', function() { return ctx.onChange(); }); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:36:50 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-06 16:11:20 -08:00
										 |  |  |         elementEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let counter = 0; | 
					
						
							|  |  |  |     const ctx = {onChange: () => counter++}; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, ctx, 1, 0, deps); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     buttonToggle !.change.next(); | 
					
						
							|  |  |  |     expect(counter).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     buttonToggle !.change.next(); | 
					
						
							|  |  |  |     expect(counter).toEqual(2); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should support more than 1 output function on the same node', () => { | 
					
						
							|  |  |  |     /** <button-toggle (change)="onChange()" (reset)="onReset()"></button-toggle> */ | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     function Template(rf: RenderFlags, ctx: any) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |         elementStart(0, 'button-toggle'); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-03-01 09:46:39 -08:00
										 |  |  |           listener('change', function() { return ctx.onChange(); }); | 
					
						
							|  |  |  |           listener('reset', function() { return ctx.onReset(); }); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-06 16:11:20 -08:00
										 |  |  |         elementEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let counter = 0; | 
					
						
							|  |  |  |     let resetCounter = 0; | 
					
						
							|  |  |  |     const ctx = {onChange: () => counter++, onReset: () => resetCounter++}; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, ctx, 1, 0, deps); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     buttonToggle !.change.next(); | 
					
						
							|  |  |  |     expect(counter).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     buttonToggle !.resetStream.next(); | 
					
						
							|  |  |  |     expect(resetCounter).toEqual(1); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should eval component output expression when event is emitted', () => { | 
					
						
							|  |  |  |     /** <button-toggle (change)="counter++"></button-toggle> */ | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     function Template(rf: RenderFlags, ctx: any) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |         elementStart(0, 'button-toggle'); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:36:50 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-03-01 09:46:39 -08:00
										 |  |  |           listener('change', function() { return ctx.counter++; }); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:36:50 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-06 16:11:20 -08:00
										 |  |  |         elementEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const ctx = {counter: 0}; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, ctx, 1, 0, deps); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     buttonToggle !.change.next(); | 
					
						
							|  |  |  |     expect(ctx.counter).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     buttonToggle !.change.next(); | 
					
						
							|  |  |  |     expect(ctx.counter).toEqual(2); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should unsubscribe from output when view is destroyed', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * % if (condition) { | 
					
						
							|  |  |  |      *   <button-toggle (change)="onChange()"></button-toggle> | 
					
						
							|  |  |  |      * % } | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     function Template(rf: RenderFlags, ctx: any) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-02-06 16:11:20 -08:00
										 |  |  |         container(0); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       if (rf & RenderFlags.Update) { | 
					
						
							|  |  |  |         containerRefreshStart(0); | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           if (ctx.condition) { | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |             let rf1 = embeddedViewStart(0, 1, 0); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |             if (rf1 & RenderFlags.Create) { | 
					
						
							|  |  |  |               elementStart(0, 'button-toggle'); | 
					
						
							|  |  |  |               { | 
					
						
							|  |  |  |                 listener('change', function() { return ctx.onChange(); }); | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |               elementEnd(); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:36:50 -08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |             embeddedViewEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |         containerRefreshEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let counter = 0; | 
					
						
							|  |  |  |     const ctx = {onChange: () => counter++, condition: true}; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, ctx, 1, 0, deps); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     buttonToggle !.change.next(); | 
					
						
							|  |  |  |     expect(counter).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.condition = false; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, ctx, 1, 0, deps); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     buttonToggle !.change.next(); | 
					
						
							|  |  |  |     expect(counter).toEqual(1); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should unsubscribe from output in nested view', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * % if (condition) { | 
					
						
							|  |  |  |      *   % if (condition2) { | 
					
						
							|  |  |  |      *     <button-toggle (change)="onChange()"></button-toggle> | 
					
						
							|  |  |  |      *   % } | 
					
						
							|  |  |  |      * % } | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     function Template(rf: RenderFlags, ctx: any) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-02-06 16:11:20 -08:00
										 |  |  |         container(0); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       if (rf & RenderFlags.Update) { | 
					
						
							|  |  |  |         containerRefreshStart(0); | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           if (ctx.condition) { | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |             let rf1 = embeddedViewStart(0, 1, 0); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |             if (rf1 & RenderFlags.Create) { | 
					
						
							|  |  |  |               container(0); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             containerRefreshStart(0); | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               if (ctx.condition2) { | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |                 let rf1 = embeddedViewStart(0, 1, 0); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |                 if (rf1 & RenderFlags.Create) { | 
					
						
							|  |  |  |                   elementStart(0, 'button-toggle'); | 
					
						
							|  |  |  |                   { | 
					
						
							|  |  |  |                     listener('change', function() { return ctx.onChange(); }); | 
					
						
							|  |  |  |                   } | 
					
						
							|  |  |  |                   elementEnd(); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:36:50 -08:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |                 embeddedViewEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |               } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |             containerRefreshEnd(); | 
					
						
							|  |  |  |             embeddedViewEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |         containerRefreshEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let counter = 0; | 
					
						
							|  |  |  |     const ctx = {onChange: () => counter++, condition: true, condition2: true}; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, ctx, 1, 0, deps); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     buttonToggle !.change.next(); | 
					
						
							|  |  |  |     expect(counter).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.condition = false; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, ctx, 1, 0, deps); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     buttonToggle !.change.next(); | 
					
						
							|  |  |  |     expect(counter).toEqual(1); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should work properly when view also has listeners and destroys', () => { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * % if (condition) { | 
					
						
							|  |  |  |      *   <button (click)="onClick()">Click me</button> | 
					
						
							|  |  |  |      *   <button-toggle (change)="onChange()"></button-toggle> | 
					
						
							|  |  |  |      *   <destroy-comp></destroy-comp> | 
					
						
							|  |  |  |      * % } | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     function Template(rf: RenderFlags, ctx: any) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-02-06 16:11:20 -08:00
										 |  |  |         container(0); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       if (rf & RenderFlags.Update) { | 
					
						
							|  |  |  |         containerRefreshStart(0); | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           if (ctx.condition) { | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |             let rf1 = embeddedViewStart(0, 4, 0); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |             if (rf1 & RenderFlags.Create) { | 
					
						
							|  |  |  |               elementStart(0, 'button'); | 
					
						
							|  |  |  |               { | 
					
						
							|  |  |  |                 listener('click', function() { return ctx.onClick(); }); | 
					
						
							|  |  |  |                 text(1, 'Click me'); | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |               elementEnd(); | 
					
						
							|  |  |  |               elementStart(2, 'button-toggle'); | 
					
						
							|  |  |  |               { | 
					
						
							|  |  |  |                 listener('change', function() { return ctx.onChange(); }); | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |               elementEnd(); | 
					
						
							| 
									
										
										
										
											2018-07-27 14:28:22 -07:00
										 |  |  |               element(3, 'destroy-comp'); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:36:50 -08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |             embeddedViewEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |         containerRefreshEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let clickCounter = 0; | 
					
						
							|  |  |  |     let changeCounter = 0; | 
					
						
							|  |  |  |     const ctx = {condition: true, onChange: () => changeCounter++, onClick: () => clickCounter++}; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, ctx, 1, 0, deps); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     buttonToggle !.change.next(); | 
					
						
							|  |  |  |     expect(changeCounter).toEqual(1); | 
					
						
							|  |  |  |     expect(clickCounter).toEqual(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const button = containerEl.querySelector('button'); | 
					
						
							|  |  |  |     button !.click(); | 
					
						
							|  |  |  |     expect(changeCounter).toEqual(1); | 
					
						
							|  |  |  |     expect(clickCounter).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.condition = false; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, ctx, 1, 0, deps); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(destroyComp !.events).toEqual(['destroy']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     buttonToggle !.change.next(); | 
					
						
							|  |  |  |     button !.click(); | 
					
						
							|  |  |  |     expect(changeCounter).toEqual(1); | 
					
						
							|  |  |  |     expect(clickCounter).toEqual(1); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should fire event listeners along with outputs if they match', () => { | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     function Template(rf: RenderFlags, ctx: any) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |         elementStart(0, 'button', ['myButton', '']); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:36:50 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-03-01 09:46:39 -08:00
										 |  |  |           listener('click', function() { return ctx.onClick(); }); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:36:50 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-06 16:11:20 -08:00
										 |  |  |         elementEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let counter = 0; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, {counter, onClick: () => counter++}, 1, 0, deps); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // To match current Angular behavior, the click listener is still
 | 
					
						
							|  |  |  |     // set up in addition to any matching outputs.
 | 
					
						
							|  |  |  |     const button = containerEl.querySelector('button') !; | 
					
						
							|  |  |  |     button.click(); | 
					
						
							|  |  |  |     expect(counter).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     buttonDir !.click.next(); | 
					
						
							|  |  |  |     expect(counter).toEqual(2); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should work with two outputs of the same name', () => { | 
					
						
							|  |  |  |     /** <button-toggle (change)="onChange()" otherDir></button-toggle> */ | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     function Template(rf: RenderFlags, ctx: any) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |         elementStart(0, 'button-toggle', ['otherDir', '']); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:36:50 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-03-01 09:46:39 -08:00
										 |  |  |           listener('change', function() { return ctx.onChange(); }); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:36:50 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-06 16:11:20 -08:00
										 |  |  |         elementEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let counter = 0; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, {counter, onChange: () => counter++}, 1, 0, deps); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     buttonToggle !.change.next(); | 
					
						
							|  |  |  |     expect(counter).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 11:48:54 -08:00
										 |  |  |     otherDir !.changeStream.next(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |     expect(counter).toEqual(2); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should work with an input and output of the same name', () => { | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |     let otherDir: OtherChangeDir; | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |     class OtherChangeDir { | 
					
						
							| 
									
										
										
										
											2018-06-18 16:38:33 -07:00
										 |  |  |       // TODO(issue/24571): remove '!'.
 | 
					
						
							|  |  |  |       change !: boolean; | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |       static ngDirectiveDef = defineDirective({ | 
					
						
							|  |  |  |         type: OtherChangeDir, | 
					
						
							| 
									
										
										
										
											2018-03-29 16:41:45 -07:00
										 |  |  |         selectors: [['', 'otherChangeDir', '']], | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |         factory: () => otherDir = new OtherChangeDir, | 
					
						
							|  |  |  |         inputs: {change: 'change'} | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |     /** <button-toggle (change)="onChange()" otherChangeDir [change]="change"></button-toggle> */ | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     function Template(rf: RenderFlags, ctx: any) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-03-25 21:32:39 -07:00
										 |  |  |         elementStart(0, 'button-toggle', ['otherChangeDir', '']); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:36:50 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-03-01 09:46:39 -08:00
										 |  |  |           listener('change', function() { return ctx.onChange(); }); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:36:50 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-06 16:11:20 -08:00
										 |  |  |         elementEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       if (rf & RenderFlags.Update) { | 
					
						
							|  |  |  |         elementProperty(0, 'change', bind(ctx.change)); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let counter = 0; | 
					
						
							| 
									
										
										
										
											2018-03-29 12:58:41 -07:00
										 |  |  |     const deps = [ButtonToggle, OtherChangeDir]; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, {counter, onChange: () => counter++, change: true}, 1, 1, deps); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |     expect(otherDir !.change).toEqual(true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, {counter, onChange: () => counter++, change: false}, 1, 1, deps); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |     expect(otherDir !.change).toEqual(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     buttonToggle !.change.next(); | 
					
						
							|  |  |  |     expect(counter).toEqual(1); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 11:48:54 -08:00
										 |  |  |   it('should work with outputs at same index in if block', () => { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * <button (click)="onClick()">Click me</button>             // outputs: null
 | 
					
						
							|  |  |  |      * % if (condition) { | 
					
						
							|  |  |  |      *   <button-toggle (change)="onChange()"></button-toggle>   // outputs: {change: [0, 'change']} | 
					
						
							|  |  |  |      * % } else { | 
					
						
							|  |  |  |      *   <div otherDir (change)="onChange()"></div>             // outputs: {change: [0,
 | 
					
						
							|  |  |  |      * 'changeStream']} | 
					
						
							|  |  |  |      * % } | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |     function Template(rf: RenderFlags, ctx: any) { | 
					
						
							|  |  |  |       if (rf & RenderFlags.Create) { | 
					
						
							| 
									
										
										
										
											2018-02-06 16:11:20 -08:00
										 |  |  |         elementStart(0, 'button'); | 
					
						
							| 
									
										
										
										
											2017-12-08 11:48:54 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-03-01 09:46:39 -08:00
										 |  |  |           listener('click', function() { return ctx.onClick(); }); | 
					
						
							| 
									
										
										
										
											2018-02-06 16:11:20 -08:00
										 |  |  |           text(1, 'Click me'); | 
					
						
							| 
									
										
										
										
											2017-12-08 11:48:54 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-06 16:11:20 -08:00
										 |  |  |         elementEnd(); | 
					
						
							|  |  |  |         container(2); | 
					
						
							| 
									
										
										
										
											2017-12-08 11:48:54 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |       if (rf & RenderFlags.Update) { | 
					
						
							|  |  |  |         containerRefreshStart(2); | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           if (ctx.condition) { | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |             let rf1 = embeddedViewStart(0, 1, 0); | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |             if (rf1 & RenderFlags.Create) { | 
					
						
							|  |  |  |               elementStart(0, 'button-toggle'); | 
					
						
							|  |  |  |               { | 
					
						
							|  |  |  |                 listener('change', function() { return ctx.onChange(); }); | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |               elementEnd(); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:36:50 -08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |             embeddedViewEnd(); | 
					
						
							|  |  |  |           } else { | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |             if (embeddedViewStart(1, 1, 0)) { | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |               elementStart(0, 'div', ['otherDir', '']); | 
					
						
							|  |  |  |               { | 
					
						
							|  |  |  |                 listener('change', function() { return ctx.onChange(); }); | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |               elementEnd(); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:36:50 -08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |             embeddedViewEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-08 11:48:54 -08:00
										 |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-10 20:57:09 -07:00
										 |  |  |         containerRefreshEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-08 11:48:54 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let counter = 0; | 
					
						
							|  |  |  |     const ctx = {condition: true, onChange: () => counter++, onClick: () => {}}; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, ctx, 3, 0, deps); | 
					
						
							| 
									
										
										
										
											2017-12-08 11:48:54 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     buttonToggle !.change.next(); | 
					
						
							|  |  |  |     expect(counter).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.condition = false; | 
					
						
							| 
									
										
										
										
											2018-08-18 11:14:50 -07:00
										 |  |  |     renderToHtml(Template, ctx, 3, 0, deps); | 
					
						
							| 
									
										
										
										
											2017-12-08 11:48:54 -08:00
										 |  |  |     expect(counter).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     otherDir !.changeStream.next(); | 
					
						
							|  |  |  |     expect(counter).toEqual(2); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | }); |