angular-cn/modules/angular2/test/forms/validators_spec.ts
vsavkin 79994b2abf refactor(forms): use multibindings instead of query to get a list of validators
BREAKING CHANGE

Before:

@Directive({selector: '[credit-card]', bindings: [new Binding(NgValidator, {toAlias: forwardRef(() => CreditCardValidator)})]})
class CreditCardValidator {
  get validator() { return CreditCardValidator.validate; }
  static validate(c): StringMap<string, boolean> {...}
}

After:

function creditCardValidator(c): StringMap<string, boolean> {...}
@Directive({selector: '[credit-card]', bindings: [new Binding(NG_VALIDATORS, {toValue: creditCardValidator, multi: true})]})
class CreditCardValidator {}
2015-09-03 15:18:18 +00:00

79 lines
2.5 KiB
TypeScript

import {
ddescribe,
describe,
it,
iit,
xit,
expect,
beforeEach,
afterEach,
el
} from 'angular2/test_lib';
import {ControlGroup, Control, Validators} from 'angular2/forms';
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(Validators.required(new Control(""))).toEqual({"required": true}); });
it("should error on null",
() => { expect(Validators.required(new Control(null))).toEqual({"required": true}); });
it("should not error on a non-empty string",
() => { expect(Validators.required(new Control("not empty"))).toEqual(null); });
});
describe("compose", () => {
it("should return a null validator when given null",
() => { expect(Validators.compose(null)).toBe(Validators.nullValidator); });
it("should collect errors from all the validators", () => {
var c = Validators.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 = Validators.compose([validator("a", 1), validator("a", 2)]);
expect(c(new Control(""))).toEqual({"a": 2});
});
it("should return null when no errors", () => {
var c = Validators.compose([Validators.nullValidator, Validators.nullValidator]);
expect(c(new Control(""))).toEqual(null);
});
});
describe("controlGroupValidator", () => {
it("should collect errors from the child controls", () => {
var one = new Control("one", validator("a", true));
var two = new Control("one", validator("b", true));
var g = new ControlGroup({"one": one, "two": two});
expect(Validators.group(g)).toEqual({"a": [one], "b": [two]});
});
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});
expect(Validators.group(g)).toEqual({"a": [one]});
});
it("should return null when no errors", () => {
var g = new ControlGroup({"one": new Control("one")});
expect(Validators.group(g)).toEqual(null);
});
});
});
}