@@ -15,6 +15,7 @@ import {ActionButton, Button} from '@react-spectrum/button';
15
15
import { ButtonGroup } from '@react-spectrum/buttongroup' ;
16
16
import { Content } from '@react-spectrum/view' ;
17
17
import { Dialog , DialogTrigger } from '../' ;
18
+ import { Heading } from '@react-spectrum/text' ;
18
19
import { Item , Menu , MenuTrigger } from '@react-spectrum/menu' ;
19
20
import MatchMediaMock from 'jest-matchmedia-mock' ;
20
21
import { Provider } from '@react-spectrum/provider' ;
@@ -28,9 +29,11 @@ import userEvent from '@testing-library/user-event';
28
29
describe ( 'DialogTrigger' , function ( ) {
29
30
let matchMedia ;
30
31
beforeAll ( ( ) => {
32
+ jest . spyOn ( window . screen , 'width' , 'get' ) . mockImplementation ( ( ) => 1024 ) ;
31
33
jest . useFakeTimers ( ) ;
32
34
} ) ;
33
35
afterAll ( ( ) => {
36
+ jest . clearAllMocks ( ) ;
34
37
jest . useRealTimers ( ) ;
35
38
} ) ;
36
39
@@ -927,4 +930,54 @@ describe('DialogTrigger', function () {
927
930
928
931
expect ( document . activeElement ) . toBe ( innerInput ) ;
929
932
} ) ;
933
+
934
+ it ( 'will not lose focus to body' , async ( ) => {
935
+ let { getByRole, getByTestId} = render (
936
+ < Provider theme = { theme } >
937
+ < DialogTrigger type = "popover" >
938
+ < ActionButton > Trigger</ ActionButton >
939
+ < Dialog >
940
+ < Heading > The Heading</ Heading >
941
+ < Content >
942
+ < MenuTrigger >
943
+ < ActionButton data-testid = "innerButton" > Test</ ActionButton >
944
+ < Menu autoFocus = "first" >
945
+ < Item > Item 1</ Item >
946
+ < Item > Item 2</ Item >
947
+ < Item > Item 3</ Item >
948
+ </ Menu >
949
+ </ MenuTrigger >
950
+ </ Content >
951
+ </ Dialog >
952
+ </ DialogTrigger >
953
+ </ Provider >
954
+ ) ;
955
+ let button = getByRole ( 'button' ) ;
956
+ triggerPress ( button ) ;
957
+
958
+ act ( ( ) => {
959
+ jest . runAllTimers ( ) ;
960
+ } ) ;
961
+
962
+ let outerDialog = getByRole ( 'dialog' ) ;
963
+
964
+ await waitFor ( ( ) => {
965
+ expect ( outerDialog ) . toBeVisible ( ) ;
966
+ } ) ; // wait for animation
967
+ let innerButton = getByTestId ( 'innerButton' ) ;
968
+ userEvent . tab ( ) ;
969
+ fireEvent . keyDown ( document . activeElement , { key : 'Enter' } ) ;
970
+ fireEvent . keyUp ( document . activeElement , { key : 'Enter' } ) ;
971
+
972
+ act ( ( ) => {
973
+ jest . runAllTimers ( ) ;
974
+ } ) ;
975
+ userEvent . tab ( ) ;
976
+ act ( ( ) => {
977
+ jest . runAllTimers ( ) ;
978
+ } ) ;
979
+
980
+ expect ( document . activeElement ) . toBe ( innerButton ) ;
981
+ } ) ;
982
+
930
983
} ) ;
0 commit comments