@@ -105,8 +105,12 @@ export function main() {
105105 var loginControlDir ;
106106
107107 beforeEach ( ( ) => {
108- form = new NgFormModel ( ) ;
109- formModel = new ControlGroup ( { "login" : new Control ( null ) } ) ;
108+ form = new NgFormModel ( [ ] ) ;
109+ formModel = new ControlGroup ( {
110+ "login" : new Control ( ) ,
111+ "passwords" :
112+ new ControlGroup ( { "password" : new Control ( ) , "passwordConfirm" : new Control ( ) } )
113+ } ) ;
110114 form . form = formModel ;
111115
112116 loginControlDir = new NgControlName ( form , [ ] , [ defaultAccessor ] ) ;
@@ -167,6 +171,26 @@ export function main() {
167171 } ) ;
168172 } ) ;
169173
174+ describe ( "addControlGroup" , ( ) => {
175+ var matchingPasswordsValidator = ( g ) => {
176+ if ( g . controls [ "password" ] . value != g . controls [ "passwordConfirm" ] . value ) {
177+ return { "differentPasswords" : true } ;
178+ } else {
179+ return null ;
180+ }
181+ } ;
182+
183+ it ( "should set up validator" , ( ) => {
184+ var group = new NgControlGroup ( form , [ matchingPasswordsValidator ] ) ;
185+ group . name = "passwords" ;
186+ form . addControlGroup ( group ) ;
187+
188+ formModel . find ( [ "passwords" , "password" ] ) . updateValue ( "somePassword" ) ;
189+
190+ expect ( formModel . hasError ( "differentPasswords" , [ "passwords" ] ) ) . toEqual ( true ) ;
191+ } ) ;
192+ } ) ;
193+
170194 describe ( "removeControl" , ( ) => {
171195 it ( "should remove the directive to the list of directives included in the form" , ( ) => {
172196 form . addControl ( loginControlDir ) ;
@@ -181,10 +205,22 @@ export function main() {
181205
182206 formModel . find ( [ "login" ] ) . updateValue ( "new value" ) ;
183207
184- form . onChanges ( null ) ;
208+ form . onChanges ( { } ) ;
185209
186210 expect ( ( < any > loginControlDir . valueAccessor ) . writtenValue ) . toEqual ( "new value" ) ;
187211 } ) ;
212+
213+ it ( "should set up validator" , ( ) => {
214+ var formValidator = ( c ) => ( { "custom" : true } ) ;
215+ var f = new NgFormModel ( [ formValidator ] ) ;
216+ f . form = formModel ;
217+ f . onChanges ( { "form" : formModel } ) ;
218+
219+ // trigger validation
220+ formModel . controls [ "login" ] . updateValue ( "" ) ;
221+
222+ expect ( formModel . errors ) . toEqual ( { "custom" : true } ) ;
223+ } ) ;
188224 } ) ;
189225 } ) ;
190226
@@ -195,10 +231,10 @@ export function main() {
195231 var personControlGroupDir ;
196232
197233 beforeEach ( ( ) => {
198- form = new NgForm ( ) ;
234+ form = new NgForm ( [ ] ) ;
199235 formModel = form . form ;
200236
201- personControlGroupDir = new NgControlGroup ( form ) ;
237+ personControlGroupDir = new NgControlGroup ( form , [ ] ) ;
202238 personControlGroupDir . name = "person" ;
203239
204240 loginControlDir = new NgControlName ( personControlGroupDir , null , [ defaultAccessor ] ) ;
@@ -246,6 +282,17 @@ export function main() {
246282
247283 // should update the form's value and validity
248284 } ) ;
285+
286+ it ( "should set up validator" , fakeAsync ( ( ) => {
287+ var formValidator = ( c ) => ( { "custom" : true } ) ;
288+ var f = new NgForm ( [ formValidator ] ) ;
289+ f . addControlGroup ( personControlGroupDir ) ;
290+ f . addControl ( loginControlDir ) ;
291+
292+ flushMicrotasks ( ) ;
293+
294+ expect ( f . form . errors ) . toEqual ( { "custom" : true } ) ;
295+ } ) ) ;
249296 } ) ;
250297
251298 describe ( "NgControlGroup" , ( ) => {
@@ -255,9 +302,9 @@ export function main() {
255302 beforeEach ( ( ) => {
256303 formModel = new ControlGroup ( { "login" : new Control ( null ) } ) ;
257304
258- var parent = new NgFormModel ( ) ;
305+ var parent = new NgFormModel ( [ ] ) ;
259306 parent . form = new ControlGroup ( { "group" : formModel } ) ;
260- controlGroupDir = new NgControlGroup ( parent ) ;
307+ controlGroupDir = new NgControlGroup ( parent , [ ] ) ;
261308 controlGroupDir . name = "group" ;
262309 } ) ;
263310
@@ -356,7 +403,7 @@ export function main() {
356403 beforeEach ( ( ) => {
357404 formModel = new Control ( "name" ) ;
358405
359- var parent = new NgFormModel ( ) ;
406+ var parent = new NgFormModel ( [ ] ) ;
360407 parent . form = new ControlGroup ( { "name" : formModel } ) ;
361408 controlNameDir = new NgControlName ( parent , [ ] , [ defaultAccessor ] ) ;
362409 controlNameDir . name = "name" ;
0 commit comments