|
1 | 1 | import React, {useState, useRef} from 'react' |
2 | | -import {Dialog, Box, Text} from '..' |
3 | | -import {Button} from '../deprecated' |
| 2 | +import {Dialog, Box, Text, Button} from '..' |
4 | 3 | import {render as HTMLRender, fireEvent} from '@testing-library/react' |
5 | 4 | import axe from 'axe-core' |
6 | 5 | import {behavesAsComponent, checkExports} from '../utils/testing' |
@@ -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,7 +169,9 @@ describe('Dialog', () => { |
140 | 169 | }) |
141 | 170 |
|
142 | 171 | it('Returns focus to returnFocusRef on escape', async () => { |
143 | | - const {getByTestId, queryByTestId} = 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 | 177 | fireEvent.keyDown(document.body, {key: 'Escape'}) |
|
0 commit comments