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' ) ;
@@ -94,4 +97,52 @@ describe('OverlayKeyboardDispatcher', () => {
94
97
expect ( overlayTwoSpy ) . not . toHaveBeenCalled ( ) ;
95
98
} ) ;
96
99
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
+
97
132
} ) ;
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