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
14 changes: 14 additions & 0 deletions src/lib/core/option/option.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ describe('MatOption component', () => {
}).compileComponents();
}));

it('should complete the `stateChanges` stream on destroy', () => {
const fixture = TestBed.createComponent(OptionWithDisable);
fixture.detectChanges();

const optionInstance: MatOption =
fixture.debugElement.query(By.directive(MatOption)).componentInstance;
const completeSpy = jasmine.createSpy('complete spy');
const subscription = optionInstance._stateChanges.subscribe(undefined, undefined, completeSpy);

fixture.destroy();
expect(completeSpy).toHaveBeenCalled();
subscription.unsubscribe();
});

describe('ripples', () => {
let fixture: ComponentFixture<OptionWithDisable>;
let optionDebugElement: DebugElement;
Expand Down
7 changes: 6 additions & 1 deletion src/lib/core/option/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
InjectionToken,
Inject,
AfterViewChecked,
OnDestroy,
} from '@angular/core';
import {MatOptgroup} from './optgroup';

Expand Down Expand Up @@ -83,7 +84,7 @@ export const MAT_OPTION_PARENT_COMPONENT =
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MatOption implements AfterViewChecked {
export class MatOption implements AfterViewChecked, OnDestroy {
private _selected = false;
private _active = false;
private _disabled = false;
Expand Down Expand Up @@ -241,6 +242,10 @@ export class MatOption implements AfterViewChecked {
}
}

ngOnDestroy() {
this._stateChanges.complete();
}

/** Emits the selection change event. */
private _emitSelectionChangeEvent(isUserInput = false): void {
this.onSelectionChange.emit(new MatOptionSelectionChange(this, isUserInput));
Expand Down