@@ -113,18 +113,19 @@ describe('MdDialog', () => {
113113 expect ( dialogContainerElement . getAttribute ( 'role' ) ) . toBe ( 'alertdialog' ) ;
114114 } ) ;
115115
116- it ( 'should close a dialog and get back a result' , async ( ( ) => {
116+ it ( 'should close a dialog and get back a result' , fakeAsync ( ( ) => {
117117 let dialogRef = dialog . open ( PizzaMsg , { viewContainerRef : testViewContainerRef } ) ;
118118 let afterCloseCallback = jasmine . createSpy ( 'afterClose callback' ) ;
119119
120120 dialogRef . afterClosed ( ) . subscribe ( afterCloseCallback ) ;
121121 dialogRef . close ( 'Charmander' ) ;
122+
123+ tick ( 500 ) ;
122124 viewContainerFixture . detectChanges ( ) ;
125+ tick ( ) ;
123126
124- viewContainerFixture . whenStable ( ) . then ( ( ) => {
125- expect ( afterCloseCallback ) . toHaveBeenCalledWith ( 'Charmander' ) ;
126- expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
127- } ) ;
127+ expect ( afterCloseCallback ) . toHaveBeenCalledWith ( 'Charmander' ) ;
128+ expect ( overlayContainerElement . querySelector ( 'md-dialog-container' ) ) . toBeNull ( ) ;
128129 } ) ) ;
129130
130131
@@ -168,32 +169,26 @@ describe('MdDialog', () => {
168169 } ) ) . toBe ( ref ) ;
169170 } ) ;
170171
171- it ( 'should notify the observers if all open dialogs have finished closing' , async ( ( ) => {
172- const ref1 = dialog . open ( PizzaMsg , {
173- viewContainerRef : testViewContainerRef
174- } ) ;
175- const ref2 = dialog . open ( ContentElementDialog , {
176- viewContainerRef : testViewContainerRef
177- } ) ;
178- let allClosed = false ;
172+ it ( 'should notify the observers if all open dialogs have finished closing' , fakeAsync ( ( ) => {
173+ const ref1 = dialog . open ( PizzaMsg , { viewContainerRef : testViewContainerRef } ) ;
174+ const ref2 = dialog . open ( ContentElementDialog , { viewContainerRef : testViewContainerRef } ) ;
175+ const allClosedCallback = jasmine . createSpy ( 'afterAllClosed callback' ) ;
179176
180- dialog . afterAllClosed . subscribe ( ( ) => {
181- allClosed = true ;
182- } ) ;
177+ dialog . afterAllClosed . subscribe ( allClosedCallback ) ;
183178
184179 ref1 . close ( ) ;
180+ tick ( 500 ) ;
185181 viewContainerFixture . detectChanges ( ) ;
182+ tick ( ) ;
186183
187- viewContainerFixture . whenStable ( ) . then ( ( ) => {
188- expect ( allClosed ) . toBeFalsy ( ) ;
184+ expect ( allClosedCallback ) . not . toHaveBeenCalled ( ) ;
189185
190- ref2 . close ( ) ;
191- viewContainerFixture . detectChanges ( ) ;
186+ ref2 . close ( ) ;
187+ tick ( 500 ) ;
188+ viewContainerFixture . detectChanges ( ) ;
189+ tick ( ) ;
192190
193- viewContainerFixture . whenStable ( ) . then ( ( ) => {
194- expect ( allClosed ) . toBeTruthy ( ) ;
195- } ) ;
196- } ) ;
191+ expect ( allClosedCallback ) . toHaveBeenCalled ( ) ;
197192 } ) ) ;
198193
199194 it ( 'should should override the width of the overlay pane' , ( ) => {
@@ -335,28 +330,6 @@ describe('MdDialog', () => {
335330 expect ( dialogContainer . _state ) . toBe ( 'exit' ) ;
336331 } ) ;
337332
338- it ( 'should emit an event with the proper animation state' , async ( ( ) => {
339- let dialogRef = dialog . open ( PizzaMsg , { viewContainerRef : testViewContainerRef } ) ;
340- let dialogContainer : MdDialogContainer =
341- viewContainerFixture . debugElement . query ( By . directive ( MdDialogContainer ) ) . componentInstance ;
342- let spy = jasmine . createSpy ( 'animation state callback' ) ;
343-
344- dialogContainer . _onAnimationStateChange . subscribe ( spy ) ;
345- viewContainerFixture . detectChanges ( ) ;
346-
347- viewContainerFixture . whenStable ( ) . then ( ( ) => {
348- expect ( spy ) . toHaveBeenCalledWith ( 'enter' ) ;
349-
350- dialogRef . close ( ) ;
351- viewContainerFixture . detectChanges ( ) ;
352- expect ( spy ) . toHaveBeenCalledWith ( 'exit-start' ) ;
353-
354- viewContainerFixture . whenStable ( ) . then ( ( ) => {
355- expect ( spy ) . toHaveBeenCalledWith ( 'exit' ) ;
356- } ) ;
357- } ) ;
358- } ) ) ;
359-
360333 describe ( 'passing in data' , ( ) => {
361334 it ( 'should be able to pass in data' , ( ) => {
362335 let config = {
@@ -463,13 +436,42 @@ describe('MdDialog', () => {
463436 dialogRef . close ( ) ;
464437 tick ( 500 ) ;
465438 viewContainerFixture . detectChanges ( ) ;
466- flushMicrotasks ( ) ;
439+ tick ( ) ;
467440
468441 expect ( document . activeElement . id )
469442 . toBe ( 'dialog-trigger' , 'Expected that the trigger was refocused after dialog close' ) ;
470443
471444 document . body . removeChild ( button ) ;
472445 } ) ) ;
446+
447+ it ( 'should allow the consumer to shift focus in afterClosed' , fakeAsync ( ( ) => {
448+ // Create a element that has focus before the dialog is opened.
449+ let button = document . createElement ( 'button' ) ;
450+ let input = document . createElement ( 'input' ) ;
451+
452+ button . id = 'dialog-trigger' ;
453+ input . id = 'input-to-be-focused' ;
454+
455+ document . body . appendChild ( button ) ;
456+ document . body . appendChild ( input ) ;
457+ button . focus ( ) ;
458+
459+ let dialogRef = dialog . open ( PizzaMsg , { viewContainerRef : testViewContainerRef } ) ;
460+
461+ dialogRef . afterClosed ( ) . subscribe ( ( ) => input . focus ( ) ) ;
462+
463+ dialogRef . close ( ) ;
464+ tick ( 500 ) ;
465+ viewContainerFixture . detectChanges ( ) ;
466+ tick ( ) ;
467+
468+ expect ( document . activeElement . id ) . toBe ( 'input-to-be-focused' ,
469+ 'Expected that the trigger was refocused after dialog close' ) ;
470+
471+ document . body . removeChild ( button ) ;
472+ document . body . removeChild ( input ) ;
473+ } ) ) ;
474+
473475 } ) ;
474476
475477 describe ( 'dialog content elements' , ( ) => {
0 commit comments