From 68cbc009bb2181508265177ff458dcd32075732d Mon Sep 17 00:00:00 2001 From: crisbeto Date: Fri, 23 Mar 2018 15:51:44 +0100 Subject: [PATCH] fix(select): MatOption state change stream not being completed Completes the `MatOption._stateChanges` stream on destroy. --- src/lib/core/option/option.spec.ts | 14 ++++++++++++++ src/lib/core/option/option.ts | 7 ++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/lib/core/option/option.spec.ts b/src/lib/core/option/option.spec.ts index 6e963b81b767..9eceeb530e2f 100644 --- a/src/lib/core/option/option.spec.ts +++ b/src/lib/core/option/option.spec.ts @@ -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; let optionDebugElement: DebugElement; diff --git a/src/lib/core/option/option.ts b/src/lib/core/option/option.ts index b743aaf254bf..6c5a56009133 100644 --- a/src/lib/core/option/option.ts +++ b/src/lib/core/option/option.ts @@ -23,6 +23,7 @@ import { InjectionToken, Inject, AfterViewChecked, + OnDestroy, } from '@angular/core'; import {MatOptgroup} from './optgroup'; @@ -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; @@ -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));