@@ -556,7 +556,9 @@ describe('MatSelectionList with forms', () => {
556556 imports : [ MatListModule , FormsModule , ReactiveFormsModule ] ,
557557 declarations : [
558558 SelectionListWithModel ,
559- SelectionListWithFormControl
559+ SelectionListWithFormControl ,
560+ SelectionListWithPreselectedOption ,
561+ SelectionListWithPreselectedOptionAndModel
560562 ]
561563 } ) ;
562564
@@ -719,6 +721,34 @@ describe('MatSelectionList with forms', () => {
719721 expect ( listOptions [ 2 ] . selected ) . toBe ( true , 'Expected third option to be selected.' ) ;
720722 } ) ;
721723 } ) ;
724+
725+ describe ( 'preselected values' , ( ) => {
726+ it ( 'should add preselected options to the model value' , fakeAsync ( ( ) => {
727+ const fixture = TestBed . createComponent ( SelectionListWithPreselectedOption ) ;
728+ const listOptions = fixture . debugElement . queryAll ( By . directive ( MatListOption ) )
729+ . map ( optionDebugEl => optionDebugEl . componentInstance ) ;
730+
731+ fixture . detectChanges ( ) ;
732+ tick ( ) ;
733+
734+ expect ( listOptions [ 1 ] . selected ) . toBe ( true ) ;
735+ expect ( fixture . componentInstance . selectedOptions ) . toEqual ( [ 'opt2' ] ) ;
736+ } ) ) ;
737+
738+ it ( 'should handle preselected option both through the model and the view' , fakeAsync ( ( ) => {
739+ const fixture = TestBed . createComponent ( SelectionListWithPreselectedOptionAndModel ) ;
740+ const listOptions = fixture . debugElement . queryAll ( By . directive ( MatListOption ) )
741+ . map ( optionDebugEl => optionDebugEl . componentInstance ) ;
742+
743+ fixture . detectChanges ( ) ;
744+ tick ( ) ;
745+
746+ expect ( listOptions [ 0 ] . selected ) . toBe ( true ) ;
747+ expect ( listOptions [ 1 ] . selected ) . toBe ( true ) ;
748+ expect ( fixture . componentInstance . selectedOptions ) . toEqual ( [ 'opt1' , 'opt2' ] ) ;
749+ } ) ) ;
750+
751+ } ) ;
722752} ) ;
723753
724754
@@ -842,3 +872,27 @@ class SelectionListWithModel {
842872class SelectionListWithFormControl {
843873 formControl = new FormControl ( ) ;
844874}
875+
876+
877+ @Component ( {
878+ template : `
879+ <mat-selection-list [(ngModel)]="selectedOptions">
880+ <mat-list-option value="opt1">Option 1</mat-list-option>
881+ <mat-list-option value="opt2" selected>Option 2</mat-list-option>
882+ </mat-selection-list>`
883+ } )
884+ class SelectionListWithPreselectedOption {
885+ selectedOptions : string [ ] ;
886+ }
887+
888+
889+ @Component ( {
890+ template : `
891+ <mat-selection-list [(ngModel)]="selectedOptions">
892+ <mat-list-option value="opt1">Option 1</mat-list-option>
893+ <mat-list-option value="opt2" selected>Option 2</mat-list-option>
894+ </mat-selection-list>`
895+ } )
896+ class SelectionListWithPreselectedOptionAndModel {
897+ selectedOptions = [ 'opt1' ] ;
898+ }
0 commit comments