6
6
inject ,
7
7
TestBed ,
8
8
tick ,
9
+ flush ,
9
10
} from '@angular/core/testing' ;
10
11
import {
11
12
ChangeDetectionStrategy ,
@@ -165,21 +166,21 @@ describe('MatDialog', () => {
165
166
expect ( dialogContainerElement . getAttribute ( 'aria-describedby' ) ) . toBe ( 'description-element' ) ;
166
167
} ) ;
167
168
168
- it ( 'should close a dialog and get back a result' , async ( ( ) => {
169
+ it ( 'should close a dialog and get back a result' , fakeAsync ( ( ) => {
169
170
let dialogRef = dialog . open ( PizzaMsg , { viewContainerRef : testViewContainerRef } ) ;
170
171
let afterCloseCallback = jasmine . createSpy ( 'afterClose callback' ) ;
171
172
172
173
dialogRef . afterClosed ( ) . subscribe ( afterCloseCallback ) ;
173
174
dialogRef . close ( 'Charmander' ) ;
174
175
viewContainerFixture . detectChanges ( ) ;
176
+ flush ( ) ;
175
177
176
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
177
- expect ( afterCloseCallback ) . toHaveBeenCalledWith ( 'Charmander' ) ;
178
- expect ( overlayContainerElement . querySelector ( 'mat-dialog-container' ) ) . toBeNull ( ) ;
179
- } ) ;
178
+ expect ( afterCloseCallback ) . toHaveBeenCalledWith ( 'Charmander' ) ;
179
+ expect ( overlayContainerElement . querySelector ( 'mat-dialog-container' ) ) . toBeNull ( ) ;
180
+ flush ( ) ;
180
181
} ) ) ;
181
182
182
- it ( 'should close a dialog and get back a result before it is closed' , async ( ( ) => {
183
+ it ( 'should close a dialog and get back a result before it is closed' , fakeAsync ( ( ) => {
183
184
const dialogRef = dialog . open ( PizzaMsg , { viewContainerRef : testViewContainerRef } ) ;
184
185
185
186
// beforeClose should emit before dialog container is destroyed
@@ -191,24 +192,24 @@ describe('MatDialog', () => {
191
192
dialogRef . beforeClose ( ) . subscribe ( beforeCloseHandler ) ;
192
193
dialogRef . close ( 'Bulbasaurus' ) ;
193
194
viewContainerFixture . detectChanges ( ) ;
195
+ flush ( ) ;
194
196
195
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
196
- expect ( beforeCloseHandler ) . toHaveBeenCalledWith ( 'Bulbasaurus' ) ;
197
- expect ( overlayContainerElement . querySelector ( 'mat-dialog-container' ) ) . toBeNull ( ) ;
198
- } ) ;
197
+ expect ( beforeCloseHandler ) . toHaveBeenCalledWith ( 'Bulbasaurus' ) ;
198
+ expect ( overlayContainerElement . querySelector ( 'mat-dialog-container' ) ) . toBeNull ( ) ;
199
+ flush ( ) ;
199
200
} ) ) ;
200
201
201
- it ( 'should close a dialog via the escape key' , async ( ( ) => {
202
+ it ( 'should close a dialog via the escape key' , fakeAsync ( ( ) => {
202
203
dialog . open ( PizzaMsg , {
203
204
viewContainerRef : testViewContainerRef
204
205
} ) ;
205
206
206
207
dispatchKeyboardEvent ( document , 'keydown' , ESCAPE ) ;
207
208
viewContainerFixture . detectChanges ( ) ;
209
+ flush ( ) ;
208
210
209
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
210
- expect ( overlayContainerElement . querySelector ( 'mat-dialog-container' ) ) . toBeNull ( ) ;
211
- } ) ;
211
+ expect ( overlayContainerElement . querySelector ( 'mat-dialog-container' ) ) . toBeNull ( ) ;
212
+ flush ( ) ;
212
213
} ) ) ;
213
214
214
215
it ( 'should close from a ViewContainerRef with OnPush change detection' , fakeAsync ( ( ) => {
@@ -236,7 +237,7 @@ describe('MatDialog', () => {
236
237
. toBe ( 0 , 'Expected no open dialogs.' ) ;
237
238
} ) ) ;
238
239
239
- it ( 'should close when clicking on the overlay backdrop' , async ( ( ) => {
240
+ it ( 'should close when clicking on the overlay backdrop' , fakeAsync ( ( ) => {
240
241
dialog . open ( PizzaMsg , {
241
242
viewContainerRef : testViewContainerRef
242
243
} ) ;
@@ -247,13 +248,13 @@ describe('MatDialog', () => {
247
248
248
249
backdrop . click ( ) ;
249
250
viewContainerFixture . detectChanges ( ) ;
251
+ flush ( ) ;
250
252
251
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
252
- expect ( overlayContainerElement . querySelector ( 'mat-dialog-container' ) ) . toBeFalsy ( ) ;
253
- } ) ;
253
+ expect ( overlayContainerElement . querySelector ( 'mat-dialog-container' ) ) . toBeFalsy ( ) ;
254
+ flush ( ) ;
254
255
} ) ) ;
255
256
256
- it ( 'should emit the backdropClick stream when clicking on the overlay backdrop' , async ( ( ) => {
257
+ it ( 'should emit the backdropClick stream when clicking on the overlay backdrop' , fakeAsync ( ( ) => {
257
258
const dialogRef = dialog . open ( PizzaMsg , {
258
259
viewContainerRef : testViewContainerRef
259
260
} ) ;
@@ -269,12 +270,12 @@ describe('MatDialog', () => {
269
270
expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
270
271
271
272
viewContainerFixture . detectChanges ( ) ;
273
+ flush ( ) ;
272
274
273
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
274
- // Additional clicks after the dialog has closed should not be emitted
275
- backdrop . click ( ) ;
276
- expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
277
- } ) ;
275
+ // Additional clicks after the dialog has closed should not be emitted
276
+ backdrop . click ( ) ;
277
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
278
+ flush ( ) ;
278
279
} ) ) ;
279
280
280
281
it ( 'should notify the observers if a dialog has been opened' , ( ) => {
@@ -285,7 +286,7 @@ describe('MatDialog', () => {
285
286
} ) ;
286
287
} ) ;
287
288
288
- it ( 'should notify the observers if all open dialogs have finished closing' , async ( ( ) => {
289
+ it ( 'should notify the observers if all open dialogs have finished closing' , fakeAsync ( ( ) => {
289
290
const ref1 = dialog . open ( PizzaMsg , { viewContainerRef : testViewContainerRef } ) ;
290
291
const ref2 = dialog . open ( ContentElementDialog , { viewContainerRef : testViewContainerRef } ) ;
291
292
const spy = jasmine . createSpy ( 'afterAllClosed spy' ) ;
@@ -294,14 +295,16 @@ describe('MatDialog', () => {
294
295
295
296
ref1 . close ( ) ;
296
297
viewContainerFixture . detectChanges ( ) ;
298
+ flush ( ) ;
297
299
298
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
299
- expect ( spy ) . not . toHaveBeenCalled ( ) ;
300
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
300
301
301
- ref2 . close ( ) ;
302
- viewContainerFixture . detectChanges ( ) ;
303
- viewContainerFixture . whenStable ( ) . then ( ( ) => expect ( spy ) . toHaveBeenCalled ( ) ) ;
304
- } ) ;
302
+ ref2 . close ( ) ;
303
+ viewContainerFixture . detectChanges ( ) ;
304
+ flush ( ) ;
305
+
306
+ expect ( spy ) . toHaveBeenCalled ( ) ;
307
+ flush ( ) ;
305
308
} ) ) ;
306
309
307
310
it ( 'should emit the afterAllClosed stream on subscribe if there are no open dialogs' , ( ) => {
@@ -441,8 +444,8 @@ describe('MatDialog', () => {
441
444
442
445
expect ( dialogRef . componentInstance . directionality . value ) . toBe ( 'rtl' ) ;
443
446
} ) ;
444
-
445
- it ( 'should close all of the dialogs' , async ( ( ) => {
447
+
448
+ it ( 'should close all of the dialogs' , fakeAsync ( ( ) => {
446
449
dialog . open ( PizzaMsg ) ;
447
450
dialog . open ( PizzaMsg ) ;
448
451
dialog . open ( PizzaMsg ) ;
@@ -451,10 +454,10 @@ describe('MatDialog', () => {
451
454
452
455
dialog . closeAll ( ) ;
453
456
viewContainerFixture . detectChanges ( ) ;
457
+ flush ( ) ;
454
458
455
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
456
- expect ( overlayContainerElement . querySelectorAll ( 'mat-dialog-container' ) . length ) . toBe ( 0 ) ;
457
- } ) ;
459
+ expect ( overlayContainerElement . querySelectorAll ( 'mat-dialog-container' ) . length ) . toBe ( 0 ) ;
460
+ flush ( ) ;
458
461
} ) ) ;
459
462
460
463
it ( 'should set the proper animation states' , ( ) => {
@@ -469,32 +472,32 @@ describe('MatDialog', () => {
469
472
expect ( dialogContainer . _state ) . toBe ( 'exit' ) ;
470
473
} ) ;
471
474
472
- it ( 'should close all dialogs when the user goes forwards/backwards in history' , async ( ( ) => {
475
+ it ( 'should close all dialogs when the user goes forwards/backwards in history' , fakeAsync ( ( ) => {
473
476
dialog . open ( PizzaMsg ) ;
474
477
dialog . open ( PizzaMsg ) ;
475
478
476
479
expect ( overlayContainerElement . querySelectorAll ( 'mat-dialog-container' ) . length ) . toBe ( 2 ) ;
477
480
478
481
mockLocation . simulateUrlPop ( '' ) ;
479
482
viewContainerFixture . detectChanges ( ) ;
483
+ flush ( ) ;
480
484
481
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
482
- expect ( overlayContainerElement . querySelectorAll ( 'mat-dialog-container' ) . length ) . toBe ( 0 ) ;
483
- } ) ;
485
+ expect ( overlayContainerElement . querySelectorAll ( 'mat-dialog-container' ) . length ) . toBe ( 0 ) ;
486
+ flush ( ) ;
484
487
} ) ) ;
485
488
486
- it ( 'should close all open dialogs when the location hash changes' , async ( ( ) => {
489
+ it ( 'should close all open dialogs when the location hash changes' , fakeAsync ( ( ) => {
487
490
dialog . open ( PizzaMsg ) ;
488
491
dialog . open ( PizzaMsg ) ;
489
492
490
493
expect ( overlayContainerElement . querySelectorAll ( 'mat-dialog-container' ) . length ) . toBe ( 2 ) ;
491
494
492
495
mockLocation . simulateHashChange ( '' ) ;
493
496
viewContainerFixture . detectChanges ( ) ;
497
+ flush ( ) ;
494
498
495
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
496
- expect ( overlayContainerElement . querySelectorAll ( 'mat-dialog-container' ) . length ) . toBe ( 0 ) ;
497
- } ) ;
499
+ expect ( overlayContainerElement . querySelectorAll ( 'mat-dialog-container' ) . length ) . toBe ( 0 ) ;
500
+ flush ( ) ;
498
501
} ) ) ;
499
502
500
503
it ( 'should have the componentInstance available in the afterClosed callback' , fakeAsync ( ( ) => {
@@ -543,17 +546,17 @@ describe('MatDialog', () => {
543
546
} ) ;
544
547
} ) ;
545
548
546
- it ( 'should not keep a reference to the component after the dialog is closed' , async ( ( ) => {
549
+ it ( 'should not keep a reference to the component after the dialog is closed' , fakeAsync ( ( ) => {
547
550
let dialogRef = dialog . open ( PizzaMsg ) ;
548
551
549
552
expect ( dialogRef . componentInstance ) . toBeTruthy ( ) ;
550
553
551
554
dialogRef . close ( ) ;
552
555
viewContainerFixture . detectChanges ( ) ;
556
+ flush ( ) ;
553
557
554
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
555
- expect ( dialogRef . componentInstance ) . toBeFalsy ( 'Expected reference to have been cleared.' ) ;
556
- } ) ;
558
+ expect ( dialogRef . componentInstance ) . toBeFalsy ( 'Expected reference to have been cleared.' ) ;
559
+ flush ( ) ;
557
560
} ) ) ;
558
561
559
562
it ( 'should assign a unique id to each dialog' , ( ) => {
@@ -607,7 +610,7 @@ describe('MatDialog', () => {
607
610
expect ( overlayContainerElement . querySelector ( 'mat-dialog-container' ) ) . toBeTruthy ( ) ;
608
611
} ) ;
609
612
610
- it ( 'should allow for the disableClose option to be updated while open' , async ( ( ) => {
613
+ it ( 'should allow for the disableClose option to be updated while open' , fakeAsync ( ( ) => {
611
614
let dialogRef = dialog . open ( PizzaMsg , {
612
615
disableClose : true ,
613
616
viewContainerRef : testViewContainerRef
@@ -624,9 +627,10 @@ describe('MatDialog', () => {
624
627
backdrop . click ( ) ;
625
628
626
629
viewContainerFixture . detectChanges ( ) ;
627
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
628
- expect ( overlayContainerElement . querySelector ( 'mat-dialog-container' ) ) . toBeFalsy ( ) ;
629
- } ) ;
630
+ flush ( ) ;
631
+
632
+ expect ( overlayContainerElement . querySelector ( 'mat-dialog-container' ) ) . toBeFalsy ( ) ;
633
+ flush ( ) ;
630
634
} ) ) ;
631
635
} ) ;
632
636
@@ -887,7 +891,7 @@ describe('MatDialog with a parent MatDialog', () => {
887
891
} ) ;
888
892
889
893
it ( 'should close dialogs opened by a parent when calling closeAll on a child MatDialog' ,
890
- async ( ( ) => {
894
+ fakeAsync ( ( ) => {
891
895
parentDialog . open ( PizzaMsg ) ;
892
896
fixture . detectChanges ( ) ;
893
897
@@ -896,15 +900,15 @@ describe('MatDialog with a parent MatDialog', () => {
896
900
897
901
childDialog . closeAll ( ) ;
898
902
fixture . detectChanges ( ) ;
903
+ flush ( ) ;
899
904
900
- fixture . whenStable ( ) . then ( ( ) => {
901
- expect ( overlayContainerElement . textContent ! . trim ( ) )
902
- . toBe ( '' , 'Expected closeAll on child MatDialog to close dialog opened by parent' ) ;
903
- } ) ;
905
+ expect ( overlayContainerElement . textContent ! . trim ( ) )
906
+ . toBe ( '' , 'Expected closeAll on child MatDialog to close dialog opened by parent' ) ;
907
+ flush ( ) ;
904
908
} ) ) ;
905
909
906
910
it ( 'should close dialogs opened by a child when calling closeAll on a parent MatDialog' ,
907
- async ( ( ) => {
911
+ fakeAsync ( ( ) => {
908
912
childDialog . open ( PizzaMsg ) ;
909
913
fixture . detectChanges ( ) ;
910
914
@@ -913,22 +917,22 @@ describe('MatDialog with a parent MatDialog', () => {
913
917
914
918
parentDialog . closeAll ( ) ;
915
919
fixture . detectChanges ( ) ;
920
+ flush ( ) ;
916
921
917
- fixture . whenStable ( ) . then ( ( ) => {
918
- expect ( overlayContainerElement . textContent ! . trim ( ) )
919
- . toBe ( '' , 'Expected closeAll on parent MatDialog to close dialog opened by child' ) ;
920
- } ) ;
922
+ expect ( overlayContainerElement . textContent ! . trim ( ) )
923
+ . toBe ( '' , 'Expected closeAll on parent MatDialog to close dialog opened by child' ) ;
924
+ flush ( ) ;
921
925
} ) ) ;
922
926
923
- it ( 'should close the top dialog via the escape key' , async ( ( ) => {
927
+ it ( 'should close the top dialog via the escape key' , fakeAsync ( ( ) => {
924
928
childDialog . open ( PizzaMsg ) ;
925
929
926
930
dispatchKeyboardEvent ( document , 'keydown' , ESCAPE ) ;
927
931
fixture . detectChanges ( ) ;
932
+ flush ( ) ;
928
933
929
- fixture . whenStable ( ) . then ( ( ) => {
930
- expect ( overlayContainerElement . querySelector ( 'mat-dialog-container' ) ) . toBeNull ( ) ;
931
- } ) ;
934
+ expect ( overlayContainerElement . querySelector ( 'mat-dialog-container' ) ) . toBeNull ( ) ;
935
+ flush ( ) ;
932
936
} ) ) ;
933
937
} ) ;
934
938
0 commit comments