@@ -3459,6 +3459,84 @@ describe('MatSelect', () => {
3459
3459
} ) ) ;
3460
3460
3461
3461
} ) ;
3462
+
3463
+ describe ( 'value propagation when options are added/removed' , ( ) => {
3464
+ let fixture : ComponentFixture < BasicSelectWithoutFormsMultiple > ;
3465
+ let testComponent : BasicSelectWithoutFormsMultiple ;
3466
+
3467
+ beforeEach ( fakeAsync ( ( ) => {
3468
+ configureMatSelectTestingModule ( [ BasicSelectWithoutFormsMultiple ] ) ;
3469
+ fixture = TestBed . createComponent ( BasicSelectWithoutFormsMultiple ) ;
3470
+ testComponent = fixture . componentInstance ;
3471
+ fixture . detectChanges ( ) ;
3472
+ } ) ) ;
3473
+
3474
+ it ( 'should propagate the changes when a selected option is removed' , fakeAsync ( ( ) => {
3475
+ testComponent . selectedFoods = [ 'steak-0' , 'pizza-1' ] ;
3476
+ fixture . detectChanges ( ) ;
3477
+ flush ( ) ;
3478
+
3479
+ expect ( testComponent . select . value ) . toEqual ( [ 'steak-0' , 'pizza-1' ] ) ;
3480
+ testComponent . selectionChange . calls . reset ( ) ;
3481
+
3482
+ testComponent . foods . shift ( ) ;
3483
+ fixture . detectChanges ( ) ;
3484
+ flush ( ) ;
3485
+
3486
+ expect ( testComponent . selectionChange ) . toHaveBeenCalledTimes ( 1 ) ;
3487
+ expect ( testComponent . select . value ) . toEqual ( [ 'pizza-1' ] ) ;
3488
+ expect ( testComponent . selectedFoods ) . toEqual ( [ 'pizza-1' ] ) ;
3489
+ } ) ) ;
3490
+
3491
+ it ( 'should propagate the changes when a selected option is added' , fakeAsync ( ( ) => {
3492
+ testComponent . selectedFoods = [ 'steak-0' ] ;
3493
+ fixture . detectChanges ( ) ;
3494
+ flush ( ) ;
3495
+
3496
+ expect ( testComponent . select . value ) . toEqual ( [ 'steak-0' ] ) ;
3497
+ testComponent . selectionChange . calls . reset ( ) ;
3498
+
3499
+ testComponent . foods . push ( { value : 'pasta-4' , viewValue : 'Pasta' } ) ;
3500
+ testComponent . selectedFoods . push ( 'pasta-4' ) ;
3501
+ fixture . detectChanges ( ) ;
3502
+ flush ( ) ;
3503
+
3504
+ expect ( testComponent . selectionChange ) . toHaveBeenCalledTimes ( 1 ) ;
3505
+ expect ( testComponent . select . value ) . toEqual ( [ 'steak-0' , 'pasta-4' ] ) ;
3506
+ expect ( testComponent . selectedFoods ) . toEqual ( [ 'steak-0' , 'pasta-4' ] ) ;
3507
+ } ) ) ;
3508
+
3509
+ it ( 'should not propagate changes when a non-selected option is removed' , fakeAsync ( ( ) => {
3510
+ testComponent . selectedFoods = [ 'steak-0' ] ;
3511
+ fixture . detectChanges ( ) ;
3512
+ flush ( ) ;
3513
+
3514
+ testComponent . selectionChange . calls . reset ( ) ;
3515
+ testComponent . foods . pop ( ) ;
3516
+ fixture . detectChanges ( ) ;
3517
+ flush ( ) ;
3518
+
3519
+ expect ( testComponent . selectionChange ) . not . toHaveBeenCalled ( ) ;
3520
+ expect ( testComponent . select . value ) . toEqual ( [ 'steak-0' ] ) ;
3521
+ expect ( testComponent . selectedFoods ) . toEqual ( [ 'steak-0' ] ) ;
3522
+ } ) ) ;
3523
+
3524
+ it ( 'should not propagate changes when a non-selected option is added' , fakeAsync ( ( ) => {
3525
+ testComponent . selectedFoods = [ 'steak-0' ] ;
3526
+ fixture . detectChanges ( ) ;
3527
+ flush ( ) ;
3528
+
3529
+ testComponent . selectionChange . calls . reset ( ) ;
3530
+ testComponent . foods . push ( { value : 'pasta-4' , viewValue : 'Pasta' } ) ;
3531
+ fixture . detectChanges ( ) ;
3532
+ flush ( ) ;
3533
+
3534
+ expect ( testComponent . selectionChange ) . not . toHaveBeenCalled ( ) ;
3535
+ expect ( testComponent . select . value ) . toEqual ( [ 'steak-0' ] ) ;
3536
+ expect ( testComponent . selectedFoods ) . toEqual ( [ 'steak-0' ] ) ;
3537
+ } ) ) ;
3538
+
3539
+ } ) ;
3462
3540
} ) ;
3463
3541
3464
3542
@@ -4021,7 +4099,8 @@ class BasicSelectWithoutFormsPreselected {
4021
4099
@Component ( {
4022
4100
template : `
4023
4101
<mat-form-field>
4024
- <mat-select placeholder="Food" [(value)]="selectedFoods" multiple>
4102
+ <mat-select multiple placeholder="Food" [(value)]="selectedFoods"
4103
+ (selectionChange)="selectionChange()">
4025
4104
<mat-option *ngFor="let food of foods" [value]="food.value">
4026
4105
{{ food.viewValue }}
4027
4106
</mat-option>
@@ -4031,6 +4110,7 @@ class BasicSelectWithoutFormsPreselected {
4031
4110
} )
4032
4111
class BasicSelectWithoutFormsMultiple {
4033
4112
selectedFoods : string [ ] ;
4113
+ selectionChange = jasmine . createSpy ( 'selectionChange spy' ) ;
4034
4114
foods : any [ ] = [
4035
4115
{ value : 'steak-0' , viewValue : 'Steak' } ,
4036
4116
{ value : 'pizza-1' , viewValue : 'Pizza' } ,
0 commit comments