| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  | import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, el} from 'angular2/test_lib'; | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  | import {ControlGroup, Control, required, compose, controlGroupValidator, nullValidator} from 'angular2/forms'; | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   function validator(key:string, error:any){ | 
					
						
							|  |  |  |     return function(c:Control) { | 
					
						
							|  |  |  |       var r = {}; | 
					
						
							|  |  |  |       r[key] = error; | 
					
						
							|  |  |  |       return r; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe("Validators", () => { | 
					
						
							|  |  |  |     describe("required", () => { | 
					
						
							|  |  |  |       it("should error on an empty string", () => { | 
					
						
							|  |  |  |         expect(required(new Control(""))).toEqual({"required" : true}); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should error on null", () => { | 
					
						
							|  |  |  |         expect(required(new Control(null))).toEqual({"required" : true}); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should not error on a non-empty string", () => { | 
					
						
							|  |  |  |         expect(required(new Control("not empty"))).toEqual(null); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe("compose", () => { | 
					
						
							|  |  |  |       it("should collect errors from all the validators", () => { | 
					
						
							|  |  |  |         var c = compose([validator("a", true), validator("b", true)]); | 
					
						
							|  |  |  |         expect(c(new Control(""))).toEqual({"a" : true, "b" : true}); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should run validators left to right", () => { | 
					
						
							|  |  |  |         var c = compose([validator("a", 1), validator("a", 2)]); | 
					
						
							|  |  |  |         expect(c(new Control(""))).toEqual({"a" : 2}); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it("should return null when no errors", () => { | 
					
						
							|  |  |  |         var c = compose([nullValidator, nullValidator]); | 
					
						
							|  |  |  |         expect(c(new Control(""))).toEqual(null); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe("controlGroupValidator", () => { | 
					
						
							|  |  |  |       it("should collect errors from the child controls", () => { | 
					
						
							| 
									
										
										
										
											2015-02-25 12:54:27 -08:00
										 |  |  |         var one = new Control("one", validator("a", true)); | 
					
						
							|  |  |  |         var two = new Control("one", validator("b", true)); | 
					
						
							|  |  |  |         var g = new ControlGroup({"one" : one, "two" : two}); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(controlGroupValidator(g)).toEqual({ | 
					
						
							| 
									
										
										
										
											2015-02-25 12:54:27 -08:00
										 |  |  |           "a" : [one], | 
					
						
							|  |  |  |           "b" : [two] | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 12:54:27 -08:00
										 |  |  |       it("should not include controls that have no errors", () => { | 
					
						
							|  |  |  |         var one = new Control("one", validator("a", true)); | 
					
						
							|  |  |  |         var two = new Control("two"); | 
					
						
							|  |  |  |         var g = new ControlGroup({"one" : one, "two" : two}); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(controlGroupValidator(g)).toEqual({ | 
					
						
							| 
									
										
										
										
											2015-02-25 12:54:27 -08:00
										 |  |  |           "a": [one] | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should return null when no errors", () => { | 
					
						
							|  |  |  |         var g = new ControlGroup({ | 
					
						
							|  |  |  |           "one" : new Control("one") | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(controlGroupValidator(g)).toEqual(null); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |