@@ -292,6 +292,8 @@ describe('$modal', function () {
292
292
expect ( document . activeElement . tagName ) . toBe ( 'A' ) ;
293
293
294
294
var modal = open ( { template : '<div>Content<button>inside modal</button></div>' } ) ;
295
+ $animate . flush ( ) ;
296
+ $rootScope . $digest ( ) ;
295
297
expect ( document . activeElement . tagName ) . toBe ( 'DIV' ) ;
296
298
expect ( $document ) . toHaveModalsOpen ( 1 ) ;
297
299
@@ -315,6 +317,8 @@ describe('$modal', function () {
315
317
expect ( document . activeElement . tagName ) . toBe ( 'A' ) ;
316
318
317
319
var modal = open ( { template : '<div>Content</div>' } ) ;
320
+ $animate . flush ( ) ;
321
+ $rootScope . $digest ( ) ;
318
322
expect ( document . activeElement . tagName ) . toBe ( 'DIV' ) ;
319
323
expect ( $document ) . toHaveModalsOpen ( 1 ) ;
320
324
@@ -377,10 +381,11 @@ describe('$modal', function () {
377
381
expect ( modal . opened ) . toBeRejectedWith ( 'ko' ) ;
378
382
} ) ;
379
383
380
- it ( 'should focus on the element that has autofocus attribute when the modal is open/reopen' , function ( ) {
384
+ it ( 'should focus on the element that has autofocus attribute when the modal is open/reopen and the animations have finished ' , function ( ) {
381
385
function openAndCloseModalWithAutofocusElement ( ) {
382
386
var modal = open ( { template : '<div><input type="text" id="auto-focus-element" autofocus></div>' } ) ;
383
-
387
+ $animate . flush ( ) ;
388
+ $rootScope . $digest ( ) ;
384
389
expect ( angular . element ( '#auto-focus-element' ) ) . toHaveFocus ( ) ;
385
390
386
391
close ( modal , 'closed ok' ) ;
@@ -392,6 +397,45 @@ describe('$modal', function () {
392
397
openAndCloseModalWithAutofocusElement ( ) ;
393
398
} ) ;
394
399
400
+ it ( 'should wait until the in animation is finished before attempting to focus the modal or autofocus element' , function ( ) {
401
+ function openAndCloseModalWithAutofocusElement ( ) {
402
+ var modal = open ( { template : '<div><input type="text" id="auto-focus-element" autofocus></div>' } ) ;
403
+ expect ( angular . element ( '#auto-focus-element' ) ) . not . toHaveFocus ( ) ;
404
+
405
+ $animate . flush ( ) ;
406
+ $rootScope . $digest ( ) ;
407
+
408
+ expect ( angular . element ( '#auto-focus-element' ) ) . toHaveFocus ( ) ;
409
+
410
+ close ( modal , 'closed ok' ) ;
411
+
412
+ expect ( modal . result ) . toBeResolvedWith ( 'closed ok' ) ;
413
+ }
414
+
415
+ function openAndCloseModalWithOutAutofocusElement ( ) {
416
+ var link = '<a href>Link</a>' ;
417
+ var element = angular . element ( link ) ;
418
+ angular . element ( document . body ) . append ( element ) ;
419
+ element . focus ( ) ;
420
+ expect ( document . activeElement . tagName ) . toBe ( 'A' ) ;
421
+
422
+ var modal = open ( { template : '<div><input type="text"></div>' } ) ;
423
+ expect ( document . activeElement . tagName ) . toBe ( 'A' ) ;
424
+
425
+ $animate . flush ( ) ;
426
+ $rootScope . $digest ( ) ;
427
+
428
+ expect ( document . activeElement . tagName ) . toBe ( 'DIV' ) ;
429
+
430
+ close ( modal , 'closed ok' ) ;
431
+
432
+ expect ( modal . result ) . toBeResolvedWith ( 'closed ok' ) ;
433
+ }
434
+
435
+ openAndCloseModalWithAutofocusElement ( ) ;
436
+ openAndCloseModalWithOutAutofocusElement ( ) ;
437
+ } ) ;
438
+
395
439
it ( 'should change focus to first element when tab key was pressed' , function ( ) {
396
440
var initialPage = angular . element ( '<a href="#" id="cannot-get-focus-from-modal">Outland link</a>' ) ;
397
441
angular . element ( document . body ) . append ( initialPage ) ;
@@ -918,11 +962,15 @@ describe('$modal', function () {
918
962
expect ( document . activeElement . tagName ) . toBe ( 'A' ) ;
919
963
920
964
var modal1 = open ( { template : '<div>Modal1<button id="focus">inside modal1</button></div>' } ) ;
965
+ $animate . flush ( ) ;
966
+ $rootScope . $digest ( ) ;
921
967
document . getElementById ( 'focus' ) . focus ( ) ;
922
968
expect ( document . activeElement . tagName ) . toBe ( 'BUTTON' ) ;
923
969
expect ( $document ) . toHaveModalsOpen ( 1 ) ;
924
970
925
971
var modal2 = open ( { template : '<div>Modal2</div>' } ) ;
972
+ $animate . flush ( ) ;
973
+ $rootScope . $digest ( ) ;
926
974
expect ( document . activeElement . tagName ) . toBe ( 'DIV' ) ;
927
975
expect ( $document ) . toHaveModalsOpen ( 2 ) ;
928
976
0 commit comments