@@ -2,7 +2,7 @@ import { expectTypeOf } from 'expect-type';
22import { FormGroup } from './form-group' ;
33import { FormControl } from './form-control' ;
44import { FormArray } from './form-array' ;
5- import { AbstractControl } from '@angular/forms' ;
5+ import { AbstractControl , Validators } from '@angular/forms' ;
66import { Observable , of , Subject , Subscription } from 'rxjs' ;
77import { ControlsOf } from '..' ;
88import { ValuesOf } from './types' ;
@@ -68,6 +68,32 @@ describe('FormGroup Functionality', () => {
6868 expect ( spy ) . toHaveBeenCalledTimes ( 2 ) ;
6969 } ) ;
7070
71+ it ( 'should invalidChanges$' , ( ) => {
72+ const control = new FormGroup ( {
73+ name : new FormControl < string | null > ( null , Validators . required ) ,
74+ } ) ;
75+ const spy = jest . fn ( ) ;
76+ control . invalid$ . subscribe ( spy ) ;
77+ expect ( spy ) . toHaveBeenCalledWith ( true ) ;
78+ control . setValue ( { name : 'abc' } ) ;
79+ expect ( spy ) . toHaveBeenCalledWith ( false ) ;
80+ control . setValue ( { name : null } ) ;
81+ expect ( spy ) . toHaveBeenCalledTimes ( 3 ) ;
82+ } ) ;
83+
84+ it ( 'should validChanges$' , ( ) => {
85+ const control = new FormGroup ( {
86+ name : new FormControl < string | null > ( null , Validators . required ) ,
87+ } ) ;
88+ const spy = jest . fn ( ) ;
89+ control . valid$ . subscribe ( spy ) ;
90+ expect ( spy ) . toHaveBeenCalledWith ( false ) ;
91+ control . setValue ( { name : 'abc' } ) ;
92+ expect ( spy ) . toHaveBeenCalledWith ( true ) ;
93+ control . setValue ( { name : null } ) ;
94+ expect ( spy ) . toHaveBeenCalledTimes ( 3 ) ;
95+ } ) ;
96+
7197 it ( 'should statusChanges$' , ( ) => {
7298 const control = createGroup ( ) ;
7399 const spy = jest . fn ( ) ;
@@ -198,7 +224,9 @@ describe('FormGroup Functionality', () => {
198224
199225 function areAllAllChildrenDirty ( control : AbstractControl ) {
200226 expect ( control . dirty ) . toBe ( true ) ;
201- ( control as any ) . _forEachChild ( ( control : AbstractControl ) => areAllAllChildrenDirty ( control ) ) ;
227+ ( control as any ) . _forEachChild ( ( control : AbstractControl ) =>
228+ areAllAllChildrenDirty ( control )
229+ ) ;
202230 }
203231
204232 it ( 'should markAllAsDirty' , ( ) => {
@@ -331,6 +359,8 @@ describe('FormGroup Types', () => {
331359
332360 expectTypeOf ( group . disabled$ ) . toEqualTypeOf < Observable < boolean > > ( ) ;
333361 expectTypeOf ( group . enabled$ ) . toEqualTypeOf < Observable < boolean > > ( ) ;
362+ expectTypeOf ( group . invalid$ ) . toEqualTypeOf < Observable < boolean > > ( ) ;
363+ expectTypeOf ( group . valid$ ) . toEqualTypeOf < Observable < boolean > > ( ) ;
334364 expectTypeOf ( group . status$ ) . toEqualTypeOf < Observable < ControlState > > ( ) ;
335365
336366 const name$ = group . select ( ( state ) => {
@@ -467,22 +497,19 @@ describe('FormGroup Types', () => {
467497 } ) ;
468498} ) ;
469499
470-
471-
472500describe ( 'ControlsOf' , ( ) => {
473-
474501 it ( 'should infer the type' , ( ) => {
475502 interface Foo {
476503 str : string ;
477504 nested : {
478505 one : string ;
479- two : number ,
506+ two : number ;
480507 deep : {
481508 id : number ;
482- arr : string [ ]
483- }
484- } ,
485- arr : string [ ]
509+ arr : string [ ] ;
510+ } ;
511+ } ;
512+ arr : string [ ] ;
486513 }
487514
488515 const group = new FormGroup < ControlsOf < Foo > > ( {
@@ -492,56 +519,57 @@ describe('ControlsOf', () => {
492519 two : new FormControl ( ) ,
493520 deep : new FormGroup ( {
494521 id : new FormControl ( 1 ) ,
495- arr : new FormArray ( [ ] )
496- } )
522+ arr : new FormArray ( [ ] ) ,
523+ } ) ,
497524 } ) ,
498- arr : new FormArray ( [ ] )
525+ arr : new FormArray ( [ ] ) ,
499526 } ) ;
500527
501528 expectTypeOf ( group . value ) . toEqualTypeOf < Foo > ( ) ;
502529
503530 expectTypeOf ( group . get ( 'str' ) ) . toEqualTypeOf < FormControl < string > > ( ) ;
504- expectTypeOf ( group . get ( 'nested' ) ) . toEqualTypeOf < FormGroup < ControlsOf < Foo [ 'nested' ] > > > ( ) ;
505- expectTypeOf ( group . get ( 'arr' ) ) . toEqualTypeOf < FormArray < string , FormControl < string > > > ( ) ;
531+ expectTypeOf ( group . get ( 'nested' ) ) . toEqualTypeOf <
532+ FormGroup < ControlsOf < Foo [ 'nested' ] > >
533+ > ( ) ;
534+ expectTypeOf ( group . get ( 'arr' ) ) . toEqualTypeOf <
535+ FormArray < string , FormControl < string > >
536+ > ( ) ;
506537
507538 expectTypeOf ( group . get ( 'nested' ) . value ) . toEqualTypeOf < Foo [ 'nested' ] > ( ) ;
508539 expectTypeOf ( group . get ( 'arr' ) . value ) . toEqualTypeOf < Foo [ 'arr' ] > ( ) ;
509540
510-
511541 new FormGroup < ControlsOf < Foo > > ( {
512542 // @ts -expect-error - should be typed
513543 str : new FormControl ( 1 ) ,
514544 // @ts -expect-error - should be typed
515545 nested : new FormGroup ( {
516546 // one: new FormControl(''),
517- two : new FormControl ( )
547+ two : new FormControl ( ) ,
518548 } ) ,
519549 // @ts -expect-error - should be typed
520- arr : new FormArray ( [ new FormControl ( 1 ) ] )
521- } )
522- } )
550+ arr : new FormArray ( [ new FormControl ( 1 ) ] ) ,
551+ } ) ;
552+ } ) ;
523553
524554 it ( 'should allow FormControls as objects or arrays' , ( ) => {
525-
526555 interface Bar {
527556 str : string ;
528557 controlGroup : FormControl < {
529558 one : string ;
530- two : number
531- } > ,
532- controlArr : FormControl < string [ ] > ,
559+ two : number ;
560+ } > ;
561+ controlArr : FormControl < string [ ] > ;
533562 group : {
534563 id : string ;
535564 deep : {
536565 id : number ;
537- arr : FormControl < string [ ] >
538- }
539- }
540- arr : string [ ] ,
541- arrGroup : Array < { name : string , count : number } > ;
566+ arr : FormControl < string [ ] > ;
567+ } ;
568+ } ;
569+ arr : string [ ] ;
570+ arrGroup : Array < { name : string ; count : number } > ;
542571 }
543572
544-
545573 const group = new FormGroup < ControlsOf < Bar > > ( {
546574 str : new FormControl ( '' ) ,
547575 controlGroup : new FormControl ( { one : '' , two : 1 } ) ,
@@ -550,31 +578,32 @@ describe('ControlsOf', () => {
550578 id : new FormControl ( ) ,
551579 deep : new FormGroup ( {
552580 id : new FormControl ( ) ,
553- arr : new FormControl ( [ ] )
554- } )
581+ arr : new FormControl ( [ ] ) ,
582+ } ) ,
555583 } ) ,
556584 arr : new FormArray ( [ ] ) ,
557- arrGroup : new FormArray ( [ ] )
585+ arrGroup : new FormArray ( [ ] ) ,
558586 } ) ;
559587
560588 expectTypeOf ( group . value ) . toEqualTypeOf < ValuesOf < ControlsOf < Bar > > > ( ) ;
561589
562590 new FormGroup < ControlsOf < Bar > > ( {
563591 str : new FormControl ( '' ) ,
564592 // @ts -expect-error - should be FormControl
565- controlGroup : new FormGroup ( { one : new FormControl ( '' ) , two : new FormControl ( ) } ) ,
593+ controlGroup : new FormGroup ( {
594+ one : new FormControl ( '' ) ,
595+ two : new FormControl ( ) ,
596+ } ) ,
566597 // @ts -expect-error - should be FormControl
567598 controlArr : new FormArray ( [ ] ) ,
568599 // @ts -expect-error - should be FormGroup
569600 group : new FormControl ( ) ,
570601 // @ts -expect-error - should be FormArray
571602 arr : new FormControl ( [ ] ) ,
572603 // @ts -expect-error - should be FormArray
573- arrGroup : new FormControl ( [ ] )
604+ arrGroup : new FormControl ( [ ] ) ,
574605 } ) ;
575-
576- } )
577-
606+ } ) ;
578607
579608 it ( 'should work with optional fields' , ( ) => {
580609 type Foo = {
@@ -583,29 +612,30 @@ describe('ControlsOf', () => {
583612 baz : null | string ;
584613 arr ?: string [ ] ;
585614 nested : {
586- id : string
587- }
588- }
615+ id : string ;
616+ } ;
617+ } ;
589618
590619 const group = new FormGroup < ControlsOf < Foo > > ( {
591620 foo : new FormControl ( '' ) ,
592621 name : new FormControl ( '' ) ,
593622 baz : new FormControl ( null ) ,
594623 arr : new FormArray ( [ ] ) ,
595624 nested : new FormGroup ( {
596- id : new FormControl ( '' )
597- } )
598- } )
625+ id : new FormControl ( '' ) ,
626+ } ) ,
627+ } ) ;
599628
600629 // @ts -expect-error - should be a string
601630 group . get ( 'name' ) ?. patchValue ( 1 ) ;
602631
603- expectTypeOf ( group . get ( 'name' ) ) . toEqualTypeOf < FormControl < string | undefined > | undefined > ( ) ;
632+ expectTypeOf ( group . get ( 'name' ) ) . toEqualTypeOf <
633+ FormControl < string | undefined > | undefined
634+ > ( ) ;
604635
605636 expectTypeOf ( group . value . name ) . toEqualTypeOf < string | undefined > ( ) ;
606637 expectTypeOf ( group . value . arr ) . toEqualTypeOf < string [ ] | undefined > ( ) ;
607638 expectTypeOf ( group . value . baz ) . toEqualTypeOf < string | null > ( ) ;
608639 expectTypeOf ( group . value . nested ) . toEqualTypeOf < { id : string } > ( ) ;
609- } )
610-
640+ } ) ;
611641} ) ;
0 commit comments