| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   AsyncTestCompleter, | 
					
						
							|  |  |  |   TestComponentBuilder, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   el, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   xit, | 
					
						
							|  |  |  | } from 'angular2/testing_internal'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-19 15:09:34 -08:00
										 |  |  | import {DOM} from 'angular2/src/platform/dom/dom_adapter'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-08 13:36:48 -08:00
										 |  |  | import {Component} from 'angular2/core'; | 
					
						
							| 
									
										
										
										
											2015-11-18 15:55:43 -08:00
										 |  |  | import {NgIf} from 'angular2/common'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-06 17:34:07 -08:00
										 |  |  | import {IS_DART} from 'angular2/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', | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |        inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { | 
					
						
							| 
									
										
										
										
											2015-11-23 16:02:19 -08:00
										 |  |  |          var html = '<div><copy-me template="ngIf booleanCondition">hello</copy-me></div>'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |          tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |              .createAsync(TestComponent) | 
					
						
							|  |  |  |              .then((fixture) => { | 
					
						
							|  |  |  |                fixture.detectChanges(); | 
					
						
							|  |  |  |                expect(DOM.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', | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |        inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  |          var html = | 
					
						
							| 
									
										
										
										
											2015-11-23 16:02:19 -08:00
										 |  |  |              '<div><template [ngIf]="booleanCondition"><copy-me>hello2</copy-me></template></div>'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |          tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |              .createAsync(TestComponent) | 
					
						
							|  |  |  |              .then((fixture) => { | 
					
						
							|  |  |  |                fixture.detectChanges(); | 
					
						
							|  |  |  |                expect(DOM.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', | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |        inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { | 
					
						
							| 
									
										
										
										
											2015-11-23 16:02:19 -08:00
										 |  |  |          var html = '<div><copy-me template="ngIf booleanCondition">hello</copy-me></div>'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |          tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |              .createAsync(TestComponent) | 
					
						
							|  |  |  |              .then((fixture) => { | 
					
						
							|  |  |  |                fixture.debugElement.componentInstance.booleanCondition = false; | 
					
						
							|  |  |  |                fixture.detectChanges(); | 
					
						
							|  |  |  |                expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length) | 
					
						
							|  |  |  |                    .toEqual(0); | 
					
						
							|  |  |  |                expect(fixture.debugElement.nativeElement).toHaveText(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                fixture.debugElement.componentInstance.booleanCondition = true; | 
					
						
							|  |  |  |                fixture.detectChanges(); | 
					
						
							|  |  |  |                expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length) | 
					
						
							|  |  |  |                    .toEqual(1); | 
					
						
							|  |  |  |                expect(fixture.debugElement.nativeElement).toHaveText('hello'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                fixture.debugElement.componentInstance.booleanCondition = false; | 
					
						
							|  |  |  |                fixture.detectChanges(); | 
					
						
							|  |  |  |                expect(DOM.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', | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |        inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  |          var html = | 
					
						
							| 
									
										
										
										
											2015-11-23 16:02:19 -08:00
										 |  |  |              '<div><template [ngIf]="booleanCondition"><copy-me *ngIf="nestedBooleanCondition">hello</copy-me></template></div>'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |          tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |              .createAsync(TestComponent) | 
					
						
							|  |  |  |              .then((fixture) => { | 
					
						
							|  |  |  |                fixture.debugElement.componentInstance.booleanCondition = false; | 
					
						
							|  |  |  |                fixture.detectChanges(); | 
					
						
							|  |  |  |                expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length) | 
					
						
							|  |  |  |                    .toEqual(0); | 
					
						
							|  |  |  |                expect(fixture.debugElement.nativeElement).toHaveText(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                fixture.debugElement.componentInstance.booleanCondition = true; | 
					
						
							|  |  |  |                fixture.detectChanges(); | 
					
						
							|  |  |  |                expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length) | 
					
						
							|  |  |  |                    .toEqual(1); | 
					
						
							|  |  |  |                expect(fixture.debugElement.nativeElement).toHaveText('hello'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                fixture.debugElement.componentInstance.nestedBooleanCondition = false; | 
					
						
							|  |  |  |                fixture.detectChanges(); | 
					
						
							|  |  |  |                expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length) | 
					
						
							|  |  |  |                    .toEqual(0); | 
					
						
							|  |  |  |                expect(fixture.debugElement.nativeElement).toHaveText(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                fixture.debugElement.componentInstance.nestedBooleanCondition = true; | 
					
						
							|  |  |  |                fixture.detectChanges(); | 
					
						
							|  |  |  |                expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length) | 
					
						
							|  |  |  |                    .toEqual(1); | 
					
						
							|  |  |  |                expect(fixture.debugElement.nativeElement).toHaveText('hello'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                fixture.debugElement.componentInstance.booleanCondition = false; | 
					
						
							|  |  |  |                fixture.detectChanges(); | 
					
						
							|  |  |  |                expect(DOM.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', | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |        inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |          var html = | 
					
						
							|  |  |  |              '<div>' + | 
					
						
							| 
									
										
										
										
											2015-11-23 16:02:19 -08:00
										 |  |  |              '<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>' + | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  |              '</div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |          tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |              .createAsync(TestComponent) | 
					
						
							|  |  |  |              .then((fixture) => { | 
					
						
							|  |  |  |                fixture.detectChanges(); | 
					
						
							|  |  |  |                expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length) | 
					
						
							|  |  |  |                    .toEqual(3); | 
					
						
							|  |  |  |                expect(DOM.getText(fixture.debugElement.nativeElement)) | 
					
						
							|  |  |  |                    .toEqual('helloNumberhelloStringhelloFunction'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                fixture.debugElement.componentInstance.numberCondition = 0; | 
					
						
							|  |  |  |                fixture.detectChanges(); | 
					
						
							|  |  |  |                expect(DOM.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(DOM.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)', | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |          inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { | 
					
						
							| 
									
										
										
										
											2015-11-23 16:02:19 -08:00
										 |  |  |            var html = '<div><copy-me template="ngIf numberCondition">hello</copy-me></div>'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |            tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |                .createAsync(TestComponent) | 
					
						
							|  |  |  |                .then((fixture) => { | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length) | 
					
						
							|  |  |  |                      .toEqual(1); | 
					
						
							|  |  |  |                  expect(fixture.debugElement.nativeElement).toHaveText('hello'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                  fixture.debugElement.componentInstance.numberCondition = 2; | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(DOM.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)', | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |          inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { | 
					
						
							| 
									
										
										
										
											2015-11-23 16:02:19 -08:00
										 |  |  |            var html = '<div><copy-me template="ngIf numberCondition">hello</copy-me></div>'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |            tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |                .createAsync(TestComponent) | 
					
						
							|  |  |  |                .then((fixture) => { | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  DOM.addClass(DOM.querySelector(fixture.debugElement.nativeElement, 'copy-me'), | 
					
						
							|  |  |  |                               "foo"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                  fixture.debugElement.componentInstance.numberCondition = 2; | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect( | 
					
						
							|  |  |  |                      DOM.hasClass(DOM.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)', | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |          inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { | 
					
						
							| 
									
										
										
										
											2015-11-23 16:02:19 -08:00
										 |  |  |            var html = '<div><copy-me template="ngIf numberCondition">hello</copy-me></div>'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |            tcb.overrideTemplate(TestComponent, html) | 
					
						
							|  |  |  |                .createAsync(TestComponent) | 
					
						
							|  |  |  |                .then((fixture) => { | 
					
						
							|  |  |  |                  expect(() => fixture.detectChanges()).toThrowError(); | 
					
						
							|  |  |  |                  expect(DOM.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-04-12 09:40:37 -07:00
										 |  |  |     this.stringCondition = "foo"; | 
					
						
							|  |  |  |     this.functionCondition = function(s, n) { return s == "foo" && n == 1; }; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | } |