Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/material/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,36 @@ describe('MatSelect', () => {
subscription!.unsubscribe();
}));

it('should emit to `optionSelectionChanges` after the list of options has changed',
fakeAsync(() => {
let spy = jasmine.createSpy('option selection spy');
let subscription = fixture.componentInstance.select.optionSelectionChanges.subscribe(spy);
let selectFirstOption = () => {
trigger.click();
fixture.detectChanges();
flush();

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

fixture.componentInstance.foods = [{value: 'salad-8', viewValue: 'Salad'}];
fixture.detectChanges();
selectFirstOption();

expect(spy).toHaveBeenCalledTimes(1);

fixture.componentInstance.foods = [{value: 'fruit-9', viewValue: 'Fruit'}];
fixture.detectChanges();
selectFirstOption();

expect(spy).toHaveBeenCalledTimes(2);

subscription!.unsubscribe();
}));

});

describe('forms integration', () => {
Expand Down
9 changes: 7 additions & 2 deletions src/material/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,13 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,

/** Combined stream of all of the child options' change events. */
readonly optionSelectionChanges: Observable<MatOptionSelectionChange> = defer(() => {
if (this.options) {
return merge(...this.options.map(option => option.onSelectionChange));
const options = this.options;

if (options) {
return options.changes.pipe(
startWith(options),
switchMap(() => merge(...options.map(option => option.onSelectionChange)))
);
}

return this._ngZone.onStable
Expand Down