feat(forms): add support for validations
This commit is contained in:
31
modules/angular2/test/forms/integration_spec.js
vendored
31
modules/angular2/test/forms/integration_spec.js
vendored
@ -17,7 +17,8 @@ import {Injector} from 'angular2/di';
|
||||
import {Component, Decorator, Template} from 'angular2/core';
|
||||
import {ControlGroupDirective, ControlNameDirective,
|
||||
ControlDirective, NewControlGroupDirective,
|
||||
Control, ControlGroup, ControlValueAccessor} from 'angular2/forms';
|
||||
Control, ControlGroup, ControlValueAccessor,
|
||||
RequiredValidatorDirective} from 'angular2/forms';
|
||||
|
||||
export function main() {
|
||||
function detectChanges(view) {
|
||||
@ -210,11 +211,37 @@ export function main() {
|
||||
});
|
||||
});
|
||||
|
||||
it("should support validators",(done) => {
|
||||
var t = `<div #form [new-control-group]="{'login': 'loginValue'}">
|
||||
<input type="text" control="login" required>
|
||||
</div>`;
|
||||
|
||||
compile(MyComp, t, new MyComp(), (view) => {
|
||||
var form = view.contextWithLocals.get("form");
|
||||
expect(form.valid).toEqual(true);
|
||||
|
||||
var input = queryView(view, "input");
|
||||
|
||||
input.value = "";
|
||||
dispatchEvent(input, "change");
|
||||
|
||||
expect(form.valid).toEqual(false);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Component({selector: "my-comp"})
|
||||
@Component({
|
||||
selector: "my-comp"
|
||||
})
|
||||
@Template({
|
||||
inline: "",
|
||||
directives: [ControlGroupDirective, ControlNameDirective,
|
||||
ControlDirective, NewControlGroupDirective, RequiredValidatorDirective,
|
||||
WrappedValue]
|
||||
})
|
||||
class MyComp {
|
||||
form:ControlGroup;
|
||||
name:string;
|
||||
|
43
modules/angular2/test/forms/model_spec.js
vendored
43
modules/angular2/test/forms/model_spec.js
vendored
@ -1,7 +1,28 @@
|
||||
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, el} from 'angular2/test_lib';
|
||||
import {ControlGroup, Control} from 'angular2/forms';
|
||||
import * as validations from 'angular2/forms';
|
||||
|
||||
export function main() {
|
||||
describe("Control", () => {
|
||||
describe("validator", () => {
|
||||
it("should run validator with the initial value", () => {
|
||||
var c = new Control("value", validations.required);
|
||||
expect(c.valid).toEqual(true);
|
||||
});
|
||||
|
||||
it("should rerun the validator when the value changes", () => {
|
||||
var c = new Control("value", validations.required);
|
||||
c.updateValue(null);
|
||||
expect(c.valid).toEqual(false);
|
||||
});
|
||||
|
||||
it("should return errors", () => {
|
||||
var c = new Control(null, validations.required);
|
||||
expect(c.errors).toEqual({"required" : true});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("ControlGroup", () => {
|
||||
describe("value", () => {
|
||||
it("should be the reduced value of the child controls", () => {
|
||||
@ -17,5 +38,27 @@ export function main() {
|
||||
expect(g.value).toEqual({})
|
||||
});
|
||||
});
|
||||
|
||||
describe("validator", () => {
|
||||
it("should run the validator with the initial value", () => {
|
||||
var g = new ControlGroup({
|
||||
"one": new Control(null, validations.required)
|
||||
});
|
||||
|
||||
expect(g.valid).toEqual(false);
|
||||
|
||||
expect(g.errors).toEqual({"one": {"required" : true}});
|
||||
});
|
||||
|
||||
it("should run the validator with the value changes", () => {
|
||||
var c = new Control(null, validations.required);
|
||||
var g = new ControlGroup({"one": c});
|
||||
|
||||
c.updateValue("some value");
|
||||
|
||||
expect(g.valid).toEqual(true);
|
||||
expect(g.errors).toEqual(null);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
73
modules/angular2/test/forms/validators_spec.js
vendored
Normal file
73
modules/angular2/test/forms/validators_spec.js
vendored
Normal file
@ -0,0 +1,73 @@
|
||||
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, el} from 'angular2/test_lib';
|
||||
import {ControlGroup, Control, required, compose, controlGroupValidator} 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(required(new Control(""))).toEqual({"required" : true});
|
||||
});
|
||||
|
||||
it("should error on null", () => {
|
||||
expect(required(new Control(null))).toEqual({"required" : true});
|
||||
});
|
||||
|
||||
it("should not error on a non-empty string", () => {
|
||||
expect(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)]);
|
||||
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)]);
|
||||
expect(c(new Control(""))).toEqual({"a" : 2});
|
||||
});
|
||||
});
|
||||
|
||||
describe("controlGroupValidator", () => {
|
||||
it("should collect errors from the child controls", () => {
|
||||
var g = new ControlGroup({
|
||||
"one" : new Control("one", validator("a", true)),
|
||||
"two" : new Control("two", validator("b", true))
|
||||
});
|
||||
|
||||
expect(controlGroupValidator(g)).toEqual({
|
||||
"one" : {"a" : true},
|
||||
"two" : {"b" : true}
|
||||
});
|
||||
});
|
||||
|
||||
it("should not include keys for controls that have no errors", () => {
|
||||
var g = new ControlGroup({
|
||||
"one" : new Control("one", validator("a", true)),
|
||||
"two" : new Control("one")
|
||||
});
|
||||
|
||||
expect(controlGroupValidator(g)).toEqual({
|
||||
"one" : {"a" : true}
|
||||
});
|
||||
});
|
||||
|
||||
it("should return null when no errors", () => {
|
||||
var g = new ControlGroup({
|
||||
"one" : new Control("one")
|
||||
});
|
||||
|
||||
expect(controlGroupValidator(g)).toEqual(null);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user