11import { ddescribe , describe , it , iit , xit , expect , beforeEach , afterEach , el } from 'angular2/test_lib' ;
2- import { ControlGroup , Control } from 'angular2/forms' ;
2+ import { ControlGroup , Control , OptionalControl } from 'angular2/forms' ;
33import * as validations from 'angular2/forms' ;
44
55export function main ( ) {
@@ -40,7 +40,17 @@ export function main() {
4040 } ) ;
4141
4242 describe ( "validator" , ( ) => {
43- it ( "should run the validator with the initial value" , ( ) => {
43+ it ( "should run the validator with the initial value (valid)" , ( ) => {
44+ var g = new ControlGroup ( {
45+ "one" : new Control ( 'value' , validations . required )
46+ } ) ;
47+
48+ expect ( g . valid ) . toEqual ( true ) ;
49+
50+ expect ( g . errors ) . toEqual ( null ) ;
51+ } ) ;
52+
53+ it ( "should run the validator with the initial value (invalid)" , ( ) => {
4454 var g = new ControlGroup ( {
4555 "one" : new Control ( null , validations . required )
4656 } ) ;
@@ -61,4 +71,54 @@ export function main() {
6171 } ) ;
6272 } ) ;
6373 } ) ;
74+
75+ describe ( "OptionalControl" , ( ) => {
76+ it ( "should read properties from the wrapped component" , ( ) => {
77+ var wrapperControl = new Control ( "value" , validations . required ) ;
78+ var c = new OptionalControl ( wrapperControl , true ) ;
79+
80+ expect ( c . value ) . toEqual ( 'value' ) ;
81+ expect ( c . status ) . toEqual ( 'VALID' ) ;
82+ expect ( c . validator ) . toEqual ( validations . required ) ;
83+ } ) ;
84+
85+ it ( "should update the wrapped component" , ( ) => {
86+ var wrappedControl = new Control ( "value" ) ;
87+ var c = new OptionalControl ( wrappedControl , true ) ;
88+
89+ c . validator = validations . required ;
90+ c . updateValue ( "newValue" ) ;
91+
92+
93+ expect ( wrappedControl . validator ) . toEqual ( validations . required ) ;
94+ expect ( wrappedControl . value ) . toEqual ( 'newValue' ) ;
95+ } ) ;
96+
97+ it ( "should not include an inactive component into the group value" , ( ) => {
98+ var group = new ControlGroup ( {
99+ "required" : new Control ( "requiredValue" ) ,
100+ "optional" : new OptionalControl ( new Control ( "optionalValue" ) , false )
101+ } ) ;
102+
103+ expect ( group . value ) . toEqual ( { "required" : "requiredValue" } ) ;
104+
105+ group . controls [ "optional" ] . cond = true ;
106+
107+ expect ( group . value ) . toEqual ( { "required" : "requiredValue" , "optional" : "optionalValue" } ) ;
108+ } ) ;
109+
110+ it ( "should not run validations on an inactive component" , ( ) => {
111+ var group = new ControlGroup ( {
112+ "required" : new Control ( "requiredValue" , validations . required ) ,
113+ "optional" : new OptionalControl ( new Control ( "" , validations . required ) , false )
114+ } ) ;
115+
116+ expect ( group . valid ) . toEqual ( true ) ;
117+
118+ group . controls [ "optional" ] . cond = true ;
119+
120+ expect ( group . valid ) . toEqual ( false ) ;
121+ } ) ;
122+ } ) ;
123+
64124}
0 commit comments