File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -103,6 +103,10 @@ export class OverlayRef implements PortalOutlet {
103
103
* @returns The portal detachment result.
104
104
*/
105
105
detach ( ) : any {
106
+ if ( ! this . hasAttached ( ) ) {
107
+ return ;
108
+ }
109
+
106
110
this . detachBackdrop ( ) ;
107
111
108
112
// When the overlay is detached, the pane element should disable pointer events.
@@ -133,6 +137,8 @@ export class OverlayRef implements PortalOutlet {
133
137
* Cleans up the overlay from the DOM.
134
138
*/
135
139
dispose ( ) : void {
140
+ const isAttached = this . hasAttached ( ) ;
141
+
136
142
if ( this . _config . positionStrategy ) {
137
143
this . _config . positionStrategy . dispose ( ) ;
138
144
}
@@ -145,7 +151,11 @@ export class OverlayRef implements PortalOutlet {
145
151
this . _portalOutlet . dispose ( ) ;
146
152
this . _attachments . complete ( ) ;
147
153
this . _backdropClick . complete ( ) ;
148
- this . _detachments . next ( ) ;
154
+
155
+ if ( isAttached ) {
156
+ this . _detachments . next ( ) ;
157
+ }
158
+
149
159
this . _detachments . complete ( ) ;
150
160
}
151
161
Original file line number Diff line number Diff line change @@ -177,6 +177,26 @@ describe('Overlay', () => {
177
177
expect ( spy ) . toHaveBeenCalled ( ) ;
178
178
} ) ;
179
179
180
+ it ( 'should not emit to the detach stream if the overlay has not been attached' , ( ) => {
181
+ let overlayRef = overlay . create ( ) ;
182
+ let spy = jasmine . createSpy ( 'detachments spy' ) ;
183
+
184
+ overlayRef . detachments ( ) . subscribe ( spy ) ;
185
+ overlayRef . detach ( ) ;
186
+
187
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
188
+ } ) ;
189
+
190
+ it ( 'should not emit to the detach stream on dispose if the overlay was not attached' , ( ) => {
191
+ let overlayRef = overlay . create ( ) ;
192
+ let spy = jasmine . createSpy ( 'detachments spy' ) ;
193
+
194
+ overlayRef . detachments ( ) . subscribe ( spy ) ;
195
+ overlayRef . dispose ( ) ;
196
+
197
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
198
+ } ) ;
199
+
180
200
it ( 'should emit the detachment event after the overlay is removed from the DOM' , ( ) => {
181
201
let overlayRef = overlay . create ( ) ;
182
202
You can’t perform that action at this time.
0 commit comments