Skip to content

Commit b8bb62f

Browse files
crisbetojosephperrott
authored andcommitted
fix(select): MatOption state change stream not being completed (#10540)
Completes the `MatOption._stateChanges` stream on destroy.
1 parent cb6d469 commit b8bb62f

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/lib/core/option/option.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ describe('MatOption component', () => {
1313
}).compileComponents();
1414
}));
1515

16+
it('should complete the `stateChanges` stream on destroy', () => {
17+
const fixture = TestBed.createComponent(OptionWithDisable);
18+
fixture.detectChanges();
19+
20+
const optionInstance: MatOption =
21+
fixture.debugElement.query(By.directive(MatOption)).componentInstance;
22+
const completeSpy = jasmine.createSpy('complete spy');
23+
const subscription = optionInstance._stateChanges.subscribe(undefined, undefined, completeSpy);
24+
25+
fixture.destroy();
26+
expect(completeSpy).toHaveBeenCalled();
27+
subscription.unsubscribe();
28+
});
29+
1630
describe('ripples', () => {
1731
let fixture: ComponentFixture<OptionWithDisable>;
1832
let optionDebugElement: DebugElement;

src/lib/core/option/option.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
InjectionToken,
2424
Inject,
2525
AfterViewChecked,
26+
OnDestroy,
2627
} from '@angular/core';
2728
import {MatOptgroup} from './optgroup';
2829

@@ -83,7 +84,7 @@ export const MAT_OPTION_PARENT_COMPONENT =
8384
encapsulation: ViewEncapsulation.None,
8485
changeDetection: ChangeDetectionStrategy.OnPush,
8586
})
86-
export class MatOption implements AfterViewChecked {
87+
export class MatOption implements AfterViewChecked, OnDestroy {
8788
private _selected = false;
8889
private _active = false;
8990
private _disabled = false;
@@ -241,6 +242,10 @@ export class MatOption implements AfterViewChecked {
241242
}
242243
}
243244

245+
ngOnDestroy() {
246+
this._stateChanges.complete();
247+
}
248+
244249
/** Emits the selection change event. */
245250
private _emitSelectionChangeEvent(isUserInput = false): void {
246251
this.onSelectionChange.emit(new MatOptionSelectionChange(this, isUserInput));

0 commit comments

Comments
 (0)