@@ -108,8 +108,15 @@ describe('FormService test suite', () => {
108108 const title : AbstractControl = new FormControl ( undefined , Validators . required ) ;
109109 const date : AbstractControl = new FormControl ( undefined ) ;
110110 const description : AbstractControl = new FormControl ( undefined ) ;
111- formGroup = new FormGroup ( { author, title, date, description } ) ;
112- controls = { author, title, date, description } ;
111+
112+ const addressLocation : FormGroup = new FormGroup ( {
113+ zipCode : new FormControl ( undefined ) ,
114+ state : new FormControl ( undefined ) ,
115+ city : new FormControl ( undefined ) ,
116+ } ) ;
117+
118+ formGroup = new FormGroup ( { author, title, date, description, addressLocation } ) ;
119+ controls = { author, title, date, description , addressLocation } ;
113120 service = new FormService ( builderService , store ) ;
114121 } )
115122 )
@@ -179,6 +186,32 @@ describe('FormService test suite', () => {
179186 expect ( formGroup . controls . description . touched ) . toBe ( true ) ;
180187 } ) ;
181188
189+ it ( 'should add errors to fields of concat group' , ( ) => {
190+ ( builderService as any ) . isConcatGroup . and . returnValue ( true ) ;
191+
192+ let control = controls . addressLocation ;
193+ let model = formModel . find ( ( mdl : DynamicFormControlModel ) => mdl . id === 'addressLocation' ) ;
194+ let errorKeys : string [ ] ;
195+
196+ service . addErrorToField ( control , model , 'Test error message' ) ;
197+
198+ // the group itself should get an error
199+ errorKeys = Object . keys ( control . errors ) ;
200+ expect ( errorKeys . length ) . toBe ( 1 ) ;
201+ expect ( control . hasError ( errorKeys [ 0 ] ) ) . toBe ( true ) ;
202+
203+ expect ( control . touched ) . toBe ( true ) ;
204+
205+ // the group's inputs should get an error
206+ Object . values ( control . controls ) . forEach ( ( subControl : AbstractControl ) => {
207+ errorKeys = Object . keys ( subControl . errors ) ;
208+ expect ( errorKeys . length ) . toBe ( 1 ) ;
209+ expect ( subControl . hasError ( errorKeys [ 0 ] ) ) . toBe ( true ) ;
210+ expect ( subControl . touched ) . toBe ( true ) ;
211+ } ) ;
212+
213+ } ) ;
214+
182215 it ( 'should remove error from field' , ( ) => {
183216 let control = controls . description ;
184217 let model = formModel . find ( ( mdl : DynamicFormControlModel ) => mdl . id === 'description' ) ;
@@ -209,6 +242,32 @@ describe('FormService test suite', () => {
209242 expect ( formGroup . controls . description . touched ) . toBe ( false ) ;
210243 } ) ;
211244
245+ it ( 'should remove errors from fields of concat group' , ( ) => {
246+ ( builderService as any ) . isConcatGroup . and . returnValue ( true ) ;
247+
248+ let control = controls . addressLocation ;
249+ let model = formModel . find ( ( mdl : DynamicFormControlModel ) => mdl . id === 'addressLocation' ) ;
250+ let errorKeys : string [ ] ;
251+
252+ service . addErrorToField ( control , model , 'Test error message' ) ;
253+ errorKeys = Object . keys ( control . errors ) ;
254+
255+ service . removeErrorFromField ( control , model , errorKeys [ 0 ] ) ;
256+
257+ // the group itself should no longer have an error
258+ expect ( errorKeys . length ) . toBe ( 1 ) ;
259+ expect ( control . hasError ( errorKeys [ 0 ] ) ) . toBe ( false ) ;
260+ expect ( control . touched ) . toBe ( false ) ;
261+
262+ // the group's inputs should no longer have an error
263+ Object . values ( control . controls ) . forEach ( ( subControl : AbstractControl ) => {
264+ errorKeys = Object . keys ( subControl . errors ) ;
265+ expect ( errorKeys . length ) . toBe ( 1 ) ;
266+ expect ( subControl . hasError ( errorKeys [ 0 ] ) ) . toBe ( false ) ;
267+ expect ( subControl . touched ) . toBe ( false ) ;
268+ } ) ;
269+ } ) ;
270+
212271 it ( 'should reset form group' , ( ) => {
213272 const control = controls . author ;
214273
0 commit comments