@@ -2,7 +2,7 @@ import { expectTypeOf } from 'expect-type';
2
2
import { FormGroup } from './form-group' ;
3
3
import { FormControl } from './form-control' ;
4
4
import { FormArray } from './form-array' ;
5
- import { AbstractControl } from '@angular/forms' ;
5
+ import { AbstractControl , Validators } from '@angular/forms' ;
6
6
import { Observable , of , Subject , Subscription } from 'rxjs' ;
7
7
import { ControlsOf } from '..' ;
8
8
import { ValuesOf } from './types' ;
@@ -68,6 +68,32 @@ describe('FormGroup Functionality', () => {
68
68
expect ( spy ) . toHaveBeenCalledTimes ( 2 ) ;
69
69
} ) ;
70
70
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
+
71
97
it ( 'should statusChanges$' , ( ) => {
72
98
const control = createGroup ( ) ;
73
99
const spy = jest . fn ( ) ;
@@ -198,7 +224,9 @@ describe('FormGroup Functionality', () => {
198
224
199
225
function areAllAllChildrenDirty ( control : AbstractControl ) {
200
226
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
+ ) ;
202
230
}
203
231
204
232
it ( 'should markAllAsDirty' , ( ) => {
@@ -331,6 +359,8 @@ describe('FormGroup Types', () => {
331
359
332
360
expectTypeOf ( group . disabled$ ) . toEqualTypeOf < Observable < boolean > > ( ) ;
333
361
expectTypeOf ( group . enabled$ ) . toEqualTypeOf < Observable < boolean > > ( ) ;
362
+ expectTypeOf ( group . invalid$ ) . toEqualTypeOf < Observable < boolean > > ( ) ;
363
+ expectTypeOf ( group . valid$ ) . toEqualTypeOf < Observable < boolean > > ( ) ;
334
364
expectTypeOf ( group . status$ ) . toEqualTypeOf < Observable < ControlState > > ( ) ;
335
365
336
366
const name$ = group . select ( ( state ) => {
@@ -467,22 +497,19 @@ describe('FormGroup Types', () => {
467
497
} ) ;
468
498
} ) ;
469
499
470
-
471
-
472
500
describe ( 'ControlsOf' , ( ) => {
473
-
474
501
it ( 'should infer the type' , ( ) => {
475
502
interface Foo {
476
503
str : string ;
477
504
nested : {
478
505
one : string ;
479
- two : number ,
506
+ two : number ;
480
507
deep : {
481
508
id : number ;
482
- arr : string [ ]
483
- }
484
- } ,
485
- arr : string [ ]
509
+ arr : string [ ] ;
510
+ } ;
511
+ } ;
512
+ arr : string [ ] ;
486
513
}
487
514
488
515
const group = new FormGroup < ControlsOf < Foo > > ( {
@@ -492,56 +519,57 @@ describe('ControlsOf', () => {
492
519
two : new FormControl ( ) ,
493
520
deep : new FormGroup ( {
494
521
id : new FormControl ( 1 ) ,
495
- arr : new FormArray ( [ ] )
496
- } )
522
+ arr : new FormArray ( [ ] ) ,
523
+ } ) ,
497
524
} ) ,
498
- arr : new FormArray ( [ ] )
525
+ arr : new FormArray ( [ ] ) ,
499
526
} ) ;
500
527
501
528
expectTypeOf ( group . value ) . toEqualTypeOf < Foo > ( ) ;
502
529
503
530
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
+ > ( ) ;
506
537
507
538
expectTypeOf ( group . get ( 'nested' ) . value ) . toEqualTypeOf < Foo [ 'nested' ] > ( ) ;
508
539
expectTypeOf ( group . get ( 'arr' ) . value ) . toEqualTypeOf < Foo [ 'arr' ] > ( ) ;
509
540
510
-
511
541
new FormGroup < ControlsOf < Foo > > ( {
512
542
// @ts -expect-error - should be typed
513
543
str : new FormControl ( 1 ) ,
514
544
// @ts -expect-error - should be typed
515
545
nested : new FormGroup ( {
516
546
// one: new FormControl(''),
517
- two : new FormControl ( )
547
+ two : new FormControl ( ) ,
518
548
} ) ,
519
549
// @ts -expect-error - should be typed
520
- arr : new FormArray ( [ new FormControl ( 1 ) ] )
521
- } )
522
- } )
550
+ arr : new FormArray ( [ new FormControl ( 1 ) ] ) ,
551
+ } ) ;
552
+ } ) ;
523
553
524
554
it ( 'should allow FormControls as objects or arrays' , ( ) => {
525
-
526
555
interface Bar {
527
556
str : string ;
528
557
controlGroup : FormControl < {
529
558
one : string ;
530
- two : number
531
- } > ,
532
- controlArr : FormControl < string [ ] > ,
559
+ two : number ;
560
+ } > ;
561
+ controlArr : FormControl < string [ ] > ;
533
562
group : {
534
563
id : string ;
535
564
deep : {
536
565
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 } > ;
542
571
}
543
572
544
-
545
573
const group = new FormGroup < ControlsOf < Bar > > ( {
546
574
str : new FormControl ( '' ) ,
547
575
controlGroup : new FormControl ( { one : '' , two : 1 } ) ,
@@ -550,31 +578,32 @@ describe('ControlsOf', () => {
550
578
id : new FormControl ( ) ,
551
579
deep : new FormGroup ( {
552
580
id : new FormControl ( ) ,
553
- arr : new FormControl ( [ ] )
554
- } )
581
+ arr : new FormControl ( [ ] ) ,
582
+ } ) ,
555
583
} ) ,
556
584
arr : new FormArray ( [ ] ) ,
557
- arrGroup : new FormArray ( [ ] )
585
+ arrGroup : new FormArray ( [ ] ) ,
558
586
} ) ;
559
587
560
588
expectTypeOf ( group . value ) . toEqualTypeOf < ValuesOf < ControlsOf < Bar > > > ( ) ;
561
589
562
590
new FormGroup < ControlsOf < Bar > > ( {
563
591
str : new FormControl ( '' ) ,
564
592
// @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
+ } ) ,
566
597
// @ts -expect-error - should be FormControl
567
598
controlArr : new FormArray ( [ ] ) ,
568
599
// @ts -expect-error - should be FormGroup
569
600
group : new FormControl ( ) ,
570
601
// @ts -expect-error - should be FormArray
571
602
arr : new FormControl ( [ ] ) ,
572
603
// @ts -expect-error - should be FormArray
573
- arrGroup : new FormControl ( [ ] )
604
+ arrGroup : new FormControl ( [ ] ) ,
574
605
} ) ;
575
-
576
- } )
577
-
606
+ } ) ;
578
607
579
608
it ( 'should work with optional fields' , ( ) => {
580
609
type Foo = {
@@ -583,29 +612,30 @@ describe('ControlsOf', () => {
583
612
baz : null | string ;
584
613
arr ?: string [ ] ;
585
614
nested : {
586
- id : string
587
- }
588
- }
615
+ id : string ;
616
+ } ;
617
+ } ;
589
618
590
619
const group = new FormGroup < ControlsOf < Foo > > ( {
591
620
foo : new FormControl ( '' ) ,
592
621
name : new FormControl ( '' ) ,
593
622
baz : new FormControl ( null ) ,
594
623
arr : new FormArray ( [ ] ) ,
595
624
nested : new FormGroup ( {
596
- id : new FormControl ( '' )
597
- } )
598
- } )
625
+ id : new FormControl ( '' ) ,
626
+ } ) ,
627
+ } ) ;
599
628
600
629
// @ts -expect-error - should be a string
601
630
group . get ( 'name' ) ?. patchValue ( 1 ) ;
602
631
603
- expectTypeOf ( group . get ( 'name' ) ) . toEqualTypeOf < FormControl < string | undefined > | undefined > ( ) ;
632
+ expectTypeOf ( group . get ( 'name' ) ) . toEqualTypeOf <
633
+ FormControl < string | undefined > | undefined
634
+ > ( ) ;
604
635
605
636
expectTypeOf ( group . value . name ) . toEqualTypeOf < string | undefined > ( ) ;
606
637
expectTypeOf ( group . value . arr ) . toEqualTypeOf < string [ ] | undefined > ( ) ;
607
638
expectTypeOf ( group . value . baz ) . toEqualTypeOf < string | null > ( ) ;
608
639
expectTypeOf ( group . value . nested ) . toEqualTypeOf < { id : string } > ( ) ;
609
- } )
610
-
640
+ } ) ;
611
641
} ) ;
0 commit comments