@@ -24,7 +24,7 @@ import {Location} from '@angular/common';
24
24
import { SpyLocation } from '@angular/common/testing' ;
25
25
import { Directionality } from '@angular/cdk/bidi' ;
26
26
import { MatDialogContainer } from './dialog-container' ;
27
- import { OverlayContainer , ScrollStrategy } from '@angular/cdk/overlay' ;
27
+ import { OverlayContainer , ScrollStrategy , ScrollDispatcher , Overlay } from '@angular/cdk/overlay' ;
28
28
import { A , ESCAPE } from '@angular/cdk/keycodes' ;
29
29
import { dispatchKeyboardEvent } from '@angular/cdk/testing' ;
30
30
import {
@@ -34,12 +34,14 @@ import {
34
34
MatDialogRef ,
35
35
MAT_DIALOG_DEFAULT_OPTIONS
36
36
} from './index' ;
37
+ import { Subject } from 'rxjs' ;
37
38
38
39
39
40
describe ( 'MatDialog' , ( ) => {
40
41
let dialog : MatDialog ;
41
42
let overlayContainer : OverlayContainer ;
42
43
let overlayContainerElement : HTMLElement ;
44
+ let scrolledSubject = new Subject ( ) ;
43
45
44
46
let testViewContainerRef : ViewContainerRef ;
45
47
let viewContainerFixture : ComponentFixture < ComponentWithChildViewContainer > ;
@@ -49,7 +51,10 @@ describe('MatDialog', () => {
49
51
TestBed . configureTestingModule ( {
50
52
imports : [ MatDialogModule , DialogTestModule ] ,
51
53
providers : [
52
- { provide : Location , useClass : SpyLocation }
54
+ { provide : Location , useClass : SpyLocation } ,
55
+ { provide : ScrollDispatcher , useFactory : ( ) => ( {
56
+ scrolled : ( ) => scrolledSubject . asObservable ( )
57
+ } ) } ,
53
58
] ,
54
59
} ) ;
55
60
@@ -188,6 +193,26 @@ describe('MatDialog', () => {
188
193
expect ( overlayContainerElement . querySelector ( 'mat-dialog-container' ) ) . toBeNull ( ) ;
189
194
} ) ) ;
190
195
196
+ it ( 'should dispatch the beforeClose and afterClose events when the ' +
197
+ 'overlay is detached externally' , fakeAsync ( inject ( [ Overlay ] , ( overlay : Overlay ) => {
198
+ const dialogRef = dialog . open ( PizzaMsg , {
199
+ viewContainerRef : testViewContainerRef ,
200
+ scrollStrategy : overlay . scrollStrategies . close ( )
201
+ } ) ;
202
+ const beforeCloseCallback = jasmine . createSpy ( 'beforeClosed callback' ) ;
203
+ const afterCloseCallback = jasmine . createSpy ( 'afterClosed callback' ) ;
204
+
205
+ dialogRef . beforeClose ( ) . subscribe ( beforeCloseCallback ) ;
206
+ dialogRef . afterClosed ( ) . subscribe ( afterCloseCallback ) ;
207
+
208
+ scrolledSubject . next ( ) ;
209
+ viewContainerFixture . detectChanges ( ) ;
210
+ flush ( ) ;
211
+
212
+ expect ( beforeCloseCallback ) . toHaveBeenCalledTimes ( 1 ) ;
213
+ expect ( afterCloseCallback ) . toHaveBeenCalledTimes ( 1 ) ;
214
+ } ) ) ) ;
215
+
191
216
it ( 'should close a dialog and get back a result before it is closed' , fakeAsync ( ( ) => {
192
217
const dialogRef = dialog . open ( PizzaMsg , { viewContainerRef : testViewContainerRef } ) ;
193
218
0 commit comments