Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(overlay): emitting to detachments stream when not attached #7944

Merged
merged 1 commit into from
Nov 2, 2017
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
12 changes: 11 additions & 1 deletion src/cdk/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export class OverlayRef implements PortalHost {
* @returns The portal detachment result.
*/
detach(): any {
if (!this.hasAttached()) {
return;
}

this.detachBackdrop();

// When the overlay is detached, the pane element should disable pointer events.
Expand Down Expand Up @@ -122,6 +126,8 @@ export class OverlayRef implements PortalHost {
* Cleans up the overlay from the DOM.
*/
dispose(): void {
const isAttached = this.hasAttached();

if (this._config.positionStrategy) {
this._config.positionStrategy.dispose();
}
Expand All @@ -134,7 +140,11 @@ export class OverlayRef implements PortalHost {
this._portalHost.dispose();
this._attachments.complete();
this._backdropClick.complete();
this._detachments.next();

if (isAttached) {
this._detachments.next();
}

this._detachments.complete();
}

Expand Down
20 changes: 20 additions & 0 deletions src/cdk/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ describe('Overlay', () => {
expect(spy).toHaveBeenCalled();
});

it('should not emit to the detach stream if the overlay has not been attached', () => {
let overlayRef = overlay.create();
let spy = jasmine.createSpy('detachments spy');

overlayRef.detachments().subscribe(spy);
overlayRef.detach();

expect(spy).not.toHaveBeenCalled();
});

it('should not emit to the detach stream on dispose if the overlay was not attached', () => {
let overlayRef = overlay.create();
let spy = jasmine.createSpy('detachments spy');

overlayRef.detachments().subscribe(spy);
overlayRef.dispose();

expect(spy).not.toHaveBeenCalled();
});

it('should emit the detachment event after the overlay is removed from the DOM', () => {
let overlayRef = overlay.create();

Expand Down