| 
									
										
										
										
											2016-06-23 09:47:54 -07: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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {beforeEach, ddescribe, describe, iit, inject, it, xit,} from '@angular/core/testing/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-05-20 16:11:49 -07:00
										 |  |  | import {TestComponentBuilder} from '@angular/compiler/testing'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {AsyncTestCompleter} from '@angular/core/testing/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-06-23 16:42:25 -07:00
										 |  |  | import {expect} from '@angular/platform-browser/testing/matchers'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {Component} from '@angular/core'; | 
					
						
							|  |  |  | import {NgIf} from '@angular/common'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {IS_DART} from '../../src/facade/lang'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2015-11-23 16:02:19 -08:00
										 |  |  |   describe('ngIf directive', () => { | 
					
						
							| 
									
										
										
										
											2015-06-11 18:50:41 -07:00
										 |  |  |     it('should work in a template attribute', | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |        inject( | 
					
						
							|  |  |  |            [TestComponentBuilder, AsyncTestCompleter], | 
					
						
							|  |  |  |            (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |              var html = '<div><copy-me template="ngIf booleanCondition">hello</copy-me></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |                  .createAsync(TestComponent) | 
					
						
							|  |  |  |                  .then((fixture) => { | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(getDOM() | 
					
						
							|  |  |  |                               .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                               .length) | 
					
						
							|  |  |  |                        .toEqual(1); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText('hello'); | 
					
						
							|  |  |  |                    async.done(); | 
					
						
							|  |  |  |                  }); | 
					
						
							|  |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-11 18:50:41 -07:00
										 |  |  |     it('should work in a template element', | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |        inject( | 
					
						
							|  |  |  |            [TestComponentBuilder, AsyncTestCompleter], | 
					
						
							|  |  |  |            (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |              var html = | 
					
						
							|  |  |  |                  '<div><template [ngIf]="booleanCondition"><copy-me>hello2</copy-me></template></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |                  .createAsync(TestComponent) | 
					
						
							|  |  |  |                  .then((fixture) => { | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(getDOM() | 
					
						
							|  |  |  |                               .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                               .length) | 
					
						
							|  |  |  |                        .toEqual(1); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText('hello2'); | 
					
						
							|  |  |  |                    async.done(); | 
					
						
							|  |  |  |                  }); | 
					
						
							|  |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should toggle node when condition changes', | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |        inject( | 
					
						
							|  |  |  |            [TestComponentBuilder, AsyncTestCompleter], | 
					
						
							|  |  |  |            (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |              var html = '<div><copy-me template="ngIf booleanCondition">hello</copy-me></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |                  .createAsync(TestComponent) | 
					
						
							|  |  |  |                  .then((fixture) => { | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.booleanCondition = false; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(getDOM() | 
					
						
							|  |  |  |                               .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                               .length) | 
					
						
							|  |  |  |                        .toEqual(0); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.booleanCondition = true; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(getDOM() | 
					
						
							|  |  |  |                               .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                               .length) | 
					
						
							|  |  |  |                        .toEqual(1); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText('hello'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.booleanCondition = false; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(getDOM() | 
					
						
							|  |  |  |                               .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                               .length) | 
					
						
							|  |  |  |                        .toEqual(0); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    async.done(); | 
					
						
							|  |  |  |                  }); | 
					
						
							|  |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-11 18:50:41 -07:00
										 |  |  |     it('should handle nested if correctly', | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |        inject( | 
					
						
							|  |  |  |            [TestComponentBuilder, AsyncTestCompleter], | 
					
						
							|  |  |  |            (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |              var html = | 
					
						
							|  |  |  |                  '<div><template [ngIf]="booleanCondition"><copy-me *ngIf="nestedBooleanCondition">hello</copy-me></template></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |                  .createAsync(TestComponent) | 
					
						
							|  |  |  |                  .then((fixture) => { | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.booleanCondition = false; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(getDOM() | 
					
						
							|  |  |  |                               .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                               .length) | 
					
						
							|  |  |  |                        .toEqual(0); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.booleanCondition = true; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(getDOM() | 
					
						
							|  |  |  |                               .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                               .length) | 
					
						
							|  |  |  |                        .toEqual(1); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText('hello'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.nestedBooleanCondition = false; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(getDOM() | 
					
						
							|  |  |  |                               .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                               .length) | 
					
						
							|  |  |  |                        .toEqual(0); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.nestedBooleanCondition = true; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(getDOM() | 
					
						
							|  |  |  |                               .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                               .length) | 
					
						
							|  |  |  |                        .toEqual(1); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText('hello'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.booleanCondition = false; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(getDOM() | 
					
						
							|  |  |  |                               .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                               .length) | 
					
						
							|  |  |  |                        .toEqual(0); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    async.done(); | 
					
						
							|  |  |  |                  }); | 
					
						
							|  |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-11 18:50:41 -07:00
										 |  |  |     it('should update several nodes with if', | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |        inject( | 
					
						
							|  |  |  |            [TestComponentBuilder, AsyncTestCompleter], | 
					
						
							|  |  |  |            (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |              var html = '<div>' + | 
					
						
							|  |  |  |                  '<copy-me template="ngIf numberCondition + 1 >= 2">helloNumber</copy-me>' + | 
					
						
							|  |  |  |                  '<copy-me template="ngIf stringCondition == \'foo\'">helloString</copy-me>' + | 
					
						
							|  |  |  |                  '<copy-me template="ngIf functionCondition(stringCondition, numberCondition)">helloFunction</copy-me>' + | 
					
						
							|  |  |  |                  '</div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |                  .createAsync(TestComponent) | 
					
						
							|  |  |  |                  .then((fixture) => { | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(getDOM() | 
					
						
							|  |  |  |                               .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                               .length) | 
					
						
							|  |  |  |                        .toEqual(3); | 
					
						
							|  |  |  |                    expect(getDOM().getText(fixture.debugElement.nativeElement)) | 
					
						
							|  |  |  |                        .toEqual('helloNumberhelloStringhelloFunction'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.numberCondition = 0; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(getDOM() | 
					
						
							|  |  |  |                               .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                               .length) | 
					
						
							|  |  |  |                        .toEqual(1); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText('helloString'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.numberCondition = 1; | 
					
						
							|  |  |  |                    fixture.debugElement.componentInstance.stringCondition = 'bar'; | 
					
						
							|  |  |  |                    fixture.detectChanges(); | 
					
						
							|  |  |  |                    expect(getDOM() | 
					
						
							|  |  |  |                               .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                               .length) | 
					
						
							|  |  |  |                        .toEqual(1); | 
					
						
							|  |  |  |                    expect(fixture.debugElement.nativeElement).toHaveText('helloNumber'); | 
					
						
							|  |  |  |                    async.done(); | 
					
						
							|  |  |  |                  }); | 
					
						
							|  |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 14:00:54 -07:00
										 |  |  |     if (!IS_DART) { | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  |       it('should not add the element twice if the condition goes from true to true (JS)', | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |          inject( | 
					
						
							|  |  |  |              [TestComponentBuilder, AsyncTestCompleter], | 
					
						
							|  |  |  |              (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |                var html = '<div><copy-me template="ngIf numberCondition">hello</copy-me></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |                    .createAsync(TestComponent) | 
					
						
							|  |  |  |                    .then((fixture) => { | 
					
						
							|  |  |  |                      fixture.detectChanges(); | 
					
						
							|  |  |  |                      expect(getDOM() | 
					
						
							|  |  |  |                                 .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                                 .length) | 
					
						
							|  |  |  |                          .toEqual(1); | 
					
						
							|  |  |  |                      expect(fixture.debugElement.nativeElement).toHaveText('hello'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                      fixture.debugElement.componentInstance.numberCondition = 2; | 
					
						
							|  |  |  |                      fixture.detectChanges(); | 
					
						
							|  |  |  |                      expect(getDOM() | 
					
						
							|  |  |  |                                 .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                                 .length) | 
					
						
							|  |  |  |                          .toEqual(1); | 
					
						
							|  |  |  |                      expect(fixture.debugElement.nativeElement).toHaveText('hello'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                      async.done(); | 
					
						
							|  |  |  |                    }); | 
					
						
							|  |  |  |              })); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it('should not recreate the element if the condition goes from true to true (JS)', | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |          inject( | 
					
						
							|  |  |  |              [TestComponentBuilder, AsyncTestCompleter], | 
					
						
							|  |  |  |              (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |                var html = '<div><copy-me template="ngIf numberCondition">hello</copy-me></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |                    .createAsync(TestComponent) | 
					
						
							|  |  |  |                    .then((fixture) => { | 
					
						
							|  |  |  |                      fixture.detectChanges(); | 
					
						
							|  |  |  |                      getDOM().addClass( | 
					
						
							|  |  |  |                          getDOM().querySelector(fixture.debugElement.nativeElement, 'copy-me'), | 
					
						
							|  |  |  |                          'foo'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                      fixture.debugElement.componentInstance.numberCondition = 2; | 
					
						
							|  |  |  |                      fixture.detectChanges(); | 
					
						
							|  |  |  |                      expect( | 
					
						
							|  |  |  |                          getDOM().hasClass( | 
					
						
							|  |  |  |                              getDOM().querySelector(fixture.debugElement.nativeElement, 'copy-me'), | 
					
						
							|  |  |  |                              'foo')) | 
					
						
							|  |  |  |                          .toBe(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                      async.done(); | 
					
						
							|  |  |  |                    }); | 
					
						
							|  |  |  |              })); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 14:00:54 -07:00
										 |  |  |     if (IS_DART) { | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  |       it('should not create the element if the condition is not a boolean (DART)', | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |          inject( | 
					
						
							|  |  |  |              [TestComponentBuilder, AsyncTestCompleter], | 
					
						
							|  |  |  |              (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |                var html = '<div><copy-me template="ngIf numberCondition">hello</copy-me></div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |                    .createAsync(TestComponent) | 
					
						
							|  |  |  |                    .then((fixture) => { | 
					
						
							|  |  |  |                      expect(() => fixture.detectChanges()).toThrowError(); | 
					
						
							|  |  |  |                      expect(getDOM() | 
					
						
							|  |  |  |                                 .querySelectorAll(fixture.debugElement.nativeElement, 'copy-me') | 
					
						
							|  |  |  |                                 .length) | 
					
						
							|  |  |  |                          .toEqual(0); | 
					
						
							|  |  |  |                      expect(fixture.debugElement.nativeElement).toHaveText(''); | 
					
						
							|  |  |  |                      async.done(); | 
					
						
							|  |  |  |                    }); | 
					
						
							|  |  |  |              })); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-08 13:36:48 -08:00
										 |  |  | @Component({selector: 'test-cmp', directives: [NgIf], template: ''}) | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | class TestComponent { | 
					
						
							|  |  |  |   booleanCondition: boolean; | 
					
						
							|  |  |  |   nestedBooleanCondition: boolean; | 
					
						
							|  |  |  |   numberCondition: number; | 
					
						
							|  |  |  |   stringCondition: string; | 
					
						
							|  |  |  |   functionCondition: Function; | 
					
						
							|  |  |  |   constructor() { | 
					
						
							|  |  |  |     this.booleanCondition = true; | 
					
						
							|  |  |  |     this.nestedBooleanCondition = true; | 
					
						
							|  |  |  |     this.numberCondition = 1; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     this.stringCondition = 'foo'; | 
					
						
							| 
									
										
										
										
											2016-06-11 21:23:37 -07:00
										 |  |  |     this.functionCondition = function(s: any, n: any): boolean { return s == 'foo' && n == 1; }; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | } |