| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  | import { | 
					
						
							|  |  |  |   AsyncTestCompleter, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   el, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   xit, | 
					
						
							|  |  |  | } from 'angular2/test_lib'; | 
					
						
							| 
									
										
										
										
											2015-02-27 14:50:06 -08:00
										 |  |  | import {DOM} from 'angular2/src/dom/dom_adapter'; | 
					
						
							| 
									
										
										
										
											2015-02-05 13:08:05 -08:00
										 |  |  | import {Injector} from 'angular2/di'; | 
					
						
							|  |  |  | import {Lexer, Parser, dynamicChangeDetection} from 'angular2/change_detection'; | 
					
						
							| 
									
										
										
										
											2015-02-24 16:05:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-05 13:08:05 -08:00
										 |  |  | import {Compiler, CompilerCache} from 'angular2/src/core/compiler/compiler'; | 
					
						
							|  |  |  | import {DirectiveMetadataReader} from 'angular2/src/core/compiler/directive_metadata_reader'; | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  | import {NativeShadowDomStrategy} from 'angular2/src/core/compiler/shadow_dom_strategy'; | 
					
						
							| 
									
										
										
										
											2015-02-24 16:05:45 +01:00
										 |  |  | import {ComponentUrlMapper} from 'angular2/src/core/compiler/component_url_mapper'; | 
					
						
							|  |  |  | import {UrlResolver} from 'angular2/src/core/compiler/url_resolver'; | 
					
						
							|  |  |  | import {StyleUrlResolver} from 'angular2/src/core/compiler/style_url_resolver'; | 
					
						
							| 
									
										
										
										
											2015-03-02 15:02:48 +01:00
										 |  |  | import {CssProcessor} from 'angular2/src/core/compiler/css_processor'; | 
					
						
							| 
									
										
										
										
											2015-02-24 16:05:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-05 13:08:05 -08:00
										 |  |  | import {Component} from 'angular2/src/core/annotations/annotations'; | 
					
						
							| 
									
										
										
										
											2015-02-12 14:44:59 +01:00
										 |  |  | import {Template} from 'angular2/src/core/annotations/template'; | 
					
						
							|  |  |  | import {TemplateLoader} from 'angular2/core'; | 
					
						
							| 
									
										
										
										
											2015-02-04 22:27:31 +01:00
										 |  |  | import {Switch, SwitchWhen, SwitchDefault} from 'angular2/src/directives/switch'; | 
					
						
							| 
									
										
										
										
											2015-02-24 15:32:44 +01:00
										 |  |  | import {MockTemplateResolver} from 'angular2/src/mock/template_resolver_mock'; | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2015-02-04 22:27:31 +01:00
										 |  |  |   describe('switch', () => { | 
					
						
							| 
									
										
										
										
											2015-02-12 14:44:59 +01:00
										 |  |  |     var view, cd, compiler, component, tplResolver; | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |     beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2015-02-24 16:05:45 +01:00
										 |  |  |       var urlResolver = new UrlResolver(); | 
					
						
							| 
									
										
										
										
											2015-02-24 15:32:44 +01:00
										 |  |  |       tplResolver = new MockTemplateResolver(); | 
					
						
							| 
									
										
										
										
											2015-02-24 16:05:45 +01:00
										 |  |  |       compiler = new Compiler( | 
					
						
							|  |  |  |         dynamicChangeDetection, | 
					
						
							|  |  |  |         new TemplateLoader(null, null), | 
					
						
							|  |  |  |         new DirectiveMetadataReader(), | 
					
						
							|  |  |  |         new Parser(new Lexer()), | 
					
						
							|  |  |  |         new CompilerCache(), | 
					
						
							|  |  |  |         new NativeShadowDomStrategy(new StyleUrlResolver(urlResolver)), | 
					
						
							|  |  |  |         tplResolver, | 
					
						
							|  |  |  |         new ComponentUrlMapper(), | 
					
						
							| 
									
										
										
										
											2015-03-02 15:02:48 +01:00
										 |  |  |         urlResolver, | 
					
						
							| 
									
										
										
										
											2015-03-03 11:32:19 +01:00
										 |  |  |         new CssProcessor(null) | 
					
						
							| 
									
										
										
										
											2015-03-02 15:02:48 +01:00
										 |  |  |       ); | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function createView(pv) { | 
					
						
							|  |  |  |       component = new TestComponent(); | 
					
						
							| 
									
										
										
										
											2015-03-11 19:57:21 +01:00
										 |  |  |       view = pv.instantiate(null, null); | 
					
						
							| 
									
										
										
										
											2015-03-11 21:11:39 -07:00
										 |  |  |       view.hydrate(new Injector([]), null, null, component, null); | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |       cd = view.changeDetector; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 14:44:59 +01:00
										 |  |  |     function compileWithTemplate(html) { | 
					
						
							|  |  |  |       var template = new Template({ | 
					
						
							|  |  |  |         inline: html, | 
					
						
							|  |  |  |         directives: [Switch, SwitchWhen, SwitchDefault] | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       tplResolver.setTemplate(TestComponent, template); | 
					
						
							|  |  |  |       return compiler.compile(TestComponent); | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('switch value changes', () => { | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       it('should switch amongst when values', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |         var template = '<div>' + | 
					
						
							| 
									
										
										
										
											2015-02-04 22:27:31 +01:00
										 |  |  |           '<ul [switch]="switchValue">' + | 
					
						
							|  |  |  |             '<template [switch-when]="\'a\'"><li>when a</li></template>' + | 
					
						
							|  |  |  |             '<template [switch-when]="\'b\'"><li>when b</li></template>' + | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |           '</ul></div>'; | 
					
						
							|  |  |  |         compileWithTemplate(template).then((pv) => { | 
					
						
							|  |  |  |           createView(pv); | 
					
						
							|  |  |  |           cd.detectChanges(); | 
					
						
							|  |  |  |           expect(DOM.getText(view.nodes[0])).toEqual(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           component.switchValue = 'a'; | 
					
						
							|  |  |  |           cd.detectChanges(); | 
					
						
							|  |  |  |           expect(DOM.getText(view.nodes[0])).toEqual('when a'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           component.switchValue = 'b'; | 
					
						
							|  |  |  |           cd.detectChanges(); | 
					
						
							|  |  |  |           expect(DOM.getText(view.nodes[0])).toEqual('when b'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |           async.done(); | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       it('should switch amongst when values with fallback to default', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |         var template = '<div>' + | 
					
						
							| 
									
										
										
										
											2015-02-04 22:27:31 +01:00
										 |  |  |           '<ul [switch]="switchValue">' + | 
					
						
							|  |  |  |             '<li template="switch-when \'a\'">when a</li>' + | 
					
						
							|  |  |  |             '<li template="switch-default">when default</li>' + | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |           '</ul></div>'; | 
					
						
							|  |  |  |         compileWithTemplate(template).then((pv) => { | 
					
						
							|  |  |  |           createView(pv); | 
					
						
							|  |  |  |           cd.detectChanges(); | 
					
						
							|  |  |  |           expect(DOM.getText(view.nodes[0])).toEqual('when default'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           component.switchValue = 'a'; | 
					
						
							|  |  |  |           cd.detectChanges(); | 
					
						
							|  |  |  |           expect(DOM.getText(view.nodes[0])).toEqual('when a'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           component.switchValue = 'b'; | 
					
						
							|  |  |  |           cd.detectChanges(); | 
					
						
							|  |  |  |           expect(DOM.getText(view.nodes[0])).toEqual('when default'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |           async.done(); | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       it('should support multiple whens with the same value', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |         var template = '<div>' + | 
					
						
							| 
									
										
										
										
											2015-02-04 22:27:31 +01:00
										 |  |  |           '<ul [switch]="switchValue">' + | 
					
						
							|  |  |  |             '<template [switch-when]="\'a\'"><li>when a1;</li></template>' + | 
					
						
							|  |  |  |             '<template [switch-when]="\'b\'"><li>when b1;</li></template>' + | 
					
						
							|  |  |  |             '<template [switch-when]="\'a\'"><li>when a2;</li></template>' + | 
					
						
							|  |  |  |             '<template [switch-when]="\'b\'"><li>when b2;</li></template>' + | 
					
						
							|  |  |  |             '<template [switch-default]><li>when default1;</li></template>' + | 
					
						
							|  |  |  |             '<template [switch-default]><li>when default2;</li></template>' + | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |           '</ul></div>'; | 
					
						
							|  |  |  |         compileWithTemplate(template).then((pv) => { | 
					
						
							|  |  |  |           createView(pv); | 
					
						
							|  |  |  |           cd.detectChanges(); | 
					
						
							|  |  |  |           expect(DOM.getText(view.nodes[0])).toEqual('when default1;when default2;'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           component.switchValue = 'a'; | 
					
						
							|  |  |  |           cd.detectChanges(); | 
					
						
							|  |  |  |           expect(DOM.getText(view.nodes[0])).toEqual('when a1;when a2;'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           component.switchValue = 'b'; | 
					
						
							|  |  |  |           cd.detectChanges(); | 
					
						
							|  |  |  |           expect(DOM.getText(view.nodes[0])).toEqual('when b1;when b2;'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |           async.done(); | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('when values changes', () => { | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       it('should switch amongst when values', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |         var template = '<div>' + | 
					
						
							| 
									
										
										
										
											2015-02-04 22:27:31 +01:00
										 |  |  |           '<ul [switch]="switchValue">' + | 
					
						
							|  |  |  |             '<template [switch-when]="when1"><li>when 1;</li></template>' + | 
					
						
							|  |  |  |             '<template [switch-when]="when2"><li>when 2;</li></template>' + | 
					
						
							|  |  |  |             '<template [switch-default]><li>when default;</li></template>' + | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |           '</ul></div>'; | 
					
						
							|  |  |  |         compileWithTemplate(template).then((pv) => { | 
					
						
							|  |  |  |           createView(pv); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           component.when1 = 'a'; | 
					
						
							|  |  |  |           component.when2 = 'b'; | 
					
						
							|  |  |  |           component.switchValue = 'a'; | 
					
						
							|  |  |  |           cd.detectChanges(); | 
					
						
							|  |  |  |           expect(DOM.getText(view.nodes[0])).toEqual('when 1;'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           component.switchValue = 'b'; | 
					
						
							|  |  |  |           cd.detectChanges(); | 
					
						
							|  |  |  |           expect(DOM.getText(view.nodes[0])).toEqual('when 2;'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           component.switchValue = 'c'; | 
					
						
							|  |  |  |           cd.detectChanges(); | 
					
						
							|  |  |  |           expect(DOM.getText(view.nodes[0])).toEqual('when default;'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           component.when1 = 'c'; | 
					
						
							|  |  |  |           cd.detectChanges(); | 
					
						
							|  |  |  |           expect(DOM.getText(view.nodes[0])).toEqual('when 1;'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           component.when1 = 'd'; | 
					
						
							|  |  |  |           cd.detectChanges(); | 
					
						
							|  |  |  |           expect(DOM.getText(view.nodes[0])).toEqual('when default;'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |           async.done(); | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 14:44:59 +01:00
										 |  |  | @Component({selector: 'test-cmp'}) | 
					
						
							| 
									
										
										
										
											2015-01-09 09:16:12 +01:00
										 |  |  | class TestComponent { | 
					
						
							|  |  |  |   switchValue: any; | 
					
						
							|  |  |  |   when1: any; | 
					
						
							|  |  |  |   when2: any; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor() { | 
					
						
							|  |  |  |     this.switchValue = null; | 
					
						
							|  |  |  |     this.when1 = null; | 
					
						
							|  |  |  |     this.when2 = null; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |