@@ -737,30 +737,35 @@ describe('MDC-based MatChipListbox', () => {
737
737
} ) ;
738
738
739
739
describe ( 'multiple selection' , ( ) => {
740
- beforeEach ( ( ) => {
741
- fixture = createComponent ( MultiSelectionChipListbox ) ;
742
- chips = fixture . componentInstance . chips ;
743
- } ) ;
744
-
745
- it ( 'should take an initial view value with reactive forms' , ( ) => {
746
- fixture . componentInstance . control = new FormControl ( [ 'pizza-1' ] ) ;
740
+ it ( 'should take an initial view value with reactive forms' , fakeAsync ( ( ) => {
741
+ fixture = createComponent ( MultiSelectionChipListbox , undefined , initFixture => {
742
+ initFixture . componentInstance . control = new FormControl ( [ 'pizza-1' , 'pasta-6' ] ) ;
743
+ initFixture . componentInstance . selectable = true ;
744
+ } ) ;
747
745
fixture . detectChanges ( ) ;
746
+ flush ( ) ;
748
747
749
- const array = chips . toArray ( ) ;
748
+ const array = fixture . componentInstance . chips . toArray ( ) ;
750
749
751
- expect ( array [ 1 ] . selected ) . withContext ( 'Expect pizza-1 chip to be selected' ) . toBeTruthy ( ) ;
750
+ expect ( array [ 1 ] . selected ) . withContext ( 'Expect pizza-1 chip to be selected' ) . toBe ( true ) ;
751
+ expect ( array [ 6 ] . selected ) . withContext ( 'Expect pasta-6 chip to be selected' ) . toBe ( true ) ;
752
752
753
753
dispatchKeyboardEvent ( primaryActions [ 1 ] , 'keydown' , SPACE ) ;
754
754
fixture . detectChanges ( ) ;
755
+ flush ( ) ;
755
756
756
757
expect ( array [ 1 ] . selected )
757
- . withContext ( 'Expect chip to be not selected after toggle selected' )
758
- . toBeFalsy ( ) ;
759
- } ) ;
758
+ . withContext ( 'Expect pizza-1 chip to no longer be selected' )
759
+ . toBe ( false ) ;
760
+ expect ( array [ 6 ] . selected )
761
+ . withContext ( 'Expect pasta-6 chip to remain selected' )
762
+ . toBe ( true ) ;
763
+ } ) ) ;
760
764
761
765
it ( 'should set the view value from the form' , ( ) => {
766
+ fixture = createComponent ( MultiSelectionChipListbox ) ;
762
767
const chipListbox = fixture . componentInstance . chipListbox ;
763
- const array = chips . toArray ( ) ;
768
+ const array = fixture . componentInstance . chips . toArray ( ) ;
764
769
765
770
expect ( chipListbox . value )
766
771
. withContext ( 'Expect chip listbox to have no initial value' )
@@ -773,6 +778,8 @@ describe('MDC-based MatChipListbox', () => {
773
778
} ) ;
774
779
775
780
it ( 'should update the form value when the view changes' , ( ) => {
781
+ fixture = createComponent ( MultiSelectionChipListbox ) ;
782
+
776
783
expect ( fixture . componentInstance . control . value )
777
784
. withContext ( `Expected the control's value to be empty initially.` )
778
785
. toEqual ( null ) ;
@@ -786,8 +793,10 @@ describe('MDC-based MatChipListbox', () => {
786
793
} ) ;
787
794
788
795
it ( 'should clear the selection when a nonexistent option value is selected' , ( ) => {
789
- const array = chips . toArray ( ) ;
796
+ fixture = createComponent ( MultiSelectionChipListbox ) ;
797
+ chips = fixture . componentInstance . chips ;
790
798
799
+ const array = fixture . componentInstance . chips . toArray ( ) ;
791
800
fixture . componentInstance . control . setValue ( [ 'pizza-1' ] ) ;
792
801
fixture . detectChanges ( ) ;
793
802
@@ -805,7 +814,8 @@ describe('MDC-based MatChipListbox', () => {
805
814
} ) ;
806
815
807
816
it ( 'should clear the selection when the control is reset' , ( ) => {
808
- const array = chips . toArray ( ) ;
817
+ fixture = createComponent ( MultiSelectionChipListbox ) ;
818
+ const array = fixture . componentInstance . chips . toArray ( ) ;
809
819
810
820
fixture . componentInstance . control . setValue ( [ 'pizza-1' ] ) ;
811
821
fixture . detectChanges ( ) ;
@@ -824,6 +834,7 @@ describe('MDC-based MatChipListbox', () => {
824
834
function createComponent < T > (
825
835
component : Type < T > ,
826
836
direction : Direction = 'ltr' ,
837
+ beforeInitialChangeDetection ?: ( fixture : ComponentFixture < T > ) => void ,
827
838
) : ComponentFixture < T > {
828
839
directionality = {
829
840
value : direction ,
@@ -840,6 +851,7 @@ describe('MDC-based MatChipListbox', () => {
840
851
} ) . compileComponents ( ) ;
841
852
842
853
fixture = TestBed . createComponent < T > ( component ) ;
854
+ beforeInitialChangeDetection ?.( fixture ) ;
843
855
fixture . detectChanges ( ) ;
844
856
845
857
chipListboxDebugElement = fixture . debugElement . query ( By . directive ( MatChipListbox ) ) ! ;
@@ -926,7 +938,7 @@ class MultiSelectionChipListbox {
926
938
{ value : 'pasta-6' , viewValue : 'Pasta' } ,
927
939
{ value : 'sushi-7' , viewValue : 'Sushi' } ,
928
940
] ;
929
- control = new FormControl < string | null > ( null ) ;
941
+ control = new FormControl < string [ ] | null > ( null ) ;
930
942
isRequired : boolean ;
931
943
tabIndexOverride : number ;
932
944
selectable : boolean ;
0 commit comments