@@ -4,7 +4,7 @@ import {createKeyboardEvent, dispatchFakeEvent} from '@angular/cdk/testing';
4
4
import { Component , DebugElement } from '@angular/core' ;
5
5
import { async , ComponentFixture , inject , TestBed } from '@angular/core/testing' ;
6
6
import { By } from '@angular/platform-browser' ;
7
- import { MatListModule , MatListOption , MatSelectionList } from './index' ;
7
+ import { MatListModule , MatListOption , MatSelectionList , MatListOptionChange } from './index' ;
8
8
9
9
10
10
describe ( 'MatSelectionList' , ( ) => {
@@ -483,9 +483,88 @@ describe('MatSelectionList', () => {
483
483
expect ( listItemContent . nativeElement . classList ) . toContain ( 'mat-list-item-content-reverse' ) ;
484
484
} ) ;
485
485
} ) ;
486
- } ) ;
487
486
488
487
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
+
489
568
@Component ( { template : `
490
569
<mat-selection-list id="selection-list-1">
491
570
<mat-list-option checkboxPosition="before" disabled="true" value="inbox">
@@ -507,35 +586,35 @@ class SelectionListWithListOptions {
507
586
}
508
587
509
588
@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">
512
591
Inbox (disabled selection-option)
513
592
</mat-list-option>
514
- <mat-list-option id = "testSelect" checkboxPosition = "after">
593
+ <mat-list-option id= "testSelect" checkboxPosition= "after">
515
594
Starred
516
595
</mat-list-option>
517
- <mat-list-option checkboxPosition = "after">
596
+ <mat-list-option checkboxPosition= "after">
518
597
Sent Mail
519
598
</mat-list-option>
520
- <mat-list-option checkboxPosition = "after">
599
+ <mat-list-option checkboxPosition= "after">
521
600
Drafts
522
601
</mat-list-option>
523
602
</mat-selection-list>` } )
524
603
class SelectionListWithCheckboxPositionAfter {
525
604
}
526
605
527
606
@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">
530
609
Inbox (disabled selection-option)
531
610
</mat-list-option>
532
- <mat-list-option id = "testSelect" checkboxPosition = "after">
611
+ <mat-list-option id= "testSelect" checkboxPosition= "after">
533
612
Starred
534
613
</mat-list-option>
535
- <mat-list-option checkboxPosition = "after">
614
+ <mat-list-option checkboxPosition= "after">
536
615
Sent Mail
537
616
</mat-list-option>
538
- <mat-list-option checkboxPosition = "after">
617
+ <mat-list-option checkboxPosition= "after">
539
618
Drafts
540
619
</mat-list-option>
541
620
</mat-selection-list>` } )
@@ -559,8 +638,8 @@ class SelectionListWithSelectedOption {
559
638
}
560
639
561
640
@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">
564
643
Inbox
565
644
</mat-list-option>
566
645
</mat-selection-list>` } )
@@ -579,3 +658,28 @@ class SelectionListWithTabindexBinding {
579
658
tabIndex : number ;
580
659
disabled : boolean ;
581
660
}
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