diff --git a/packages/react/src/__tests__/Dialog.test.tsx b/packages/react/src/__tests__/Dialog.test.tsx index dabb4de2985..df439ad91c8 100644 --- a/packages/react/src/__tests__/Dialog.test.tsx +++ b/packages/react/src/__tests__/Dialog.test.tsx @@ -1,11 +1,10 @@ import React, {useState, useRef} from 'react' import {Dialog, Box, Text, Button} from '..' -import {render as HTMLRender, fireEvent, waitFor} from '@testing-library/react' +import {render as HTMLRender, fireEvent} from '@testing-library/react' import axe from 'axe-core' -import userEvent from '@testing-library/user-event' import {behavesAsComponent, checkExports} from '../utils/testing' -/* Dialog Version 1 tests */ +/* Dialog Version 2 */ const comp = ( null} aria-labelledby="header"> @@ -71,6 +70,36 @@ const DialogWithCustomFocusRef = () => { ) } +const DialogWithCustomFocusRefAndReturnFocusRef = () => { + const [isOpen, setIsOpen] = useState(true) + const returnFocusRef = useRef(null) + const buttonRef = useRef(null) + return ( +
+ + setIsOpen(false)} + aria-labelledby="header" + > +
+ Title + + Some content + + +
+
+
+ ) +} + describe('Dialog', () => { // because Dialog returns a React fragment the as and sx tests fail always, so they are skipped behavesAsComponent({ @@ -140,24 +169,14 @@ describe('Dialog', () => { }) it('Returns focus to returnFocusRef on escape', async () => { - const {getByTestId, queryByTestId, getByText} = HTMLRender() + const {getByTestId, queryByTestId} = HTMLRender() + const innerButton = getByTestId('inner-button') + expect(document.activeElement).toEqual(innerButton) expect(getByTestId('inner')).toBeTruthy() - const tooltipEl = getByText('Close') - // first ESC closes the tooltip - const user = userEvent.setup() - await user.keyboard('{Escape}') - await waitFor(() => { - // Wait for the tooltip to close - expect(tooltipEl).not.toHaveAttribute(':popover-open') - }) - - // second ESC closes the dialog and returns focus - await user.keyboard('{Escape}') - await waitFor(() => { - // // Wait for the tooltip to close - expect(queryByTestId('inner')).toBeNull() - }) + fireEvent.keyDown(document.body, {key: 'Escape'}) + + expect(queryByTestId('inner')).toBeNull() const triggerButton = getByTestId('trigger-button') expect(document.activeElement).toEqual(triggerButton) })