@@ -82,9 +82,86 @@ import {FormControlStatus, StatusChangeEvent} from '../src/model/abstract_model'
8282 } ) ;
8383 } ) ;
8484
85+ describe ( 'markAllAsDirty' , ( ) => {
86+ let form : FormGroup ;
87+ let logger : any [ ] ;
88+
89+ beforeEach ( ( ) => {
90+ form = new FormGroup ( {
91+ 'c1' : new FormControl ( 'v1' ) ,
92+ 'group' : new FormGroup ( { 'c2' : new FormControl ( 'v2' ) , 'c3' : new FormControl ( 'v3' ) } ) ,
93+ 'array' : new FormArray ( [
94+ new FormControl ( 'v4' ) as any ,
95+ new FormControl ( 'v5' ) ,
96+ new FormGroup ( { 'c4' : new FormControl ( 'v4' ) } ) ,
97+ ] ) ,
98+ } ) ;
99+ logger = [ ] ;
100+ } ) ;
101+
102+ it ( 'should mark all descendants as dirty' , ( ) => {
103+ expect ( form . dirty ) . toBe ( false ) ;
104+
105+ const control1 = form . get ( 'c1' ) as FormControl ;
106+
107+ expect ( control1 . dirty ) . toBe ( false ) ;
108+
109+ const innerGroup = form . get ( 'group' ) as FormGroup ;
110+
111+ expect ( innerGroup . dirty ) . toBe ( false ) ;
112+
113+ const innerGroupFirstChildCtrl = innerGroup . get ( 'c2' ) as FormControl ;
114+
115+ expect ( innerGroupFirstChildCtrl . dirty ) . toBe ( false ) ;
116+
117+ form . markAllAsDirty ( ) ;
118+
119+ expect ( form . dirty ) . toBe ( true ) ;
120+
121+ expect ( control1 . dirty ) . toBe ( true ) ;
122+
123+ expect ( innerGroup . dirty ) . toBe ( true ) ;
124+
125+ expect ( innerGroupFirstChildCtrl . dirty ) . toBe ( true ) ;
126+
127+ const innerGroupSecondChildCtrl = innerGroup . get ( 'c3' ) as FormControl ;
128+
129+ expect ( innerGroupSecondChildCtrl . dirty ) . toBe ( true ) ;
130+
131+ const array = form . get ( 'array' ) as FormArray ;
132+
133+ expect ( array . dirty ) . toBe ( true ) ;
134+
135+ const arrayFirstChildCtrl = array . at ( 0 ) as FormControl ;
136+
137+ expect ( arrayFirstChildCtrl . dirty ) . toBe ( true ) ;
138+
139+ const arraySecondChildCtrl = array . at ( 1 ) as FormControl ;
140+
141+ expect ( arraySecondChildCtrl . dirty ) . toBe ( true ) ;
142+
143+ const arrayFirstChildGroup = array . at ( 2 ) as FormGroup ;
144+
145+ expect ( arrayFirstChildGroup . dirty ) . toBe ( true ) ;
146+
147+ const arrayFirstChildGroupFirstChildCtrl = arrayFirstChildGroup . get ( 'c4' ) as FormControl ;
148+
149+ expect ( arrayFirstChildGroupFirstChildCtrl . dirty ) . toBe ( true ) ;
150+ } ) ;
151+
152+ it ( 'should not emit events when `FormGroup.markAllAsDirty` is called with `emitEvent: false`' , ( ) => {
153+ form . valueChanges . subscribe ( ( ) => logger . push ( 'form' ) ) ;
154+ form . markAllAsDirty ( { emitEvent : false } ) ;
155+ expect ( logger ) . toEqual ( [ ] ) ;
156+ } ) ;
157+ } ) ;
158+
85159 describe ( 'markAllAsTouched' , ( ) => {
86- it ( 'should mark all descendants as touched' , ( ) => {
87- const formGroup : FormGroup = new FormGroup ( {
160+ let form : FormGroup ;
161+ let logger : any [ ] ;
162+
163+ beforeEach ( ( ) => {
164+ form = new FormGroup ( {
88165 'c1' : new FormControl ( 'v1' ) ,
89166 'group' : new FormGroup ( { 'c2' : new FormControl ( 'v2' ) , 'c3' : new FormControl ( 'v3' ) } ) ,
90167 'array' : new FormArray ( [
@@ -93,24 +170,27 @@ import {FormControlStatus, StatusChangeEvent} from '../src/model/abstract_model'
93170 new FormGroup ( { 'c4' : new FormControl ( 'v4' ) } ) ,
94171 ] ) ,
95172 } ) ;
173+ logger = [ ] ;
174+ } ) ;
96175
97- expect ( formGroup . touched ) . toBe ( false ) ;
176+ it ( 'should mark all descendants as touched' , ( ) => {
177+ expect ( form . touched ) . toBe ( false ) ;
98178
99- const control1 = formGroup . get ( 'c1' ) as FormControl ;
179+ const control1 = form . get ( 'c1' ) as FormControl ;
100180
101181 expect ( control1 . touched ) . toBe ( false ) ;
102182
103- const innerGroup = formGroup . get ( 'group' ) as FormGroup ;
183+ const innerGroup = form . get ( 'group' ) as FormGroup ;
104184
105185 expect ( innerGroup . touched ) . toBe ( false ) ;
106186
107187 const innerGroupFirstChildCtrl = innerGroup . get ( 'c2' ) as FormControl ;
108188
109189 expect ( innerGroupFirstChildCtrl . touched ) . toBe ( false ) ;
110190
111- formGroup . markAllAsTouched ( ) ;
191+ form . markAllAsTouched ( ) ;
112192
113- expect ( formGroup . touched ) . toBe ( true ) ;
193+ expect ( form . touched ) . toBe ( true ) ;
114194
115195 expect ( control1 . touched ) . toBe ( true ) ;
116196
@@ -122,7 +202,7 @@ import {FormControlStatus, StatusChangeEvent} from '../src/model/abstract_model'
122202
123203 expect ( innerGroupSecondChildCtrl . touched ) . toBe ( true ) ;
124204
125- const array = formGroup . get ( 'array' ) as FormArray ;
205+ const array = form . get ( 'array' ) as FormArray ;
126206
127207 expect ( array . touched ) . toBe ( true ) ;
128208
@@ -142,6 +222,12 @@ import {FormControlStatus, StatusChangeEvent} from '../src/model/abstract_model'
142222
143223 expect ( arrayFirstChildGroupFirstChildCtrl . touched ) . toBe ( true ) ;
144224 } ) ;
225+
226+ it ( 'should not emit events when `FormGroup.markAllAsTouched` is called with `emitEvent: false`' , ( ) => {
227+ form . valueChanges . subscribe ( ( ) => logger . push ( 'form' ) ) ;
228+ form . markAllAsTouched ( { emitEvent : false } ) ;
229+ expect ( logger ) . toEqual ( [ ] ) ;
230+ } ) ;
145231 } ) ;
146232
147233 describe ( 'adding and removing controls' , ( ) => {
0 commit comments