Skip to content

Commit

Permalink
fix(select): emitting change event twice for reset values
Browse files Browse the repository at this point in the history
Fixes `mat-select` emitting its change event twice when a reset value is selected, as well as when it's selected twice in a row. This PR covers #10859 which would've introduced another issue.

Fixes #10675.
Fixes #13579.
  • Loading branch information
crisbeto committed Oct 13, 2018
1 parent 28e19a7 commit f3120bf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
58 changes: 58 additions & 0 deletions src/lib/select/select.spec.ts
Expand Up @@ -2858,6 +2858,64 @@ describe('MatSelect', () => {
expect(spy).toHaveBeenCalledWith('steak-0');
}));

it('should emit once when a reset value is selected', fakeAsync(() => {
const fixture = TestBed.createComponent(BasicSelectWithoutForms);
const instance = fixture.componentInstance;
const spy = jasmine.createSpy('change spy');

instance.selectedFood = 'sandwich-2';
instance.foods[0].value = null;
fixture.detectChanges();

const subscription = instance.select.selectionChange.subscribe(spy);

fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement.click();
fixture.detectChanges();
flush();

(overlayContainerElement.querySelector('mat-option') as HTMLElement).click();
fixture.detectChanges();
flush();

expect(spy).toHaveBeenCalledTimes(1);

subscription.unsubscribe();
}));

it('should not emit the change event multiple times when a reset option is ' +
'selected twice in a row', fakeAsync(() => {
const fixture = TestBed.createComponent(BasicSelectWithoutForms);
const instance = fixture.componentInstance;
const spy = jasmine.createSpy('change spy');

instance.foods[0].value = null;
fixture.detectChanges();

const subscription = instance.select.selectionChange.subscribe(spy);

fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement.click();
fixture.detectChanges();
flush();

(overlayContainerElement.querySelector('mat-option') as HTMLElement).click();
fixture.detectChanges();
flush();

expect(spy).not.toHaveBeenCalled();

fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement.click();
fixture.detectChanges();
flush();

(overlayContainerElement.querySelector('mat-option') as HTMLElement).click();
fixture.detectChanges();
flush();

expect(spy).not.toHaveBeenCalled();

subscription.unsubscribe();
}));

});

describe('with option centering disabled', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/select/select.ts
Expand Up @@ -914,7 +914,10 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
if (option.value == null && !this._multiple) {
option.deselect();
this._selectionModel.clear();
this._propagateChanges(option.value);

if (this.value != null) {
this._propagateChanges(option.value);
}
} else {
option.selected ? this._selectionModel.select(option) : this._selectionModel.deselect(option);

Expand Down

0 comments on commit f3120bf

Please sign in to comment.