2015-05-22 15:32:49 -04:00
|
|
|
import {
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
it,
|
|
|
|
iit,
|
|
|
|
xit,
|
|
|
|
expect,
|
|
|
|
beforeEach,
|
|
|
|
afterEach,
|
|
|
|
el
|
|
|
|
} from 'angular2/test_lib';
|
2015-03-19 17:21:40 -04:00
|
|
|
import {ControlGroup, Control, Validators} from 'angular2/forms';
|
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) {
|
|
|
|
return function(c: Control) {
|
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
|
|
|
});
|
|
|
|
|
|
|
|
describe("compose", () => {
|
|
|
|
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-02-11 14:10:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("controlGroupValidator", () => {
|
|
|
|
it("should collect errors from the child controls", () => {
|
2015-02-25 15:54:27 -05:00
|
|
|
var one = new Control("one", validator("a", true));
|
|
|
|
var two = new Control("one", validator("b", true));
|
2015-05-22 15:32:49 -04:00
|
|
|
var g = new ControlGroup({"one": one, "two": two});
|
2015-02-11 14:10:31 -05:00
|
|
|
|
2015-05-22 15:32:49 -04:00
|
|
|
expect(Validators.group(g)).toEqual({"a": [one], "b": [two]});
|
2015-02-11 14:10:31 -05:00
|
|
|
});
|
|
|
|
|
2015-02-25 15:54:27 -05:00
|
|
|
it("should not include controls that have no errors", () => {
|
|
|
|
var one = new Control("one", validator("a", true));
|
|
|
|
var two = new Control("two");
|
2015-05-22 15:32:49 -04:00
|
|
|
var g = new ControlGroup({"one": one, "two": two});
|
2015-02-11 14:10:31 -05:00
|
|
|
|
2015-05-22 15:32:49 -04:00
|
|
|
expect(Validators.group(g)).toEqual({"a": [one]});
|
2015-02-11 14:10:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return null when no errors", () => {
|
2015-05-22 15:32:49 -04:00
|
|
|
var g = new ControlGroup({"one": new Control("one")});
|
2015-02-11 14:10:31 -05:00
|
|
|
|
2015-03-19 17:21:40 -04:00
|
|
|
expect(Validators.group(g)).toEqual(null);
|
2015-02-11 14:10:31 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|