@@ -250,6 +250,28 @@ describe('$modal', function () {
250
250
expect ( $document ) . toHaveModalsOpen ( 0 ) ;
251
251
} ) ;
252
252
253
+ it ( 'should return to the element which had focus before the dialog is invoked' , function ( ) {
254
+ var link = '<a href>Link</a>' ;
255
+ var element = angular . element ( link ) ;
256
+ angular . element ( document . body ) . append ( element ) ;
257
+ element . focus ( ) ;
258
+ expect ( document . activeElement . tagName ) . toBe ( 'A' ) ;
259
+
260
+ var modal = open ( { template : '<div>Content<button>inside modal</button></div>' } ) ;
261
+ $timeout . flush ( ) ;
262
+ expect ( document . activeElement . tagName ) . toBe ( 'DIV' ) ;
263
+ expect ( $document ) . toHaveModalsOpen ( 1 ) ;
264
+
265
+ triggerKeyDown ( $document , 27 ) ;
266
+ $timeout . flush ( ) ;
267
+ $rootScope . $digest ( ) ;
268
+
269
+ expect ( document . activeElement . tagName ) . toBe ( 'A' ) ;
270
+ expect ( $document ) . toHaveModalsOpen ( 0 ) ;
271
+
272
+ element . remove ( ) ;
273
+ } ) ;
274
+
253
275
it ( 'should resolve returned promise on close' , function ( ) {
254
276
var modal = open ( { template : '<div>Content</div>' } ) ;
255
277
close ( modal , 'closed ok' ) ;
@@ -695,6 +717,35 @@ describe('$modal', function () {
695
717
dismiss ( modal2 ) ;
696
718
expect ( body ) . not . toHaveClass ( 'modal-open' ) ;
697
719
} ) ;
720
+
721
+ it ( 'should return to the element which had focus before the dialog is invoked' , function ( ) {
722
+ var link = '<a href>Link</a>' ;
723
+ var element = angular . element ( link ) ;
724
+ angular . element ( document . body ) . append ( element ) ;
725
+ element . focus ( ) ;
726
+ expect ( document . activeElement . tagName ) . toBe ( 'A' ) ;
727
+
728
+ var modal1 = open ( { template : '<div>Modal1<button id="focus">inside modal1</button></div>' } ) ;
729
+ $timeout . flush ( ) ;
730
+ document . getElementById ( 'focus' ) . focus ( ) ;
731
+ expect ( document . activeElement . tagName ) . toBe ( 'BUTTON' ) ;
732
+ expect ( $document ) . toHaveModalsOpen ( 1 ) ;
733
+
734
+ var modal2 = open ( { template : '<div>Modal2</div>' } ) ;
735
+ $timeout . flush ( ) ;
736
+ expect ( document . activeElement . tagName ) . toBe ( 'DIV' ) ;
737
+ expect ( $document ) . toHaveModalsOpen ( 2 ) ;
738
+
739
+ dismiss ( modal2 ) ;
740
+ expect ( document . activeElement . tagName ) . toBe ( 'BUTTON' ) ;
741
+ expect ( $document ) . toHaveModalsOpen ( 1 ) ;
742
+
743
+ dismiss ( modal1 ) ;
744
+ expect ( document . activeElement . tagName ) . toBe ( 'A' ) ;
745
+ expect ( $document ) . toHaveModalsOpen ( 0 ) ;
746
+
747
+ element . remove ( ) ;
748
+ } ) ;
698
749
} ) ;
699
750
700
751
describe ( 'modal.closing event' , function ( ) {
0 commit comments