1
1
import { TestBed , inject } from '@angular/core/testing' ;
2
2
import { dispatchKeyboardEvent } from '@angular/cdk/testing' ;
3
3
import { ESCAPE } from '@angular/cdk/keycodes' ;
4
+ import { Component , NgModule } from '@angular/core' ;
4
5
import { Overlay } from '../overlay' ;
5
6
import { OverlayContainer } from '../overlay-container' ;
6
7
import { OverlayModule } from '../index' ;
7
8
import { OverlayKeyboardDispatcher } from './overlay-keyboard-dispatcher' ;
9
+ import { ComponentPortal } from '@angular/cdk/portal' ;
10
+
8
11
9
12
describe ( 'OverlayKeyboardDispatcher' , ( ) => {
10
13
let keyboardDispatcher : OverlayKeyboardDispatcher ;
@@ -13,7 +16,7 @@ describe('OverlayKeyboardDispatcher', () => {
13
16
14
17
beforeEach ( ( ) => {
15
18
TestBed . configureTestingModule ( {
16
- imports : [ OverlayModule ] ,
19
+ imports : [ OverlayModule , TestComponentModule ] ,
17
20
providers : [
18
21
{ provide : OverlayContainer , useFactory : ( ) => {
19
22
overlayContainerElement = document . createElement ( 'div' ) ;
@@ -105,4 +108,52 @@ describe('OverlayKeyboardDispatcher', () => {
105
108
expect ( completeSpy ) . toHaveBeenCalled ( ) ;
106
109
} ) ;
107
110
111
+ it ( 'should stop emitting events to detached overlays' , ( ) => {
112
+ const instance = overlay . create ( ) ;
113
+ const spy = jasmine . createSpy ( 'keyboard event spy' ) ;
114
+
115
+ instance . attach ( new ComponentPortal ( TestComponent ) ) ;
116
+ instance . keydownEvents ( ) . subscribe ( spy ) ;
117
+
118
+ dispatchKeyboardEvent ( document . body , 'keydown' , ESCAPE , instance . overlayElement ) ;
119
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
120
+
121
+ instance . detach ( ) ;
122
+ dispatchKeyboardEvent ( document . body , 'keydown' , ESCAPE , instance . overlayElement ) ;
123
+
124
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
125
+ } ) ;
126
+
127
+ it ( 'should stop emitting events to disposed overlays' , ( ) => {
128
+ const instance = overlay . create ( ) ;
129
+ const spy = jasmine . createSpy ( 'keyboard event spy' ) ;
130
+
131
+ instance . attach ( new ComponentPortal ( TestComponent ) ) ;
132
+ instance . keydownEvents ( ) . subscribe ( spy ) ;
133
+
134
+ dispatchKeyboardEvent ( document . body , 'keydown' , ESCAPE , instance . overlayElement ) ;
135
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
136
+
137
+ instance . dispose ( ) ;
138
+ dispatchKeyboardEvent ( document . body , 'keydown' , ESCAPE , instance . overlayElement ) ;
139
+
140
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
141
+ } ) ;
142
+
108
143
} ) ;
144
+
145
+
146
+ @Component ( {
147
+ template : 'Hello'
148
+ } )
149
+ class TestComponent { }
150
+
151
+
152
+ // Create a real (non-test) NgModule as a workaround for
153
+ // https://github.com/angular/angular/issues/10760
154
+ @NgModule ( {
155
+ exports : [ TestComponent ] ,
156
+ declarations : [ TestComponent ] ,
157
+ entryComponents : [ TestComponent ] ,
158
+ } )
159
+ class TestComponentModule { }
0 commit comments