11import { TestBed , inject } from '@angular/core/testing' ;
22import { dispatchKeyboardEvent } from '@angular/cdk/testing' ;
33import { ESCAPE } from '@angular/cdk/keycodes' ;
4+ import { Component , NgModule } from '@angular/core' ;
45import { Overlay } from '../overlay' ;
56import { OverlayContainer } from '../overlay-container' ;
67import { OverlayModule } from '../index' ;
78import { OverlayKeyboardDispatcher } from './overlay-keyboard-dispatcher' ;
9+ import { ComponentPortal } from '@angular/cdk/portal' ;
10+
811
912describe ( 'OverlayKeyboardDispatcher' , ( ) => {
1013 let keyboardDispatcher : OverlayKeyboardDispatcher ;
@@ -13,7 +16,7 @@ describe('OverlayKeyboardDispatcher', () => {
1316
1417 beforeEach ( ( ) => {
1518 TestBed . configureTestingModule ( {
16- imports : [ OverlayModule ] ,
19+ imports : [ OverlayModule , TestComponentModule ] ,
1720 providers : [
1821 { provide : OverlayContainer , useFactory : ( ) => {
1922 overlayContainerElement = document . createElement ( 'div' ) ;
@@ -94,4 +97,52 @@ describe('OverlayKeyboardDispatcher', () => {
9497 expect ( overlayTwoSpy ) . not . toHaveBeenCalled ( ) ;
9598 } ) ;
9699
100+ it ( 'should stop emitting events to detached overlays' , ( ) => {
101+ const instance = overlay . create ( ) ;
102+ const spy = jasmine . createSpy ( 'keyboard event spy' ) ;
103+
104+ instance . attach ( new ComponentPortal ( TestComponent ) ) ;
105+ instance . keydownEvents ( ) . subscribe ( spy ) ;
106+
107+ dispatchKeyboardEvent ( document . body , 'keydown' , ESCAPE , instance . overlayElement ) ;
108+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
109+
110+ instance . detach ( ) ;
111+ dispatchKeyboardEvent ( document . body , 'keydown' , ESCAPE , instance . overlayElement ) ;
112+
113+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
114+ } ) ;
115+
116+ it ( 'should stop emitting events to disposed overlays' , ( ) => {
117+ const instance = overlay . create ( ) ;
118+ const spy = jasmine . createSpy ( 'keyboard event spy' ) ;
119+
120+ instance . attach ( new ComponentPortal ( TestComponent ) ) ;
121+ instance . keydownEvents ( ) . subscribe ( spy ) ;
122+
123+ dispatchKeyboardEvent ( document . body , 'keydown' , ESCAPE , instance . overlayElement ) ;
124+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
125+
126+ instance . dispose ( ) ;
127+ dispatchKeyboardEvent ( document . body , 'keydown' , ESCAPE , instance . overlayElement ) ;
128+
129+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
130+ } ) ;
131+
97132} ) ;
133+
134+
135+ @Component ( {
136+ template : 'Hello'
137+ } )
138+ class TestComponent { }
139+
140+
141+ // Create a real (non-test) NgModule as a workaround for
142+ // https://github.com/angular/angular/issues/10760
143+ @NgModule ( {
144+ exports : [ TestComponent ] ,
145+ declarations : [ TestComponent ] ,
146+ entryComponents : [ TestComponent ] ,
147+ } )
148+ class TestComponentModule { }
0 commit comments