fix(autocomplete): error when closing destroyed panel (#5446)

Fixes an error that was being thrown when attempting to close the panel of an autocomplete that is inside a destroyed view. The issue is that `closePanel` can end up getting called multiple times in a row, which leads to an attempt to call `detectChanges` on a destroyed `ChangeDetectorRef`.

Fixes #5413.
This commit is contained in:
Kristiyan Kostadinov
2017-07-09 06:10:43 +02:00
committed by mmalerba
parent 372549c378
commit 880e6d513d
2 changed files with 25 additions and 7 deletions
@@ -170,6 +170,10 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
/** Closes the autocomplete suggestion panel. */
closePanel(): void {
if (!this.panelOpen) {
return;
}
if (this._overlayRef && this._overlayRef.hasAttached()) {
this._overlayRef.detach();
this._closingActionsSubscription.unsubscribe();
+21 -7
View File
@@ -221,17 +221,31 @@ describe('MdAutocomplete', () => {
});
}));
it('should close the panel programmatically', () => {
it('should close the panel programmatically', async(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
fixture.componentInstance.trigger.closePanel();
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.componentInstance.trigger.closePanel();
fixture.detectChanges();
expect(fixture.componentInstance.trigger.panelOpen)
.toBe(false, `Expected closing programmatically to set the panel state to closed.`);
expect(overlayContainerElement.textContent)
.toEqual('', `Expected closing programmatically to close the panel.`);
fixture.whenStable().then(() => {
expect(fixture.componentInstance.trigger.panelOpen)
.toBe(false, `Expected closing programmatically to set the panel state to closed.`);
expect(overlayContainerElement.textContent)
.toEqual('', `Expected closing programmatically to close the panel.`);
});
});
}));
it('should not throw when attempting to close the panel of a destroyed autocomplete', () => {
const trigger = fixture.componentInstance.trigger;
trigger.openPanel();
fixture.detectChanges();
fixture.destroy();
expect(() => trigger.closePanel()).not.toThrow();
});
it('should hide the panel when the options list is empty', async(() => {