@@ -20,6 +20,8 @@ import {ComponentFixture, fakeAsync, flush, TestBed, tick} from '@angular/core/t
20
20
import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
21
21
import { By } from '@angular/platform-browser' ;
22
22
import { MatChipListbox , MatChipOption , MatChipsModule } from './index' ;
23
+ import { asyncScheduler , BehaviorSubject , Observable } from 'rxjs' ;
24
+ import { observeOn } from 'rxjs/operators' ;
23
25
24
26
describe ( 'MDC-based MatChipListbox' , ( ) => {
25
27
let fixture : ComponentFixture < any > ;
@@ -462,8 +464,8 @@ describe('MDC-based MatChipListbox', () => {
462
464
fixture . detectChanges ( ) ;
463
465
464
466
expect ( fixture . componentInstance . chipListbox . value )
465
- . withContext ( 'Expect value to be undefined ' )
466
- . toBeUndefined ( ) ;
467
+ . withContext ( 'Expect value to be null ' )
468
+ . toBeNull ( ) ;
467
469
expect ( array [ 2 ] . selected ) . withContext ( 'Expect disabled chip not selected' ) . toBeFalsy ( ) ;
468
470
expect ( fixture . componentInstance . chipListbox . selected )
469
471
. withContext ( 'Expect no selected chips' )
@@ -828,6 +830,60 @@ describe('MDC-based MatChipListbox', () => {
828
830
. toBeFalsy ( ) ;
829
831
} ) ;
830
832
} ) ;
833
+
834
+ describe ( 'async multiple selection' , ( ) => {
835
+ it ( 'should select initial async chips' , fakeAsync ( ( ) => {
836
+ fixture = createComponent ( AsyncMultiSelectionChipListbox , undefined , initFixture => {
837
+ initFixture . componentInstance . control = new FormControl ( [ 'tutorial-1' , 'tutorial-2' ] ) ;
838
+ } ) ;
839
+ fixture . detectChanges ( ) ;
840
+ flush ( ) ;
841
+
842
+ tick ( 400 ) ;
843
+ fixture . detectChanges ( ) ;
844
+
845
+ let array = fixture . componentInstance . chips . toArray ( ) ;
846
+
847
+ expect ( array . length ) . withContext ( 'Expect chips not to be rendered yet' ) . toBe ( 0 ) ;
848
+
849
+ tick ( 100 ) ;
850
+ fixture . detectChanges ( ) ;
851
+
852
+ array = fixture . componentInstance . chips . toArray ( ) ;
853
+ flush ( ) ;
854
+
855
+ expect ( array [ 0 ] . selected )
856
+ . withContext ( 'Expect "tutorial-1" chip to be selected' )
857
+ . toBe ( true ) ;
858
+ expect ( array [ 1 ] . selected )
859
+ . withContext ( 'Expect "tutorial-2" chip to be selected' )
860
+ . toBe ( true ) ;
861
+ } ) ) ;
862
+
863
+ it ( 'should select async chips that changed over time' , fakeAsync ( ( ) => {
864
+ fixture = createComponent ( AsyncMultiSelectionChipListbox , undefined , initFixture => {
865
+ initFixture . componentInstance . control = new FormControl ( [ 'tutorial-1' ] ) ;
866
+ } ) ;
867
+ fixture . detectChanges ( ) ;
868
+ flush ( ) ;
869
+
870
+ tick ( 500 ) ;
871
+ fixture . detectChanges ( ) ;
872
+
873
+ fixture . componentInstance . control . setValue ( [ 'tutorial-4' ] ) ;
874
+ fixture . componentInstance . updateChips ( [ 'tutorial-3' , 'tutorial-4' ] ) ;
875
+
876
+ tick ( 500 ) ;
877
+ fixture . detectChanges ( ) ;
878
+
879
+ const array = fixture . componentInstance . chips . toArray ( ) ;
880
+ flush ( ) ;
881
+
882
+ expect ( array [ 1 ] . selected )
883
+ . withContext ( 'Expect "tutorial-4" chip to be selected' )
884
+ . toBe ( true ) ;
885
+ } ) ) ;
886
+ } ) ;
831
887
} ) ;
832
888
} ) ;
833
889
@@ -947,6 +1003,27 @@ class MultiSelectionChipListbox {
947
1003
@ViewChildren ( MatChipOption ) chips : QueryList < MatChipOption > ;
948
1004
}
949
1005
1006
+ @Component ( {
1007
+ template : `
1008
+ <mat-chip-listbox [multiple]="true" [formControl]="control">
1009
+ <mat-chip-option *ngFor="let chip of chips$ | async" [value]="chip">
1010
+ {{ chip }}
1011
+ </mat-chip-option>
1012
+ </mat-chip-listbox>
1013
+ ` ,
1014
+ } )
1015
+ class AsyncMultiSelectionChipListbox {
1016
+ private _chipsSubject = new BehaviorSubject ( [ 'tutorial-1' , 'tutorial-2' , 'tutorial-3' ] ) ;
1017
+ chips$ : Observable < string [ ] > = this . _chipsSubject . pipe ( observeOn ( asyncScheduler , 500 ) ) ;
1018
+ control = new FormControl < string [ ] | null > ( null ) ;
1019
+ @ViewChild ( MatChipListbox ) chipListbox : MatChipListbox ;
1020
+ @ViewChildren ( MatChipOption ) chips : QueryList < MatChipOption > ;
1021
+
1022
+ updateChips ( chips : string [ ] ) : void {
1023
+ this . _chipsSubject . next ( chips ) ;
1024
+ }
1025
+ }
1026
+
950
1027
@Component ( {
951
1028
template : `
952
1029
<mat-chip-listbox [formControl]="control">
0 commit comments