@@ -4015,6 +4015,84 @@ describe('MatSelect', () => {
40154015 } ) ;
40164016
40174017 } ) ;
4018+
4019+ describe ( 'value propagation when options are added/removed' , ( ) => {
4020+ let fixture : ComponentFixture < BasicSelectWithoutFormsMultiple > ;
4021+ let testComponent : BasicSelectWithoutFormsMultiple ;
4022+
4023+ beforeEach ( fakeAsync ( ( ) => {
4024+ configureMatSelectTestingModule ( [ BasicSelectWithoutFormsMultiple ] ) ;
4025+ fixture = TestBed . createComponent ( BasicSelectWithoutFormsMultiple ) ;
4026+ testComponent = fixture . componentInstance ;
4027+ fixture . detectChanges ( ) ;
4028+ } ) ) ;
4029+
4030+ it ( 'should propagate the changes when a selected option is removed' , fakeAsync ( ( ) => {
4031+ testComponent . selectedFoods = [ 'steak-0' , 'pizza-1' ] ;
4032+ fixture . detectChanges ( ) ;
4033+ flush ( ) ;
4034+
4035+ expect ( testComponent . select . value ) . toEqual ( [ 'steak-0' , 'pizza-1' ] ) ;
4036+ testComponent . selectionChange . calls . reset ( ) ;
4037+
4038+ testComponent . foods . shift ( ) ;
4039+ fixture . detectChanges ( ) ;
4040+ flush ( ) ;
4041+
4042+ expect ( testComponent . selectionChange ) . toHaveBeenCalledTimes ( 1 ) ;
4043+ expect ( testComponent . select . value ) . toEqual ( [ 'pizza-1' ] ) ;
4044+ expect ( testComponent . selectedFoods ) . toEqual ( [ 'pizza-1' ] ) ;
4045+ } ) ) ;
4046+
4047+ it ( 'should propagate the changes when a selected option is added' , fakeAsync ( ( ) => {
4048+ testComponent . selectedFoods = [ 'steak-0' ] ;
4049+ fixture . detectChanges ( ) ;
4050+ flush ( ) ;
4051+
4052+ expect ( testComponent . select . value ) . toEqual ( [ 'steak-0' ] ) ;
4053+ testComponent . selectionChange . calls . reset ( ) ;
4054+
4055+ testComponent . foods . push ( { value : 'pasta-4' , viewValue : 'Pasta' } ) ;
4056+ testComponent . selectedFoods . push ( 'pasta-4' ) ;
4057+ fixture . detectChanges ( ) ;
4058+ flush ( ) ;
4059+
4060+ expect ( testComponent . selectionChange ) . toHaveBeenCalledTimes ( 1 ) ;
4061+ expect ( testComponent . select . value ) . toEqual ( [ 'steak-0' , 'pasta-4' ] ) ;
4062+ expect ( testComponent . selectedFoods ) . toEqual ( [ 'steak-0' , 'pasta-4' ] ) ;
4063+ } ) ) ;
4064+
4065+ it ( 'should not propagate changes when a non-selected option is removed' , fakeAsync ( ( ) => {
4066+ testComponent . selectedFoods = [ 'steak-0' ] ;
4067+ fixture . detectChanges ( ) ;
4068+ flush ( ) ;
4069+
4070+ testComponent . selectionChange . calls . reset ( ) ;
4071+ testComponent . foods . pop ( ) ;
4072+ fixture . detectChanges ( ) ;
4073+ flush ( ) ;
4074+
4075+ expect ( testComponent . selectionChange ) . not . toHaveBeenCalled ( ) ;
4076+ expect ( testComponent . select . value ) . toEqual ( [ 'steak-0' ] ) ;
4077+ expect ( testComponent . selectedFoods ) . toEqual ( [ 'steak-0' ] ) ;
4078+ } ) ) ;
4079+
4080+ it ( 'should not propagate changes when a non-selected option is added' , fakeAsync ( ( ) => {
4081+ testComponent . selectedFoods = [ 'steak-0' ] ;
4082+ fixture . detectChanges ( ) ;
4083+ flush ( ) ;
4084+
4085+ testComponent . selectionChange . calls . reset ( ) ;
4086+ testComponent . foods . push ( { value : 'pasta-4' , viewValue : 'Pasta' } ) ;
4087+ fixture . detectChanges ( ) ;
4088+ flush ( ) ;
4089+
4090+ expect ( testComponent . selectionChange ) . not . toHaveBeenCalled ( ) ;
4091+ expect ( testComponent . select . value ) . toEqual ( [ 'steak-0' ] ) ;
4092+ expect ( testComponent . selectedFoods ) . toEqual ( [ 'steak-0' ] ) ;
4093+ } ) ) ;
4094+
4095+ } ) ;
40184096} ) ;
40194097
40204098
@@ -4601,7 +4679,8 @@ class BasicSelectWithoutFormsPreselected {
46014679@Component ( {
46024680 template : `
46034681 <mat-form-field>
4604- <mat-select placeholder="Food" [(value)]="selectedFoods" multiple>
4682+ <mat-select multiple placeholder="Food" [(value)]="selectedFoods"
4683+ (selectionChange)="selectionChange()">
46054684 <mat-option *ngFor="let food of foods" [value]="food.value">
46064685 {{ food.viewValue }}
46074686 </mat-option>
@@ -4611,6 +4690,7 @@ class BasicSelectWithoutFormsPreselected {
46114690} )
46124691class BasicSelectWithoutFormsMultiple {
46134692 selectedFoods : string [ ] ;
4693+ selectionChange = jasmine . createSpy ( 'selectionChange spy' ) ;
46144694 foods : any [ ] = [
46154695 { value : 'steak-0' , viewValue : 'Steak' } ,
46164696 { value : 'pizza-1' , viewValue : 'Pizza' } ,
0 commit comments