@@ -188,6 +188,16 @@ describe('MatSelectionList without forms', () => {
188188 expect ( selectList . selected . length ) . toBe ( 0 ) ;
189189 } ) ;
190190
191+ it ( 'should not add the mat-list-single-selected-option class (in multiple mode)' , ( ) => {
192+ let testListItem = listOptions [ 2 ] . injector . get < MatListOption > ( MatListOption ) ;
193+
194+ testListItem . _handleClick ( ) ;
195+ fixture . detectChanges ( ) ;
196+
197+ expect ( listOptions [ 2 ] . nativeElement . classList . contains ( 'mat-list-single-selected-option' ) )
198+ . toBe ( false ) ;
199+ } ) ;
200+
191201 it ( 'should not allow selection of disabled items' , ( ) => {
192202 let testListItem = listOptions [ 0 ] . injector . get < MatListOption > ( MatListOption ) ;
193203 let selectList =
@@ -882,6 +892,80 @@ describe('MatSelectionList without forms', () => {
882892 expect ( listOption . classList ) . toContain ( 'mat-list-item-with-avatar' ) ;
883893 } ) ;
884894 } ) ;
895+
896+ describe ( 'with single selection' , ( ) => {
897+ let fixture : ComponentFixture < SelectionListWithListOptions > ;
898+ let listOption : DebugElement [ ] ;
899+ let selectionList : DebugElement ;
900+
901+ beforeEach ( async ( ( ) => {
902+ TestBed . configureTestingModule ( {
903+ imports : [ MatListModule ] ,
904+ declarations : [
905+ SelectionListWithListOptions ,
906+ ] ,
907+ } ) . compileComponents ( ) ;
908+
909+ fixture = TestBed . createComponent ( SelectionListWithListOptions ) ;
910+ fixture . componentInstance . multiple = false ;
911+ listOption = fixture . debugElement . queryAll ( By . directive ( MatListOption ) ) ;
912+ selectionList = fixture . debugElement . query ( By . directive ( MatSelectionList ) ) ! ;
913+ fixture . detectChanges ( ) ;
914+ } ) ) ;
915+
916+ it ( 'should select one option at a time' , ( ) => {
917+ const testListItem1 = listOption [ 1 ] . injector . get < MatListOption > ( MatListOption ) ;
918+ const testListItem2 = listOption [ 2 ] . injector . get < MatListOption > ( MatListOption ) ;
919+ const selectList =
920+ selectionList . injector . get < MatSelectionList > ( MatSelectionList ) . selectedOptions ;
921+
922+ expect ( selectList . selected . length ) . toBe ( 0 ) ;
923+
924+ dispatchFakeEvent ( testListItem1 . _getHostElement ( ) , 'click' ) ;
925+ fixture . detectChanges ( ) ;
926+
927+ expect ( selectList . selected ) . toEqual ( [ testListItem1 ] ) ;
928+ expect ( listOption [ 1 ] . nativeElement . classList . contains ( 'mat-list-single-selected-option' ) )
929+ . toBe ( true ) ;
930+
931+ dispatchFakeEvent ( testListItem2 . _getHostElement ( ) , 'click' ) ;
932+ fixture . detectChanges ( ) ;
933+
934+ expect ( selectList . selected ) . toEqual ( [ testListItem2 ] ) ;
935+ expect ( listOption [ 1 ] . nativeElement . classList . contains ( 'mat-list-single-selected-option' ) )
936+ . toBe ( false ) ;
937+ expect ( listOption [ 2 ] . nativeElement . classList . contains ( 'mat-list-single-selected-option' ) )
938+ . toBe ( true ) ;
939+ } ) ;
940+
941+ it ( 'should not show check boxes' , ( ) => {
942+ expect ( fixture . nativeElement . querySelector ( 'mat-pseudo-checkbox' ) ) . toBeFalsy ( ) ;
943+ } ) ;
944+
945+ it ( 'should not deselect the target option on click' , ( ) => {
946+ const testListItem1 = listOption [ 1 ] . injector . get < MatListOption > ( MatListOption ) ;
947+ const selectList =
948+ selectionList . injector . get < MatSelectionList > ( MatSelectionList ) . selectedOptions ;
949+
950+ expect ( selectList . selected . length ) . toBe ( 0 ) ;
951+
952+ dispatchFakeEvent ( testListItem1 . _getHostElement ( ) , 'click' ) ;
953+ fixture . detectChanges ( ) ;
954+
955+ expect ( selectList . selected ) . toEqual ( [ testListItem1 ] ) ;
956+
957+ dispatchFakeEvent ( testListItem1 . _getHostElement ( ) , 'click' ) ;
958+ fixture . detectChanges ( ) ;
959+
960+ expect ( selectList . selected ) . toEqual ( [ testListItem1 ] ) ;
961+ } ) ;
962+
963+ it ( 'throws an exception when toggling single/multiple mode after bootstrap' , ( ) => {
964+ fixture . componentInstance . multiple = true ;
965+ expect ( ( ) => fixture . detectChanges ( ) ) . toThrow ( new Error (
966+ 'Cannot change `multiple` mode of mat-selection-list after initialization.' ) ) ;
967+ } ) ;
968+ } ) ;
885969} ) ;
886970
887971describe ( 'MatSelectionList with forms' , ( ) => {
@@ -1255,7 +1339,8 @@ describe('MatSelectionList with forms', () => {
12551339 id="selection-list-1"
12561340 (selectionChange)="onValueChange($event)"
12571341 [disableRipple]="listRippleDisabled"
1258- [color]="selectionListColor">
1342+ [color]="selectionListColor"
1343+ [multiple]="multiple">
12591344 <mat-list-option checkboxPosition="before" disabled="true" value="inbox"
12601345 [color]="firstOptionColor">
12611346 Inbox (disabled selection-option)
@@ -1274,6 +1359,7 @@ describe('MatSelectionList with forms', () => {
12741359class SelectionListWithListOptions {
12751360 showLastOption : boolean = true ;
12761361 listRippleDisabled = false ;
1362+ multiple = true ;
12771363 selectionListColor : ThemePalette ;
12781364 firstOptionColor : ThemePalette ;
12791365
0 commit comments