| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  | import { | 
					
						
							|  |  |  |   AsyncTestCompleter, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   el, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   IS_DARTIUM, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   xit, | 
					
						
							|  |  |  | } from 'angular2/test_lib'; | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-27 14:50:06 -08:00
										 |  |  | import {DOM} from 'angular2/src/dom/dom_adapter'; | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  | import {TestBed} from 'angular2/src/test_lib/test_bed'; | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-28 18:17:00 -07:00
										 |  |  | import {Component} from 'angular2/src/core/annotations_impl/annotations'; | 
					
						
							|  |  |  | import {View} from 'angular2/src/core/annotations_impl/view'; | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 15:58:59 -07:00
										 |  |  | import {NgIf} from 'angular2/src/directives/ng_if'; | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2015-02-04 22:27:31 +01:00
										 |  |  |   describe('if directive', () => { | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |     it('should work in a template attribute', inject([TestBed, AsyncTestCompleter], (tb, async) => { | 
					
						
							| 
									
										
										
										
											2015-05-11 15:58:59 -07:00
										 |  |  |       var html = '<div><copy-me template="ng-if booleanCondition">hello</copy-me></div>'; | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |       tb.createView(TestComponent, {html: html}).then((view) => { | 
					
						
							|  |  |  |         view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |         expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(1); | 
					
						
							|  |  |  |         expect(DOM.getText(view.rootNodes[0])).toEqual('hello'); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |         async.done(); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |     })); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |     it('should work in a template element', inject([TestBed, AsyncTestCompleter], (tb, async) => { | 
					
						
							| 
									
										
										
										
											2015-05-11 15:58:59 -07:00
										 |  |  |       var html = '<div><template [ng-if]="booleanCondition"><copy-me>hello2</copy-me></template></div>'; | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |       tb.createView(TestComponent, {html: html}).then((view) => { | 
					
						
							|  |  |  |         view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |         expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(1); | 
					
						
							|  |  |  |         expect(DOM.getText(view.rootNodes[0])).toEqual('hello2'); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |         async.done(); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |     })); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |     it('should toggle node when condition changes', inject([TestBed, AsyncTestCompleter], (tb, async) => { | 
					
						
							| 
									
										
										
										
											2015-05-11 15:58:59 -07:00
										 |  |  |       var html = '<div><copy-me template="ng-if booleanCondition">hello</copy-me></div>'; | 
					
						
							| 
									
										
										
										
											2015-01-23 09:27:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |       tb.createView(TestComponent, {html: html}).then((view) => { | 
					
						
							|  |  |  |         view.context.booleanCondition = false; | 
					
						
							|  |  |  |         view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |         expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(0); | 
					
						
							|  |  |  |         expect(DOM.getText(view.rootNodes[0])).toEqual(''); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |         view.context.booleanCondition = true; | 
					
						
							|  |  |  |         view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |         expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(1); | 
					
						
							|  |  |  |         expect(DOM.getText(view.rootNodes[0])).toEqual('hello'); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |         view.context.booleanCondition = false; | 
					
						
							|  |  |  |         view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |         expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(0); | 
					
						
							|  |  |  |         expect(DOM.getText(view.rootNodes[0])).toEqual(''); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |         async.done(); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |     })); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |     it('should handle nested if correctly', inject([TestBed, AsyncTestCompleter], (tb, async) => { | 
					
						
							| 
									
										
										
										
											2015-05-11 15:58:59 -07:00
										 |  |  |       var html = '<div><template [ng-if]="booleanCondition"><copy-me *ng-if="nestedBooleanCondition">hello</copy-me></template></div>'; | 
					
						
							| 
									
										
										
										
											2015-03-19 10:04:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |       tb.createView(TestComponent, {html: html}).then((view) => { | 
					
						
							|  |  |  |         view.context.booleanCondition = false; | 
					
						
							|  |  |  |         view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |         expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(0); | 
					
						
							|  |  |  |         expect(DOM.getText(view.rootNodes[0])).toEqual(''); | 
					
						
							| 
									
										
										
										
											2015-03-19 10:04:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |         view.context.booleanCondition = true; | 
					
						
							|  |  |  |         view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |         expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(1); | 
					
						
							|  |  |  |         expect(DOM.getText(view.rootNodes[0])).toEqual('hello'); | 
					
						
							| 
									
										
										
										
											2015-03-19 10:04:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |         view.context.nestedBooleanCondition = false; | 
					
						
							|  |  |  |         view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |         expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(0); | 
					
						
							|  |  |  |         expect(DOM.getText(view.rootNodes[0])).toEqual(''); | 
					
						
							| 
									
										
										
										
											2015-03-19 10:04:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |         view.context.nestedBooleanCondition = true; | 
					
						
							|  |  |  |         view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |         expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(1); | 
					
						
							|  |  |  |         expect(DOM.getText(view.rootNodes[0])).toEqual('hello'); | 
					
						
							| 
									
										
										
										
											2015-03-19 10:04:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |         view.context.booleanCondition = false; | 
					
						
							|  |  |  |         view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |         expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(0); | 
					
						
							|  |  |  |         expect(DOM.getText(view.rootNodes[0])).toEqual(''); | 
					
						
							| 
									
										
										
										
											2015-03-19 10:04:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         async.done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |     it('should update several nodes with if', inject([TestBed, AsyncTestCompleter], (tb, async) => { | 
					
						
							|  |  |  |       var html = | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  |       '<div>' + | 
					
						
							| 
									
										
										
										
											2015-05-11 15:58:59 -07:00
										 |  |  |         '<copy-me template="ng-if numberCondition + 1 >= 2">helloNumber</copy-me>' + | 
					
						
							|  |  |  |         '<copy-me template="ng-if stringCondition == \'foo\'">helloString</copy-me>' + | 
					
						
							|  |  |  |         '<copy-me template="ng-if functionCondition(stringCondition, numberCondition)">helloFunction</copy-me>' + | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  |       '</div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |       tb.createView(TestComponent, {html: html}).then((view) => { | 
					
						
							|  |  |  |         view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |         expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(3); | 
					
						
							|  |  |  |         expect(DOM.getText(view.rootNodes[0])).toEqual('helloNumberhelloStringhelloFunction'); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |         view.context.numberCondition = 0; | 
					
						
							|  |  |  |         view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |         expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(1); | 
					
						
							|  |  |  |         expect(DOM.getText(view.rootNodes[0])).toEqual('helloString'); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |         view.context.numberCondition = 1; | 
					
						
							|  |  |  |         view.context.stringCondition = "bar"; | 
					
						
							|  |  |  |         view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |         expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(1); | 
					
						
							|  |  |  |         expect(DOM.getText(view.rootNodes[0])).toEqual('helloNumber'); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |         async.done(); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |     })); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-23 09:27:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |     if (!IS_DARTIUM) { | 
					
						
							|  |  |  |       it('should not add the element twice if the condition goes from true to true (JS)', | 
					
						
							|  |  |  |         inject([TestBed, AsyncTestCompleter], (tb, async) => { | 
					
						
							| 
									
										
										
										
											2015-05-11 15:58:59 -07:00
										 |  |  |         var html = '<div><copy-me template="ng-if numberCondition">hello</copy-me></div>'; | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |         tb.createView(TestComponent, {html: html}).then((view) => { | 
					
						
							|  |  |  |           view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |           expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(1); | 
					
						
							|  |  |  |           expect(DOM.getText(view.rootNodes[0])).toEqual('hello'); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |           view.context.numberCondition = 2; | 
					
						
							|  |  |  |           view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |           expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(1); | 
					
						
							|  |  |  |           expect(DOM.getText(view.rootNodes[0])).toEqual('hello'); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |           async.done(); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |       it('should not recreate the element if the condition goes from true to true (JS)', | 
					
						
							|  |  |  |         inject([TestBed, AsyncTestCompleter], (tb, async) => { | 
					
						
							| 
									
										
										
										
											2015-05-11 15:58:59 -07:00
										 |  |  |           var html = '<div><copy-me template="ng-if numberCondition">hello</copy-me></div>'; | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |           tb.createView(TestComponent, {html: html}).then((view) => { | 
					
						
							|  |  |  |             view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |             DOM.addClass(view.rootNodes[0].childNodes[1], "foo"); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |             view.context.numberCondition = 2; | 
					
						
							|  |  |  |             view.detectChanges(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |             expect(DOM.hasClass(view.rootNodes[0].childNodes[1], "foo")).toBe(true); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |             async.done(); | 
					
						
							|  |  |  |           }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (IS_DARTIUM) { | 
					
						
							|  |  |  |       it('should not create the element if the condition is not a boolean (DART)', | 
					
						
							|  |  |  |         inject([TestBed, AsyncTestCompleter], (tb, async) => { | 
					
						
							| 
									
										
										
										
											2015-05-11 15:58:59 -07:00
										 |  |  |           var html = '<div><copy-me template="ng-if numberCondition">hello</copy-me></div>'; | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |           tb.createView(TestComponent, {html: html}).then((view) => { | 
					
						
							|  |  |  |             expect(() => view.detectChanges()).toThrowError(); | 
					
						
							| 
									
										
										
										
											2015-04-07 20:54:20 -07:00
										 |  |  |             expect(DOM.querySelectorAll(view.rootNodes[0], 'copy-me').length).toEqual(0); | 
					
						
							|  |  |  |             expect(DOM.getText(view.rootNodes[0])).toEqual(''); | 
					
						
							| 
									
										
										
										
											2015-03-30 17:11:18 +02:00
										 |  |  |             async.done(); | 
					
						
							|  |  |  |           }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 14:44:59 +01:00
										 |  |  | @Component({selector: 'test-cmp'}) | 
					
						
							| 
									
										
										
										
											2015-05-11 15:58:59 -07:00
										 |  |  | @View({directives: [NgIf]}) | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  | class TestComponent { | 
					
						
							|  |  |  |   booleanCondition: boolean; | 
					
						
							| 
									
										
										
										
											2015-03-19 10:04:42 +01:00
										 |  |  |   nestedBooleanCondition: boolean; | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  |   numberCondition: number; | 
					
						
							|  |  |  |   stringCondition: string; | 
					
						
							|  |  |  |   functionCondition: Function; | 
					
						
							|  |  |  |   constructor() { | 
					
						
							|  |  |  |     this.booleanCondition = true; | 
					
						
							| 
									
										
										
										
											2015-03-19 10:04:42 +01:00
										 |  |  |     this.nestedBooleanCondition = true; | 
					
						
							| 
									
										
										
										
											2014-12-17 10:01:08 +01:00
										 |  |  |     this.numberCondition = 1; | 
					
						
							|  |  |  |     this.stringCondition = "foo"; | 
					
						
							|  |  |  |     this.functionCondition = function(s, n){ | 
					
						
							|  |  |  |       return s == "foo" && n == 1; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |