1+ import { ddescribe , describe , it , iit , xit , expect , beforeEach , afterEach , el } from 'angular2/test_lib' ;
2+ import { ControlGroup , Control , required , compose , controlGroupValidator } from 'angular2/forms' ;
3+
4+ export function main ( ) {
5+ function validator ( key :string , error :any ) {
6+ return function ( c :Control ) {
7+ var r = { } ;
8+ r [ key ] = error ;
9+ return r ;
10+ }
11+ }
12+
13+ describe ( "Validators" , ( ) => {
14+ describe ( "required" , ( ) => {
15+ it ( "should error on an empty string" , ( ) => {
16+ expect ( required ( new Control ( "" ) ) ) . toEqual ( { "required" : true } ) ;
17+ } ) ;
18+
19+ it ( "should error on null" , ( ) => {
20+ expect ( required ( new Control ( null ) ) ) . toEqual ( { "required" : true } ) ;
21+ } ) ;
22+
23+ it ( "should not error on a non-empty string" , ( ) => {
24+ expect ( required ( new Control ( "not empty" ) ) ) . toEqual ( null ) ;
25+ } ) ;
26+ } ) ;
27+
28+ describe ( "compose" , ( ) => {
29+ it ( "should collect errors from all the validators" , ( ) => {
30+ var c = compose ( [ validator ( "a" , true ) , validator ( "b" , true ) ] ) ;
31+ expect ( c ( new Control ( "" ) ) ) . toEqual ( { "a" : true , "b" : true } ) ;
32+ } ) ;
33+
34+ it ( "should run validators left to right" , ( ) => {
35+ var c = compose ( [ validator ( "a" , 1 ) , validator ( "a" , 2 ) ] ) ;
36+ expect ( c ( new Control ( "" ) ) ) . toEqual ( { "a" : 2 } ) ;
37+ } ) ;
38+ } ) ;
39+
40+ describe ( "controlGroupValidator" , ( ) => {
41+ it ( "should collect errors from the child controls" , ( ) => {
42+ var g = new ControlGroup ( {
43+ "one" : new Control ( "one" , validator ( "a" , true ) ) ,
44+ "two" : new Control ( "two" , validator ( "b" , true ) )
45+ } ) ;
46+
47+ expect ( controlGroupValidator ( g ) ) . toEqual ( {
48+ "one" : { "a" : true } ,
49+ "two" : { "b" : true }
50+ } ) ;
51+ } ) ;
52+
53+ it ( "should not include keys for controls that have no errors" , ( ) => {
54+ var g = new ControlGroup ( {
55+ "one" : new Control ( "one" , validator ( "a" , true ) ) ,
56+ "two" : new Control ( "one" )
57+ } ) ;
58+
59+ expect ( controlGroupValidator ( g ) ) . toEqual ( {
60+ "one" : { "a" : true }
61+ } ) ;
62+ } ) ;
63+
64+ it ( "should return null when no errors" , ( ) => {
65+ var g = new ControlGroup ( {
66+ "one" : new Control ( "one" )
67+ } ) ;
68+
69+ expect ( controlGroupValidator ( g ) ) . toEqual ( null ) ;
70+ } ) ;
71+ } ) ;
72+ } ) ;
73+ }
0 commit comments