import CSSMotion from 'rc-motion'; import { genCSSMotion } from 'rc-motion/lib/CSSMotion'; import KeyCode from 'rc-util/lib/KeyCode'; import React from 'react'; import { act } from 'react-dom/test-utils'; import Modal from '..'; import { fireEvent, render, waitFakeTimer } from '../../../tests/utils'; import Button from '../../button'; import ConfigProvider from '../../config-provider'; import Input from '../../input'; import type { ModalFunc } from '../confirm'; jest.mock('rc-util/lib/Portal'); jest.mock('rc-motion'); describe('Modal.hook', () => { // Inject CSSMotion to replace with No transition support const MockCSSMotion = genCSSMotion(false); Object.keys(MockCSSMotion).forEach((key) => { // @ts-ignore CSSMotion[key] = MockCSSMotion[key]; }); it('hooks support context', () => { jest.useFakeTimers(); const Context = React.createContext('light'); let instance: ReturnType; const Demo = () => { const [modal, contextHolder] = Modal.useModal(); return ( , }); }, []); return {contextHolder}; }; render(); expect(document.body.querySelector('.bamboo')?.textContent).toEqual('好的'); }); it('it should call forwarded afterClose', () => { const afterClose = jest.fn(); const Demo = () => { const [modal, contextHolder] = Modal.useModal(); React.useEffect(() => { modal.confirm({ title: 'Confirm', afterClose }); }, []); return contextHolder; }; render(); const btns = document.body.querySelectorAll('.ant-btn'); fireEvent.click(btns[btns.length - 1]); expect(afterClose).toHaveBeenCalledTimes(1); }); });