| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |   fakeAsync, | 
					
						
							|  |  |  |   flushMicrotasks, | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |   it, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   xit, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   afterEach, | 
					
						
							|  |  |  |   el, | 
					
						
							|  |  |  |   AsyncTestCompleter, | 
					
						
							|  |  |  |   inject | 
					
						
							|  |  |  | } from 'angular2/test_lib'; | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import {QueryList} from 'angular2/angular2'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   ControlGroup, | 
					
						
							|  |  |  |   Control, | 
					
						
							| 
									
										
										
										
											2015-06-10 13:51:44 -07:00
										 |  |  |   NgControlName, | 
					
						
							|  |  |  |   NgControlGroup, | 
					
						
							|  |  |  |   NgFormModel, | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |   ControlValueAccessor, | 
					
						
							|  |  |  |   Validators, | 
					
						
							| 
									
										
										
										
											2015-06-10 13:51:44 -07:00
										 |  |  |   NgForm, | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  |   NgModel, | 
					
						
							|  |  |  |   NgFormControl, | 
					
						
							|  |  |  |   NgRequiredValidator | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  | } from 'angular2/forms'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DummyControlValueAccessor implements ControlValueAccessor { | 
					
						
							|  |  |  |   writtenValue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   registerOnChange(fn) {} | 
					
						
							| 
									
										
										
										
											2015-06-02 08:41:33 -07:00
										 |  |  |   registerOnTouched(fn) {} | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   writeValue(obj: any): void { this.writtenValue = obj; } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-05-11 12:00:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe("Form Directives", () => { | 
					
						
							| 
									
										
										
										
											2015-06-10 13:51:44 -07:00
										 |  |  |     describe("NgFormModel", () => { | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |       var form; | 
					
						
							|  |  |  |       var formModel; | 
					
						
							|  |  |  |       var loginControlDir; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2015-06-10 13:51:44 -07:00
										 |  |  |         form = new NgFormModel(); | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |         formModel = new ControlGroup({"login": new Control(null)}); | 
					
						
							|  |  |  |         form.form = formModel; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  |         loginControlDir = new NgControlName(form, new QueryList<any>()); | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |         loginControlDir.name = "login"; | 
					
						
							|  |  |  |         loginControlDir.valueAccessor = new DummyControlValueAccessor(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       describe("addControl", () => { | 
					
						
							|  |  |  |         it("should throw when no control found", () => { | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  |           var dir = new NgControlName(form, null); | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |           dir.name = "invalidName"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(() => form.addControl(dir)) | 
					
						
							|  |  |  |               .toThrowError(new RegExp("Cannot find control 'invalidName'")); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it("should throw when no value accessor", () => { | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  |           var dir = new NgControlName(form, null); | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |           dir.name = "login"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(() => form.addControl(dir)) | 
					
						
							|  |  |  |               .toThrowError(new RegExp("No value accessor for 'login'")); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it("should set up validator", () => { | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  |           loginControlDir.ngValidators.reset([new NgRequiredValidator()]); | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(formModel.find(["login"]).valid).toBe(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           // this will add the required validator and recalculate the validity
 | 
					
						
							|  |  |  |           form.addControl(loginControlDir); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(formModel.find(["login"]).valid).toBe(false); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it("should write value to the DOM", () => { | 
					
						
							| 
									
										
										
										
											2015-06-01 10:41:50 -07:00
										 |  |  |           formModel.find(["login"]).updateValue("initValue"); | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           form.addControl(loginControlDir); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect((<any>loginControlDir.valueAccessor).writtenValue).toEqual("initValue"); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it("should add the directive to the list of directives included in the form", () => { | 
					
						
							|  |  |  |           form.addControl(loginControlDir); | 
					
						
							|  |  |  |           expect(form.directives).toEqual([loginControlDir]); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-05-11 12:00:56 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |       describe("removeControl", () => { | 
					
						
							|  |  |  |         it("should remove the directive to the list of directives included in the form", () => { | 
					
						
							|  |  |  |           form.addControl(loginControlDir); | 
					
						
							|  |  |  |           form.removeControl(loginControlDir); | 
					
						
							|  |  |  |           expect(form.directives).toEqual([]); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       describe("onChange", () => { | 
					
						
							|  |  |  |         it("should update dom values of all the directives", () => { | 
					
						
							|  |  |  |           form.addControl(loginControlDir); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 10:41:50 -07:00
										 |  |  |           formModel.find(["login"]).updateValue("new value"); | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           form.onChange(null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect((<any>loginControlDir.valueAccessor).writtenValue).toEqual("new value"); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-10 13:51:44 -07:00
										 |  |  |     describe("NgForm", () => { | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |       var form; | 
					
						
							|  |  |  |       var formModel; | 
					
						
							|  |  |  |       var loginControlDir; | 
					
						
							|  |  |  |       var personControlGroupDir; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2015-06-10 13:51:44 -07:00
										 |  |  |         form = new NgForm(); | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |         formModel = form.form; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-10 13:51:44 -07:00
										 |  |  |         personControlGroupDir = new NgControlGroup(form); | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |         personControlGroupDir.name = "person"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  |         loginControlDir = new NgControlName(personControlGroupDir, null); | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |         loginControlDir.name = "login"; | 
					
						
							|  |  |  |         loginControlDir.valueAccessor = new DummyControlValueAccessor(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       describe("addControl & addControlGroup", () => { | 
					
						
							|  |  |  |         it("should create a control with the given name", fakeAsync(() => { | 
					
						
							|  |  |  |              form.addControlGroup(personControlGroupDir); | 
					
						
							|  |  |  |              form.addControl(loginControlDir); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              flushMicrotasks(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              expect(formModel.find(["person", "login"])).not.toBeNull; | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // should update the form's value and validity
 | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       describe("removeControl & removeControlGroup", () => { | 
					
						
							|  |  |  |         it("should remove control", fakeAsync(() => { | 
					
						
							|  |  |  |              form.addControlGroup(personControlGroupDir); | 
					
						
							|  |  |  |              form.addControl(loginControlDir); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              form.removeControlGroup(personControlGroupDir); | 
					
						
							|  |  |  |              form.removeControl(loginControlDir); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              flushMicrotasks(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              expect(formModel.find(["person"])).toBeNull(); | 
					
						
							|  |  |  |              expect(formModel.find(["person", "login"])).toBeNull(); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // should update the form's value and validity
 | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-10 13:51:44 -07:00
										 |  |  |     describe("NgFormControl", () => { | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |       var controlDir; | 
					
						
							|  |  |  |       var control; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  |         controlDir = new NgFormControl(new QueryList<any>()); | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |         controlDir.valueAccessor = new DummyControlValueAccessor(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         control = new Control(null); | 
					
						
							| 
									
										
										
										
											2015-06-03 11:56:01 -07:00
										 |  |  |         controlDir.form = control; | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should set up validator", () => { | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  |         controlDir.ngValidators.reset([new NgRequiredValidator()]); | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(control.valid).toBe(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // this will add the required validator and recalculate the validity
 | 
					
						
							| 
									
										
										
										
											2015-06-01 10:41:50 -07:00
										 |  |  |         controlDir.onChange({}); | 
					
						
							| 
									
										
										
										
											2015-05-11 12:00:56 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |         expect(control.valid).toBe(false); | 
					
						
							| 
									
										
										
										
											2015-05-11 12:00:56 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     describe("NgModel", () => { | 
					
						
							|  |  |  |       var ngModel; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       beforeEach(() => { | 
					
						
							|  |  |  |         ngModel = new NgModel(new QueryList<any>()); | 
					
						
							|  |  |  |         ngModel.valueAccessor = new DummyControlValueAccessor(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should set up validator", () => { | 
					
						
							|  |  |  |         ngModel.ngValidators.reset([new NgRequiredValidator()]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(ngModel.control.valid).toBe(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // this will add the required validator and recalculate the validity
 | 
					
						
							|  |  |  |         ngModel.onChange({}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(ngModel.control.valid).toBe(false); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-05-11 12:00:56 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | } |