@@ -18,6 +18,8 @@ import {
1818} from '@angular/core' ;
1919import { By } from '@angular/platform-browser' ;
2020import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
21+ import { Location } from '@angular/common' ;
22+ import { SpyLocation } from '@angular/common/testing' ;
2123import { MdDialogModule } from './index' ;
2224import { MdDialog } from './dialog' ;
2325import { MdDialogContainer } from './dialog-container' ;
@@ -33,6 +35,7 @@ describe('MdDialog', () => {
3335
3436 let testViewContainerRef : ViewContainerRef ;
3537 let viewContainerFixture : ComponentFixture < ComponentWithChildViewContainer > ;
38+ let mockLocation : SpyLocation ;
3639
3740 beforeEach ( async ( ( ) => {
3841 TestBed . configureTestingModule ( {
@@ -41,15 +44,17 @@ describe('MdDialog', () => {
4144 { provide : OverlayContainer , useFactory : ( ) => {
4245 overlayContainerElement = document . createElement ( 'div' ) ;
4346 return { getContainerElement : ( ) => overlayContainerElement } ;
44- } }
47+ } } ,
48+ { provide : Location , useClass : SpyLocation }
4549 ] ,
4650 } ) ;
4751
4852 TestBed . compileComponents ( ) ;
4953 } ) ) ;
5054
51- beforeEach ( inject ( [ MdDialog ] , ( d : MdDialog ) => {
55+ beforeEach ( inject ( [ MdDialog , Location ] , ( d : MdDialog , l : Location ) => {
5256 dialog = d ;
57+ mockLocation = l as SpyLocation ;
5358 } ) ) ;
5459
5560 beforeEach ( ( ) => {
@@ -325,6 +330,34 @@ describe('MdDialog', () => {
325330 } ) ;
326331 } ) ) ;
327332
333+ it ( 'should close all dialogs when the user goes forwards/backwards in history' , async ( ( ) => {
334+ dialog . open ( PizzaMsg ) ;
335+ dialog . open ( PizzaMsg ) ;
336+
337+ expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 2 ) ;
338+
339+ mockLocation . simulateUrlPop ( '' ) ;
340+ viewContainerFixture . detectChanges ( ) ;
341+
342+ viewContainerFixture . whenStable ( ) . then ( ( ) => {
343+ expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 0 ) ;
344+ } ) ;
345+ } ) ) ;
346+
347+ it ( 'should close all open dialogs when the location hash changes' , async ( ( ) => {
348+ dialog . open ( PizzaMsg ) ;
349+ dialog . open ( PizzaMsg ) ;
350+
351+ expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 2 ) ;
352+
353+ mockLocation . simulateHashChange ( '' ) ;
354+ viewContainerFixture . detectChanges ( ) ;
355+
356+ viewContainerFixture . whenStable ( ) . then ( ( ) => {
357+ expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 0 ) ;
358+ } ) ;
359+ } ) ) ;
360+
328361 describe ( 'passing in data' , ( ) => {
329362 it ( 'should be able to pass in data' , ( ) => {
330363 let config = {
@@ -501,7 +534,8 @@ describe('MdDialog with a parent MdDialog', () => {
501534 { provide : OverlayContainer , useFactory : ( ) => {
502535 overlayContainerElement = document . createElement ( 'div' ) ;
503536 return { getContainerElement : ( ) => overlayContainerElement } ;
504- } }
537+ } } ,
538+ { provide : Location , useClass : SpyLocation }
505539 ] ,
506540 } ) ;
507541
0 commit comments