| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 15:58:42 +02:00
										 |  |  | import {EventEmitter} from '@angular/core'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {AttributeMarker, defineDirective} from '../../src/render3/index'; | 
					
						
							|  |  |  | import {bind, elementEnd, elementProperty, elementStart, listener, loadDirective} from '../../src/render3/instructions'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {TemplateFixture} from './render_util'; | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('directive', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('host', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should support host bindings in directives', () => { | 
					
						
							|  |  |  |       let directiveInstance: Directive|undefined; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       class Directive { | 
					
						
							|  |  |  |         klass = 'foo'; | 
					
						
							| 
									
										
										
										
											2018-01-08 21:57:50 -08:00
										 |  |  |         static ngDirectiveDef = defineDirective({ | 
					
						
							| 
									
										
										
										
											2018-01-22 15:27:21 -08:00
										 |  |  |           type: Directive, | 
					
						
							| 
									
										
										
										
											2018-03-29 16:41:45 -07:00
										 |  |  |           selectors: [['', 'dir', '']], | 
					
						
							| 
									
										
										
										
											2018-01-08 21:57:50 -08:00
										 |  |  |           factory: () => directiveInstance = new Directive, | 
					
						
							| 
									
										
										
										
											2018-01-22 19:52:06 -08:00
										 |  |  |           hostBindings: (directiveIndex: number, elementIndex: number) => { | 
					
						
							| 
									
										
										
										
											2018-03-21 15:10:34 -07:00
										 |  |  |             elementProperty( | 
					
						
							|  |  |  |                 elementIndex, 'className', bind(loadDirective<Directive>(directiveIndex).klass)); | 
					
						
							| 
									
										
										
										
											2018-01-08 21:57:50 -08:00
										 |  |  |           } | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 15:58:42 +02:00
										 |  |  |       function Template() { | 
					
						
							| 
									
										
										
										
											2018-06-08 15:25:39 -07:00
										 |  |  |         elementStart(0, 'span', [AttributeMarker.SelectOnly, 'dir']); | 
					
						
							| 
									
										
										
										
											2018-05-04 15:58:42 +02:00
										 |  |  |         elementEnd(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 15:58:42 +02:00
										 |  |  |       const fixture = new TemplateFixture(Template, () => {}, [Directive]); | 
					
						
							|  |  |  |       expect(fixture.html).toEqual('<span class="foo"></span>'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |       directiveInstance !.klass = 'bar'; | 
					
						
							| 
									
										
										
										
											2018-05-04 15:58:42 +02:00
										 |  |  |       fixture.update(); | 
					
						
							|  |  |  |       expect(fixture.html).toEqual('<span class="bar"></span>'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('selectors', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should match directives with attribute selectors on bindings', () => { | 
					
						
							|  |  |  |       let directiveInstance: Directive; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       class Directive { | 
					
						
							|  |  |  |         static ngDirectiveDef = defineDirective({ | 
					
						
							|  |  |  |           type: Directive, | 
					
						
							|  |  |  |           selectors: [['', 'test', '']], | 
					
						
							|  |  |  |           factory: () => directiveInstance = new Directive, | 
					
						
							|  |  |  |           inputs: {test: 'test', other: 'other'} | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-18 16:38:33 -07:00
										 |  |  |         // TODO(issue/24571): remove '!'.
 | 
					
						
							|  |  |  |         testValue !: boolean; | 
					
						
							|  |  |  |         // TODO(issue/24571): remove '!'.
 | 
					
						
							|  |  |  |         other !: boolean; | 
					
						
							| 
									
										
										
										
											2018-05-04 15:58:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** | 
					
						
							|  |  |  |          * A setter to assert that a binding is not invoked with stringified attribute value | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         set test(value: any) { | 
					
						
							|  |  |  |           // if a binding is processed correctly we should only be invoked with a false Boolean
 | 
					
						
							|  |  |  |           // and never with the "false" string literal
 | 
					
						
							|  |  |  |           this.testValue = value; | 
					
						
							|  |  |  |           if (value !== false) { | 
					
						
							|  |  |  |             fail('Should only be called with a false Boolean value, got a non-falsy value'); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       /** | 
					
						
							|  |  |  |        * <span [test]="false" [other]="true"></span> | 
					
						
							|  |  |  |        */ | 
					
						
							|  |  |  |       function createTemplate() { | 
					
						
							|  |  |  |         // using 2 bindings to show example shape of attributes array
 | 
					
						
							| 
									
										
										
										
											2018-06-08 15:25:39 -07:00
										 |  |  |         elementStart(0, 'span', ['class', 'fade', AttributeMarker.SelectOnly, 'test', 'other']); | 
					
						
							| 
									
										
										
										
											2018-05-04 15:58:42 +02:00
										 |  |  |         elementEnd(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       function updateTemplate() { elementProperty(0, 'test', bind(false)); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const fixture = new TemplateFixture(createTemplate, updateTemplate, [Directive]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // the "test" attribute should not be reflected in the DOM as it is here only for directive
 | 
					
						
							|  |  |  |       // matching purposes
 | 
					
						
							|  |  |  |       expect(fixture.html).toEqual('<span class="fade"></span>'); | 
					
						
							|  |  |  |       expect(directiveInstance !.testValue).toBe(false); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should not accidentally set inputs from attributes extracted from bindings / outputs', | 
					
						
							|  |  |  |        () => { | 
					
						
							|  |  |  |          let directiveInstance: Directive; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          class Directive { | 
					
						
							|  |  |  |            static ngDirectiveDef = defineDirective({ | 
					
						
							|  |  |  |              type: Directive, | 
					
						
							|  |  |  |              selectors: [['', 'test', '']], | 
					
						
							|  |  |  |              factory: () => directiveInstance = new Directive, | 
					
						
							|  |  |  |              inputs: {test: 'test', prop1: 'prop1', prop2: 'prop2'} | 
					
						
							|  |  |  |            }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-18 16:38:33 -07:00
										 |  |  |            // TODO(issue/24571): remove '!'.
 | 
					
						
							|  |  |  |            prop1 !: boolean; | 
					
						
							|  |  |  |            // TODO(issue/24571): remove '!'.
 | 
					
						
							|  |  |  |            prop2 !: boolean; | 
					
						
							|  |  |  |            // TODO(issue/24571): remove '!'.
 | 
					
						
							|  |  |  |            testValue !: boolean; | 
					
						
							| 
									
										
										
										
											2018-05-04 15:58:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            /** | 
					
						
							|  |  |  |             * A setter to assert that a binding is not invoked with stringified attribute value | 
					
						
							|  |  |  |             */ | 
					
						
							|  |  |  |            set test(value: any) { | 
					
						
							|  |  |  |              // if a binding is processed correctly we should only be invoked with a false Boolean
 | 
					
						
							|  |  |  |              // and never with the "false" string literal
 | 
					
						
							|  |  |  |              this.testValue = value; | 
					
						
							|  |  |  |              if (value !== false) { | 
					
						
							|  |  |  |                fail('Should only be called with a false Boolean value, got a non-falsy value'); | 
					
						
							|  |  |  |              } | 
					
						
							|  |  |  |            } | 
					
						
							|  |  |  |          } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          /** | 
					
						
							| 
									
										
										
										
											2018-06-08 15:25:39 -07:00
										 |  |  |           * <span class="fade" [prop1]="true" [test]="false" [prop2]="true"></span> | 
					
						
							| 
									
										
										
										
											2018-05-04 15:58:42 +02:00
										 |  |  |           */ | 
					
						
							|  |  |  |          function createTemplate() { | 
					
						
							|  |  |  |            // putting name (test) in the "usual" value position
 | 
					
						
							|  |  |  |            elementStart( | 
					
						
							| 
									
										
										
										
											2018-06-08 15:25:39 -07:00
										 |  |  |                0, 'span', ['class', 'fade', AttributeMarker.SelectOnly, 'prop1', 'test', 'prop2']); | 
					
						
							| 
									
										
										
										
											2018-05-04 15:58:42 +02:00
										 |  |  |            elementEnd(); | 
					
						
							|  |  |  |          } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          function updateTemplate() { | 
					
						
							|  |  |  |            elementProperty(0, 'prop1', bind(true)); | 
					
						
							|  |  |  |            elementProperty(0, 'test', bind(false)); | 
					
						
							|  |  |  |            elementProperty(0, 'prop2', bind(true)); | 
					
						
							|  |  |  |          } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          const fixture = new TemplateFixture(createTemplate, updateTemplate, [Directive]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          // the "test" attribute should not be reflected in the DOM as it is here only for directive
 | 
					
						
							|  |  |  |          // matching purposes
 | 
					
						
							|  |  |  |          expect(fixture.html).toEqual('<span class="fade"></span>'); | 
					
						
							|  |  |  |          expect(directiveInstance !.testValue).toBe(false); | 
					
						
							|  |  |  |        }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should match directives with attribute selectors on outputs', () => { | 
					
						
							|  |  |  |       let directiveInstance: Directive; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       class Directive { | 
					
						
							|  |  |  |         static ngDirectiveDef = defineDirective({ | 
					
						
							|  |  |  |           type: Directive, | 
					
						
							|  |  |  |           selectors: [['', 'out', '']], | 
					
						
							|  |  |  |           factory: () => directiveInstance = new Directive, | 
					
						
							|  |  |  |           outputs: {out: 'out'} | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         out = new EventEmitter(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       /** | 
					
						
							|  |  |  |        * <span (out)="someVar = true"></span> | 
					
						
							|  |  |  |        */ | 
					
						
							|  |  |  |       function createTemplate() { | 
					
						
							| 
									
										
										
										
											2018-06-08 15:25:39 -07:00
										 |  |  |         elementStart(0, 'span', [AttributeMarker.SelectOnly, 'out']); | 
					
						
							| 
									
										
										
										
											2018-05-04 15:58:42 +02:00
										 |  |  |         { listener('out', () => {}); } | 
					
						
							|  |  |  |         elementEnd(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const fixture = new TemplateFixture(createTemplate, () => {}, [Directive]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // "out" should not be part of reflected attributes
 | 
					
						
							|  |  |  |       expect(fixture.html).toEqual('<span></span>'); | 
					
						
							|  |  |  |       expect(directiveInstance !).not.toBeUndefined(); | 
					
						
							| 
									
										
										
										
											2017-12-01 14:23:03 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |