@@ -18,6 +18,8 @@ import {ComponentFixture, TestBed, fakeAsync, flush, tick} from '@angular/core/t
1818import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
1919import { By } from '@angular/platform-browser' ;
2020import { MatChipListbox , MatChipOption , MatChipsModule } from './index' ;
21+ import { asyncScheduler , BehaviorSubject , Observable } from 'rxjs' ;
22+ import { observeOn } from 'rxjs/operators' ;
2123
2224describe ( 'MatChipListbox' , ( ) => {
2325 let fixture : ComponentFixture < any > ;
@@ -862,6 +864,60 @@ describe('MatChipListbox', () => {
862864 . toBeFalsy ( ) ;
863865 } ) ;
864866 } ) ;
867+
868+ describe ( 'async multiple selection' , ( ) => {
869+ it ( 'should select initial async chips' , fakeAsync ( ( ) => {
870+ fixture = createComponent ( AsyncMultiSelectionChipListbox , undefined , initFixture => {
871+ initFixture . componentInstance . control = new FormControl ( [ 'tutorial-1' , 'tutorial-2' ] ) ;
872+ } ) ;
873+ fixture . detectChanges ( ) ;
874+ flush ( ) ;
875+
876+ tick ( 400 ) ;
877+ fixture . detectChanges ( ) ;
878+
879+ let array = fixture . componentInstance . chips . toArray ( ) ;
880+
881+ expect ( array . length ) . withContext ( 'Expect chips not to be rendered yet' ) . toBe ( 0 ) ;
882+
883+ tick ( 100 ) ;
884+ fixture . detectChanges ( ) ;
885+
886+ array = fixture . componentInstance . chips . toArray ( ) ;
887+ flush ( ) ;
888+
889+ expect ( array [ 0 ] . selected )
890+ . withContext ( 'Expect "tutorial-1" chip to be selected' )
891+ . toBe ( true ) ;
892+ expect ( array [ 1 ] . selected )
893+ . withContext ( 'Expect "tutorial-2" chip to be selected' )
894+ . toBe ( true ) ;
895+ } ) ) ;
896+
897+ it ( 'should select async chips that changed over time' , fakeAsync ( ( ) => {
898+ fixture = createComponent ( AsyncMultiSelectionChipListbox , undefined , initFixture => {
899+ initFixture . componentInstance . control = new FormControl ( [ 'tutorial-1' ] ) ;
900+ } ) ;
901+ fixture . detectChanges ( ) ;
902+ flush ( ) ;
903+
904+ tick ( 500 ) ;
905+ fixture . detectChanges ( ) ;
906+
907+ fixture . componentInstance . control . setValue ( [ 'tutorial-4' ] ) ;
908+ fixture . componentInstance . updateChips ( [ 'tutorial-3' , 'tutorial-4' ] ) ;
909+
910+ tick ( 500 ) ;
911+ fixture . detectChanges ( ) ;
912+
913+ const array = fixture . componentInstance . chips . toArray ( ) ;
914+ flush ( ) ;
915+
916+ expect ( array [ 1 ] . selected )
917+ . withContext ( 'Expect "tutorial-4" chip to be selected' )
918+ . toBe ( true ) ;
919+ } ) ) ;
920+ } ) ;
865921 } ) ;
866922 } ) ;
867923
@@ -986,6 +1042,27 @@ class MultiSelectionChipListbox {
9861042 @ViewChildren ( MatChipOption ) chips : QueryList < MatChipOption > ;
9871043}
9881044
1045+ @Component ( {
1046+ template : `
1047+ <mat-chip-listbox [multiple]="true" [formControl]="control">
1048+ <mat-chip-option *ngFor="let chip of chips$ | async" [value]="chip">
1049+ {{ chip }}
1050+ </mat-chip-option>
1051+ </mat-chip-listbox>
1052+ ` ,
1053+ } )
1054+ class AsyncMultiSelectionChipListbox {
1055+ private _chipsSubject = new BehaviorSubject ( [ 'tutorial-1' , 'tutorial-2' , 'tutorial-3' ] ) ;
1056+ chips$ : Observable < string [ ] > = this . _chipsSubject . pipe ( observeOn ( asyncScheduler , 500 ) ) ;
1057+ control = new FormControl < string [ ] | null > ( null ) ;
1058+ @ViewChild ( MatChipListbox ) chipListbox : MatChipListbox ;
1059+ @ViewChildren ( MatChipOption ) chips : QueryList < MatChipOption > ;
1060+
1061+ updateChips ( chips : string [ ] ) : void {
1062+ this . _chipsSubject . next ( chips ) ;
1063+ }
1064+ }
1065+
9891066@Component ( {
9901067 template : `
9911068 <mat-chip-listbox [formControl]="control">
0 commit comments