| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   xit, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   afterEach, | 
					
						
							| 
									
										
										
										
											2015-11-02 09:59:40 -08:00
										 |  |  |   fakeAsync, | 
					
						
							|  |  |  |   tick, | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |   el | 
					
						
							| 
									
										
										
										
											2015-10-13 00:29:13 -07:00
										 |  |  | } from 'angular2/testing_internal'; | 
					
						
							| 
									
										
										
										
											2015-11-18 15:55:43 -08:00
										 |  |  | import {ControlGroup, Control, Validators, AbstractControl, ControlArray} from 'angular2/common'; | 
					
						
							| 
									
										
										
										
											2015-11-06 17:34:07 -08:00
										 |  |  | import {PromiseWrapper} from 'angular2/src/facade/promise'; | 
					
						
							|  |  |  | import {EventEmitter, ObservableWrapper, TimerWrapper} from 'angular2/src/facade/async'; | 
					
						
							|  |  |  | import {CONST_EXPR} from 'angular2/src/facade/lang'; | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |   function validator(key: string, error: any) { | 
					
						
							| 
									
										
										
											
												feat(validators): Allow errors at both the group/array level or their children
Allow ControlGroups and ControlArrays to contain errors from their level, and
errors from their children. [Design Doc](https://docs.google.com/document/d/1EnJ3-_iFpVKFz1ifN1LkXSGQ7h3A72OQGry2g8eo7IA/edit?pli=1#heading=h.j53rt81eegm4)
BREAKING CHANGE: errors format has changed from validators. Now errors from
a control or an array's children are prefixed with 'controls' while errors
from the object itself are left at the root level.
Example:
Given a Control group as follows:
var group = new ControlGroup({
  login: new Control("", required),
  password: new Control("", required),
  passwordConfirm: new Control("", required)
});
Before:
group.errors
{
  login: {required: true},
  password: {required: true},
  passwordConfirm: {required: true},
}
After:
group.errors
{
  controls: {
    login: {required: true},
    password: {required: true},
    passwordConfirm: {required: true},
  }
}
											
										 
											2015-10-16 16:13:14 -07:00
										 |  |  |     return function(c: AbstractControl) { | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  |       var r = {}; | 
					
						
							|  |  |  |       r[key] = error; | 
					
						
							|  |  |  |       return r; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe("Validators", () => { | 
					
						
							|  |  |  |     describe("required", () => { | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |       it("should error on an empty string", | 
					
						
							|  |  |  |          () => { expect(Validators.required(new Control(""))).toEqual({"required": true}); }); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |       it("should error on null", | 
					
						
							|  |  |  |          () => { expect(Validators.required(new Control(null))).toEqual({"required": true}); }); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |       it("should not error on a non-empty string", | 
					
						
							|  |  |  |          () => { expect(Validators.required(new Control("not empty"))).toEqual(null); }); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-13 14:38:13 -07:00
										 |  |  |     describe("minLength", () => { | 
					
						
							|  |  |  |       it("should not error on an empty string", | 
					
						
							|  |  |  |          () => { expect(Validators.minLength(2)(new Control(""))).toEqual(null); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should not error on null", | 
					
						
							|  |  |  |          () => { expect(Validators.minLength(2)(new Control(null))).toEqual(null); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should not error on valid strings", | 
					
						
							|  |  |  |          () => { expect(Validators.minLength(2)(new Control("aa"))).toEqual(null); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should error on short strings", () => { | 
					
						
							|  |  |  |         expect(Validators.minLength(2)(new Control("a"))) | 
					
						
							|  |  |  |             .toEqual({"minlength": {"requiredLength": 2, "actualLength": 1}}); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe("maxLength", () => { | 
					
						
							|  |  |  |       it("should not error on an empty string", | 
					
						
							|  |  |  |          () => { expect(Validators.maxLength(2)(new Control(""))).toEqual(null); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should not error on null", | 
					
						
							|  |  |  |          () => { expect(Validators.maxLength(2)(new Control(null))).toEqual(null); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should not error on valid strings", | 
					
						
							|  |  |  |          () => { expect(Validators.maxLength(2)(new Control("aa"))).toEqual(null); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should error on short strings", () => { | 
					
						
							|  |  |  |         expect(Validators.maxLength(2)(new Control("aaa"))) | 
					
						
							|  |  |  |             .toEqual({"maxlength": {"requiredLength": 2, "actualLength": 3}}); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  |     describe("compose", () => { | 
					
						
							| 
									
										
										
										
											2015-11-02 09:59:40 -08:00
										 |  |  |       it("should return null when given null", | 
					
						
							|  |  |  |          () => { expect(Validators.compose(null)).toBe(null); }); | 
					
						
							| 
									
										
										
										
											2015-09-02 10:24:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  |       it("should collect errors from all the validators", () => { | 
					
						
							| 
									
										
										
										
											2015-03-19 14:21:40 -07:00
										 |  |  |         var c = Validators.compose([validator("a", true), validator("b", true)]); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |         expect(c(new Control(""))).toEqual({"a": true, "b": true}); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should run validators left to right", () => { | 
					
						
							| 
									
										
										
										
											2015-03-19 14:21:40 -07:00
										 |  |  |         var c = Validators.compose([validator("a", 1), validator("a", 2)]); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |         expect(c(new Control(""))).toEqual({"a": 2}); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it("should return null when no errors", () => { | 
					
						
							| 
									
										
										
										
											2015-03-19 14:21:40 -07:00
										 |  |  |         var c = Validators.compose([Validators.nullValidator, Validators.nullValidator]); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  |         expect(c(new Control(""))).toEqual(null); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it("should ignore nulls", () => { | 
					
						
							|  |  |  |         var c = Validators.compose([null, Validators.required]); | 
					
						
							|  |  |  |         expect(c(new Control(""))).toEqual({"required": true}); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-11-02 09:59:40 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     describe("composeAsync", () => { | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  |       function asyncValidator(expected, response) { | 
					
						
							| 
									
										
										
										
											2015-11-02 09:59:40 -08:00
										 |  |  |         return (c) => { | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  |           var emitter = new EventEmitter(); | 
					
						
							| 
									
										
										
										
											2015-11-02 09:59:40 -08:00
										 |  |  |           var res = c.value != expected ? response : null; | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           PromiseWrapper.scheduleMicrotask(() => { | 
					
						
							| 
									
										
										
										
											2015-11-15 23:58:59 -08:00
										 |  |  |             ObservableWrapper.callEmit(emitter, res); | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  |             // this is required because of a bug in ObservableWrapper
 | 
					
						
							| 
									
										
										
										
											2015-11-15 23:58:59 -08:00
										 |  |  |             // where callComplete can fire before callEmit
 | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  |             // remove this one the bug is fixed
 | 
					
						
							|  |  |  |             TimerWrapper.setTimeout(() => { ObservableWrapper.callComplete(emitter); }, 0); | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |           return emitter; | 
					
						
							| 
									
										
										
										
											2015-11-02 09:59:40 -08:00
										 |  |  |         }; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should return null when given null", | 
					
						
							|  |  |  |          () => { expect(Validators.composeAsync(null)).toEqual(null); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should collect errors from all the validators", fakeAsync(() => { | 
					
						
							|  |  |  |            var c = Validators.composeAsync([ | 
					
						
							|  |  |  |              asyncValidator("expected", {"one": true}), | 
					
						
							|  |  |  |              asyncValidator("expected", {"two": true}) | 
					
						
							|  |  |  |            ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            var value = null; | 
					
						
							|  |  |  |            c(new Control("invalid")).then(v => value = v); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            tick(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            expect(value).toEqual({"one": true, "two": true}); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should return null when no errors", fakeAsync(() => { | 
					
						
							|  |  |  |            var c = Validators.composeAsync([asyncValidator("expected", {"one": true})]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            var value = null; | 
					
						
							|  |  |  |            c(new Control("expected")).then(v => value = v); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            tick(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            expect(value).toEqual(null); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it("should ignore nulls", fakeAsync(() => { | 
					
						
							|  |  |  |            var c = Validators.composeAsync([asyncValidator("expected", {"one": true}), null]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            var value = null; | 
					
						
							|  |  |  |            c(new Control("invalid")).then(v => value = v); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            tick(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            expect(value).toEqual({"one": true}); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
											
												feat(validators): Allow errors at both the group/array level or their children
Allow ControlGroups and ControlArrays to contain errors from their level, and
errors from their children. [Design Doc](https://docs.google.com/document/d/1EnJ3-_iFpVKFz1ifN1LkXSGQ7h3A72OQGry2g8eo7IA/edit?pli=1#heading=h.j53rt81eegm4)
BREAKING CHANGE: errors format has changed from validators. Now errors from
a control or an array's children are prefixed with 'controls' while errors
from the object itself are left at the root level.
Example:
Given a Control group as follows:
var group = new ControlGroup({
  login: new Control("", required),
  password: new Control("", required),
  passwordConfirm: new Control("", required)
});
Before:
group.errors
{
  login: {required: true},
  password: {required: true},
  passwordConfirm: {required: true},
}
After:
group.errors
{
  controls: {
    login: {required: true},
    password: {required: true},
    passwordConfirm: {required: true},
  }
}
											
										 
											2015-10-16 16:13:14 -07:00
										 |  |  | } |