@@ -4,7 +4,7 @@ import {createKeyboardEvent, dispatchFakeEvent} from '@angular/cdk/testing';
44import { Component , DebugElement } from '@angular/core' ;
55import { async , ComponentFixture , inject , TestBed } from '@angular/core/testing' ;
66import { By } from '@angular/platform-browser' ;
7- import { MatListModule , MatListOption , MatSelectionList } from './index' ;
7+ import { MatListModule , MatListOption , MatSelectionList , MatListOptionChange } from './index' ;
88
99
1010describe ( 'MatSelectionList' , ( ) => {
@@ -483,9 +483,88 @@ describe('MatSelectionList', () => {
483483 expect ( listItemContent . nativeElement . classList ) . toContain ( 'mat-list-item-content-reverse' ) ;
484484 } ) ;
485485 } ) ;
486- } ) ;
487486
488487
488+ describe ( 'with multiple values' , ( ) => {
489+ let fixture : ComponentFixture < SelectionListWithMultipleValues > ;
490+ let listOption : DebugElement [ ] ;
491+ let listItemEl : DebugElement ;
492+ let selectionList : DebugElement ;
493+
494+ beforeEach ( async ( ( ) => {
495+ TestBed . configureTestingModule ( {
496+ imports : [ MatListModule ] ,
497+ declarations : [
498+ SelectionListWithMultipleValues
499+ ] ,
500+ } ) ;
501+
502+ TestBed . compileComponents ( ) ;
503+ } ) ) ;
504+
505+ beforeEach ( async ( ( ) => {
506+ fixture = TestBed . createComponent ( SelectionListWithMultipleValues ) ;
507+ listOption = fixture . debugElement . queryAll ( By . directive ( MatListOption ) ) ;
508+ listItemEl = fixture . debugElement . query ( By . css ( '.mat-list-item' ) ) ;
509+ selectionList = fixture . debugElement . query ( By . directive ( MatSelectionList ) ) ;
510+ fixture . detectChanges ( ) ;
511+ } ) ) ;
512+
513+ it ( 'should have a value for each item' , ( ) => {
514+ expect ( listOption [ 0 ] . componentInstance . value ) . toBe ( 1 ) ;
515+ expect ( listOption [ 1 ] . componentInstance . value ) . toBe ( 'a' ) ;
516+ expect ( listOption [ 2 ] . componentInstance . value ) . toBe ( true ) ;
517+ } ) ;
518+
519+ } ) ;
520+
521+ describe ( 'with option selected events' , ( ) => {
522+ let fixture : ComponentFixture < SelectionListWithOptionEvents > ;
523+ let testComponent : SelectionListWithOptionEvents ;
524+ let listOption : DebugElement [ ] ;
525+ let selectionList : DebugElement ;
526+
527+ beforeEach ( async ( ( ) => {
528+ TestBed . configureTestingModule ( {
529+ imports : [ MatListModule ] ,
530+ declarations : [
531+ SelectionListWithOptionEvents
532+ ] ,
533+ } ) ;
534+
535+ TestBed . compileComponents ( ) ;
536+ } ) ) ;
537+
538+ beforeEach ( async ( ( ) => {
539+ fixture = TestBed . createComponent ( SelectionListWithOptionEvents ) ;
540+ testComponent = fixture . debugElement . componentInstance ;
541+ listOption = fixture . debugElement . queryAll ( By . directive ( MatListOption ) ) ;
542+ selectionList = fixture . debugElement . query ( By . directive ( MatSelectionList ) ) ;
543+ fixture . detectChanges ( ) ;
544+ } ) ) ;
545+
546+ it ( 'should trigger the selected and deselected events when clicked in succession.' , ( ) => {
547+
548+ let selected : boolean = false ;
549+
550+ spyOn ( testComponent , 'onOptionSelectionChange' )
551+ . and . callFake ( ( event : MatListOptionChange ) => {
552+ selected = event . selected ;
553+ } ) ;
554+
555+ listOption [ 0 ] . nativeElement . click ( ) ;
556+ expect ( testComponent . onOptionSelectionChange ) . toHaveBeenCalledTimes ( 1 ) ;
557+ expect ( selected ) . toBe ( true ) ;
558+
559+ listOption [ 0 ] . nativeElement . click ( ) ;
560+ expect ( testComponent . onOptionSelectionChange ) . toHaveBeenCalledTimes ( 2 ) ;
561+ expect ( selected ) . toBe ( false ) ;
562+ } ) ;
563+
564+ } ) ;
565+
566+ } ) ;
567+
489568@Component ( { template : `
490569 <mat-selection-list id="selection-list-1">
491570 <mat-list-option checkboxPosition="before" disabled="true" value="inbox">
@@ -507,35 +586,35 @@ class SelectionListWithListOptions {
507586}
508587
509588@Component ( { template : `
510- <mat-selection-list id = "selection-list-2">
511- <mat-list-option checkboxPosition = "after">
589+ <mat-selection-list id= "selection-list-2">
590+ <mat-list-option checkboxPosition= "after">
512591 Inbox (disabled selection-option)
513592 </mat-list-option>
514- <mat-list-option id = "testSelect" checkboxPosition = "after">
593+ <mat-list-option id= "testSelect" checkboxPosition= "after">
515594 Starred
516595 </mat-list-option>
517- <mat-list-option checkboxPosition = "after">
596+ <mat-list-option checkboxPosition= "after">
518597 Sent Mail
519598 </mat-list-option>
520- <mat-list-option checkboxPosition = "after">
599+ <mat-list-option checkboxPosition= "after">
521600 Drafts
522601 </mat-list-option>
523602 </mat-selection-list>` } )
524603class SelectionListWithCheckboxPositionAfter {
525604}
526605
527606@Component ( { template : `
528- <mat-selection-list id = "selection-list-3" [disabled] = true>
529- <mat-list-option checkboxPosition = "after">
607+ <mat-selection-list id= "selection-list-3" [disabled]= true>
608+ <mat-list-option checkboxPosition= "after">
530609 Inbox (disabled selection-option)
531610 </mat-list-option>
532- <mat-list-option id = "testSelect" checkboxPosition = "after">
611+ <mat-list-option id= "testSelect" checkboxPosition= "after">
533612 Starred
534613 </mat-list-option>
535- <mat-list-option checkboxPosition = "after">
614+ <mat-list-option checkboxPosition= "after">
536615 Sent Mail
537616 </mat-list-option>
538- <mat-list-option checkboxPosition = "after">
617+ <mat-list-option checkboxPosition= "after">
539618 Drafts
540619 </mat-list-option>
541620 </mat-selection-list>` } )
@@ -559,8 +638,8 @@ class SelectionListWithSelectedOption {
559638}
560639
561640@Component ( { template : `
562- <mat-selection-list id = "selection-list-4">
563- <mat-list-option checkboxPosition = "after" class="test-focus" id="123">
641+ <mat-selection-list id= "selection-list-4">
642+ <mat-list-option checkboxPosition= "after" class="test-focus" id="123">
564643 Inbox
565644 </mat-list-option>
566645 </mat-selection-list>` } )
@@ -579,3 +658,28 @@ class SelectionListWithTabindexBinding {
579658 tabIndex : number ;
580659 disabled : boolean ;
581660}
661+
662+ @Component ( { template : `
663+ <mat-selection-list id="selection-list-5">
664+ <mat-list-option [value]="1" checkboxPosition="after">
665+ 1
666+ </mat-list-option>
667+ <mat-list-option value="a" checkboxPosition="after">
668+ a
669+ </mat-list-option>
670+ <mat-list-option [value]="true" checkboxPosition="after">
671+ true
672+ </mat-list-option>
673+ </mat-selection-list>` } )
674+ class SelectionListWithMultipleValues {
675+ }
676+
677+ @Component ( { template : `
678+ <mat-selection-list id="selection-list-6">
679+ <mat-list-option (selectionChange)="onOptionSelectionChange($event)">
680+ Inbox
681+ </mat-list-option>
682+ </mat-selection-list>` } )
683+ class SelectionListWithOptionEvents {
684+ onOptionSelectionChange : ( event ?: MatListOptionChange ) => void = ( ) => { } ;
685+ }
0 commit comments