Skip to content

Commit

Permalink
fix(select): don't emit change event multiple times when a reset opti…
Browse files Browse the repository at this point in the history
…on is selected twice in a row

Fixes the select's change events being fired when a reset option is selected twice in a row.

Fixes #10675.
  • Loading branch information
crisbeto authored and josephperrott committed May 30, 2018
1 parent 9062640 commit 192f38b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
34 changes: 34 additions & 0 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2744,6 +2744,40 @@ describe('MatSelect', () => {
expect(spy).toHaveBeenCalledWith('steak-0');
}));

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).toHaveBeenCalledTimes(1);

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();
}));

});

describe('with option centering disabled', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -889,11 +889,11 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
} else {
this._clearSelection(option.value == null ? undefined : option);

if (option.value == null) {
this._propagateChanges(option.value);
} else {
if (option.value != null) {
this._selectionModel.select(option);
this.stateChanges.next();
} else if (this.value !== option.value) {
this._propagateChanges(option.value);
}
}

Expand Down

0 comments on commit 192f38b

Please sign in to comment.