feat(forms): add optional controls

This commit is contained in:
vsavkin
2015-02-24 11:59:10 -08:00
parent a73c643322
commit f27e538e2c
7 changed files with 220 additions and 41 deletions

View File

@ -15,7 +15,7 @@ import {MockTemplateResolver} from 'angular2/src/mock/template_resolver_mock';
import {Injector} from 'angular2/di';
import {Component, Decorator, Template} from 'angular2/core';
import {ControlGroupDirective, ControlDirective, Control, ControlGroup,
import {ControlGroupDirective, ControlDirective, Control, ControlGroup, OptionalControl,
ControlValueAccessor, RequiredValidatorDirective} from 'angular2/forms';
import * as validators from 'angular2/src/forms/validators';
@ -42,7 +42,7 @@ export function main() {
tplResolver.setTemplate(componentType, new Template({
inline: template,
directives: [ControlGroupDirective, ControlDirective, WrappedValue]
directives: [ControlGroupDirective, ControlDirective, WrappedValue, RequiredValidatorDirective]
}));
compiler.compile(componentType).then((pv) => {

View File

@ -1,5 +1,5 @@
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, el} from 'angular2/test_lib';
import {ControlGroup, Control} from 'angular2/forms';
import {ControlGroup, Control, OptionalControl} from 'angular2/forms';
import * as validations from 'angular2/forms';
export function main() {
@ -40,7 +40,17 @@ export function main() {
});
describe("validator", () => {
it("should run the validator with the initial value", () => {
it("should run the validator with the initial value (valid)", () => {
var g = new ControlGroup({
"one": new Control('value', validations.required)
});
expect(g.valid).toEqual(true);
expect(g.errors).toEqual(null);
});
it("should run the validator with the initial value (invalid)", () => {
var g = new ControlGroup({
"one": new Control(null, validations.required)
});
@ -61,4 +71,54 @@ export function main() {
});
});
});
describe("OptionalControl", () => {
it("should read properties from the wrapped component", () => {
var wrapperControl = new Control("value", validations.required);
var c = new OptionalControl(wrapperControl, true);
expect(c.value).toEqual('value');
expect(c.status).toEqual('VALID');
expect(c.validator).toEqual(validations.required);
});
it("should update the wrapped component", () => {
var wrappedControl = new Control("value");
var c = new OptionalControl(wrappedControl, true);
c.validator = validations.required;
c.updateValue("newValue");
expect(wrappedControl.validator).toEqual(validations.required);
expect(wrappedControl.value).toEqual('newValue');
});
it("should not include an inactive component into the group value", () => {
var group = new ControlGroup({
"required" : new Control("requiredValue"),
"optional" : new OptionalControl(new Control("optionalValue"), false)
});
expect(group.value).toEqual({"required" : "requiredValue"});
group.controls["optional"].cond = true;
expect(group.value).toEqual({"required" : "requiredValue", "optional" : "optionalValue"});
});
it("should not run validations on an inactive component", () => {
var group = new ControlGroup({
"required" : new Control("requiredValue", validations.required),
"optional" : new OptionalControl(new Control("", validations.required), false)
});
expect(group.valid).toEqual(true);
group.controls["optional"].cond = true;
expect(group.valid).toEqual(false);
});
});
}

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} from 'angular2/forms';
import {ControlGroup, Control, required, compose, controlGroupValidator, nullValidator} from 'angular2/forms';
export function main() {
function validator(key:string, error:any){
@ -35,6 +35,11 @@ export function main() {
var c = 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]);
expect(c(new Control(""))).toEqual(null);
});
});
describe("controlGroupValidator", () => {