|
1 | 1 | /* eslint-disable react/no-render-return-value, max-classes-per-file, func-names, no-console */
|
2 |
| -import React, { cloneElement } from 'react'; |
| 2 | +import React, { cloneElement, useEffect } from 'react'; |
3 | 3 | import { act } from 'react-dom/test-utils';
|
4 | 4 | import { render } from '@testing-library/react';
|
5 | 5 | import type { ReactWrapper } from 'enzyme';
|
@@ -362,6 +362,60 @@ describe('dialog', () => {
|
362 | 362 | expect(modalRender.find('.rc-dialog-content').props().style.background).toEqual('#1890ff');
|
363 | 363 | });
|
364 | 364 |
|
| 365 | + describe('focusTriggerAfterClose', () => { |
| 366 | + it('should focus trigger after close dialog', () => { |
| 367 | + const Demo = () => { |
| 368 | + const [visible, setVisible] = React.useState(false); |
| 369 | + return ( |
| 370 | + <> |
| 371 | + <button onClick={() => setVisible(true)}>trigger</button> |
| 372 | + <Dialog visible={visible} onClose={() => setVisible(false)}> |
| 373 | + content |
| 374 | + </Dialog> |
| 375 | + </> |
| 376 | + ); |
| 377 | + }; |
| 378 | + const wrapper = mount(<Demo />, { attachTo: document.body }); |
| 379 | + const trigger = wrapper.find('button').at(0); |
| 380 | + (trigger.getDOMNode() as any).focus(); |
| 381 | + trigger.simulate('click'); |
| 382 | + jest.runAllTimers(); |
| 383 | + const closeButton = wrapper.find('.rc-dialog-close'); |
| 384 | + closeButton.simulate('click'); |
| 385 | + jest.runAllTimers(); |
| 386 | + expect(document.activeElement).toBe(trigger.getDOMNode()); |
| 387 | + wrapper.unmount(); |
| 388 | + }); |
| 389 | + |
| 390 | + it('should focus trigger after close dialog when contains focusable element', () => { |
| 391 | + const Demo = () => { |
| 392 | + const [visible, setVisible] = React.useState(false); |
| 393 | + const inputRef = React.useRef(null); |
| 394 | + useEffect(() => { |
| 395 | + inputRef.current?.focus(); |
| 396 | + }, []); |
| 397 | + return ( |
| 398 | + <> |
| 399 | + <button onClick={() => setVisible(true)}>trigger</button> |
| 400 | + <Dialog visible={visible} onClose={() => setVisible(false)}> |
| 401 | + <input ref={inputRef} /> |
| 402 | + </Dialog> |
| 403 | + </> |
| 404 | + ); |
| 405 | + }; |
| 406 | + const wrapper = mount(<Demo />, { attachTo: document.body }); |
| 407 | + const trigger = wrapper.find('button').at(0); |
| 408 | + (trigger.getDOMNode() as any).focus(); |
| 409 | + trigger.simulate('click'); |
| 410 | + jest.runAllTimers(); |
| 411 | + const closeButton = wrapper.find('.rc-dialog-close'); |
| 412 | + closeButton.simulate('click'); |
| 413 | + jest.runAllTimers(); |
| 414 | + expect(document.activeElement).toBe(trigger.getDOMNode()); |
| 415 | + wrapper.unmount(); |
| 416 | + }); |
| 417 | + }); |
| 418 | + |
365 | 419 | describe('size should work', () => {
|
366 | 420 | it('width', () => {
|
367 | 421 | const wrapper = mount(<Dialog visible width={1128} />);
|
|
0 commit comments