@@ -23,14 +23,15 @@ import {By} from '@angular/platform-browser';
23
23
import { Location } from '@angular/common' ;
24
24
import { SpyLocation } from '@angular/common/testing' ;
25
25
import { Directionality } from '@angular/cdk/bidi' ;
26
- import { Overlay , OverlayContainer } from '@angular/cdk/overlay' ;
26
+ import { Overlay , OverlayContainer , ScrollDispatcher } from '@angular/cdk/overlay' ;
27
27
import { A , ESCAPE } from '@angular/cdk/keycodes' ;
28
28
import { _supportsShadowDom } from '@angular/cdk/platform' ;
29
29
import {
30
30
dispatchKeyboardEvent ,
31
31
createKeyboardEvent ,
32
32
dispatchEvent ,
33
33
} from '@angular/cdk/testing/private' ;
34
+ import { Subject } from 'rxjs' ;
34
35
import { DIALOG_DATA , Dialog , DialogModule , DialogRef } from './index' ;
35
36
36
37
describe ( 'Dialog' , ( ) => {
@@ -41,6 +42,7 @@ describe('Dialog', () => {
41
42
let viewContainerFixture : ComponentFixture < ComponentWithChildViewContainer > ;
42
43
let mockLocation : SpyLocation ;
43
44
let overlay : Overlay ;
45
+ let scrolledSubject = new Subject ( ) ;
44
46
45
47
beforeEach ( fakeAsync ( ( ) => {
46
48
TestBed . configureTestingModule ( {
@@ -59,6 +61,10 @@ describe('Dialog', () => {
59
61
providers : [
60
62
{ provide : Location , useClass : SpyLocation } ,
61
63
{ provide : TEMPLATE_INJECTOR_TEST_TOKEN , useValue : 'hello from test module' } ,
64
+ {
65
+ provide : ScrollDispatcher ,
66
+ useFactory : ( ) => ( { scrolled : ( ) => scrolledSubject } ) ,
67
+ } ,
62
68
] ,
63
69
} ) ;
64
70
@@ -504,24 +510,28 @@ describe('Dialog', () => {
504
510
} ) ) ;
505
511
506
512
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 ) ;
508
515
viewContainerFixture . detectChanges ( ) ;
509
- dialog . open ( PizzaMsg ) ;
516
+ dialog . open ( PizzaMsg ) . closed . subscribe ( closeSpy ) ;
510
517
viewContainerFixture . detectChanges ( ) ;
511
518
512
519
expect ( overlayContainerElement . querySelectorAll ( 'cdk-dialog-container' ) . length ) . toBe ( 2 ) ;
520
+ expect ( closeSpy ) . not . toHaveBeenCalled ( ) ;
513
521
514
522
mockLocation . simulateUrlPop ( '' ) ;
515
523
viewContainerFixture . detectChanges ( ) ;
516
524
flush ( ) ;
517
525
518
526
expect ( overlayContainerElement . querySelectorAll ( 'cdk-dialog-container' ) . length ) . toBe ( 0 ) ;
527
+ expect ( closeSpy ) . toHaveBeenCalledTimes ( 2 ) ;
519
528
} ) ) ;
520
529
521
530
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 ) ;
523
533
viewContainerFixture . detectChanges ( ) ;
524
- dialog . open ( PizzaMsg ) ;
534
+ dialog . open ( PizzaMsg ) . closed . subscribe ( closeSpy ) ;
525
535
viewContainerFixture . detectChanges ( ) ;
526
536
527
537
expect ( overlayContainerElement . querySelectorAll ( 'cdk-dialog-container' ) . length ) . toBe ( 2 ) ;
@@ -533,6 +543,28 @@ describe('Dialog', () => {
533
543
expect ( overlayContainerElement . querySelectorAll ( 'cdk-dialog-container' ) . length ) . toBe ( 0 ) ;
534
544
} ) ) ;
535
545
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
+
536
568
it ( 'should have the componentInstance available in the afterClosed callback' , fakeAsync ( ( ) => {
537
569
let dialogRef = dialog . open ( PizzaMsg ) ;
538
570
let spy = jasmine . createSpy ( 'afterClosed spy' ) ;
0 commit comments