2015-05-22 15:32:49 -04:00
|
|
|
import {
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
it,
|
|
|
|
iit,
|
|
|
|
xit,
|
|
|
|
expect,
|
|
|
|
beforeEach,
|
|
|
|
afterEach,
|
2015-11-02 12:59:40 -05:00
|
|
|
fakeAsync,
|
|
|
|
tick,
|
2015-05-22 15:32:49 -04:00
|
|
|
el
|
2015-10-13 03:29:13 -04:00
|
|
|
} from 'angular2/testing_internal';
|
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 19:13:14 -04:00
|
|
|
import {ControlGroup, Control, Validators, AbstractControl, ControlArray} from 'angular2/core';
|
2015-11-06 20:34:07 -05: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 14:10:31 -05:00
|
|
|
|
|
|
|
export function main() {
|
2015-05-22 15:32:49 -04: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 19:13:14 -04:00
|
|
|
return function(c: AbstractControl) {
|
2015-02-11 14:10:31 -05:00
|
|
|
var r = {};
|
|
|
|
r[key] = error;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("Validators", () => {
|
|
|
|
describe("required", () => {
|
2015-05-22 15:32:49 -04:00
|
|
|
it("should error on an empty string",
|
|
|
|
() => { expect(Validators.required(new Control(""))).toEqual({"required": true}); });
|
2015-02-11 14:10:31 -05:00
|
|
|
|
2015-05-22 15:32:49 -04:00
|
|
|
it("should error on null",
|
|
|
|
() => { expect(Validators.required(new Control(null))).toEqual({"required": true}); });
|
2015-02-11 14:10:31 -05:00
|
|
|
|
2015-05-22 15:32:49 -04:00
|
|
|
it("should not error on a non-empty string",
|
|
|
|
() => { expect(Validators.required(new Control("not empty"))).toEqual(null); });
|
2015-02-11 14:10:31 -05:00
|
|
|
});
|
|
|
|
|
2015-10-13 17:38:13 -04: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 14:10:31 -05:00
|
|
|
describe("compose", () => {
|
2015-11-02 12:59:40 -05:00
|
|
|
it("should return null when given null",
|
|
|
|
() => { expect(Validators.compose(null)).toBe(null); });
|
2015-09-02 13:24:22 -04:00
|
|
|
|
2015-02-11 14:10:31 -05:00
|
|
|
it("should collect errors from all the validators", () => {
|
2015-03-19 17:21:40 -04:00
|
|
|
var c = Validators.compose([validator("a", true), validator("b", true)]);
|
2015-05-22 15:32:49 -04:00
|
|
|
expect(c(new Control(""))).toEqual({"a": true, "b": true});
|
2015-02-11 14:10:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should run validators left to right", () => {
|
2015-03-19 17:21:40 -04:00
|
|
|
var c = Validators.compose([validator("a", 1), validator("a", 2)]);
|
2015-05-22 15:32:49 -04:00
|
|
|
expect(c(new Control(""))).toEqual({"a": 2});
|
2015-02-11 14:10:31 -05:00
|
|
|
});
|
2015-02-24 14:59:10 -05:00
|
|
|
|
|
|
|
it("should return null when no errors", () => {
|
2015-03-19 17:21:40 -04:00
|
|
|
var c = Validators.compose([Validators.nullValidator, Validators.nullValidator]);
|
2015-02-24 14:59:10 -05:00
|
|
|
expect(c(new Control(""))).toEqual(null);
|
|
|
|
});
|
2015-10-29 20:45:24 -04:00
|
|
|
|
|
|
|
it("should ignore nulls", () => {
|
|
|
|
var c = Validators.compose([null, Validators.required]);
|
|
|
|
expect(c(new Control(""))).toEqual({"required": true});
|
|
|
|
});
|
2015-02-11 14:10:31 -05:00
|
|
|
});
|
2015-11-02 12:59:40 -05:00
|
|
|
|
|
|
|
describe("composeAsync", () => {
|
2015-11-05 20:18:16 -05:00
|
|
|
function asyncValidator(expected, response) {
|
2015-11-02 12:59:40 -05:00
|
|
|
return (c) => {
|
2015-11-05 20:18:16 -05:00
|
|
|
var emitter = new EventEmitter();
|
2015-11-02 12:59:40 -05:00
|
|
|
var res = c.value != expected ? response : null;
|
2015-11-05 20:18:16 -05:00
|
|
|
|
|
|
|
PromiseWrapper.scheduleMicrotask(() => {
|
2015-11-16 02:58:59 -05:00
|
|
|
ObservableWrapper.callEmit(emitter, res);
|
2015-11-05 20:18:16 -05:00
|
|
|
// this is required because of a bug in ObservableWrapper
|
2015-11-16 02:58:59 -05:00
|
|
|
// where callComplete can fire before callEmit
|
2015-11-05 20:18:16 -05:00
|
|
|
// remove this one the bug is fixed
|
|
|
|
TimerWrapper.setTimeout(() => { ObservableWrapper.callComplete(emitter); }, 0);
|
|
|
|
});
|
|
|
|
return emitter;
|
2015-11-02 12:59:40 -05: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 14:10:31 -05: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 19:13:14 -04:00
|
|
|
}
|