|
1 | 1 | import React, {useState, useRef} from 'react'
|
2 | 2 | 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' |
4 | 4 | import axe from 'axe-core'
|
5 |
| -import userEvent from '@testing-library/user-event' |
6 | 5 | import {behavesAsComponent, checkExports} from '../utils/testing'
|
7 | 6 |
|
8 |
| -/* Dialog Version 1 tests */ |
| 7 | +/* Dialog Version 2 */ |
9 | 8 |
|
10 | 9 | const comp = (
|
11 | 10 | <Dialog isOpen onDismiss={() => null} aria-labelledby="header">
|
@@ -71,6 +70,36 @@ const DialogWithCustomFocusRef = () => {
|
71 | 70 | )
|
72 | 71 | }
|
73 | 72 |
|
| 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 | + |
74 | 103 | describe('Dialog', () => {
|
75 | 104 | // because Dialog returns a React fragment the as and sx tests fail always, so they are skipped
|
76 | 105 | behavesAsComponent({
|
@@ -140,24 +169,14 @@ describe('Dialog', () => {
|
140 | 169 | })
|
141 | 170 |
|
142 | 171 | 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) |
144 | 175 |
|
145 | 176 | 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() |
161 | 180 | const triggerButton = getByTestId('trigger-button')
|
162 | 181 | expect(document.activeElement).toEqual(triggerButton)
|
163 | 182 | })
|
|
0 commit comments