refactor(forms): wrapped all validators into the Validator class

This commit is contained in:
vsavkin
2015-03-19 14:21:40 -07:00
parent 41b53e71e1
commit a12dc7d75a
8 changed files with 69 additions and 72 deletions
+9 -10
View File
@@ -1,6 +1,5 @@
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, el} from 'angular2/test_lib';
import {Control, FormBuilder} from 'angular2/forms';
import * as validations from 'angular2/forms';
import {Control, FormBuilder, Validators} from 'angular2/forms';
export function main() {
describe("Form Builder", () => {
@@ -21,21 +20,21 @@ export function main() {
it("should create controls from an array", () => {
var g = b.group({
"login": ["some value"],
"password": ["some value", validations.required]
"password": ["some value", Validators.required]
});
expect(g.controls["login"].value).toEqual("some value");
expect(g.controls["password"].value).toEqual("some value");
expect(g.controls["password"].validator).toEqual(validations.required);
expect(g.controls["password"].validator).toEqual(Validators.required);
});
it("should use controls", () => {
var g = b.group({
"login": b.control("some value", validations.required)
"login": b.control("some value", Validators.required)
});
expect(g.controls["login"].value).toEqual("some value");
expect(g.controls["login"].validator).toBe(validations.required);
expect(g.controls["login"].validator).toBe(Validators.required);
});
it("should create groups with optional controls", () => {
@@ -49,17 +48,17 @@ export function main() {
it("should create groups with a custom validator", () => {
var g = b.group({
"login": "some value"
}, {"validator": validations.nullValidator});
}, {"validator": Validators.nullValidator});
expect(g.validator).toBe(validations.nullValidator);
expect(g.validator).toBe(Validators.nullValidator);
});
it("should use default validators when no validators are provided", () => {
var g = b.group({
"login": "some value"
});
expect(g.controls["login"].validator).toBe(validations.nullValidator);
expect(g.validator).toBe(validations.controlGroupValidator);
expect(g.controls["login"].validator).toBe(Validators.nullValidator);
expect(g.validator).toBe(Validators.group);
});
});
}
+2 -4
View File
@@ -31,9 +31,7 @@ import {Injector} from 'angular2/di';
import {Component, Decorator, Template} from 'angular2/angular2';
import {ControlGroupDirective, ControlDirective, Control, ControlGroup, OptionalControl,
ControlValueAccessor, RequiredValidatorDirective} from 'angular2/forms';
import * as validators from 'angular2/src/forms/validators';
ControlValueAccessor, RequiredValidatorDirective, Validators} from 'angular2/forms';
export function main() {
function detectChanges(view) {
@@ -261,7 +259,7 @@ export function main() {
}));
it("should use validators defined in the model", inject([AsyncTestCompleter], (async) => {
var form = new ControlGroup({"login": new Control("aa", validators.required)});
var form = new ControlGroup({"login": new Control("aa", Validators.required)});
var ctx = new MyComp(form);
var t = `<div [control-group]="form">
+10 -11
View File
@@ -1,24 +1,23 @@
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, el} from 'angular2/test_lib';
import {ControlGroup, Control, OptionalControl} from 'angular2/forms';
import * as validations from 'angular2/forms';
import {ControlGroup, Control, OptionalControl, Validators} from 'angular2/forms';
export function main() {
describe("Form Model", () => {
describe("Control", () => {
describe("validator", () => {
it("should run validator with the initial value", () => {
var c = new Control("value", validations.required);
var c = new Control("value", Validators.required);
expect(c.valid).toEqual(true);
});
it("should rerun the validator when the value changes", () => {
var c = new Control("value", validations.required);
var c = new Control("value", Validators.required);
c.updateValue(null);
expect(c.valid).toEqual(false);
});
it("should return errors", () => {
var c = new Control(null, validations.required);
var c = new Control(null, Validators.required);
expect(c.errors).toEqual({"required" : true});
});
});
@@ -83,7 +82,7 @@ export function main() {
describe("validator", () => {
it("should run the validator with the initial value (valid)", () => {
var g = new ControlGroup({
"one": new Control('value', validations.required)
"one": new Control('value', Validators.required)
});
expect(g.valid).toEqual(true);
@@ -92,7 +91,7 @@ export function main() {
});
it("should run the validator with the initial value (invalid)", () => {
var one = new Control(null, validations.required);
var one = new Control(null, Validators.required);
var g = new ControlGroup({"one": one});
expect(g.valid).toEqual(false);
@@ -101,7 +100,7 @@ export function main() {
});
it("should run the validator with the value changes", () => {
var c = new Control(null, validations.required);
var c = new Control(null, Validators.required);
var g = new ControlGroup({"one": c});
c.updateValue("some value");
@@ -174,10 +173,10 @@ export function main() {
expect(group.value).toEqual({"required" : "requiredValue", "optional" : "optionalValue"});
});
it("should not run validations on an inactive component", () => {
it("should not run Validators on an inactive component", () => {
var group = new ControlGroup({
"required": new Control("requiredValue", validations.required),
"optional": new Control("", validations.required)
"required": new Control("requiredValue", Validators.required),
"optional": new Control("", Validators.required)
}, {
"optional": false
});
+10 -10
View File
@@ -1,5 +1,5 @@
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, el} from 'angular2/test_lib';
import {ControlGroup, Control, required, compose, controlGroupValidator, nullValidator} from 'angular2/forms';
import {ControlGroup, Control, Validators} from 'angular2/forms';
export function main() {
function validator(key:string, error:any){
@@ -13,31 +13,31 @@ export function main() {
describe("Validators", () => {
describe("required", () => {
it("should error on an empty string", () => {
expect(required(new Control(""))).toEqual({"required" : true});
expect(Validators.required(new Control(""))).toEqual({"required" : true});
});
it("should error on null", () => {
expect(required(new Control(null))).toEqual({"required" : true});
expect(Validators.required(new Control(null))).toEqual({"required" : true});
});
it("should not error on a non-empty string", () => {
expect(required(new Control("not empty"))).toEqual(null);
expect(Validators.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)]);
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 = compose([validator("a", 1), validator("a", 2)]);
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 = compose([nullValidator, nullValidator]);
var c = Validators.compose([Validators.nullValidator, Validators.nullValidator]);
expect(c(new Control(""))).toEqual(null);
});
});
@@ -48,7 +48,7 @@ export function main() {
var two = new Control("one", validator("b", true));
var g = new ControlGroup({"one" : one, "two" : two});
expect(controlGroupValidator(g)).toEqual({
expect(Validators.group(g)).toEqual({
"a" : [one],
"b" : [two]
});
@@ -59,7 +59,7 @@ export function main() {
var two = new Control("two");
var g = new ControlGroup({"one" : one, "two" : two});
expect(controlGroupValidator(g)).toEqual({
expect(Validators.group(g)).toEqual({
"a": [one]
});
});
@@ -69,7 +69,7 @@ export function main() {
"one" : new Control("one")
});
expect(controlGroupValidator(g)).toEqual(null);
expect(Validators.group(g)).toEqual(null);
});
});
});