Skip to content

fix(overlay): disposed overlays not removed from the key event stack #8226

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

Merged
merged 1 commit into from
Nov 21, 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
53 changes: 52 additions & 1 deletion src/cdk/overlay/keyboard/overlay-keyboard-dispatcher.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {TestBed, inject} from '@angular/core/testing';
import {dispatchKeyboardEvent} from '@angular/cdk/testing';
import {ESCAPE} from '@angular/cdk/keycodes';
import {Component, NgModule} from '@angular/core';
import {Overlay} from '../overlay';
import {OverlayContainer} from '../overlay-container';
import {OverlayModule} from '../index';
import {OverlayKeyboardDispatcher} from './overlay-keyboard-dispatcher';
import {ComponentPortal} from '@angular/cdk/portal';


describe('OverlayKeyboardDispatcher', () => {
let keyboardDispatcher: OverlayKeyboardDispatcher;
Expand All @@ -13,7 +16,7 @@ describe('OverlayKeyboardDispatcher', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [OverlayModule],
imports: [OverlayModule, TestComponentModule],
providers: [
{provide: OverlayContainer, useFactory: () => {
overlayContainerElement = document.createElement('div');
Expand Down Expand Up @@ -105,4 +108,52 @@ describe('OverlayKeyboardDispatcher', () => {
expect(completeSpy).toHaveBeenCalled();
});

it('should stop emitting events to detached overlays', () => {
const instance = overlay.create();
const spy = jasmine.createSpy('keyboard event spy');

instance.attach(new ComponentPortal(TestComponent));
instance.keydownEvents().subscribe(spy);

dispatchKeyboardEvent(document.body, 'keydown', ESCAPE, instance.overlayElement);
expect(spy).toHaveBeenCalledTimes(1);

instance.detach();
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE, instance.overlayElement);

expect(spy).toHaveBeenCalledTimes(1);
});

it('should stop emitting events to disposed overlays', () => {
const instance = overlay.create();
const spy = jasmine.createSpy('keyboard event spy');

instance.attach(new ComponentPortal(TestComponent));
instance.keydownEvents().subscribe(spy);

dispatchKeyboardEvent(document.body, 'keydown', ESCAPE, instance.overlayElement);
expect(spy).toHaveBeenCalledTimes(1);

instance.dispose();
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE, instance.overlayElement);

expect(spy).toHaveBeenCalledTimes(1);
});

});


@Component({
template: 'Hello'
})
class TestComponent { }


// Create a real (non-test) NgModule as a workaround for
// https://github.com/angular/angular/issues/10760
@NgModule({
exports: [TestComponent],
declarations: [TestComponent],
entryComponents: [TestComponent],
})
class TestComponentModule { }
1 change: 1 addition & 0 deletions src/cdk/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export class OverlayRef implements PortalOutlet {
}

this.detachBackdrop();
this._keyboardDispatcher.remove(this);
this._portalOutlet.dispose();
this._attachments.complete();
this._backdropClick.complete();
Expand Down