2021-06-09 15:36:59 +08:00
|
|
|
import { SmileOutlined } from '@ant-design/icons';
|
2022-06-22 14:57:09 +08:00
|
|
|
import CSSMotion from 'rc-motion';
|
2020-09-29 19:33:37 +08:00
|
|
|
import { genCSSMotion } from 'rc-motion/lib/CSSMotion';
|
|
|
|
import KeyCode from 'rc-util/lib/KeyCode';
|
2021-03-02 11:53:12 +08:00
|
|
|
import { resetWarned } from 'rc-util/lib/warning';
|
2022-06-22 14:57:09 +08:00
|
|
|
import * as React from 'react';
|
2022-08-08 13:31:55 +08:00
|
|
|
import TestUtils from 'react-dom/test-utils';
|
2022-09-05 19:41:32 +08:00
|
|
|
import type { ModalFuncProps } from '..';
|
2017-03-17 11:47:36 +08:00
|
|
|
import Modal from '..';
|
2022-08-08 13:31:55 +08:00
|
|
|
import { sleep, act } from '../../../tests/utils';
|
2021-02-09 21:49:15 +08:00
|
|
|
import ConfigProvider from '../../config-provider';
|
2022-09-05 19:41:32 +08:00
|
|
|
import type { ModalFunc } from '../confirm';
|
2022-06-22 14:57:09 +08:00
|
|
|
import destroyFns from '../destroyFns';
|
2017-03-17 11:47:36 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2017-10-09 13:23:20 +08:00
|
|
|
const { confirm } = Modal;
|
2017-03-17 11:47:36 +08:00
|
|
|
|
2020-09-29 19:33:37 +08:00
|
|
|
jest.mock('rc-motion');
|
|
|
|
|
2017-03-17 11:47:36 +08:00
|
|
|
describe('Modal.confirm triggers callbacks correctly', () => {
|
2020-09-29 19:33:37 +08:00
|
|
|
// Inject CSSMotion to replace with No transition support
|
|
|
|
const MockCSSMotion = genCSSMotion(false);
|
|
|
|
Object.keys(MockCSSMotion).forEach(key => {
|
2022-09-05 19:41:32 +08:00
|
|
|
(CSSMotion as any)[key] = (MockCSSMotion as any)[key];
|
2020-09-29 19:33:37 +08:00
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
// // Mock for rc-util raf
|
|
|
|
// window.requestAnimationFrame = callback => {
|
|
|
|
// const ret = window.setTimeout(callback, 16);
|
|
|
|
// return ret;
|
|
|
|
// };
|
|
|
|
// window.cancelAnimationFrame = id => {
|
|
|
|
// window.clearTimeout(id);
|
|
|
|
// };
|
|
|
|
|
|
|
|
// jest.spyOn(window, 'requestAnimationFrame').mockImplementation(callback => {
|
|
|
|
// const id = window.setTimeout(callback);
|
|
|
|
// console.log('Mock Raf:', id);
|
|
|
|
// return id;
|
|
|
|
// });
|
|
|
|
// jest.spyOn(window, 'cancelAnimationFrame').mockImplementation(id => window.clearTimeout(id));
|
2020-09-29 19:33:37 +08:00
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
const errorSpy = jest.spyOn(console, 'error');
|
2017-03-17 18:56:30 +08:00
|
|
|
|
2020-09-29 19:33:37 +08:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
// Hack error to remove act warning
|
|
|
|
const originError = console.error;
|
|
|
|
console.error = (...args) => {
|
|
|
|
const errorStr = String(args[0]);
|
|
|
|
if (errorStr.includes('was not wrapped in act(...)')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
originError(...args);
|
|
|
|
};
|
|
|
|
/* eslint-enable */
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
afterEach(async () => {
|
|
|
|
jest.clearAllTimers();
|
2017-03-17 18:56:30 +08:00
|
|
|
errorSpy.mockReset();
|
2020-04-18 01:09:14 +08:00
|
|
|
Modal.destroyAll();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await sleep();
|
|
|
|
document.body.innerHTML = '';
|
2017-03-17 18:56:30 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
function $$(className: string) {
|
|
|
|
return document.body.querySelectorAll<HTMLElement>(className);
|
2017-03-17 11:47:36 +08:00
|
|
|
}
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
function open(args?: ModalFuncProps) {
|
2020-05-03 00:53:32 +08:00
|
|
|
jest.useFakeTimers();
|
2017-03-17 11:47:36 +08:00
|
|
|
confirm({
|
|
|
|
title: 'Want to delete these items?',
|
|
|
|
content: 'some descriptions',
|
|
|
|
...args,
|
|
|
|
});
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2020-05-03 00:53:32 +08:00
|
|
|
jest.useRealTimers();
|
2017-03-17 11:47:36 +08:00
|
|
|
}
|
|
|
|
|
2019-10-26 10:31:58 +08:00
|
|
|
it('should not render title when title not defined', () => {
|
2020-05-03 00:53:32 +08:00
|
|
|
jest.useFakeTimers();
|
2019-10-26 10:31:58 +08:00
|
|
|
confirm({
|
|
|
|
content: 'some descriptions',
|
|
|
|
});
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2019-10-26 10:31:58 +08:00
|
|
|
expect(document.querySelector('.ant-modal-confirm-title')).toBe(null);
|
2020-05-03 00:53:32 +08:00
|
|
|
jest.useRealTimers();
|
2019-10-26 10:31:58 +08:00
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('trigger onCancel once when click on cancel button', async () => {
|
2017-03-17 11:47:36 +08:00
|
|
|
const onCancel = jest.fn();
|
|
|
|
const onOk = jest.fn();
|
|
|
|
open({
|
|
|
|
onCancel,
|
|
|
|
onOk,
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2017-03-17 11:47:36 +08:00
|
|
|
// first Modal
|
2022-04-06 11:07:15 +08:00
|
|
|
await sleep();
|
2017-03-17 11:47:36 +08:00
|
|
|
$$('.ant-btn')[0].click();
|
|
|
|
expect(onCancel.mock.calls.length).toBe(1);
|
|
|
|
expect(onOk.mock.calls.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('trigger onOk once when click on ok button', async () => {
|
2017-03-17 11:47:36 +08:00
|
|
|
const onCancel = jest.fn();
|
|
|
|
const onOk = jest.fn();
|
|
|
|
open({
|
|
|
|
onCancel,
|
|
|
|
onOk,
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2017-03-17 11:47:36 +08:00
|
|
|
// second Modal
|
2022-04-06 11:07:15 +08:00
|
|
|
await sleep();
|
2017-11-11 00:07:03 +08:00
|
|
|
$$('.ant-btn-primary')[0].click();
|
2017-03-17 11:47:36 +08:00
|
|
|
expect(onCancel.mock.calls.length).toBe(0);
|
|
|
|
expect(onOk.mock.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('should allow Modal.confirm without onCancel been set', async () => {
|
2017-03-17 11:47:36 +08:00
|
|
|
open();
|
2022-04-06 11:07:15 +08:00
|
|
|
await sleep();
|
|
|
|
|
2017-03-17 11:47:36 +08:00
|
|
|
// Third Modal
|
2017-11-11 00:07:03 +08:00
|
|
|
$$('.ant-btn')[0].click();
|
2017-03-17 18:56:30 +08:00
|
|
|
expect(errorSpy).not.toHaveBeenCalled();
|
2017-03-17 11:47:36 +08:00
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('should allow Modal.confirm without onOk been set', async () => {
|
2017-03-17 11:47:36 +08:00
|
|
|
open();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2017-03-17 11:47:36 +08:00
|
|
|
// Fourth Modal
|
2022-04-06 11:07:15 +08:00
|
|
|
await sleep();
|
2017-11-11 00:07:03 +08:00
|
|
|
$$('.ant-btn-primary')[0].click();
|
2017-03-17 18:56:30 +08:00
|
|
|
expect(errorSpy).not.toHaveBeenCalled();
|
2017-03-17 11:47:36 +08:00
|
|
|
});
|
2017-12-02 22:10:27 +08:00
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('should close confirm modal when press ESC', async () => {
|
2020-09-29 19:33:37 +08:00
|
|
|
jest.useFakeTimers();
|
2022-04-06 11:07:15 +08:00
|
|
|
jest.clearAllTimers();
|
2020-09-29 19:33:37 +08:00
|
|
|
const onCancel = jest.fn();
|
|
|
|
Modal.confirm({
|
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
|
|
|
onCancel,
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
await sleep();
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2020-09-29 19:33:37 +08:00
|
|
|
expect($$(`.ant-modal-confirm-confirm`)).toHaveLength(1);
|
|
|
|
TestUtils.Simulate.keyDown($$('.ant-modal')[0], {
|
|
|
|
keyCode: KeyCode.ESC,
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
await sleep(0);
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2020-09-29 19:33:37 +08:00
|
|
|
expect($$(`.ant-modal-confirm-confirm`)).toHaveLength(0);
|
|
|
|
expect(onCancel).toHaveBeenCalledTimes(1);
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('should not hide confirm when onOk return Promise.resolve', async () => {
|
2020-04-18 01:09:14 +08:00
|
|
|
open({
|
|
|
|
onOk: () => Promise.resolve(''),
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await sleep();
|
2020-04-18 01:09:14 +08:00
|
|
|
$$('.ant-btn-primary')[0].click();
|
|
|
|
expect($$('.ant-modal-confirm')).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
2020-09-29 19:33:37 +08:00
|
|
|
it('should emit error when onOk return Promise.reject', async () => {
|
2019-05-09 12:24:59 +08:00
|
|
|
const error = new Error('something wrong');
|
|
|
|
open({
|
2020-12-09 17:12:32 +08:00
|
|
|
onOk: () => Promise.reject(error),
|
2019-05-09 12:24:59 +08:00
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await sleep();
|
2019-05-09 12:24:59 +08:00
|
|
|
$$('.ant-btn-primary')[0].click();
|
2020-09-29 19:33:37 +08:00
|
|
|
|
2019-05-09 12:24:59 +08:00
|
|
|
// wait promise
|
2020-09-29 19:33:37 +08:00
|
|
|
await sleep();
|
|
|
|
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(error);
|
2019-05-09 12:24:59 +08:00
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('shows animation when close', async () => {
|
2019-07-03 20:14:39 +08:00
|
|
|
open();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2020-05-03 00:53:32 +08:00
|
|
|
jest.useFakeTimers();
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
await sleep();
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
expect($$('.ant-modal-confirm')).toHaveLength(1);
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await sleep();
|
2019-07-19 11:54:08 +08:00
|
|
|
$$('.ant-btn')[0].click();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
await sleep();
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
expect($$('.ant-modal-confirm')).toHaveLength(0);
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2017-12-02 22:10:27 +08:00
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('ok only', async () => {
|
2017-12-02 22:10:27 +08:00
|
|
|
open({ okCancel: false });
|
2022-04-06 11:07:15 +08:00
|
|
|
await sleep();
|
2017-12-02 22:10:27 +08:00
|
|
|
expect($$('.ant-btn')).toHaveLength(1);
|
|
|
|
expect($$('.ant-btn')[0].innerHTML).toContain('OK');
|
|
|
|
});
|
2018-05-25 20:59:17 +08:00
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('allows extra props on buttons', async () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
open({
|
|
|
|
okButtonProps: { disabled: true },
|
|
|
|
cancelButtonProps: { 'data-test': 'baz' } as ModalFuncProps['cancelButtonProps'],
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await sleep();
|
2018-09-30 14:50:03 +08:00
|
|
|
expect($$('.ant-btn')).toHaveLength(2);
|
2022-09-05 19:41:32 +08:00
|
|
|
expect(($$('.ant-btn')[0].attributes as any)['data-test'].value).toBe('baz');
|
|
|
|
expect(($$('.ant-btn')[1] as HTMLButtonElement).disabled).toBe(true);
|
2018-09-30 14:50:03 +08:00
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
describe('should close modals when click confirm button', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
(['info', 'success', 'warning', 'error'] as const).forEach(type => {
|
2022-04-06 11:07:15 +08:00
|
|
|
it(type, async () => {
|
|
|
|
jest.useFakeTimers();
|
2022-09-05 19:41:32 +08:00
|
|
|
Modal[type]?.({ title: 'title', content: 'content' });
|
2022-04-06 11:07:15 +08:00
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
|
|
|
|
$$('.ant-btn')[0].click();
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
|
|
|
jest.useRealTimers();
|
2018-05-25 20:59:17 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-08-25 21:58:40 +08:00
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('should close confirm modal when click cancel button', async () => {
|
2020-08-26 16:42:47 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
const onCancel = jest.fn();
|
|
|
|
Modal.confirm({
|
2022-10-14 20:01:50 +08:00
|
|
|
// test legacy visible
|
|
|
|
visible: true,
|
2020-08-26 16:42:47 +08:00
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
|
|
|
onCancel,
|
|
|
|
});
|
2020-10-14 16:35:29 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2020-08-26 16:42:47 +08:00
|
|
|
expect($$(`.ant-modal-confirm-confirm`)).toHaveLength(1);
|
|
|
|
$$('.ant-btn')[0].click();
|
2022-04-06 11:07:15 +08:00
|
|
|
await act(async () => {
|
2020-10-14 16:35:29 +08:00
|
|
|
jest.runAllTimers();
|
2022-04-06 11:07:15 +08:00
|
|
|
await sleep();
|
2020-10-14 16:35:29 +08:00
|
|
|
});
|
2020-08-26 16:42:47 +08:00
|
|
|
expect($$(`.ant-modal-confirm-confirm`)).toHaveLength(0);
|
|
|
|
expect(onCancel).toHaveBeenCalledTimes(1);
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('should close confirm modal when click close button', async () => {
|
2020-11-21 21:13:28 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
const onCancel = jest.fn();
|
|
|
|
Modal.confirm({
|
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
|
|
|
closable: true,
|
|
|
|
closeIcon: 'X',
|
|
|
|
onCancel,
|
|
|
|
});
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
expect($$(`.ant-modal-close`)).toHaveLength(1);
|
|
|
|
$$('.ant-btn')[0].click();
|
2022-04-06 11:07:15 +08:00
|
|
|
await act(async () => {
|
2020-11-21 21:13:28 +08:00
|
|
|
jest.runAllTimers();
|
2022-04-06 11:07:15 +08:00
|
|
|
await sleep();
|
2020-11-21 21:13:28 +08:00
|
|
|
});
|
|
|
|
expect($$(`.ant-modal-close`)).toHaveLength(0);
|
|
|
|
expect(onCancel).toHaveBeenCalledTimes(1);
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
describe('should not close modals when click confirm button when onOk has argument', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
(['confirm', 'info', 'success', 'warning', 'error'] as const).forEach(type => {
|
2022-04-06 11:07:15 +08:00
|
|
|
it(type, async () => {
|
|
|
|
jest.useFakeTimers();
|
2022-09-05 19:41:32 +08:00
|
|
|
Modal[type]?.({
|
2022-04-06 11:07:15 +08:00
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
2022-09-05 19:41:32 +08:00
|
|
|
onOk: _ => null, // eslint-disable-line no-unused-vars
|
2022-04-06 11:07:15 +08:00
|
|
|
});
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
2022-07-23 16:04:23 +08:00
|
|
|
$$('.ant-btn-primary')[0].click();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
jest.useRealTimers();
|
2020-10-14 16:35:29 +08:00
|
|
|
});
|
2020-04-18 01:09:14 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
describe('could be update by new config', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
(['info', 'success', 'warning', 'error'] as const).forEach(type => {
|
2022-04-06 11:07:15 +08:00
|
|
|
it(type, async () => {
|
|
|
|
jest.useFakeTimers();
|
2022-09-05 19:41:32 +08:00
|
|
|
const instance = Modal[type]?.({
|
2022-04-06 11:07:15 +08:00
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
|
|
|
});
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
expect($$('.ant-modal-confirm-title')[0].innerHTML).toBe('title');
|
|
|
|
expect($$('.ant-modal-confirm-content')[0].innerHTML).toBe('content');
|
|
|
|
instance.update({
|
|
|
|
title: 'new title',
|
|
|
|
content: 'new content',
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
expect($$('.ant-modal-confirm-title')[0].innerHTML).toBe('new title');
|
|
|
|
expect($$('.ant-modal-confirm-content')[0].innerHTML).toBe('new content');
|
|
|
|
instance.destroy();
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
jest.useRealTimers();
|
2020-10-14 16:35:29 +08:00
|
|
|
});
|
2018-08-25 21:58:40 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
describe('could be update by call function', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
(['info', 'success', 'warning', 'error'] as const).forEach(type => {
|
2022-04-06 11:07:15 +08:00
|
|
|
it(type, () => {
|
|
|
|
jest.useFakeTimers();
|
2022-09-05 19:41:32 +08:00
|
|
|
const instance = Modal[type]?.({
|
2022-04-06 11:07:15 +08:00
|
|
|
title: 'title',
|
2022-09-05 19:41:32 +08:00
|
|
|
okButtonProps: { loading: true, style: { color: 'red' } },
|
2022-04-06 11:07:15 +08:00
|
|
|
});
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
expect($$('.ant-modal-confirm-title')[0].innerHTML).toBe('title');
|
|
|
|
expect($$('.ant-modal-confirm-btns .ant-btn-primary')[0].classList).toContain(
|
|
|
|
'ant-btn-loading',
|
|
|
|
);
|
|
|
|
expect($$('.ant-modal-confirm-btns .ant-btn-primary')[0].style.color).toBe('red');
|
|
|
|
instance.update(prevConfig => ({
|
|
|
|
...prevConfig,
|
|
|
|
okButtonProps: {
|
|
|
|
...prevConfig.okButtonProps,
|
|
|
|
loading: false,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
expect($$('.ant-modal-confirm-title')[0].innerHTML).toBe('title');
|
|
|
|
expect($$('.ant-modal-confirm-btns .ant-btn-primary')[0].classList).not.toContain(
|
|
|
|
'ant-btn-loading',
|
|
|
|
);
|
|
|
|
expect($$('.ant-modal-confirm-btns .ant-btn-primary')[0].style.color).toBe('red');
|
|
|
|
instance.destroy();
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
jest.useRealTimers();
|
2020-10-24 14:35:00 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
describe('could be destroy', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
(['info', 'success', 'warning', 'error'] as const).forEach(type => {
|
2022-04-06 11:07:15 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
it(type, async () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
const instance = Modal[type]?.({
|
2022-04-06 11:07:15 +08:00
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
|
|
|
});
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
|
|
|
|
instance.destroy();
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
2020-10-14 16:35:29 +08:00
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
jest.useRealTimers();
|
2018-08-25 21:58:40 +08:00
|
|
|
});
|
|
|
|
});
|
2018-10-12 14:04:26 +08:00
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('could be Modal.destroyAll', async () => {
|
2018-12-03 17:08:48 +08:00
|
|
|
jest.useFakeTimers();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
// Show
|
2022-09-05 19:41:32 +08:00
|
|
|
(['info', 'success', 'warning', 'error'] as const).forEach(type => {
|
|
|
|
Modal[type]?.({
|
2018-12-03 17:08:48 +08:00
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
|
|
|
['info', 'success', 'warning', 'error'].forEach(type => {
|
2018-12-03 17:08:48 +08:00
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
// Destroy
|
2018-12-03 21:49:00 +08:00
|
|
|
Modal.destroyAll();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
2019-01-07 09:40:55 +08:00
|
|
|
['info', 'success', 'warning', 'error'].forEach(type => {
|
2018-12-03 17:08:48 +08:00
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
|
|
|
});
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('prefixCls', async () => {
|
2018-10-12 14:04:26 +08:00
|
|
|
open({ prefixCls: 'custom-modal' });
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await sleep();
|
2018-10-12 14:04:26 +08:00
|
|
|
expect($$('.custom-modal-mask')).toHaveLength(1);
|
|
|
|
expect($$('.custom-modal-wrap')).toHaveLength(1);
|
|
|
|
expect($$('.custom-modal-confirm')).toHaveLength(1);
|
|
|
|
expect($$('.custom-modal-confirm-body-wrapper')).toHaveLength(1);
|
|
|
|
});
|
2019-01-10 01:21:51 +08:00
|
|
|
|
|
|
|
it('should be Modal.confirm without mask', () => {
|
|
|
|
open({ mask: false });
|
|
|
|
expect($$('.ant-modal-mask')).toHaveLength(0);
|
|
|
|
});
|
2019-01-30 10:46:30 +08:00
|
|
|
|
|
|
|
it('destroyFns should reduce when instance.destroy', () => {
|
|
|
|
jest.useFakeTimers();
|
2020-09-29 19:33:37 +08:00
|
|
|
|
2019-01-30 10:46:30 +08:00
|
|
|
Modal.destroyAll(); // clear destroyFns
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2020-09-29 19:33:37 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
const instances: ReturnType<ModalFunc>[] = [];
|
|
|
|
(['info', 'success', 'warning', 'error'] as const).forEach(type => {
|
|
|
|
const instance = Modal[type]?.({
|
2019-01-30 10:46:30 +08:00
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
|
|
|
});
|
2020-09-29 19:33:37 +08:00
|
|
|
|
|
|
|
// Render modal
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
2019-02-19 11:42:05 +08:00
|
|
|
instances.push(instance);
|
2019-01-30 10:46:30 +08:00
|
|
|
});
|
2019-02-19 11:42:05 +08:00
|
|
|
const { length } = instances;
|
2019-01-30 10:46:30 +08:00
|
|
|
instances.forEach((instance, index) => {
|
|
|
|
expect(destroyFns.length).toBe(length - index);
|
2020-09-29 19:33:37 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
instance.destroy();
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2019-01-30 10:46:30 +08:00
|
|
|
expect(destroyFns.length).toBe(length - index - 1);
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|
2020-09-29 19:33:37 +08:00
|
|
|
|
2019-01-30 10:46:30 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2019-12-13 10:18:12 +08:00
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('should warning when pass a string as icon props', async () => {
|
2020-05-03 00:53:32 +08:00
|
|
|
jest.useFakeTimers();
|
2019-12-13 10:18:12 +08:00
|
|
|
const warnSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
confirm({
|
|
|
|
content: 'some descriptions',
|
|
|
|
icon: 'ab',
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
2019-12-13 10:18:12 +08:00
|
|
|
expect(warnSpy).not.toHaveBeenCalled();
|
|
|
|
confirm({
|
|
|
|
content: 'some descriptions',
|
|
|
|
icon: 'question',
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
2019-12-13 10:18:12 +08:00
|
|
|
expect(warnSpy).toHaveBeenCalledWith(
|
|
|
|
`Warning: [antd: Modal] \`icon\` is using ReactNode instead of string naming in v4. Please check \`question\` at https://ant.design/components/icon`,
|
|
|
|
);
|
|
|
|
warnSpy.mockRestore();
|
2020-05-03 00:53:32 +08:00
|
|
|
jest.useRealTimers();
|
2019-12-13 10:18:12 +08:00
|
|
|
});
|
2020-04-06 21:57:51 +08:00
|
|
|
|
2022-09-13 18:09:58 +08:00
|
|
|
it('icon can be null to hide icon', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
confirm({
|
|
|
|
title: 'some title',
|
|
|
|
content: 'some descriptions',
|
|
|
|
icon: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
|
|
|
// We check icon is not exist in the body
|
|
|
|
expect(document.querySelector('.ant-modal-confirm-body')!.children).toHaveLength(2);
|
|
|
|
expect(
|
|
|
|
document.querySelector('.ant-modal-confirm-body')!.querySelector('.anticon'),
|
|
|
|
).toBeFalsy();
|
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('ok button should trigger onOk once when click it many times quickly', async () => {
|
2020-04-06 21:57:51 +08:00
|
|
|
const onOk = jest.fn();
|
|
|
|
open({ onOk });
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await sleep();
|
2020-04-06 21:57:51 +08:00
|
|
|
$$('.ant-btn-primary')[0].click();
|
|
|
|
$$('.ant-btn-primary')[0].click();
|
|
|
|
expect(onOk).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2020-04-18 01:09:14 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/23358
|
2022-04-06 11:07:15 +08:00
|
|
|
it('ok button should trigger onOk multiple times when onOk has close argument', async () => {
|
2020-04-18 01:09:14 +08:00
|
|
|
const onOk = jest.fn();
|
|
|
|
open({
|
2022-09-05 19:41:32 +08:00
|
|
|
onOk(close?: any) {
|
2020-04-18 01:09:14 +08:00
|
|
|
onOk();
|
2022-09-05 19:41:32 +08:00
|
|
|
// @ts-ignore
|
2020-04-18 01:09:14 +08:00
|
|
|
(() => {})(close); // do nothing
|
|
|
|
},
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await sleep();
|
2020-04-18 01:09:14 +08:00
|
|
|
$$('.ant-btn-primary')[0].click();
|
|
|
|
$$('.ant-btn-primary')[0].click();
|
|
|
|
$$('.ant-btn-primary')[0].click();
|
|
|
|
expect(onOk).toHaveBeenCalledTimes(3);
|
2020-07-14 11:14:46 +08:00
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('should be able to global config rootPrefixCls', async () => {
|
2021-02-09 21:49:15 +08:00
|
|
|
jest.useFakeTimers();
|
2021-06-09 15:36:59 +08:00
|
|
|
ConfigProvider.config({ prefixCls: 'my', iconPrefixCls: 'bamboo' });
|
|
|
|
confirm({ title: 'title', icon: <SmileOutlined /> });
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
2021-02-09 21:49:15 +08:00
|
|
|
expect(document.querySelectorAll('.ant-btn').length).toBe(0);
|
|
|
|
expect(document.querySelectorAll('.my-btn').length).toBe(2);
|
2021-06-09 15:36:59 +08:00
|
|
|
expect(document.querySelectorAll('.bamboo-smile').length).toBe(1);
|
2021-02-09 21:49:15 +08:00
|
|
|
expect(document.querySelectorAll('.my-modal-confirm').length).toBe(1);
|
2022-09-05 19:41:32 +08:00
|
|
|
ConfigProvider.config({ prefixCls: 'ant', iconPrefixCls: undefined });
|
2021-02-09 21:49:15 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('should be able to config rootPrefixCls', async () => {
|
2021-03-02 11:53:12 +08:00
|
|
|
resetWarned();
|
|
|
|
|
2020-07-14 11:14:46 +08:00
|
|
|
jest.useFakeTimers();
|
2021-03-02 11:53:12 +08:00
|
|
|
|
2020-07-14 11:14:46 +08:00
|
|
|
Modal.config({
|
2020-07-14 18:43:55 +08:00
|
|
|
rootPrefixCls: 'my',
|
2020-07-14 11:14:46 +08:00
|
|
|
});
|
2021-03-02 11:53:12 +08:00
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Modal] Modal.config is deprecated. Please use ConfigProvider.config instead.',
|
|
|
|
);
|
|
|
|
|
2020-07-14 11:14:46 +08:00
|
|
|
confirm({
|
|
|
|
title: 'title',
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
2020-07-14 18:43:55 +08:00
|
|
|
expect(document.querySelectorAll('.ant-btn').length).toBe(0);
|
|
|
|
expect(document.querySelectorAll('.my-btn').length).toBe(2);
|
|
|
|
expect(document.querySelectorAll('.my-modal-confirm').length).toBe(1);
|
2020-07-14 11:14:46 +08:00
|
|
|
Modal.config({
|
2020-07-14 18:43:55 +08:00
|
|
|
rootPrefixCls: 'your',
|
2020-07-14 11:14:46 +08:00
|
|
|
});
|
|
|
|
confirm({
|
|
|
|
title: 'title',
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
2020-07-14 18:43:55 +08:00
|
|
|
expect(document.querySelectorAll('.ant-btn').length).toBe(0);
|
|
|
|
expect(document.querySelectorAll('.my-btn').length).toBe(2);
|
|
|
|
expect(document.querySelectorAll('.my-modal-confirm').length).toBe(1);
|
|
|
|
expect(document.querySelectorAll('.your-btn').length).toBe(2);
|
|
|
|
expect(document.querySelectorAll('.your-modal-confirm').length).toBe(1);
|
2020-07-14 11:14:46 +08:00
|
|
|
Modal.config({
|
2021-02-09 21:49:15 +08:00
|
|
|
rootPrefixCls: '',
|
2020-07-14 11:14:46 +08:00
|
|
|
});
|
2020-05-03 00:53:32 +08:00
|
|
|
jest.useRealTimers();
|
2020-04-18 01:09:14 +08:00
|
|
|
});
|
2020-12-07 13:44:06 +08:00
|
|
|
|
2020-12-17 17:59:16 +08:00
|
|
|
it('trigger afterClose once when click on cancel button', async () => {
|
2020-12-07 13:44:06 +08:00
|
|
|
const afterClose = jest.fn();
|
|
|
|
open({
|
|
|
|
afterClose,
|
|
|
|
});
|
|
|
|
// first Modal
|
2022-04-06 11:07:15 +08:00
|
|
|
await sleep();
|
2020-12-07 13:44:06 +08:00
|
|
|
$$('.ant-btn')[0].click();
|
2020-12-17 17:59:16 +08:00
|
|
|
expect(afterClose).not.toHaveBeenCalled();
|
|
|
|
await sleep(500);
|
|
|
|
expect(afterClose).toHaveBeenCalled();
|
2020-12-07 13:44:06 +08:00
|
|
|
});
|
|
|
|
|
2020-12-17 17:59:16 +08:00
|
|
|
it('trigger afterClose once when click on ok button', async () => {
|
2020-12-07 13:44:06 +08:00
|
|
|
const afterClose = jest.fn();
|
|
|
|
open({
|
|
|
|
afterClose,
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2020-12-07 13:44:06 +08:00
|
|
|
// second Modal
|
2022-04-06 11:07:15 +08:00
|
|
|
await sleep();
|
2020-12-07 13:44:06 +08:00
|
|
|
$$('.ant-btn-primary')[0].click();
|
2020-12-17 17:59:16 +08:00
|
|
|
expect(afterClose).not.toHaveBeenCalled();
|
|
|
|
await sleep(500);
|
|
|
|
expect(afterClose).toHaveBeenCalled();
|
2020-12-07 13:44:06 +08:00
|
|
|
});
|
2021-11-18 18:29:29 +08:00
|
|
|
|
|
|
|
it('bodyStyle', async () => {
|
2021-11-26 10:15:39 +08:00
|
|
|
open({ bodyStyle: { width: 500 } });
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
await sleep();
|
2021-11-26 10:15:39 +08:00
|
|
|
const { width } = $$('.ant-modal-body')[0].style;
|
|
|
|
expect(width).toBe('500px');
|
2021-11-18 18:29:29 +08:00
|
|
|
});
|
2022-07-23 16:04:23 +08:00
|
|
|
|
|
|
|
describe('the callback close should be a method when onCancel has a close parameter', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
(['confirm', 'info', 'success', 'warning', 'error'] as const).forEach(type => {
|
2022-07-23 16:04:23 +08:00
|
|
|
it(`click the close icon to trigger ${type} onCancel`, async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const mock = jest.fn();
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
Modal[type]?.({
|
2022-07-23 16:04:23 +08:00
|
|
|
closable: true,
|
|
|
|
onCancel: close => mock(close),
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
$$('.ant-modal-close')[0].click();
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
2022-08-30 10:57:13 +08:00
|
|
|
expect(mock).toHaveBeenCalledWith(expect.any(Function));
|
2022-07-23 16:04:23 +08:00
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
(['confirm', 'info', 'success', 'warning', 'error'] as const).forEach(type => {
|
2022-07-23 16:04:23 +08:00
|
|
|
it(`press ESC to trigger ${type} onCancel`, async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const mock = jest.fn();
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
Modal[type]?.({
|
2022-07-23 16:04:23 +08:00
|
|
|
keyboard: true,
|
|
|
|
onCancel: close => mock(close),
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-07-23 16:04:23 +08:00
|
|
|
await sleep();
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-07-23 16:04:23 +08:00
|
|
|
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
TestUtils.Simulate.keyDown($$('.ant-modal')[0], {
|
|
|
|
keyCode: KeyCode.ESC,
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-07-23 16:04:23 +08:00
|
|
|
await sleep(0);
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-07-23 16:04:23 +08:00
|
|
|
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
2022-08-30 10:57:13 +08:00
|
|
|
expect(mock).toHaveBeenCalledWith(expect.any(Function));
|
2022-07-23 16:04:23 +08:00
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
(['confirm', 'info', 'success', 'warning', 'error'] as const).forEach(type => {
|
2022-07-23 16:04:23 +08:00
|
|
|
it(`click the mask to trigger ${type} onCancel`, async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const mock = jest.fn();
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
Modal[type]?.({
|
2022-07-23 16:04:23 +08:00
|
|
|
maskClosable: true,
|
|
|
|
onCancel: close => mock(close),
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect($$('.ant-modal-mask')).toHaveLength(1);
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
|
|
|
|
$$('.ant-modal-wrap')[0].click();
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
2022-08-30 10:57:13 +08:00
|
|
|
expect(mock).toHaveBeenCalledWith(expect.any(Function));
|
2022-07-23 16:04:23 +08:00
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('confirm modal click Cancel button close callback is a function', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const mock = jest.fn();
|
|
|
|
|
|
|
|
Modal.confirm({
|
|
|
|
onCancel: close => mock(close),
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
|
|
|
$$('.ant-modal-confirm-btns > .ant-btn')[0].click();
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
2022-08-30 10:57:13 +08:00
|
|
|
expect(mock).toHaveBeenCalledWith(expect.any(Function));
|
2022-07-23 16:04:23 +08:00
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('close can close modal when onCancel has a close parameter', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
|
|
Modal.confirm({
|
|
|
|
onCancel: close => close(),
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect($$('.ant-modal-confirm-confirm')).toHaveLength(1);
|
|
|
|
|
|
|
|
$$('.ant-modal-confirm-btns > .ant-btn')[0].click();
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect($$('.ant-modal-confirm-confirm')).toHaveLength(0);
|
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2022-09-08 17:30:49 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/37461
|
|
|
|
it('Update should closable', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
|
|
Modal.confirm({}).update({
|
2022-09-14 14:41:20 +08:00
|
|
|
open: true,
|
2022-09-08 17:30:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect($$('.ant-modal-confirm-confirm')).toHaveLength(1);
|
|
|
|
|
|
|
|
$$('.ant-modal-confirm-btns > .ant-btn')[0].click();
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await sleep();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect($$('.ant-modal-confirm-confirm')).toHaveLength(0);
|
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2017-03-17 11:47:36 +08:00
|
|
|
});
|