@@ -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,30 @@ describe('FormService test suite', () => {
179186 expect ( formGroup . controls . description . touched ) . toBe ( true ) ;
180187 } ) ;
181188
189+ it ( 'should add errors to fields of group' , ( ) => {
190+ let control = controls . addressLocation ;
191+ let model = formModel . find ( ( mdl : DynamicFormControlModel ) => mdl . id === 'addressLocation' ) ;
192+ let errorKeys : string [ ] ;
193+
194+ service . addErrorToField ( control , model , 'Test error message' ) ;
195+
196+ // the group itself should get an error
197+ errorKeys = Object . keys ( control . errors ) ;
198+ expect ( errorKeys . length ) . toBe ( 1 ) ;
199+ expect ( control . hasError ( errorKeys [ 0 ] ) ) . toBe ( true ) ;
200+
201+ expect ( control . touched ) . toBe ( true ) ;
202+
203+ // the group's inputs should get an error
204+ Object . values ( control . controls ) . forEach ( ( subControl : AbstractControl ) => {
205+ errorKeys = Object . keys ( subControl . errors ) ;
206+ expect ( errorKeys . length ) . toBe ( 1 ) ;
207+ expect ( subControl . hasError ( errorKeys [ 0 ] ) ) . toBe ( true ) ;
208+ expect ( subControl . touched ) . toBe ( true ) ;
209+ } ) ;
210+
211+ } ) ;
212+
182213 it ( 'should remove error from field' , ( ) => {
183214 let control = controls . description ;
184215 let model = formModel . find ( ( mdl : DynamicFormControlModel ) => mdl . id === 'description' ) ;
@@ -209,6 +240,30 @@ describe('FormService test suite', () => {
209240 expect ( formGroup . controls . description . touched ) . toBe ( false ) ;
210241 } ) ;
211242
243+ it ( 'should remove errors from fields of group' , ( ) => {
244+ let control = controls . addressLocation ;
245+ let model = formModel . find ( ( mdl : DynamicFormControlModel ) => mdl . id === 'addressLocation' ) ;
246+ let errorKeys : string [ ] ;
247+
248+ service . addErrorToField ( control , model , 'Test error message' ) ;
249+ errorKeys = Object . keys ( control . errors ) ;
250+
251+ service . removeErrorFromField ( control , model , errorKeys [ 0 ] ) ;
252+
253+ // the group itself should no longer have an error
254+ expect ( errorKeys . length ) . toBe ( 1 ) ;
255+ expect ( control . hasError ( errorKeys [ 0 ] ) ) . toBe ( false ) ;
256+ expect ( control . touched ) . toBe ( false ) ;
257+
258+ // the group's inputs should no longer have an error
259+ Object . values ( control . controls ) . forEach ( ( subControl : AbstractControl ) => {
260+ errorKeys = Object . keys ( subControl . errors ) ;
261+ expect ( errorKeys . length ) . toBe ( 1 ) ;
262+ expect ( subControl . hasError ( errorKeys [ 0 ] ) ) . toBe ( false ) ;
263+ expect ( subControl . touched ) . toBe ( false ) ;
264+ } ) ;
265+ } ) ;
266+
212267 it ( 'should reset form group' , ( ) => {
213268 const control = controls . author ;
214269
0 commit comments