1- import { inject , fakeAsync , async , ComponentFixture , TestBed } from '@angular/core/testing' ;
1+ import { inject , async , ComponentFixture , TestBed } from '@angular/core/testing' ;
22import { NgModule , Component , Directive , ViewChild , ViewContainerRef } from '@angular/core' ;
33import { MdDialog , MdDialogModule } from './dialog' ;
44import { OverlayContainer } from '@angular2-material/core/overlay/overlay-container' ;
@@ -27,9 +27,9 @@ describe('MdDialog', () => {
2727 TestBed . compileComponents ( ) ;
2828 } ) ) ;
2929
30- beforeEach ( inject ( [ MdDialog ] , fakeAsync ( ( d : MdDialog ) => {
30+ beforeEach ( inject ( [ MdDialog ] , ( d : MdDialog ) => {
3131 dialog = d ;
32- } ) ) ) ;
32+ } ) ) ;
3333
3434 beforeEach ( ( ) => {
3535 viewContainerFixture = TestBed . createComponent ( ComponentWithChildViewContainer ) ;
@@ -38,72 +38,58 @@ describe('MdDialog', () => {
3838 testViewContainerRef = viewContainerFixture . componentInstance . childViewContainer ;
3939 } ) ;
4040
41- it ( 'should open a dialog with a component' , async ( ( ) => {
41+ it ( 'should open a dialog with a component' , ( ) => {
4242 let config = new MdDialogConfig ( ) ;
4343 config . viewContainerRef = testViewContainerRef ;
4444
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 ) ;
4946
50- viewContainerFixture . detectChanges ( ) ;
51- let dialogContainerElement = overlayContainerElement . querySelector ( 'md-dialog-container' ) ;
52- expect ( dialogContainerElement . getAttribute ( 'role' ) ) . toBe ( 'dialog' ) ;
53- } ) ;
47+ viewContainerFixture . detectChanges ( ) ;
5448
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+ } ) ;
5757
58- it ( 'should apply the configured role to the dialog element' , async ( ( ) => {
58+ it ( 'should apply the configured role to the dialog element' , ( ) => {
5959 let config = new MdDialogConfig ( ) ;
6060 config . viewContainerRef = testViewContainerRef ;
6161 config . role = 'alertdialog' ;
6262
63- dialog . open ( PizzaMsg , config ) . then ( dialogRef => {
64- viewContainerFixture . detectChanges ( ) ;
63+ dialog . open ( PizzaMsg , config ) ;
6564
66- let dialogContainerElement = overlayContainerElement . querySelector ( 'md-dialog-container' ) ;
67- expect ( dialogContainerElement . getAttribute ( 'role' ) ) . toBe ( 'alertdialog' ) ;
68- } ) ;
65+ viewContainerFixture . detectChanges ( ) ;
6966
70- detectChangesForDialogOpen ( viewContainerFixture ) ;
71- } ) ) ;
67+ let dialogContainerElement = overlayContainerElement . querySelector ( 'md-dialog-container' ) ;
68+ expect ( dialogContainerElement . getAttribute ( 'role' ) ) . toBe ( 'alertdialog' ) ;
69+ } ) ;
7270
73- it ( 'should close a dialog and get back a result' , async ( ( ) => {
71+ it ( 'should close a dialog and get back a result' , ( ) => {
7472 let config = new MdDialogConfig ( ) ;
7573 config . viewContainerRef = testViewContainerRef ;
7674
77- dialog . open ( PizzaMsg , config ) . then ( dialogRef => {
78- viewContainerFixture . detectChanges ( ) ;
75+ let dialogRef = dialog . open ( PizzaMsg , config ) ;
7976
80- let afterCloseResult : string ;
81- dialogRef . afterClosed ( ) . subscribe ( result => {
82- afterCloseResult = result ;
83- } ) ;
77+ viewContainerFixture . detectChanges ( ) ;
8478
85- dialogRef . close ( 'Charmander' ) ;
79+ viewContainerFixture . detectChanges ( ) ;
8680
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 ;
9184 } ) ;
9285
93- detectChangesForDialogOpen ( viewContainerFixture ) ;
94- } ) ) ;
95- } ) ;
86+ dialogRef . close ( 'Charmander' ) ;
9687
88+ expect ( afterCloseResult ) . toBe ( 'Charmander' ) ;
89+ expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
90+ } ) ;
91+ } ) ;
9792
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- }
10793
10894@Directive ( { selector : 'dir-with-view-container' } )
10995class DirectiveWithViewContainer {
@@ -123,10 +109,7 @@ class ComponentWithChildViewContainer {
123109}
124110
125111/** Simple component for testing ComponentPortal. */
126- @Component ( {
127- selector : 'pizza-msg' ,
128- template : '<p>Pizza</p>' ,
129- } )
112+ @Component ( { template : '<p>Pizza</p>' } )
130113class PizzaMsg {
131114 constructor ( public dialogRef : MdDialogRef < PizzaMsg > ) { }
132115}
0 commit comments