@@ -93,11 +93,13 @@ describe('MatSelectionList without forms', () => {
9393
9494 expect ( selectList . selected . length ) . toBe ( 0 ) ;
9595 expect ( listOptions [ 2 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'false' ) ;
96+ expect ( listOptions [ 2 ] . nativeElement . classList . contains ( 'mat-selected' ) ) . toBe ( false ) ;
9697
9798 testListItem . toggle ( ) ;
9899 fixture . detectChanges ( ) ;
99100
100101 expect ( listOptions [ 2 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'true' ) ;
102+ expect ( listOptions [ 2 ] . nativeElement . classList . contains ( 'mat-selected' ) ) . toBe ( true ) ;
101103 expect ( listOptions [ 2 ] . nativeElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'false' ) ;
102104 expect ( selectList . selected . length ) . toBe ( 1 ) ;
103105 } ) ;
@@ -110,7 +112,9 @@ describe('MatSelectionList without forms', () => {
110112
111113 expect ( selectList . selected . length ) . toBe ( 0 ) ;
112114 expect ( listOptions [ 2 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'false' ) ;
115+ expect ( listOptions [ 2 ] . nativeElement . classList . contains ( 'mat-selected' ) ) . toBe ( false ) ;
113116 expect ( listOptions [ 1 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'false' ) ;
117+ expect ( listOptions [ 1 ] . nativeElement . classList . contains ( 'mat-selected' ) ) . toBe ( false ) ;
114118
115119 testListItem . toggle ( ) ;
116120 fixture . detectChanges ( ) ;
@@ -120,7 +124,9 @@ describe('MatSelectionList without forms', () => {
120124
121125 expect ( selectList . selected . length ) . toBe ( 2 ) ;
122126 expect ( listOptions [ 2 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'true' ) ;
127+ expect ( listOptions [ 2 ] . nativeElement . classList . contains ( 'mat-selected' ) ) . toBe ( true ) ;
123128 expect ( listOptions [ 1 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'true' ) ;
129+ expect ( listOptions [ 1 ] . nativeElement . classList . contains ( 'mat-selected' ) ) . toBe ( true ) ;
124130 expect ( listOptions [ 1 ] . nativeElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'false' ) ;
125131 expect ( listOptions [ 2 ] . nativeElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'false' ) ;
126132 } ) ;
@@ -882,6 +888,97 @@ describe('MatSelectionList without forms', () => {
882888 expect ( listOption . classList ) . toContain ( 'mat-list-item-with-avatar' ) ;
883889 } ) ;
884890 } ) ;
891+
892+ describe ( 'with single selection' , ( ) => {
893+ let fixture : ComponentFixture < SelectionListWithListOptions > ;
894+ let listOption : DebugElement [ ] ;
895+ let selectionList : DebugElement ;
896+
897+ beforeEach ( async ( ( ) => {
898+ TestBed . configureTestingModule ( {
899+ imports : [ MatListModule ] ,
900+ declarations : [
901+ SelectionListWithListOptions ,
902+ ] ,
903+ } ) . compileComponents ( ) ;
904+
905+ fixture = TestBed . createComponent ( SelectionListWithListOptions ) ;
906+ fixture . componentInstance . multiple = false ;
907+ listOption = fixture . debugElement . queryAll ( By . directive ( MatListOption ) ) ;
908+ selectionList = fixture . debugElement . query ( By . directive ( MatSelectionList ) ) ! ;
909+ fixture . detectChanges ( ) ;
910+ } ) ) ;
911+
912+ it ( 'should select one option at a time' , ( ) => {
913+ const testListItem1 = listOption [ 1 ] . injector . get < MatListOption > ( MatListOption ) ;
914+ const testListItem2 = listOption [ 2 ] . injector . get < MatListOption > ( MatListOption ) ;
915+ const selectList =
916+ selectionList . injector . get < MatSelectionList > ( MatSelectionList ) . selectedOptions ;
917+
918+ expect ( selectList . selected . length ) . toBe ( 0 ) ;
919+
920+ dispatchFakeEvent ( testListItem1 . _getHostElement ( ) , 'click' ) ;
921+ fixture . detectChanges ( ) ;
922+
923+ expect ( selectList . selected ) . toEqual ( [ testListItem1 ] ) ;
924+
925+ dispatchFakeEvent ( testListItem2 . _getHostElement ( ) , 'click' ) ;
926+ fixture . detectChanges ( ) ;
927+
928+ expect ( selectList . selected ) . toEqual ( [ testListItem2 ] ) ;
929+ } ) ;
930+
931+ it ( 'should not show check boxes' , ( ) => {
932+ expect ( fixture . nativeElement . querySelector ( 'mat-pseudo-checkbox' ) ) . toBeFalsy ( ) ;
933+ } ) ;
934+
935+ it ( 'should not deselect the target option on click' , ( ) => {
936+ const testListItem1 = listOption [ 1 ] . injector . get < MatListOption > ( MatListOption ) ;
937+ const selectList =
938+ selectionList . injector . get < MatSelectionList > ( MatSelectionList ) . selectedOptions ;
939+
940+ expect ( selectList . selected . length ) . toBe ( 0 ) ;
941+
942+ dispatchFakeEvent ( testListItem1 . _getHostElement ( ) , 'click' ) ;
943+ fixture . detectChanges ( ) ;
944+
945+ expect ( selectList . selected ) . toEqual ( [ testListItem1 ] ) ;
946+
947+ dispatchFakeEvent ( testListItem1 . _getHostElement ( ) , 'click' ) ;
948+ fixture . detectChanges ( ) ;
949+
950+ expect ( selectList . selected ) . toEqual ( [ testListItem1 ] ) ;
951+ } ) ;
952+
953+ it ( 'sanely handles toggling single/multiple mode after bootstrap' , ( ) => {
954+ const testListItem1 = listOption [ 1 ] . injector . get < MatListOption > ( MatListOption ) ;
955+ const testListItem2 = listOption [ 2 ] . injector . get < MatListOption > ( MatListOption ) ;
956+ const selected = ( ) => selectionList . injector . get < MatSelectionList > ( MatSelectionList )
957+ . selectedOptions . selected ;
958+
959+ expect ( selected ( ) . length ) . toBe ( 0 ) ;
960+
961+ dispatchFakeEvent ( testListItem1 . _getHostElement ( ) , 'click' ) ;
962+ fixture . detectChanges ( ) ;
963+
964+ expect ( selected ( ) ) . toEqual ( [ testListItem1 ] ) ;
965+
966+ fixture . componentInstance . multiple = true ;
967+ fixture . detectChanges ( ) ;
968+
969+ expect ( selected ( ) ) . toEqual ( [ testListItem1 ] ) ;
970+
971+ dispatchFakeEvent ( testListItem2 . _getHostElement ( ) , 'click' ) ;
972+ fixture . detectChanges ( ) ;
973+
974+ expect ( selected ( ) ) . toEqual ( [ testListItem1 , testListItem2 ] ) ;
975+
976+ fixture . componentInstance . multiple = false ;
977+ fixture . detectChanges ( ) ;
978+
979+ expect ( selected ( ) ) . toEqual ( [ testListItem1 ] ) ;
980+ } ) ;
981+ } ) ;
885982} ) ;
886983
887984describe ( 'MatSelectionList with forms' , ( ) => {
@@ -1255,7 +1352,8 @@ describe('MatSelectionList with forms', () => {
12551352 id="selection-list-1"
12561353 (selectionChange)="onValueChange($event)"
12571354 [disableRipple]="listRippleDisabled"
1258- [color]="selectionListColor">
1355+ [color]="selectionListColor"
1356+ [multiple]="multiple">
12591357 <mat-list-option checkboxPosition="before" disabled="true" value="inbox"
12601358 [color]="firstOptionColor">
12611359 Inbox (disabled selection-option)
@@ -1274,6 +1372,7 @@ describe('MatSelectionList with forms', () => {
12741372class SelectionListWithListOptions {
12751373 showLastOption : boolean = true ;
12761374 listRippleDisabled = false ;
1375+ multiple = true ;
12771376 selectionListColor : ThemePalette ;
12781377 firstOptionColor : ThemePalette ;
12791378
0 commit comments