@@ -1422,6 +1422,59 @@ describe('reactive forms integration tests', () => {
14221422 expect ( events [ 3 ] . source ) . toBe ( form ) ;
14231423 } ) ;
14241424
1425+ it ( 'formControl should not emit an event when resetting a form with emit:false' , ( ) => {
1426+ const fixture = initTest ( FormGroupComp ) ;
1427+ const form = new FormGroup ( { 'login' : new FormControl ( '' , Validators . required ) } ) ;
1428+ fixture . componentInstance . form = form ;
1429+ fixture . detectChanges ( ) ;
1430+
1431+ const formGroupDir = fixture . debugElement . children [ 0 ] . injector . get ( FormGroupDirective ) ;
1432+
1433+ const events : ControlEvent [ ] = [ ] ;
1434+ fixture . componentInstance . form . events . subscribe ( ( event ) => events . push ( event ) ) ;
1435+
1436+ formGroupDir . resetForm ( undefined , { emitEvent : false } ) ;
1437+
1438+ expect ( events . length ) . toBe ( 1 ) ;
1439+ expect ( events [ 0 ] ) . toBeInstanceOf ( TouchedChangeEvent ) ;
1440+ } ) ;
1441+
1442+ it ( 'formControl should only update self' , ( ) => {
1443+ const fixture = initTest ( FormGroupComp ) ;
1444+ const form = new FormGroup ( { 'login' : new FormControl ( '' , Validators . required ) } ) ;
1445+ const parentForm = new FormGroup ( { form} ) ;
1446+ fixture . componentInstance . form = form ;
1447+ fixture . detectChanges ( ) ;
1448+
1449+ const formGroupDir = fixture . debugElement . children [ 0 ] . injector . get ( FormGroupDirective ) ;
1450+
1451+ const events : ControlEvent [ ] = [ ] ;
1452+ fixture . componentInstance . form . events . subscribe ( ( event ) => events . push ( event ) ) ;
1453+ const parentEvents : ControlEvent [ ] = [ ] ;
1454+ parentForm . events . subscribe ( ( event ) => parentEvents . push ( event ) ) ;
1455+
1456+ formGroupDir . resetForm ( undefined , { onlySelf : true } ) ;
1457+
1458+ expect ( events . length ) . toBe ( 4 ) ;
1459+ expect ( events [ 0 ] ) . toBeInstanceOf ( TouchedChangeEvent ) ;
1460+ expect ( events [ 1 ] ) . toBeInstanceOf ( ValueChangeEvent ) ;
1461+ expect ( events [ 2 ] ) . toBeInstanceOf ( StatusChangeEvent ) ;
1462+
1463+ // The event that matters
1464+ expect ( events [ 3 ] ) . toBeInstanceOf ( FormResetEvent ) ;
1465+ expect ( events [ 3 ] . source ) . toBe ( form ) ;
1466+
1467+ // Self:false = parent won't receive the events
1468+ expect ( parentEvents . length ) . toBe ( 0 ) ;
1469+
1470+ // Not only self
1471+ formGroupDir . resetForm ( { login : 'new value' } ) ;
1472+ expect ( parentEvents . length ) . toBe ( 3 ) ;
1473+ expect ( parentEvents [ 0 ] ) . toBeInstanceOf ( TouchedChangeEvent ) ;
1474+ expect ( parentEvents [ 1 ] ) . toBeInstanceOf ( ValueChangeEvent ) ;
1475+ expect ( parentEvents [ 2 ] ) . toBeInstanceOf ( StatusChangeEvent ) ;
1476+ } ) ;
1477+
14251478 it ( 'formControl should emit an event when submitting a form' , ( ) => {
14261479 const fixture = initTest ( FormGroupComp ) ;
14271480 const form = new FormGroup ( { 'login' : new FormControl ( '' , Validators . required ) } ) ;
0 commit comments