@@ -23,14 +23,15 @@ import {By} from '@angular/platform-browser';
2323import { Location } from '@angular/common' ;
2424import { SpyLocation } from '@angular/common/testing' ;
2525import { Directionality } from '@angular/cdk/bidi' ;
26- import { Overlay , OverlayContainer } from '@angular/cdk/overlay' ;
26+ import { Overlay , OverlayContainer , ScrollDispatcher } from '@angular/cdk/overlay' ;
2727import { A , ESCAPE } from '@angular/cdk/keycodes' ;
2828import { _supportsShadowDom } from '@angular/cdk/platform' ;
2929import {
3030 dispatchKeyboardEvent ,
3131 createKeyboardEvent ,
3232 dispatchEvent ,
3333} from '@angular/cdk/testing/private' ;
34+ import { Subject } from 'rxjs' ;
3435import { DIALOG_DATA , Dialog , DialogModule , DialogRef } from './index' ;
3536
3637describe ( 'Dialog' , ( ) => {
@@ -41,6 +42,7 @@ describe('Dialog', () => {
4142 let viewContainerFixture : ComponentFixture < ComponentWithChildViewContainer > ;
4243 let mockLocation : SpyLocation ;
4344 let overlay : Overlay ;
45+ let scrolledSubject = new Subject ( ) ;
4446
4547 beforeEach ( fakeAsync ( ( ) => {
4648 TestBed . configureTestingModule ( {
@@ -59,6 +61,10 @@ describe('Dialog', () => {
5961 providers : [
6062 { provide : Location , useClass : SpyLocation } ,
6163 { provide : TEMPLATE_INJECTOR_TEST_TOKEN , useValue : 'hello from test module' } ,
64+ {
65+ provide : ScrollDispatcher ,
66+ useFactory : ( ) => ( { scrolled : ( ) => scrolledSubject } ) ,
67+ } ,
6268 ] ,
6369 } ) ;
6470
@@ -504,24 +510,28 @@ describe('Dialog', () => {
504510 } ) ) ;
505511
506512 it ( 'should close all dialogs when the user goes forwards/backwards in history' , fakeAsync ( ( ) => {
507- dialog . open ( PizzaMsg ) ;
513+ const closeSpy = jasmine . createSpy ( 'closed' ) ;
514+ dialog . open ( PizzaMsg ) . closed . subscribe ( closeSpy ) ;
508515 viewContainerFixture . detectChanges ( ) ;
509- dialog . open ( PizzaMsg ) ;
516+ dialog . open ( PizzaMsg ) . closed . subscribe ( closeSpy ) ;
510517 viewContainerFixture . detectChanges ( ) ;
511518
512519 expect ( overlayContainerElement . querySelectorAll ( 'cdk-dialog-container' ) . length ) . toBe ( 2 ) ;
520+ expect ( closeSpy ) . not . toHaveBeenCalled ( ) ;
513521
514522 mockLocation . simulateUrlPop ( '' ) ;
515523 viewContainerFixture . detectChanges ( ) ;
516524 flush ( ) ;
517525
518526 expect ( overlayContainerElement . querySelectorAll ( 'cdk-dialog-container' ) . length ) . toBe ( 0 ) ;
527+ expect ( closeSpy ) . toHaveBeenCalledTimes ( 2 ) ;
519528 } ) ) ;
520529
521530 it ( 'should close all open dialogs when the location hash changes' , fakeAsync ( ( ) => {
522- dialog . open ( PizzaMsg ) ;
531+ const closeSpy = jasmine . createSpy ( 'closed' ) ;
532+ dialog . open ( PizzaMsg ) . closed . subscribe ( closeSpy ) ;
523533 viewContainerFixture . detectChanges ( ) ;
524- dialog . open ( PizzaMsg ) ;
534+ dialog . open ( PizzaMsg ) . closed . subscribe ( closeSpy ) ;
525535 viewContainerFixture . detectChanges ( ) ;
526536
527537 expect ( overlayContainerElement . querySelectorAll ( 'cdk-dialog-container' ) . length ) . toBe ( 2 ) ;
@@ -533,6 +543,28 @@ describe('Dialog', () => {
533543 expect ( overlayContainerElement . querySelectorAll ( 'cdk-dialog-container' ) . length ) . toBe ( 0 ) ;
534544 } ) ) ;
535545
546+ it ( 'should close the dialog when detached externally' , fakeAsync ( ( ) => {
547+ const closeSpy = jasmine . createSpy ( 'closed' ) ;
548+ dialog
549+ . open ( PizzaMsg , { scrollStrategy : overlay . scrollStrategies . close ( ) } )
550+ . closed . subscribe ( closeSpy ) ;
551+ viewContainerFixture . detectChanges ( ) ;
552+ dialog
553+ . open ( PizzaMsg , { scrollStrategy : overlay . scrollStrategies . close ( ) } )
554+ . closed . subscribe ( closeSpy ) ;
555+ viewContainerFixture . detectChanges ( ) ;
556+
557+ expect ( overlayContainerElement . querySelectorAll ( 'cdk-dialog-container' ) . length ) . toBe ( 2 ) ;
558+ expect ( closeSpy ) . not . toHaveBeenCalled ( ) ;
559+
560+ scrolledSubject . next ( ) ;
561+ viewContainerFixture . detectChanges ( ) ;
562+ flush ( ) ;
563+
564+ expect ( overlayContainerElement . querySelectorAll ( 'cdk-dialog-container' ) . length ) . toBe ( 0 ) ;
565+ expect ( closeSpy ) . toHaveBeenCalledTimes ( 2 ) ;
566+ } ) ) ;
567+
536568 it ( 'should have the componentInstance available in the afterClosed callback' , fakeAsync ( ( ) => {
537569 let dialogRef = dialog . open ( PizzaMsg ) ;
538570 let spy = jasmine . createSpy ( 'afterClosed spy' ) ;
0 commit comments