| 
									
										
										
										
											2015-02-03 07:27:09 -08:00
										 |  |  | import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, | 
					
						
							| 
									
										
										
										
											2015-02-05 14:09:26 -08:00
										 |  |  |   el, queryView, dispatchEvent} from 'angular2/test_lib'; | 
					
						
							| 
									
										
										
										
											2015-02-03 07:27:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-05 14:09:26 -08:00
										 |  |  | import {Lexer, Parser, ChangeDetector, dynamicChangeDetection} from 'angular2/change_detection'; | 
					
						
							|  |  |  | 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-12 14:44:59 +01:00
										 |  |  | import {TemplateLoader} from 'angular2/src/core/compiler/template_loader'; | 
					
						
							|  |  |  | import {TemplateResolver} from 'angular2/src/core/compiler/template_resolver'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-05 14:09:26 -08:00
										 |  |  | import {Injector} from 'angular2/di'; | 
					
						
							| 
									
										
										
										
											2015-02-12 14:44:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-05 14:09:26 -08:00
										 |  |  | import {DOM} from 'angular2/src/facade/dom'; | 
					
						
							| 
									
										
										
										
											2015-02-12 14:44:59 +01:00
										 |  |  | import {Map, MapWrapper} from 'angular2/src/facade/collection'; | 
					
						
							|  |  |  | import {Type, isPresent} from 'angular2/src/facade/lang'; | 
					
						
							| 
									
										
										
										
											2015-02-03 07:27:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 14:44:59 +01:00
										 |  |  | import {Component, Decorator, Template} from 'angular2/core'; | 
					
						
							| 
									
										
										
										
											2015-02-07 14:14:07 -08:00
										 |  |  | import {ControlGroupDirective, ControlNameDirective, | 
					
						
							|  |  |  |   ControlDirective, NewControlGroupDirective, | 
					
						
							|  |  |  |   Control, ControlGroup, ControlValueAccessor} from 'angular2/forms'; | 
					
						
							| 
									
										
										
										
											2015-02-04 11:45:33 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-03 07:27:09 -08:00
										 |  |  | export function main() { | 
					
						
							|  |  |  |   function detectChanges(view) { | 
					
						
							|  |  |  |     view.changeDetector.detectChanges(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function compile(componentType, template, context, callback) { | 
					
						
							| 
									
										
										
										
											2015-02-12 14:44:59 +01:00
										 |  |  |     var tplResolver = new FakeTemplateResolver(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-04 11:45:33 -08:00
										 |  |  |     var compiler = new Compiler(dynamicChangeDetection, | 
					
						
							| 
									
										
										
										
											2015-02-12 14:44:59 +01:00
										 |  |  |       new TemplateLoader(null), | 
					
						
							| 
									
										
										
										
											2015-02-04 11:45:33 -08:00
										 |  |  |       new DirectiveMetadataReader(), | 
					
						
							|  |  |  |       new Parser(new Lexer()), | 
					
						
							|  |  |  |       new CompilerCache(), | 
					
						
							| 
									
										
										
										
											2015-02-12 14:44:59 +01:00
										 |  |  |       new NativeShadowDomStrategy(), | 
					
						
							|  |  |  |       tplResolver | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     tplResolver.setTemplate(componentType, new Template({ | 
					
						
							|  |  |  |       inline: template, | 
					
						
							|  |  |  |       directives: [ControlGroupDirective, ControlNameDirective, ControlDirective, | 
					
						
							|  |  |  |         NewControlGroupDirective, WrappedValue] | 
					
						
							|  |  |  |     })); | 
					
						
							| 
									
										
										
										
											2015-02-03 07:27:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 14:44:59 +01:00
										 |  |  |     compiler.compile(componentType).then((pv) => { | 
					
						
							| 
									
										
										
										
											2015-02-09 15:11:31 +01:00
										 |  |  |       var view = pv.instantiate(null, null); | 
					
						
							| 
									
										
										
										
											2015-02-03 07:27:09 -08:00
										 |  |  |       view.hydrate(new Injector([]), null, context); | 
					
						
							|  |  |  |       detectChanges(view); | 
					
						
							|  |  |  |       callback(view); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe("integration tests", () => { | 
					
						
							|  |  |  |     it("should initialize DOM elements with the given form object", (done) => { | 
					
						
							|  |  |  |       var ctx = new MyComp(new ControlGroup({ | 
					
						
							|  |  |  |         "login": new Control("loginValue") | 
					
						
							|  |  |  |       })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       var t = `<div [control-group]="form">
 | 
					
						
							| 
									
										
										
										
											2015-02-07 14:14:07 -08:00
										 |  |  |                 <input type="text" control-name="login"> | 
					
						
							| 
									
										
										
										
											2015-02-03 07:27:09 -08:00
										 |  |  |               </div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       compile(MyComp, t, ctx, (view) => { | 
					
						
							|  |  |  |         var input = queryView(view, "input") | 
					
						
							|  |  |  |         expect(input.value).toEqual("loginValue"); | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("should update the control group values on DOM change", (done) => { | 
					
						
							|  |  |  |       var form = new ControlGroup({ | 
					
						
							|  |  |  |         "login": new Control("oldValue") | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       var ctx = new MyComp(form); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       var t = `<div [control-group]="form">
 | 
					
						
							| 
									
										
										
										
											2015-02-07 14:14:07 -08:00
										 |  |  |                 <input type="text" control-name="login"> | 
					
						
							| 
									
										
										
										
											2015-02-03 07:27:09 -08:00
										 |  |  |               </div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       compile(MyComp, t, ctx, (view) => { | 
					
						
							|  |  |  |         var input = queryView(view, "input") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         input.value = "updatedValue"; | 
					
						
							|  |  |  |         dispatchEvent(input, "change"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(form.value).toEqual({"login": "updatedValue"}); | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("should update DOM elements when rebinding the control group", (done) => { | 
					
						
							|  |  |  |       var form = new ControlGroup({ | 
					
						
							|  |  |  |         "login": new Control("oldValue") | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       var ctx = new MyComp(form); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       var t = `<div [control-group]="form">
 | 
					
						
							| 
									
										
										
										
											2015-02-07 14:14:07 -08:00
										 |  |  |                 <input type="text" control-name="login"> | 
					
						
							| 
									
										
										
										
											2015-02-03 07:27:09 -08:00
										 |  |  |               </div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       compile(MyComp, t, ctx, (view) => { | 
					
						
							|  |  |  |         ctx.form = new ControlGroup({ | 
					
						
							|  |  |  |           "login": new Control("newValue") | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         detectChanges(view); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var input = queryView(view, "input") | 
					
						
							|  |  |  |         expect(input.value).toEqual("newValue"); | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("should update DOM element when rebinding the control name", (done) => { | 
					
						
							|  |  |  |       var ctx = new MyComp(new ControlGroup({ | 
					
						
							|  |  |  |         "one": new Control("one"), | 
					
						
							|  |  |  |         "two": new Control("two") | 
					
						
							|  |  |  |       }), "one"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       var t = `<div [control-group]="form">
 | 
					
						
							| 
									
										
										
										
											2015-02-07 14:14:07 -08:00
										 |  |  |                 <input type="text" [control-name]="name"> | 
					
						
							| 
									
										
										
										
											2015-02-03 07:27:09 -08:00
										 |  |  |               </div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       compile(MyComp, t, ctx, (view) => { | 
					
						
							|  |  |  |         var input = queryView(view, "input") | 
					
						
							|  |  |  |         expect(input.value).toEqual("one"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ctx.name = "two"; | 
					
						
							|  |  |  |         detectChanges(view); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(input.value).toEqual("two"); | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-02-04 11:45:33 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-07 14:14:07 -08:00
										 |  |  |     describe("different control types", () => { | 
					
						
							|  |  |  |       it("should support type=checkbox", (done) => { | 
					
						
							|  |  |  |         var ctx = new MyComp(new ControlGroup({"checkbox": new Control(true)})); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var t = `<div [control-group]="form">
 | 
					
						
							|  |  |  |                   <input type="checkbox" control-name="checkbox"> | 
					
						
							|  |  |  |                 </div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         compile(MyComp, t, ctx, (view) => { | 
					
						
							|  |  |  |           var input = queryView(view, "input") | 
					
						
							|  |  |  |           expect(input.checked).toBe(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           input.checked = false; | 
					
						
							|  |  |  |           dispatchEvent(input, "change"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(ctx.form.value).toEqual({"checkbox" : false}); | 
					
						
							|  |  |  |           done(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should support custom value accessors", (done) => { | 
					
						
							|  |  |  |         var ctx = new MyComp(new ControlGroup({"name": new Control("aa")})); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var t = `<div [control-group]="form">
 | 
					
						
							|  |  |  |                   <input type="text" control-name="name" wrapped-value> | 
					
						
							|  |  |  |                 </div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         compile(MyComp, t, ctx, (view) => { | 
					
						
							|  |  |  |           var input = queryView(view, "input") | 
					
						
							|  |  |  |           expect(input.value).toEqual("!aa!"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           input.value = "!bb!"; | 
					
						
							|  |  |  |           dispatchEvent(input, "change"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(ctx.form.value).toEqual({"name" : "bb"}); | 
					
						
							|  |  |  |           done(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-04 11:45:33 -08:00
										 |  |  |     describe("declarative forms", () => { | 
					
						
							|  |  |  |       it("should initialize dom elements", (done) => { | 
					
						
							|  |  |  |         var t = `<div [new-control-group]="{'login': 'loginValue', 'password':'passValue'}">
 | 
					
						
							| 
									
										
										
										
											2015-02-07 14:14:07 -08:00
										 |  |  |                   <input type="text" id="login" control="login"> | 
					
						
							|  |  |  |                   <input type="password" id="password" control="password"> | 
					
						
							| 
									
										
										
										
											2015-02-04 11:45:33 -08:00
										 |  |  |                 </div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         compile(MyComp, t, new MyComp(), (view) => { | 
					
						
							|  |  |  |           var loginInput = queryView(view, "#login") | 
					
						
							|  |  |  |           expect(loginInput.value).toEqual("loginValue"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           var passInput = queryView(view, "#password") | 
					
						
							|  |  |  |           expect(passInput.value).toEqual("passValue"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           done(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should update the control group values on DOM change", (done) => { | 
					
						
							| 
									
										
										
										
											2015-02-07 14:14:07 -08:00
										 |  |  |         var t = `<div #form [new-control-group]="{'login': 'loginValue'}">
 | 
					
						
							|  |  |  |                   <input type="text" control="login"> | 
					
						
							| 
									
										
										
										
											2015-02-04 11:45:33 -08:00
										 |  |  |                 </div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         compile(MyComp, t, new MyComp(), (view) => { | 
					
						
							|  |  |  |           var input = queryView(view, "input") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           input.value = "updatedValue"; | 
					
						
							|  |  |  |           dispatchEvent(input, "change"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-07 14:14:07 -08:00
										 |  |  |           var form = view.contextWithLocals.get("form"); | 
					
						
							|  |  |  |           expect(form.value).toEqual({'login': 'updatedValue'}); | 
					
						
							| 
									
										
										
										
											2015-02-04 11:45:33 -08:00
										 |  |  |           done(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-02-03 07:27:09 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 14:44:59 +01:00
										 |  |  | @Component({selector: "my-comp"}) | 
					
						
							| 
									
										
										
										
											2015-02-03 07:27:09 -08:00
										 |  |  | class MyComp { | 
					
						
							|  |  |  |   form:ControlGroup; | 
					
						
							|  |  |  |   name:string; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-04 11:45:33 -08:00
										 |  |  |   constructor(form = null, name = null) { | 
					
						
							| 
									
										
										
										
											2015-02-03 07:27:09 -08:00
										 |  |  |     this.form = form; | 
					
						
							|  |  |  |     this.name = name; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-02-07 14:14:07 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class WrappedValueAccessor extends ControlValueAccessor { | 
					
						
							|  |  |  |   readValue(el){ | 
					
						
							|  |  |  |     return el.value.substring(1, el.value.length - 1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   writeValue(el, value):void { | 
					
						
							|  |  |  |     el.value = `!${value}!`; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Decorator({ | 
					
						
							|  |  |  |   selector:'[wrapped-value]' | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | class WrappedValue { | 
					
						
							|  |  |  |   constructor(cd:ControlNameDirective) { | 
					
						
							|  |  |  |     cd.valueAccessor = new WrappedValueAccessor(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-02-12 14:44:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FakeTemplateResolver extends TemplateResolver { | 
					
						
							|  |  |  |   _cmpTemplates: Map; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor() { | 
					
						
							|  |  |  |     super(); | 
					
						
							|  |  |  |     this._cmpTemplates = MapWrapper.create(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   setTemplate(component: Type, template: Template) { | 
					
						
							|  |  |  |     MapWrapper.set(this._cmpTemplates, component, template); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   resolve(component: Type): Template { | 
					
						
							|  |  |  |     var override = MapWrapper.get(this._cmpTemplates, component); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (isPresent(override)) { | 
					
						
							|  |  |  |       return override; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return super.resolve(component); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |