@@ -556,7 +556,9 @@ describe('MatSelectionList with forms', () => {
556
556
imports : [ MatListModule , FormsModule , ReactiveFormsModule ] ,
557
557
declarations : [
558
558
SelectionListWithModel ,
559
- SelectionListWithFormControl
559
+ SelectionListWithFormControl ,
560
+ SelectionListWithPreselectedOption ,
561
+ SelectionListWithPreselectedOptionAndModel
560
562
]
561
563
} ) ;
562
564
@@ -719,6 +721,34 @@ describe('MatSelectionList with forms', () => {
719
721
expect ( listOptions [ 2 ] . selected ) . toBe ( true , 'Expected third option to be selected.' ) ;
720
722
} ) ;
721
723
} ) ;
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
+ } ) ;
722
752
} ) ;
723
753
724
754
@@ -842,3 +872,27 @@ class SelectionListWithModel {
842
872
class SelectionListWithFormControl {
843
873
formControl = new FormControl ( ) ;
844
874
}
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