@@ -32,11 +32,12 @@ import {
3232 DefaultValueAccessor ,
3333 CheckboxControlValueAccessor ,
3434 SelectControlValueAccessor ,
35- QueryList
35+ QueryList ,
36+ Validator
3637} from 'angular2/core' ;
3738
3839
39- import { selectValueAccessor } from 'angular2/src/core/forms/directives/shared' ;
40+ import { selectValueAccessor , composeValidators } from 'angular2/src/core/forms/directives/shared' ;
4041
4142import { SimpleChange } from 'angular2/src/core/change_detection' ;
4243
@@ -49,6 +50,10 @@ class DummyControlValueAccessor implements ControlValueAccessor {
4950 writeValue ( obj : any ) : void { this . writtenValue = obj ; }
5051}
5152
53+ class CustomValidatorDirective implements Validator {
54+ validate ( c : Control ) : { [ key : string ] : any } { return { "custom" : true } ; }
55+ }
56+
5257export function main ( ) {
5358 describe ( "Form Directives" , ( ) => {
5459 var defaultAccessor ;
@@ -97,6 +102,21 @@ export function main() {
97102 expect ( ( ) => selectValueAccessor ( dir , [ customAccessor , customAccessor ] ) ) . toThrowError ( ) ;
98103 } ) ;
99104 } ) ;
105+
106+ describe ( "composeValidators" , ( ) => {
107+ it ( "should compose functions" , ( ) => {
108+ var dummy1 = ( _ ) => ( { "dummy1" : true } ) ;
109+ var dummy2 = ( _ ) => ( { "dummy2" : true } ) ;
110+ var v = composeValidators ( [ dummy1 , dummy2 ] ) ;
111+ expect ( v ( new Control ( "" ) ) ) . toEqual ( { "dummy1" : true , "dummy2" : true } ) ;
112+ } ) ;
113+
114+ it ( "should compose validator directives" , ( ) => {
115+ var dummy1 = ( _ ) => ( { "dummy1" : true } ) ;
116+ var v = composeValidators ( [ dummy1 , new CustomValidatorDirective ( ) ] ) ;
117+ expect ( v ( new Control ( "" ) ) ) . toEqual ( { "dummy1" : true , "custom" : true } ) ;
118+ } ) ;
119+ } ) ;
100120 } ) ;
101121
102122 describe ( "NgFormModel" , ( ) => {
@@ -113,7 +133,7 @@ export function main() {
113133 } ) ;
114134 form . form = formModel ;
115135
116- loginControlDir = new NgControlName ( form , [ ] , [ defaultAccessor ] ) ;
136+ loginControlDir = new NgControlName ( form , [ Validators . required ] , [ defaultAccessor ] ) ;
117137 loginControlDir . name = "login" ;
118138 loginControlDir . valueAccessor = new DummyControlValueAccessor ( ) ;
119139 } ) ;
@@ -147,8 +167,6 @@ export function main() {
147167 } ) ;
148168
149169 it ( "should set up validator" , ( ) => {
150- loginControlDir . validators = [ Validators . required ] ;
151-
152170 expect ( formModel . find ( [ "login" ] ) . valid ) . toBe ( true ) ;
153171
154172 // this will add the required validator and recalculate the validity
@@ -335,7 +353,7 @@ export function main() {
335353 } ;
336354
337355 beforeEach ( ( ) => {
338- controlDir = new NgFormControl ( [ ] , [ defaultAccessor ] ) ;
356+ controlDir = new NgFormControl ( [ Validators . required ] , [ defaultAccessor ] ) ;
339357 controlDir . valueAccessor = new DummyControlValueAccessor ( ) ;
340358
341359 control = new Control ( null ) ;
@@ -353,8 +371,6 @@ export function main() {
353371 } ) ;
354372
355373 it ( "should set up validator" , ( ) => {
356- controlDir . validators = [ Validators . required ] ;
357-
358374 expect ( control . valid ) . toBe ( true ) ;
359375
360376 // this will add the required validator and recalculate the validity
@@ -368,7 +384,7 @@ export function main() {
368384 var ngModel ;
369385
370386 beforeEach ( ( ) => {
371- ngModel = new NgModel ( [ ] , [ defaultAccessor ] ) ;
387+ ngModel = new NgModel ( [ Validators . required ] , [ defaultAccessor ] ) ;
372388 ngModel . valueAccessor = new DummyControlValueAccessor ( ) ;
373389 } ) ;
374390
@@ -385,8 +401,6 @@ export function main() {
385401 } ) ;
386402
387403 it ( "should set up validator" , ( ) => {
388- ngModel . validators = [ Validators . required ] ;
389-
390404 expect ( ngModel . control . valid ) . toBe ( true ) ;
391405
392406 // this will add the required validator and recalculate the validity
0 commit comments