6
6
ComponentFixture ,
7
7
TestBed ,
8
8
tick ,
9
+ flush ,
9
10
} from '@angular/core/testing' ;
10
11
import {
11
12
NgModule ,
@@ -164,21 +165,21 @@ describe('MdDialog', () => {
164
165
expect ( dialogContainerElement . getAttribute ( 'aria-describedby' ) ) . toBe ( 'description-element' ) ;
165
166
} ) ;
166
167
167
- it ( 'should close a dialog and get back a result' , async ( ( ) => {
168
+ it ( 'should close a dialog and get back a result' , fakeAsync ( ( ) => {
168
169
let dialogRef = dialog . open ( PizzaMsg , { viewContainerRef : testViewContainerRef } ) ;
169
170
let afterCloseCallback = jasmine . createSpy ( 'afterClose callback' ) ;
170
171
171
172
dialogRef . afterClosed ( ) . subscribe ( afterCloseCallback ) ;
172
173
dialogRef . close ( 'Charmander' ) ;
173
174
viewContainerFixture . detectChanges ( ) ;
175
+ flush ( ) ;
174
176
175
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
176
- expect ( afterCloseCallback ) . toHaveBeenCalledWith ( 'Charmander' ) ;
177
- expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
178
- } ) ;
177
+ expect ( afterCloseCallback ) . toHaveBeenCalledWith ( 'Charmander' ) ;
178
+ expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
179
+ flush ( ) ;
179
180
} ) ) ;
180
181
181
- it ( 'should close a dialog and get back a result before it is closed' , async ( ( ) => {
182
+ it ( 'should close a dialog and get back a result before it is closed' , fakeAsync ( ( ) => {
182
183
const dialogRef = dialog . open ( PizzaMsg , { viewContainerRef : testViewContainerRef } ) ;
183
184
184
185
// beforeClose should emit before dialog container is destroyed
@@ -190,24 +191,24 @@ describe('MdDialog', () => {
190
191
dialogRef . beforeClose ( ) . subscribe ( beforeCloseHandler ) ;
191
192
dialogRef . close ( 'Bulbasaurus' ) ;
192
193
viewContainerFixture . detectChanges ( ) ;
194
+ flush ( ) ;
193
195
194
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
195
- expect ( beforeCloseHandler ) . toHaveBeenCalledWith ( 'Bulbasaurus' ) ;
196
- expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
197
- } ) ;
196
+ expect ( beforeCloseHandler ) . toHaveBeenCalledWith ( 'Bulbasaurus' ) ;
197
+ expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
198
+ flush ( ) ;
198
199
} ) ) ;
199
200
200
- it ( 'should close a dialog via the escape key' , async ( ( ) => {
201
+ it ( 'should close a dialog via the escape key' , fakeAsync ( ( ) => {
201
202
dialog . open ( PizzaMsg , {
202
203
viewContainerRef : testViewContainerRef
203
204
} ) ;
204
205
205
206
dispatchKeyboardEvent ( document , 'keydown' , ESCAPE ) ;
206
207
viewContainerFixture . detectChanges ( ) ;
208
+ flush ( ) ;
207
209
208
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
209
- expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
210
- } ) ;
210
+ expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
211
+ flush ( ) ;
211
212
} ) ) ;
212
213
213
214
it ( 'should close from a ViewContainerRef with OnPush change detection' , fakeAsync ( ( ) => {
@@ -235,7 +236,7 @@ describe('MdDialog', () => {
235
236
. toBe ( 0 , 'Expected no open dialogs.' ) ;
236
237
} ) ) ;
237
238
238
- it ( 'should close when clicking on the overlay backdrop' , async ( ( ) => {
239
+ it ( 'should close when clicking on the overlay backdrop' , fakeAsync ( ( ) => {
239
240
dialog . open ( PizzaMsg , {
240
241
viewContainerRef : testViewContainerRef
241
242
} ) ;
@@ -246,13 +247,13 @@ describe('MdDialog', () => {
246
247
247
248
backdrop . click ( ) ;
248
249
viewContainerFixture . detectChanges ( ) ;
250
+ flush ( ) ;
249
251
250
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
251
- expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeFalsy ( ) ;
252
- } ) ;
252
+ expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeFalsy ( ) ;
253
+ flush ( ) ;
253
254
} ) ) ;
254
255
255
- it ( 'should emit the backdropClick stream when clicking on the overlay backdrop' , async ( ( ) => {
256
+ it ( 'should emit the backdropClick stream when clicking on the overlay backdrop' , fakeAsync ( ( ) => {
256
257
const dialogRef = dialog . open ( PizzaMsg , {
257
258
viewContainerRef : testViewContainerRef
258
259
} ) ;
@@ -268,12 +269,12 @@ describe('MdDialog', () => {
268
269
expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
269
270
270
271
viewContainerFixture . detectChanges ( ) ;
272
+ flush ( ) ;
271
273
272
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
273
- // Additional clicks after the dialog has closed should not be emitted
274
- backdrop . click ( ) ;
275
- expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
276
- } ) ;
274
+ // Additional clicks after the dialog has closed should not be emitted
275
+ backdrop . click ( ) ;
276
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
277
+ flush ( ) ;
277
278
} ) ) ;
278
279
279
280
it ( 'should notify the observers if a dialog has been opened' , ( ) => {
@@ -284,7 +285,7 @@ describe('MdDialog', () => {
284
285
} ) ;
285
286
} ) ;
286
287
287
- it ( 'should notify the observers if all open dialogs have finished closing' , async ( ( ) => {
288
+ it ( 'should notify the observers if all open dialogs have finished closing' , fakeAsync ( ( ) => {
288
289
const ref1 = dialog . open ( PizzaMsg , { viewContainerRef : testViewContainerRef } ) ;
289
290
const ref2 = dialog . open ( ContentElementDialog , { viewContainerRef : testViewContainerRef } ) ;
290
291
const spy = jasmine . createSpy ( 'afterAllClosed spy' ) ;
@@ -293,14 +294,16 @@ describe('MdDialog', () => {
293
294
294
295
ref1 . close ( ) ;
295
296
viewContainerFixture . detectChanges ( ) ;
297
+ flush ( ) ;
296
298
297
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
298
- expect ( spy ) . not . toHaveBeenCalled ( ) ;
299
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
299
300
300
- ref2 . close ( ) ;
301
- viewContainerFixture . detectChanges ( ) ;
302
- viewContainerFixture . whenStable ( ) . then ( ( ) => expect ( spy ) . toHaveBeenCalled ( ) ) ;
303
- } ) ;
301
+ ref2 . close ( ) ;
302
+ viewContainerFixture . detectChanges ( ) ;
303
+ flush ( ) ;
304
+
305
+ expect ( spy ) . toHaveBeenCalled ( ) ;
306
+ flush ( ) ;
304
307
} ) ) ;
305
308
306
309
it ( 'should emit the afterAllClosed stream on subscribe if there are no open dialogs' , ( ) => {
@@ -433,7 +436,7 @@ describe('MdDialog', () => {
433
436
expect ( overlayPane . getAttribute ( 'dir' ) ) . toBe ( 'rtl' ) ;
434
437
} ) ;
435
438
436
- it ( 'should close all of the dialogs' , async ( ( ) => {
439
+ it ( 'should close all of the dialogs' , fakeAsync ( ( ) => {
437
440
dialog . open ( PizzaMsg ) ;
438
441
dialog . open ( PizzaMsg ) ;
439
442
dialog . open ( PizzaMsg ) ;
@@ -442,10 +445,10 @@ describe('MdDialog', () => {
442
445
443
446
dialog . closeAll ( ) ;
444
447
viewContainerFixture . detectChanges ( ) ;
448
+ flush ( ) ;
445
449
446
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
447
- expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 0 ) ;
448
- } ) ;
450
+ expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 0 ) ;
451
+ flush ( ) ;
449
452
} ) ) ;
450
453
451
454
it ( 'should set the proper animation states' , ( ) => {
@@ -460,32 +463,32 @@ describe('MdDialog', () => {
460
463
expect ( dialogContainer . _state ) . toBe ( 'exit' ) ;
461
464
} ) ;
462
465
463
- it ( 'should close all dialogs when the user goes forwards/backwards in history' , async ( ( ) => {
466
+ it ( 'should close all dialogs when the user goes forwards/backwards in history' , fakeAsync ( ( ) => {
464
467
dialog . open ( PizzaMsg ) ;
465
468
dialog . open ( PizzaMsg ) ;
466
469
467
470
expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 2 ) ;
468
471
469
472
mockLocation . simulateUrlPop ( '' ) ;
470
473
viewContainerFixture . detectChanges ( ) ;
474
+ flush ( ) ;
471
475
472
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
473
- expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 0 ) ;
474
- } ) ;
476
+ expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 0 ) ;
477
+ flush ( ) ;
475
478
} ) ) ;
476
479
477
- it ( 'should close all open dialogs when the location hash changes' , async ( ( ) => {
480
+ it ( 'should close all open dialogs when the location hash changes' , fakeAsync ( ( ) => {
478
481
dialog . open ( PizzaMsg ) ;
479
482
dialog . open ( PizzaMsg ) ;
480
483
481
484
expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 2 ) ;
482
485
483
486
mockLocation . simulateHashChange ( '' ) ;
484
487
viewContainerFixture . detectChanges ( ) ;
488
+ flush ( ) ;
485
489
486
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
487
- expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 0 ) ;
488
- } ) ;
490
+ expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 0 ) ;
491
+ flush ( ) ;
489
492
} ) ) ;
490
493
491
494
it ( 'should have the componentInstance available in the afterClosed callback' , fakeAsync ( ( ) => {
@@ -534,17 +537,17 @@ describe('MdDialog', () => {
534
537
} ) ;
535
538
} ) ;
536
539
537
- it ( 'should not keep a reference to the component after the dialog is closed' , async ( ( ) => {
540
+ it ( 'should not keep a reference to the component after the dialog is closed' , fakeAsync ( ( ) => {
538
541
let dialogRef = dialog . open ( PizzaMsg ) ;
539
542
540
543
expect ( dialogRef . componentInstance ) . toBeTruthy ( ) ;
541
544
542
545
dialogRef . close ( ) ;
543
546
viewContainerFixture . detectChanges ( ) ;
547
+ flush ( ) ;
544
548
545
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
546
- expect ( dialogRef . componentInstance ) . toBeFalsy ( 'Expected reference to have been cleared.' ) ;
547
- } ) ;
549
+ expect ( dialogRef . componentInstance ) . toBeFalsy ( 'Expected reference to have been cleared.' ) ;
550
+ flush ( ) ;
548
551
} ) ) ;
549
552
550
553
it ( 'should assign a unique id to each dialog' , ( ) => {
@@ -598,7 +601,7 @@ describe('MdDialog', () => {
598
601
expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeTruthy ( ) ;
599
602
} ) ;
600
603
601
- it ( 'should allow for the disableClose option to be updated while open' , async ( ( ) => {
604
+ it ( 'should allow for the disableClose option to be updated while open' , fakeAsync ( ( ) => {
602
605
let dialogRef = dialog . open ( PizzaMsg , {
603
606
disableClose : true ,
604
607
viewContainerRef : testViewContainerRef
@@ -615,9 +618,10 @@ describe('MdDialog', () => {
615
618
backdrop . click ( ) ;
616
619
617
620
viewContainerFixture . detectChanges ( ) ;
618
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
619
- expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeFalsy ( ) ;
620
- } ) ;
621
+ flush ( ) ;
622
+
623
+ expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeFalsy ( ) ;
624
+ flush ( ) ;
621
625
} ) ) ;
622
626
} ) ;
623
627
@@ -878,7 +882,7 @@ describe('MdDialog with a parent MdDialog', () => {
878
882
} ) ;
879
883
880
884
it ( 'should close dialogs opened by a parent when calling closeAll on a child MdDialog' ,
881
- async ( ( ) => {
885
+ fakeAsync ( ( ) => {
882
886
parentDialog . open ( PizzaMsg ) ;
883
887
fixture . detectChanges ( ) ;
884
888
@@ -887,15 +891,15 @@ describe('MdDialog with a parent MdDialog', () => {
887
891
888
892
childDialog . closeAll ( ) ;
889
893
fixture . detectChanges ( ) ;
894
+ flush ( ) ;
890
895
891
- fixture . whenStable ( ) . then ( ( ) => {
892
- expect ( overlayContainerElement . textContent ! . trim ( ) )
893
- . toBe ( '' , 'Expected closeAll on child MdDialog to close dialog opened by parent' ) ;
894
- } ) ;
896
+ expect ( overlayContainerElement . textContent ! . trim ( ) )
897
+ . toBe ( '' , 'Expected closeAll on child MdDialog to close dialog opened by parent' ) ;
898
+ flush ( ) ;
895
899
} ) ) ;
896
900
897
901
it ( 'should close dialogs opened by a child when calling closeAll on a parent MdDialog' ,
898
- async ( ( ) => {
902
+ fakeAsync ( ( ) => {
899
903
childDialog . open ( PizzaMsg ) ;
900
904
fixture . detectChanges ( ) ;
901
905
@@ -904,22 +908,22 @@ describe('MdDialog with a parent MdDialog', () => {
904
908
905
909
parentDialog . closeAll ( ) ;
906
910
fixture . detectChanges ( ) ;
911
+ flush ( ) ;
907
912
908
- fixture . whenStable ( ) . then ( ( ) => {
909
- expect ( overlayContainerElement . textContent ! . trim ( ) )
910
- . toBe ( '' , 'Expected closeAll on parent MdDialog to close dialog opened by child' ) ;
911
- } ) ;
913
+ expect ( overlayContainerElement . textContent ! . trim ( ) )
914
+ . toBe ( '' , 'Expected closeAll on parent MdDialog to close dialog opened by child' ) ;
915
+ flush ( ) ;
912
916
} ) ) ;
913
917
914
- it ( 'should close the top dialog via the escape key' , async ( ( ) => {
918
+ it ( 'should close the top dialog via the escape key' , fakeAsync ( ( ) => {
915
919
childDialog . open ( PizzaMsg ) ;
916
920
917
921
dispatchKeyboardEvent ( document , 'keydown' , ESCAPE ) ;
918
922
fixture . detectChanges ( ) ;
923
+ flush ( ) ;
919
924
920
- fixture . whenStable ( ) . then ( ( ) => {
921
- expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
922
- } ) ;
925
+ expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
926
+ flush ( ) ;
923
927
} ) ) ;
924
928
} ) ;
925
929
0 commit comments