|
| 1 | +import { cleanup, render, act } from '@testing-library/react'; |
| 2 | +import React from 'react'; |
| 3 | +import ReactDOM from 'react-dom'; |
| 4 | +import Drawer from '../src'; |
| 5 | + |
| 6 | +// Mock useLockFocus to track calls |
| 7 | +jest.mock('@rc-component/util/lib/Dom/focus', () => { |
| 8 | + const actual = jest.requireActual('@rc-component/util/lib/Dom/focus'); |
| 9 | + |
| 10 | + const useLockFocus = (visible: boolean, ...rest: any[]) => { |
| 11 | + (globalThis as any).__useLockFocusVisible = visible; |
| 12 | + const hooks = actual.useLockFocus(visible, ...rest); |
| 13 | + const hooksArray = Array.isArray(hooks) ? hooks : [hooks]; |
| 14 | + const proxyIgnoreElement = (ele: HTMLElement) => { |
| 15 | + (globalThis as any).__ignoredElement = ele; |
| 16 | + hooksArray[0](ele); |
| 17 | + }; |
| 18 | + return [proxyIgnoreElement, ...hooksArray.slice(1)] as ReturnType< |
| 19 | + typeof actual.useLockFocus |
| 20 | + >; |
| 21 | + }; |
| 22 | + |
| 23 | + return { |
| 24 | + ...actual, |
| 25 | + useLockFocus, |
| 26 | + }; |
| 27 | +}); |
| 28 | + |
| 29 | +describe('Drawer.Focus', () => { |
| 30 | + beforeEach(() => { |
| 31 | + jest.useFakeTimers(); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + jest.useRealTimers(); |
| 36 | + cleanup(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should call ignoreElement when input in portal is focused', () => { |
| 40 | + render( |
| 41 | + <Drawer open> |
| 42 | + <input type="text" id="drawer-input" /> |
| 43 | + {ReactDOM.createPortal( |
| 44 | + <input type="text" id="portal-input" />, |
| 45 | + document.body, |
| 46 | + )} |
| 47 | + </Drawer>, |
| 48 | + ); |
| 49 | + |
| 50 | + act(() => { |
| 51 | + jest.runAllTimers(); |
| 52 | + }); |
| 53 | + |
| 54 | + const input = document.getElementById('portal-input') as HTMLElement; |
| 55 | + act(() => { |
| 56 | + input.focus(); |
| 57 | + }); |
| 58 | + |
| 59 | + expect((globalThis as any).__ignoredElement).toBe(input); |
| 60 | + }); |
| 61 | +}); |
0 commit comments