|
1 | 1 | import React from 'react'; |
2 | 2 | import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import { |
| 4 | + Dropdown, |
| 5 | + type DropdownTriggerProps, |
| 6 | + useDropdownContext, |
| 7 | +} from '../../Form/Dropdown'; |
3 | 8 | import { useDialogOnNearestManager } from '../hooks'; |
4 | 9 | import { DialogAnchor } from '../service'; |
5 | 10 | import { DialogManagerProvider } from '../../../context'; |
@@ -29,6 +34,32 @@ const DialogFixture = ({ |
29 | 34 | ); |
30 | 35 | }; |
31 | 36 |
|
| 37 | +const DropdownTriggerButton = ({ |
| 38 | + children, |
| 39 | + onClick, |
| 40 | + referenceRef, |
| 41 | + ...props |
| 42 | +}: DropdownTriggerProps) => ( |
| 43 | + <button |
| 44 | + {...props} |
| 45 | + onClick={onClick} |
| 46 | + ref={referenceRef as React.Ref<HTMLButtonElement>} |
| 47 | + type='button' |
| 48 | + > |
| 49 | + {children} |
| 50 | + </button> |
| 51 | +); |
| 52 | + |
| 53 | +const DropdownItem = ({ label }: { label: string }) => { |
| 54 | + const { close } = useDropdownContext(); |
| 55 | + |
| 56 | + return ( |
| 57 | + <button onClick={close} role='menuitem' type='button'> |
| 58 | + {label} |
| 59 | + </button> |
| 60 | + ); |
| 61 | +}; |
| 62 | + |
32 | 63 | describe('DialogPortal', () => { |
33 | 64 | it('does not close dialogs from another manager when clicking in a different manager overlay', async () => { |
34 | 65 | render( |
@@ -118,4 +149,47 @@ describe('DialogPortal', () => { |
118 | 149 | const results = await axe(document.body); |
119 | 150 | expect(results).toHaveNoViolations(); |
120 | 151 | }); |
| 152 | + |
| 153 | + it('does not close the dialog when Escape is handled by a nested dropdown', async () => { |
| 154 | + render( |
| 155 | + <DialogManagerProvider> |
| 156 | + <DialogFixture |
| 157 | + dialogId='dialog-with-dropdown' |
| 158 | + testId='dialog-dropdown-content' |
| 159 | + trapFocus |
| 160 | + > |
| 161 | + <Dropdown |
| 162 | + TriggerComponent={DropdownTriggerButton} |
| 163 | + triggerProps={{ children: 'Duration' }} |
| 164 | + > |
| 165 | + <DropdownItem label='15 minutes' /> |
| 166 | + <DropdownItem label='1 hour' /> |
| 167 | + </Dropdown> |
| 168 | + </DialogFixture> |
| 169 | + </DialogManagerProvider>, |
| 170 | + ); |
| 171 | + |
| 172 | + fireEvent.click(screen.getByTestId('open-dialog-with-dropdown')); |
| 173 | + expect(screen.getByRole('dialog')).toBeInTheDocument(); |
| 174 | + |
| 175 | + fireEvent.click(screen.getByRole('button', { name: 'Duration' })); |
| 176 | + await screen.findByRole('menu'); |
| 177 | + |
| 178 | + const item = screen.getByRole('menuitem', { name: '15 minutes' }); |
| 179 | + item.focus(); |
| 180 | + |
| 181 | + fireEvent.keyDown(item, { key: 'Escape' }); |
| 182 | + fireEvent.keyUp(item, { key: 'Escape' }); |
| 183 | + |
| 184 | + await waitFor(() => { |
| 185 | + expect(screen.queryByRole('menu')).not.toBeInTheDocument(); |
| 186 | + }); |
| 187 | + expect(screen.getByRole('dialog')).toBeInTheDocument(); |
| 188 | + |
| 189 | + fireEvent.keyUp(document, { key: 'Escape' }); |
| 190 | + |
| 191 | + await waitFor(() => { |
| 192 | + expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); |
| 193 | + }); |
| 194 | + }); |
121 | 195 | }); |
0 commit comments