@@ -7,9 +7,14 @@ import {
77 expect ,
88 beforeEach ,
99 afterEach ,
10+ fakeAsync ,
11+ tick ,
1012 el
1113} from 'angular2/testing_internal' ;
1214import { ControlGroup , Control , Validators , AbstractControl , ControlArray } from 'angular2/core' ;
15+ import { PromiseWrapper } from 'angular2/src/core/facade/promise' ;
16+ import { TimerWrapper } from 'angular2/src/core/facade/async' ;
17+ import { CONST_EXPR } from 'angular2/src/core/facade/lang' ;
1318
1419export function main ( ) {
1520 function validator ( key : string , error : any ) {
@@ -65,8 +70,8 @@ export function main() {
6570 } ) ;
6671
6772 describe ( "compose" , ( ) => {
68- it ( "should return a null validator when given null" ,
69- ( ) => { expect ( Validators . compose ( null ) ) . toBe ( Validators . nullValidator ) ; } ) ;
73+ it ( "should return null when given null" ,
74+ ( ) => { expect ( Validators . compose ( null ) ) . toBe ( null ) ; } ) ;
7075
7176 it ( "should collect errors from all the validators" , ( ) => {
7277 var c = Validators . compose ( [ validator ( "a" , true ) , validator ( "b" , true ) ] ) ;
@@ -88,5 +93,55 @@ export function main() {
8893 expect ( c ( new Control ( "" ) ) ) . toEqual ( { "required" : true } ) ;
8994 } ) ;
9095 } ) ;
96+
97+ describe ( "composeAsync" , ( ) => {
98+ function asyncValidator ( expected , response , timeout = 0 ) {
99+ return ( c ) => {
100+ var completer = PromiseWrapper . completer ( ) ;
101+ var res = c . value != expected ? response : null ;
102+ TimerWrapper . setTimeout ( ( ) => { completer . resolve ( res ) ; } , timeout ) ;
103+ return completer . promise ;
104+ } ;
105+ }
106+
107+ it ( "should return null when given null" ,
108+ ( ) => { expect ( Validators . composeAsync ( null ) ) . toEqual ( null ) ; } ) ;
109+
110+ it ( "should collect errors from all the validators" , fakeAsync ( ( ) => {
111+ var c = Validators . composeAsync ( [
112+ asyncValidator ( "expected" , { "one" : true } ) ,
113+ asyncValidator ( "expected" , { "two" : true } )
114+ ] ) ;
115+
116+ var value = null ;
117+ c ( new Control ( "invalid" ) ) . then ( v => value = v ) ;
118+
119+ tick ( 1 ) ;
120+
121+ expect ( value ) . toEqual ( { "one" : true , "two" : true } ) ;
122+ } ) ) ;
123+
124+ it ( "should return null when no errors" , fakeAsync ( ( ) => {
125+ var c = Validators . composeAsync ( [ asyncValidator ( "expected" , { "one" : true } ) ] ) ;
126+
127+ var value = null ;
128+ c ( new Control ( "expected" ) ) . then ( v => value = v ) ;
129+
130+ tick ( 1 ) ;
131+
132+ expect ( value ) . toEqual ( null ) ;
133+ } ) ) ;
134+
135+ it ( "should ignore nulls" , fakeAsync ( ( ) => {
136+ var c = Validators . composeAsync ( [ asyncValidator ( "expected" , { "one" : true } ) , null ] ) ;
137+
138+ var value = null ;
139+ c ( new Control ( "invalid" ) ) . then ( v => value = v ) ;
140+
141+ tick ( 1 ) ;
142+
143+ expect ( value ) . toEqual ( { "one" : true } ) ;
144+ } ) ) ;
145+ } ) ;
91146 } ) ;
92147}
0 commit comments