| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   xit, | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | } from '@angular/core/testing/testing_internal'; | 
					
						
							|  |  |  | import {AsyncTestCompleter} from '@angular/core/testing/testing_internal'; | 
					
						
							|  |  |  | import {Component} from '@angular/core'; | 
					
						
							|  |  |  | import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {NgSwitch, NgSwitchWhen, NgSwitchDefault} from '@angular/common'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('switch', () => { | 
					
						
							|  |  |  |     describe('switch value changes', () => { | 
					
						
							| 
									
										
										
										
											2015-06-11 18:50:41 -07:00
										 |  |  |       it('should switch amongst when values', | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |          inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  |            var template = '<div>' + | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |                           '<ul [ngSwitch]="switchValue">' + | 
					
						
							|  |  |  |                           '<template ngSwitchWhen="a"><li>when a</li></template>' + | 
					
						
							|  |  |  |                           '<template ngSwitchWhen="b"><li>when b</li></template>' + | 
					
						
							|  |  |  |                           '</ul></div>'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |            tcb.overrideTemplate(TestComponent, template) | 
					
						
							|  |  |  |                .createAsync(TestComponent) | 
					
						
							| 
									
										
										
										
											2015-10-31 09:50:19 -07:00
										 |  |  |                .then((fixture) => { | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(fixture.debugElement.nativeElement).toHaveText(''); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 09:50:19 -07:00
										 |  |  |                  fixture.debugElement.componentInstance.switchValue = 'a'; | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(fixture.debugElement.nativeElement).toHaveText('when a'); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 09:50:19 -07:00
										 |  |  |                  fixture.debugElement.componentInstance.switchValue = 'b'; | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(fixture.debugElement.nativeElement).toHaveText('when b'); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should switch amongst when values with fallback to default', | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |          inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  |            var template = '<div>' + | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |                           '<ul [ngSwitch]="switchValue">' + | 
					
						
							|  |  |  |                           '<li template="ngSwitchWhen \'a\'">when a</li>' + | 
					
						
							|  |  |  |                           '<li template="ngSwitchDefault">when default</li>' + | 
					
						
							|  |  |  |                           '</ul></div>'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |            tcb.overrideTemplate(TestComponent, template) | 
					
						
							|  |  |  |                .createAsync(TestComponent) | 
					
						
							| 
									
										
										
										
											2015-10-31 09:50:19 -07:00
										 |  |  |                .then((fixture) => { | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(fixture.debugElement.nativeElement).toHaveText('when default'); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 09:50:19 -07:00
										 |  |  |                  fixture.debugElement.componentInstance.switchValue = 'a'; | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(fixture.debugElement.nativeElement).toHaveText('when a'); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 09:50:19 -07:00
										 |  |  |                  fixture.debugElement.componentInstance.switchValue = 'b'; | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(fixture.debugElement.nativeElement).toHaveText('when default'); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should support multiple whens with the same value', | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |          inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  |            var template = '<div>' + | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |                           '<ul [ngSwitch]="switchValue">' + | 
					
						
							|  |  |  |                           '<template ngSwitchWhen="a"><li>when a1;</li></template>' + | 
					
						
							|  |  |  |                           '<template ngSwitchWhen="b"><li>when b1;</li></template>' + | 
					
						
							|  |  |  |                           '<template ngSwitchWhen="a"><li>when a2;</li></template>' + | 
					
						
							|  |  |  |                           '<template ngSwitchWhen="b"><li>when b2;</li></template>' + | 
					
						
							|  |  |  |                           '<template ngSwitchDefault><li>when default1;</li></template>' + | 
					
						
							|  |  |  |                           '<template ngSwitchDefault><li>when default2;</li></template>' + | 
					
						
							|  |  |  |                           '</ul></div>'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |            tcb.overrideTemplate(TestComponent, template) | 
					
						
							|  |  |  |                .createAsync(TestComponent) | 
					
						
							| 
									
										
										
										
											2015-10-31 09:50:19 -07:00
										 |  |  |                .then((fixture) => { | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(fixture.debugElement.nativeElement) | 
					
						
							| 
									
										
										
										
											2015-09-11 13:45:31 -07:00
										 |  |  |                      .toHaveText('when default1;when default2;'); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 09:50:19 -07:00
										 |  |  |                  fixture.debugElement.componentInstance.switchValue = 'a'; | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(fixture.debugElement.nativeElement).toHaveText('when a1;when a2;'); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 09:50:19 -07:00
										 |  |  |                  fixture.debugElement.componentInstance.switchValue = 'b'; | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(fixture.debugElement.nativeElement).toHaveText('when b1;when b2;'); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('when values changes', () => { | 
					
						
							| 
									
										
										
										
											2015-06-11 18:50:41 -07:00
										 |  |  |       it('should switch amongst when values', | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |          inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  |            var template = '<div>' + | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |                           '<ul [ngSwitch]="switchValue">' + | 
					
						
							|  |  |  |                           '<template [ngSwitchWhen]="when1"><li>when 1;</li></template>' + | 
					
						
							|  |  |  |                           '<template [ngSwitchWhen]="when2"><li>when 2;</li></template>' + | 
					
						
							|  |  |  |                           '<template ngSwitchDefault><li>when default;</li></template>' + | 
					
						
							|  |  |  |                           '</ul></div>'; | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 13:07:15 -07:00
										 |  |  |            tcb.overrideTemplate(TestComponent, template) | 
					
						
							|  |  |  |                .createAsync(TestComponent) | 
					
						
							| 
									
										
										
										
											2015-10-31 09:50:19 -07:00
										 |  |  |                .then((fixture) => { | 
					
						
							|  |  |  |                  fixture.debugElement.componentInstance.when1 = 'a'; | 
					
						
							|  |  |  |                  fixture.debugElement.componentInstance.when2 = 'b'; | 
					
						
							|  |  |  |                  fixture.debugElement.componentInstance.switchValue = 'a'; | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(fixture.debugElement.nativeElement).toHaveText('when 1;'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                  fixture.debugElement.componentInstance.switchValue = 'b'; | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(fixture.debugElement.nativeElement).toHaveText('when 2;'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                  fixture.debugElement.componentInstance.switchValue = 'c'; | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(fixture.debugElement.nativeElement).toHaveText('when default;'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                  fixture.debugElement.componentInstance.when1 = 'c'; | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(fixture.debugElement.nativeElement).toHaveText('when 1;'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                  fixture.debugElement.componentInstance.when1 = 'd'; | 
					
						
							|  |  |  |                  fixture.detectChanges(); | 
					
						
							|  |  |  |                  expect(fixture.debugElement.nativeElement).toHaveText('when default;'); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-08 13:36:48 -08:00
										 |  |  | @Component( | 
					
						
							|  |  |  |     {selector: 'test-cmp', directives: [NgSwitch, NgSwitchWhen, NgSwitchDefault], template: ''}) | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | class TestComponent { | 
					
						
							|  |  |  |   switchValue: any; | 
					
						
							|  |  |  |   when1: any; | 
					
						
							|  |  |  |   when2: any; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor() { | 
					
						
							|  |  |  |     this.switchValue = null; | 
					
						
							|  |  |  |     this.when1 = null; | 
					
						
							|  |  |  |     this.when2 = null; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |