Skip to content

Commit

Permalink
fix(autocomplete): error when closing destroyed panel (#5446)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
crisbeto authored and mmalerba committed Jul 9, 2017
1 parent 372549c commit 880e6d5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
26 changes: 20 additions & 6 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.whenStable().then(() => {
fixture.componentInstance.trigger.closePanel();
fixture.detectChanges();

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(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.`);
expect(() => trigger.closePanel()).not.toThrow();
});

it('should hide the panel when the options list is empty', async(() => {
Expand Down

0 comments on commit 880e6d5

Please sign in to comment.