6
6
ComponentFixture ,
7
7
TestBed ,
8
8
tick ,
9
+ flush ,
9
10
} from '@angular/core/testing' ;
10
11
import {
11
12
NgModule ,
@@ -149,21 +150,21 @@ describe('MdDialog', () => {
149
150
expect ( dialogContainerElement . getAttribute ( 'aria-describedby' ) ) . toBe ( 'description-element' ) ;
150
151
} ) ;
151
152
152
- it ( 'should close a dialog and get back a result' , async ( ( ) => {
153
+ it ( 'should close a dialog and get back a result' , fakeAsync ( ( ) => {
153
154
let dialogRef = dialog . open ( PizzaMsg , { viewContainerRef : testViewContainerRef } ) ;
154
155
let afterCloseCallback = jasmine . createSpy ( 'afterClose callback' ) ;
155
156
156
157
dialogRef . afterClosed ( ) . subscribe ( afterCloseCallback ) ;
157
158
dialogRef . close ( 'Charmander' ) ;
158
159
viewContainerFixture . detectChanges ( ) ;
160
+ flush ( ) ;
159
161
160
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
161
- expect ( afterCloseCallback ) . toHaveBeenCalledWith ( 'Charmander' ) ;
162
- expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
163
- } ) ;
162
+ expect ( afterCloseCallback ) . toHaveBeenCalledWith ( 'Charmander' ) ;
163
+ expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
164
+ flush ( ) ;
164
165
} ) ) ;
165
166
166
- it ( 'should close a dialog and get back a result before it is closed' , async ( ( ) => {
167
+ it ( 'should close a dialog and get back a result before it is closed' , fakeAsync ( ( ) => {
167
168
const dialogRef = dialog . open ( PizzaMsg , { viewContainerRef : testViewContainerRef } ) ;
168
169
169
170
// beforeClose should emit before dialog container is destroyed
@@ -175,24 +176,24 @@ describe('MdDialog', () => {
175
176
dialogRef . beforeClose ( ) . subscribe ( beforeCloseHandler ) ;
176
177
dialogRef . close ( 'Bulbasaurus' ) ;
177
178
viewContainerFixture . detectChanges ( ) ;
179
+ flush ( ) ;
178
180
179
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
180
- expect ( beforeCloseHandler ) . toHaveBeenCalledWith ( 'Bulbasaurus' ) ;
181
- expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
182
- } ) ;
181
+ expect ( beforeCloseHandler ) . toHaveBeenCalledWith ( 'Bulbasaurus' ) ;
182
+ expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
183
+ flush ( ) ;
183
184
} ) ) ;
184
185
185
- it ( 'should close a dialog via the escape key' , async ( ( ) => {
186
+ it ( 'should close a dialog via the escape key' , fakeAsync ( ( ) => {
186
187
dialog . open ( PizzaMsg , {
187
188
viewContainerRef : testViewContainerRef
188
189
} ) ;
189
190
190
191
dispatchKeyboardEvent ( document , 'keydown' , ESCAPE ) ;
191
192
viewContainerFixture . detectChanges ( ) ;
193
+ flush ( ) ;
192
194
193
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
194
- expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
195
- } ) ;
195
+ expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
196
+ flush ( ) ;
196
197
} ) ) ;
197
198
198
199
it ( 'should close from a ViewContainerRef with OnPush change detection' , fakeAsync ( ( ) => {
@@ -220,7 +221,7 @@ describe('MdDialog', () => {
220
221
. toBe ( 0 , 'Expected no open dialogs.' ) ;
221
222
} ) ) ;
222
223
223
- it ( 'should close when clicking on the overlay backdrop' , async ( ( ) => {
224
+ it ( 'should close when clicking on the overlay backdrop' , fakeAsync ( ( ) => {
224
225
dialog . open ( PizzaMsg , {
225
226
viewContainerRef : testViewContainerRef
226
227
} ) ;
@@ -231,13 +232,13 @@ describe('MdDialog', () => {
231
232
232
233
backdrop . click ( ) ;
233
234
viewContainerFixture . detectChanges ( ) ;
235
+ flush ( ) ;
234
236
235
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
236
- expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeFalsy ( ) ;
237
- } ) ;
237
+ expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeFalsy ( ) ;
238
+ flush ( ) ;
238
239
} ) ) ;
239
240
240
- it ( 'should emit the backdropClick stream when clicking on the overlay backdrop' , async ( ( ) => {
241
+ it ( 'should emit the backdropClick stream when clicking on the overlay backdrop' , fakeAsync ( ( ) => {
241
242
const dialogRef = dialog . open ( PizzaMsg , {
242
243
viewContainerRef : testViewContainerRef
243
244
} ) ;
@@ -253,12 +254,12 @@ describe('MdDialog', () => {
253
254
expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
254
255
255
256
viewContainerFixture . detectChanges ( ) ;
257
+ flush ( ) ;
256
258
257
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
258
- // Additional clicks after the dialog has closed should not be emitted
259
- backdrop . click ( ) ;
260
- expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
261
- } ) ;
259
+ // Additional clicks after the dialog has closed should not be emitted
260
+ backdrop . click ( ) ;
261
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
262
+ flush ( ) ;
262
263
} ) ) ;
263
264
264
265
it ( 'should notify the observers if a dialog has been opened' , ( ) => {
@@ -269,7 +270,7 @@ describe('MdDialog', () => {
269
270
} ) ;
270
271
} ) ;
271
272
272
- it ( 'should notify the observers if all open dialogs have finished closing' , async ( ( ) => {
273
+ it ( 'should notify the observers if all open dialogs have finished closing' , fakeAsync ( ( ) => {
273
274
const ref1 = dialog . open ( PizzaMsg , { viewContainerRef : testViewContainerRef } ) ;
274
275
const ref2 = dialog . open ( ContentElementDialog , { viewContainerRef : testViewContainerRef } ) ;
275
276
const spy = jasmine . createSpy ( 'afterAllClosed spy' ) ;
@@ -278,14 +279,16 @@ describe('MdDialog', () => {
278
279
279
280
ref1 . close ( ) ;
280
281
viewContainerFixture . detectChanges ( ) ;
282
+ flush ( ) ;
281
283
282
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
283
- expect ( spy ) . not . toHaveBeenCalled ( ) ;
284
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
284
285
285
- ref2 . close ( ) ;
286
- viewContainerFixture . detectChanges ( ) ;
287
- viewContainerFixture . whenStable ( ) . then ( ( ) => expect ( spy ) . toHaveBeenCalled ( ) ) ;
288
- } ) ;
286
+ ref2 . close ( ) ;
287
+ viewContainerFixture . detectChanges ( ) ;
288
+ flush ( ) ;
289
+
290
+ expect ( spy ) . toHaveBeenCalled ( ) ;
291
+ flush ( ) ;
289
292
} ) ) ;
290
293
291
294
it ( 'should emit the afterAllClosed stream on subscribe if there are no open dialogs' , ( ) => {
@@ -418,7 +421,7 @@ describe('MdDialog', () => {
418
421
expect ( overlayPane . getAttribute ( 'dir' ) ) . toBe ( 'rtl' ) ;
419
422
} ) ;
420
423
421
- it ( 'should close all of the dialogs' , async ( ( ) => {
424
+ it ( 'should close all of the dialogs' , fakeAsync ( ( ) => {
422
425
dialog . open ( PizzaMsg ) ;
423
426
dialog . open ( PizzaMsg ) ;
424
427
dialog . open ( PizzaMsg ) ;
@@ -427,10 +430,10 @@ describe('MdDialog', () => {
427
430
428
431
dialog . closeAll ( ) ;
429
432
viewContainerFixture . detectChanges ( ) ;
433
+ flush ( ) ;
430
434
431
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
432
- expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 0 ) ;
433
- } ) ;
435
+ expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 0 ) ;
436
+ flush ( ) ;
434
437
} ) ) ;
435
438
436
439
it ( 'should set the proper animation states' , ( ) => {
@@ -445,32 +448,32 @@ describe('MdDialog', () => {
445
448
expect ( dialogContainer . _state ) . toBe ( 'exit' ) ;
446
449
} ) ;
447
450
448
- it ( 'should close all dialogs when the user goes forwards/backwards in history' , async ( ( ) => {
451
+ it ( 'should close all dialogs when the user goes forwards/backwards in history' , fakeAsync ( ( ) => {
449
452
dialog . open ( PizzaMsg ) ;
450
453
dialog . open ( PizzaMsg ) ;
451
454
452
455
expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 2 ) ;
453
456
454
457
mockLocation . simulateUrlPop ( '' ) ;
455
458
viewContainerFixture . detectChanges ( ) ;
459
+ flush ( ) ;
456
460
457
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
458
- expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 0 ) ;
459
- } ) ;
461
+ expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 0 ) ;
462
+ flush ( ) ;
460
463
} ) ) ;
461
464
462
- it ( 'should close all open dialogs when the location hash changes' , async ( ( ) => {
465
+ it ( 'should close all open dialogs when the location hash changes' , fakeAsync ( ( ) => {
463
466
dialog . open ( PizzaMsg ) ;
464
467
dialog . open ( PizzaMsg ) ;
465
468
466
469
expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 2 ) ;
467
470
468
471
mockLocation . simulateHashChange ( '' ) ;
469
472
viewContainerFixture . detectChanges ( ) ;
473
+ flush ( ) ;
470
474
471
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
472
- expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 0 ) ;
473
- } ) ;
475
+ expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 0 ) ;
476
+ flush ( ) ;
474
477
} ) ) ;
475
478
476
479
it ( 'should have the componentInstance available in the afterClosed callback' , fakeAsync ( ( ) => {
@@ -519,17 +522,17 @@ describe('MdDialog', () => {
519
522
} ) ;
520
523
} ) ;
521
524
522
- it ( 'should not keep a reference to the component after the dialog is closed' , async ( ( ) => {
525
+ it ( 'should not keep a reference to the component after the dialog is closed' , fakeAsync ( ( ) => {
523
526
let dialogRef = dialog . open ( PizzaMsg ) ;
524
527
525
528
expect ( dialogRef . componentInstance ) . toBeTruthy ( ) ;
526
529
527
530
dialogRef . close ( ) ;
528
531
viewContainerFixture . detectChanges ( ) ;
532
+ flush ( ) ;
529
533
530
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
531
- expect ( dialogRef . componentInstance ) . toBeFalsy ( 'Expected reference to have been cleared.' ) ;
532
- } ) ;
534
+ expect ( dialogRef . componentInstance ) . toBeFalsy ( 'Expected reference to have been cleared.' ) ;
535
+ flush ( ) ;
533
536
} ) ) ;
534
537
535
538
it ( 'should assign a unique id to each dialog' , ( ) => {
@@ -583,7 +586,7 @@ describe('MdDialog', () => {
583
586
expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeTruthy ( ) ;
584
587
} ) ;
585
588
586
- it ( 'should allow for the disableClose option to be updated while open' , async ( ( ) => {
589
+ it ( 'should allow for the disableClose option to be updated while open' , fakeAsync ( ( ) => {
587
590
let dialogRef = dialog . open ( PizzaMsg , {
588
591
disableClose : true ,
589
592
viewContainerRef : testViewContainerRef
@@ -600,9 +603,10 @@ describe('MdDialog', () => {
600
603
backdrop . click ( ) ;
601
604
602
605
viewContainerFixture . detectChanges ( ) ;
603
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
604
- expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeFalsy ( ) ;
605
- } ) ;
606
+ flush ( ) ;
607
+
608
+ expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeFalsy ( ) ;
609
+ flush ( ) ;
606
610
} ) ) ;
607
611
} ) ;
608
612
@@ -863,7 +867,7 @@ describe('MdDialog with a parent MdDialog', () => {
863
867
} ) ;
864
868
865
869
it ( 'should close dialogs opened by a parent when calling closeAll on a child MdDialog' ,
866
- async ( ( ) => {
870
+ fakeAsync ( ( ) => {
867
871
parentDialog . open ( PizzaMsg ) ;
868
872
fixture . detectChanges ( ) ;
869
873
@@ -872,15 +876,15 @@ describe('MdDialog with a parent MdDialog', () => {
872
876
873
877
childDialog . closeAll ( ) ;
874
878
fixture . detectChanges ( ) ;
879
+ flush ( ) ;
875
880
876
- fixture . whenStable ( ) . then ( ( ) => {
877
- expect ( overlayContainerElement . textContent ! . trim ( ) )
878
- . toBe ( '' , 'Expected closeAll on child MdDialog to close dialog opened by parent' ) ;
879
- } ) ;
881
+ expect ( overlayContainerElement . textContent ! . trim ( ) )
882
+ . toBe ( '' , 'Expected closeAll on child MdDialog to close dialog opened by parent' ) ;
883
+ flush ( ) ;
880
884
} ) ) ;
881
885
882
886
it ( 'should close dialogs opened by a child when calling closeAll on a parent MdDialog' ,
883
- async ( ( ) => {
887
+ fakeAsync ( ( ) => {
884
888
childDialog . open ( PizzaMsg ) ;
885
889
fixture . detectChanges ( ) ;
886
890
@@ -889,22 +893,22 @@ describe('MdDialog with a parent MdDialog', () => {
889
893
890
894
parentDialog . closeAll ( ) ;
891
895
fixture . detectChanges ( ) ;
896
+ flush ( ) ;
892
897
893
- fixture . whenStable ( ) . then ( ( ) => {
894
- expect ( overlayContainerElement . textContent ! . trim ( ) )
895
- . toBe ( '' , 'Expected closeAll on parent MdDialog to close dialog opened by child' ) ;
896
- } ) ;
898
+ expect ( overlayContainerElement . textContent ! . trim ( ) )
899
+ . toBe ( '' , 'Expected closeAll on parent MdDialog to close dialog opened by child' ) ;
900
+ flush ( ) ;
897
901
} ) ) ;
898
902
899
- it ( 'should close the top dialog via the escape key' , async ( ( ) => {
903
+ it ( 'should close the top dialog via the escape key' , fakeAsync ( ( ) => {
900
904
childDialog . open ( PizzaMsg ) ;
901
905
902
906
dispatchKeyboardEvent ( document , 'keydown' , ESCAPE ) ;
903
907
fixture . detectChanges ( ) ;
908
+ flush ( ) ;
904
909
905
- fixture . whenStable ( ) . then ( ( ) => {
906
- expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
907
- } ) ;
910
+ expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
911
+ flush ( ) ;
908
912
} ) ) ;
909
913
} ) ;
910
914
0 commit comments