1
- import { inject , fakeAsync , async , ComponentFixture , TestBed } from '@angular/core/testing' ;
1
+ import { inject , async , ComponentFixture , TestBed } from '@angular/core/testing' ;
2
2
import { NgModule , Component , Directive , ViewChild , ViewContainerRef } from '@angular/core' ;
3
3
import { MdDialog , MdDialogModule } from './dialog' ;
4
4
import { OverlayContainer } from '@angular2-material/core/overlay/overlay-container' ;
@@ -27,9 +27,9 @@ describe('MdDialog', () => {
27
27
TestBed . compileComponents ( ) ;
28
28
} ) ) ;
29
29
30
- beforeEach ( inject ( [ MdDialog ] , fakeAsync ( ( d : MdDialog ) => {
30
+ beforeEach ( inject ( [ MdDialog ] , ( d : MdDialog ) => {
31
31
dialog = d ;
32
- } ) ) ) ;
32
+ } ) ) ;
33
33
34
34
beforeEach ( ( ) => {
35
35
viewContainerFixture = TestBed . createComponent ( ComponentWithChildViewContainer ) ;
@@ -38,72 +38,58 @@ describe('MdDialog', () => {
38
38
testViewContainerRef = viewContainerFixture . componentInstance . childViewContainer ;
39
39
} ) ;
40
40
41
- it ( 'should open a dialog with a component' , async ( ( ) => {
41
+ it ( 'should open a dialog with a component' , ( ) => {
42
42
let config = new MdDialogConfig ( ) ;
43
43
config . viewContainerRef = testViewContainerRef ;
44
44
45
- dialog . open ( PizzaMsg , config ) . then ( dialogRef => {
46
- expect ( overlayContainerElement . textContent ) . toContain ( 'Pizza' ) ;
47
- expect ( dialogRef . componentInstance ) . toEqual ( jasmine . any ( PizzaMsg ) ) ;
48
- expect ( dialogRef . componentInstance . dialogRef ) . toBe ( dialogRef ) ;
45
+ let dialogRef = dialog . open ( PizzaMsg , config ) ;
49
46
50
- viewContainerFixture . detectChanges ( ) ;
51
- let dialogContainerElement = overlayContainerElement . querySelector ( 'md-dialog-container' ) ;
52
- expect ( dialogContainerElement . getAttribute ( 'role' ) ) . toBe ( 'dialog' ) ;
53
- } ) ;
47
+ viewContainerFixture . detectChanges ( ) ;
54
48
55
- detectChangesForDialogOpen ( viewContainerFixture ) ;
56
- } ) ) ;
49
+ expect ( overlayContainerElement . textContent ) . toContain ( 'Pizza' ) ;
50
+ expect ( dialogRef . componentInstance ) . toEqual ( jasmine . any ( PizzaMsg ) ) ;
51
+ expect ( dialogRef . componentInstance . dialogRef ) . toBe ( dialogRef ) ;
52
+
53
+ viewContainerFixture . detectChanges ( ) ;
54
+ let dialogContainerElement = overlayContainerElement . querySelector ( 'md-dialog-container' ) ;
55
+ expect ( dialogContainerElement . getAttribute ( 'role' ) ) . toBe ( 'dialog' ) ;
56
+ } ) ;
57
57
58
- it ( 'should apply the configured role to the dialog element' , async ( ( ) => {
58
+ it ( 'should apply the configured role to the dialog element' , ( ) => {
59
59
let config = new MdDialogConfig ( ) ;
60
60
config . viewContainerRef = testViewContainerRef ;
61
61
config . role = 'alertdialog' ;
62
62
63
- dialog . open ( PizzaMsg , config ) . then ( dialogRef => {
64
- viewContainerFixture . detectChanges ( ) ;
63
+ dialog . open ( PizzaMsg , config ) ;
65
64
66
- let dialogContainerElement = overlayContainerElement . querySelector ( 'md-dialog-container' ) ;
67
- expect ( dialogContainerElement . getAttribute ( 'role' ) ) . toBe ( 'alertdialog' ) ;
68
- } ) ;
65
+ viewContainerFixture . detectChanges ( ) ;
69
66
70
- detectChangesForDialogOpen ( viewContainerFixture ) ;
71
- } ) ) ;
67
+ let dialogContainerElement = overlayContainerElement . querySelector ( 'md-dialog-container' ) ;
68
+ expect ( dialogContainerElement . getAttribute ( 'role' ) ) . toBe ( 'alertdialog' ) ;
69
+ } ) ;
72
70
73
- it ( 'should close a dialog and get back a result' , async ( ( ) => {
71
+ it ( 'should close a dialog and get back a result' , ( ) => {
74
72
let config = new MdDialogConfig ( ) ;
75
73
config . viewContainerRef = testViewContainerRef ;
76
74
77
- dialog . open ( PizzaMsg , config ) . then ( dialogRef => {
78
- viewContainerFixture . detectChanges ( ) ;
75
+ let dialogRef = dialog . open ( PizzaMsg , config ) ;
79
76
80
- let afterCloseResult : string ;
81
- dialogRef . afterClosed ( ) . subscribe ( result => {
82
- afterCloseResult = result ;
83
- } ) ;
77
+ viewContainerFixture . detectChanges ( ) ;
84
78
85
- dialogRef . close ( 'Charmander' ) ;
79
+ viewContainerFixture . detectChanges ( ) ;
86
80
87
- viewContainerFixture . whenStable ( ) . then ( ( ) => {
88
- expect ( afterCloseResult ) . toBe ( 'Charmander' ) ;
89
- expect ( overlayContainerElement . childNodes . length ) . toBe ( 0 ) ;
90
- } ) ;
81
+ let afterCloseResult : string ;
82
+ dialogRef . afterClosed ( ) . subscribe ( result => {
83
+ afterCloseResult = result ;
91
84
} ) ;
92
85
93
- detectChangesForDialogOpen ( viewContainerFixture ) ;
94
- } ) ) ;
95
- } ) ;
86
+ dialogRef . close ( 'Charmander' ) ;
96
87
88
+ expect ( afterCloseResult ) . toBe ( 'Charmander' ) ;
89
+ expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
90
+ } ) ;
91
+ } ) ;
97
92
98
- /** Runs the necessary detectChanges for a dialog to complete its opening. */
99
- function detectChangesForDialogOpen ( fixture : ComponentFixture < ComponentWithChildViewContainer > ) {
100
- // TODO(jelbourn): figure out why the test zone is "stable" when there are still pending
101
- // tasks, such that we have to use `setTimeout` to run the second round of change detection.
102
- // Two rounds of change detection are necessary: one to *create* the dialog container, and
103
- // another to cause the lifecycle events of the container to run and load the dialog content.
104
- fixture . detectChanges ( ) ;
105
- setTimeout ( ( ) => fixture . detectChanges ( ) , 50 ) ;
106
- }
107
93
108
94
@Directive ( { selector : 'dir-with-view-container' } )
109
95
class DirectiveWithViewContainer {
@@ -123,10 +109,7 @@ class ComponentWithChildViewContainer {
123
109
}
124
110
125
111
/** Simple component for testing ComponentPortal. */
126
- @Component ( {
127
- selector : 'pizza-msg' ,
128
- template : '<p>Pizza</p>' ,
129
- } )
112
+ @Component ( { template : '<p>Pizza</p>' } )
130
113
class PizzaMsg {
131
114
constructor ( public dialogRef : MdDialogRef < PizzaMsg > ) { }
132
115
}
0 commit comments