2015-02-05 17:55:41 -05:00
|
|
|
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, el} from 'angular2/test_lib';
|
2015-02-24 14:59:10 -05:00
|
|
|
import {ControlGroup, Control, OptionalControl} from 'angular2/forms';
|
2015-02-11 14:10:31 -05:00
|
|
|
import * as validations from 'angular2/forms';
|
2015-02-03 10:27:09 -05:00
|
|
|
|
|
|
|
export function main() {
|
2015-02-25 18:10:27 -05:00
|
|
|
describe("Form Model", () => {
|
|
|
|
describe("Control", () => {
|
|
|
|
describe("validator", () => {
|
|
|
|
it("should run validator with the initial value", () => {
|
|
|
|
var c = new Control("value", validations.required);
|
|
|
|
expect(c.valid).toEqual(true);
|
|
|
|
});
|
2015-02-11 14:10:31 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
it("should rerun the validator when the value changes", () => {
|
|
|
|
var c = new Control("value", validations.required);
|
|
|
|
c.updateValue(null);
|
|
|
|
expect(c.valid).toEqual(false);
|
|
|
|
});
|
2015-02-11 14:10:31 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
it("should return errors", () => {
|
|
|
|
var c = new Control(null, validations.required);
|
|
|
|
expect(c.errors).toEqual({"required" : true});
|
|
|
|
});
|
2015-02-11 14:10:31 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
describe("ControlGroup", () => {
|
|
|
|
describe("value", () => {
|
|
|
|
it("should be the reduced value of the child controls", () => {
|
|
|
|
var g = new ControlGroup({
|
|
|
|
"one": new Control("111"),
|
|
|
|
"two": new Control("222")
|
|
|
|
});
|
|
|
|
expect(g.value).toEqual({"one": "111", "two": "222"});
|
2015-02-03 10:27:09 -05:00
|
|
|
});
|
2015-02-11 14:10:31 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
it("should be empty when there are no child controls", () => {
|
|
|
|
var g = new ControlGroup({});
|
|
|
|
expect(g.value).toEqual({});
|
2015-02-24 14:59:10 -05:00
|
|
|
});
|
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
it("should support nested groups", () => {
|
|
|
|
var g = new ControlGroup({
|
|
|
|
"one": new Control("111"),
|
|
|
|
"nested": new ControlGroup({
|
|
|
|
"two" : new Control("222")
|
|
|
|
})
|
|
|
|
});
|
|
|
|
expect(g.value).toEqual({"one": "111", "nested" : {"two": "222"}});
|
2015-02-24 14:59:10 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
g.controls["nested"].controls["two"].updateValue("333");
|
|
|
|
|
|
|
|
expect(g.value).toEqual({"one": "111", "nested" : {"two": "333"}});
|
|
|
|
});
|
2015-02-24 14:59:10 -05:00
|
|
|
});
|
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
describe("validator", () => {
|
|
|
|
it("should run the validator with the initial value (valid)", () => {
|
|
|
|
var g = new ControlGroup({
|
|
|
|
"one": new Control('value', validations.required)
|
|
|
|
});
|
2015-02-11 14:10:31 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
expect(g.valid).toEqual(true);
|
2015-02-11 14:10:31 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
expect(g.errors).toEqual(null);
|
|
|
|
});
|
2015-02-11 14:10:31 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
it("should run the validator with the initial value (invalid)", () => {
|
|
|
|
var one = new Control(null, validations.required);
|
|
|
|
var g = new ControlGroup({"one": one});
|
2015-02-11 14:10:31 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
expect(g.valid).toEqual(false);
|
2015-02-11 14:10:31 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
expect(g.errors).toEqual({"required": [one]});
|
|
|
|
});
|
2015-02-24 14:59:10 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
it("should run the validator with the value changes", () => {
|
|
|
|
var c = new Control(null, validations.required);
|
|
|
|
var g = new ControlGroup({"one": c});
|
2015-02-24 14:59:10 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
c.updateValue("some value");
|
|
|
|
|
|
|
|
expect(g.valid).toEqual(true);
|
|
|
|
expect(g.errors).toEqual(null);
|
|
|
|
});
|
|
|
|
});
|
2015-02-24 14:59:10 -05:00
|
|
|
});
|
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
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);
|
|
|
|
});
|
2015-02-24 14:59:10 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
it("should update the wrapped component", () => {
|
|
|
|
var wrappedControl = new Control("value");
|
|
|
|
var c = new OptionalControl(wrappedControl, true);
|
2015-02-24 14:59:10 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
c.validator = validations.required;
|
|
|
|
c.updateValue("newValue");
|
2015-02-24 14:59:10 -05:00
|
|
|
|
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
expect(wrappedControl.validator).toEqual(validations.required);
|
|
|
|
expect(wrappedControl.value).toEqual('newValue');
|
2015-02-24 14:59:10 -05:00
|
|
|
});
|
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
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)
|
|
|
|
});
|
2015-02-24 14:59:10 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
expect(group.value).toEqual({"required" : "requiredValue"});
|
2015-02-24 14:59:10 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
group.controls["optional"].cond = true;
|
2015-02-24 14:59:10 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
expect(group.value).toEqual({"required" : "requiredValue", "optional" : "optionalValue"});
|
2015-02-24 14:59:10 -05:00
|
|
|
});
|
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
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);
|
2015-02-24 14:59:10 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
group.controls["optional"].cond = true;
|
2015-02-24 14:59:10 -05:00
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
expect(group.valid).toEqual(false);
|
|
|
|
});
|
2015-02-24 14:59:10 -05:00
|
|
|
});
|
|
|
|
|
2015-02-25 18:10:27 -05:00
|
|
|
});
|
2015-02-03 10:27:09 -05:00
|
|
|
}
|