|
1 | 1 | /* eslint-disable no-undef */ |
2 | 2 | import { act, fireEvent, render } from '@testing-library/react'; |
3 | | -import { resetWarned } from '@rc-component/util'; |
| 3 | +import { KeyCode, resetWarned } from '@rc-component/util'; |
4 | 4 | import React from 'react'; |
5 | 5 | import Menu, { MenuItem, SubMenu } from '../src'; |
6 | 6 | import { isActive, last } from './util'; |
@@ -203,6 +203,28 @@ describe('SubMenu', () => { |
203 | 203 | }); |
204 | 204 | expect(container.querySelector('.rc-menu-submenu-open')).toBeFalsy(); |
205 | 205 | }); |
| 206 | + |
| 207 | + it('closes open submenus when Escape is pressed on the root menu', () => { |
| 208 | + const onOpenChange = jest.fn(); |
| 209 | + const { container } = render( |
| 210 | + createMenu({ |
| 211 | + triggerSubMenuAction: 'click', |
| 212 | + onOpenChange, |
| 213 | + }), |
| 214 | + ); |
| 215 | + |
| 216 | + fireEvent.click(container.querySelector('.rc-menu-submenu-title')); |
| 217 | + runAllTimer(); |
| 218 | + expect(container.querySelector('.rc-menu-submenu-open')).toBeTruthy(); |
| 219 | + |
| 220 | + const rootMenu = container.querySelector<HTMLElement>('.rc-menu-root'); |
| 221 | + rootMenu.focus(); |
| 222 | + fireEvent.keyDown(rootMenu, { keyCode: KeyCode.ESC, which: KeyCode.ESC }); |
| 223 | + runAllTimer(); |
| 224 | + |
| 225 | + expect(onOpenChange).toHaveBeenLastCalledWith([]); |
| 226 | + expect(container.querySelector('.rc-menu-submenu-open')).toBeFalsy(); |
| 227 | + }); |
206 | 228 | }); |
207 | 229 |
|
208 | 230 | it('fires openChange event', () => { |
|
0 commit comments