Skip to content

Commit fd6bd57

Browse files
update tests
1 parent a659cc9 commit fd6bd57

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

packages/react/src/__tests__/Dialog.test.tsx

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import React, {useState, useRef} from 'react'
22
import {Dialog, Box, Text, Button} from '..'
3-
import {render as HTMLRender, fireEvent, waitFor} from '@testing-library/react'
3+
import {render as HTMLRender, fireEvent} from '@testing-library/react'
44
import axe from 'axe-core'
5-
import userEvent from '@testing-library/user-event'
65
import {behavesAsComponent, checkExports} from '../utils/testing'
76

8-
/* Dialog Version 1 tests */
7+
/* Dialog Version 2 */
98

109
const comp = (
1110
<Dialog isOpen onDismiss={() => null} aria-labelledby="header">
@@ -71,6 +70,36 @@ const DialogWithCustomFocusRef = () => {
7170
)
7271
}
7372

73+
const DialogWithCustomFocusRefAndReturnFocusRef = () => {
74+
const [isOpen, setIsOpen] = useState(true)
75+
const returnFocusRef = useRef(null)
76+
const buttonRef = useRef(null)
77+
return (
78+
<div>
79+
<Button data-testid="trigger-button" ref={returnFocusRef} onClick={() => setIsOpen(true)}>
80+
Show Dialog
81+
</Button>
82+
<Dialog
83+
returnFocusRef={returnFocusRef}
84+
isOpen={isOpen}
85+
initialFocusRef={buttonRef}
86+
onDismiss={() => setIsOpen(false)}
87+
aria-labelledby="header"
88+
>
89+
<div data-testid="inner">
90+
<Dialog.Header id="header">Title</Dialog.Header>
91+
<Box p={3}>
92+
<Text fontFamily="sans-serif">Some content</Text>
93+
<button data-testid="inner-button" ref={buttonRef}>
94+
hi
95+
</button>
96+
</Box>
97+
</div>
98+
</Dialog>
99+
</div>
100+
)
101+
}
102+
74103
describe('Dialog', () => {
75104
// because Dialog returns a React fragment the as and sx tests fail always, so they are skipped
76105
behavesAsComponent({
@@ -140,24 +169,14 @@ describe('Dialog', () => {
140169
})
141170

142171
it('Returns focus to returnFocusRef on escape', async () => {
143-
const {getByTestId, queryByTestId, getByText} = HTMLRender(<Component />)
172+
const {getByTestId, queryByTestId} = HTMLRender(<DialogWithCustomFocusRefAndReturnFocusRef />)
173+
const innerButton = getByTestId('inner-button')
174+
expect(document.activeElement).toEqual(innerButton)
144175

145176
expect(getByTestId('inner')).toBeTruthy()
146-
const tooltipEl = getByText('Close')
147-
// first ESC closes the tooltip
148-
const user = userEvent.setup()
149-
await user.keyboard('{Escape}')
150-
await waitFor(() => {
151-
// Wait for the tooltip to close
152-
expect(tooltipEl).not.toHaveAttribute(':popover-open')
153-
})
154-
155-
// second ESC closes the dialog and returns focus
156-
await user.keyboard('{Escape}')
157-
await waitFor(() => {
158-
// // Wait for the tooltip to close
159-
expect(queryByTestId('inner')).toBeNull()
160-
})
177+
fireEvent.keyDown(document.body, {key: 'Escape'})
178+
179+
expect(queryByTestId('inner')).toBeNull()
161180
const triggerButton = getByTestId('trigger-button')
162181
expect(document.activeElement).toEqual(triggerButton)
163182
})

0 commit comments

Comments
 (0)