import React from 'react'; import CSSMotion from 'rc-motion'; import { genCSSMotion } from 'rc-motion/lib/CSSMotion'; import KeyCode from 'rc-util/lib/KeyCode'; 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 zhCN from '../../locale/zh_CN'; 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); }); it('should be applied correctly locale', async () => { jest.useFakeTimers(); const Demo: React.FC<{ count: number }> = ({ count }) => { React.useEffect(() => { const instance = Modal.confirm({}); return () => { instance.destroy(); }; }, [count]); let node = null; for (let i = 0; i < count; i += 1) { node = {node}; } return node; }; const { rerender } = render(
); for (let i = 10; i > 0; i -= 1) { rerender(); // eslint-disable-next-line no-await-in-loop await waitFakeTimer(); expect(document.body.querySelector('.ant-btn-primary')!.textContent).toEqual('确 定'); fireEvent.click(document.body.querySelector('.ant-btn-primary')!); // eslint-disable-next-line no-await-in-loop await waitFakeTimer(); } rerender(); await waitFakeTimer(); expect(document.body.querySelector('.ant-btn-primary')!.textContent).toEqual('OK'); jest.useRealTimers(); }); describe('support await', () => { it('click', async () => { jest.useFakeTimers(); let notReady = true; let lastResult: boolean | null = null; const Demo: React.FC = () => { const [modal, contextHolder] = Modal.useModal(); React.useEffect(() => { (async () => { lastResult = await modal.confirm({ content: , onOk: async () => { if (notReady) { notReady = false; return Promise.reject(); } }, }); })(); }, []); return contextHolder; }; render(); // Wait for modal show await waitFakeTimer(); // First time click should not close fireEvent.click(document.querySelector('.ant-btn-primary')!); await waitFakeTimer(); expect(lastResult).toBeFalsy(); // Second time click to close fireEvent.click(document.querySelector('.ant-btn-primary')!); await waitFakeTimer(); expect(lastResult).toBeTruthy(); jest.useRealTimers(); }); }); it('esc', async () => { jest.useFakeTimers(); let lastResult: boolean | null = null; const Demo: React.FC = () => { const [modal, contextHolder] = Modal.useModal(); React.useEffect(() => { (async () => { lastResult = await modal.confirm({ content: , }); })(); }, []); return contextHolder; }; render(); // Wait for modal show await waitFakeTimer(); // ESC to close fireEvent.keyDown(document.querySelector('.ant-modal')!, { key: 'Esc', keyCode: KeyCode.ESC, }); await waitFakeTimer(); expect(lastResult).toBe(false); }); });