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';
|
2023-05-05 20:52:44 +08:00
|
|
|
import 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 '..';
|
2023-02-24 16:59:57 +08:00
|
|
|
import { act, waitFakeTimer } 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');
|
|
|
|
|
2023-01-08 00:27:33 +08:00
|
|
|
(global as any).injectPromise = false;
|
|
|
|
(global as any).rejectPromise = null;
|
|
|
|
|
|
|
|
jest.mock('../../_util/ActionButton', () => {
|
|
|
|
const ActionButton = jest.requireActual('../../_util/ActionButton').default;
|
|
|
|
return (props: any) => {
|
|
|
|
const { actionFn } = props;
|
|
|
|
let mockActionFn: any = actionFn;
|
|
|
|
if (actionFn && (global as any).injectPromise) {
|
|
|
|
mockActionFn = (...args: any) => {
|
|
|
|
let ret = actionFn(...args);
|
|
|
|
|
|
|
|
if (ret.then) {
|
|
|
|
let resolveFn: any;
|
|
|
|
let rejectFn: any;
|
|
|
|
|
|
|
|
ret = ret.then(
|
|
|
|
(v: any) => {
|
|
|
|
resolveFn?.(v);
|
|
|
|
},
|
|
|
|
(e: any) => {
|
|
|
|
rejectFn?.(e)?.catch((err: Error) => {
|
|
|
|
(global as any).rejectPromise = err;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
ret.then = (resolve: any, reject: any) => {
|
|
|
|
resolveFn = resolve;
|
|
|
|
rejectFn = reject;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return <ActionButton {...props} actionFn={mockActionFn} />;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
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);
|
2022-11-19 13:47:33 +08:00
|
|
|
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-11-28 14:19:25 +08:00
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
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 */
|
|
|
|
|
2023-01-08 00:27:33 +08:00
|
|
|
beforeEach(() => {
|
2023-02-24 16:59:57 +08:00
|
|
|
jest.useFakeTimers();
|
2023-01-08 00:27:33 +08:00
|
|
|
(global as any).injectPromise = false;
|
|
|
|
(global as any).rejectPromise = null;
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
afterEach(async () => {
|
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
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
document.body.innerHTML = '';
|
2022-10-31 10:15:52 +08:00
|
|
|
jest.clearAllTimers();
|
2017-03-17 18:56:30 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2022-10-31 10:15:52 +08:00
|
|
|
jest.useRealTimers();
|
2017-03-17 18:56:30 +08:00
|
|
|
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-10-31 10:15:52 +08:00
|
|
|
async function open(args?: ModalFuncProps) {
|
2017-03-17 11:47:36 +08:00
|
|
|
confirm({
|
|
|
|
title: 'Want to delete these items?',
|
|
|
|
content: 'some descriptions',
|
|
|
|
...args,
|
|
|
|
});
|
2022-10-31 10:15:52 +08:00
|
|
|
|
|
|
|
await waitFakeTimer();
|
2017-03-17 11:47:36 +08:00
|
|
|
}
|
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
it('should not render title when title not defined', async () => {
|
2019-10-26 10:31:58 +08:00
|
|
|
confirm({
|
|
|
|
content: 'some descriptions',
|
|
|
|
});
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2019-10-26 10:31:58 +08:00
|
|
|
expect(document.querySelector('.ant-modal-confirm-title')).toBe(null);
|
|
|
|
});
|
|
|
|
|
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();
|
2022-10-31 10:15:52 +08:00
|
|
|
await open({
|
2017-03-17 11:47:36 +08:00
|
|
|
onCancel,
|
|
|
|
onOk,
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
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();
|
2022-10-31 10:15:52 +08:00
|
|
|
await open({
|
2017-03-17 11:47:36 +08:00
|
|
|
onCancel,
|
|
|
|
onOk,
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
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 () => {
|
2022-10-31 10:15:52 +08:00
|
|
|
await open();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
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 () => {
|
2022-10-31 10:15:52 +08:00
|
|
|
await open();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2017-03-17 11:47:36 +08:00
|
|
|
// Fourth Modal
|
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
|
|
|
const onCancel = jest.fn();
|
|
|
|
Modal.confirm({
|
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
|
|
|
onCancel,
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
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-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer(0);
|
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);
|
|
|
|
});
|
|
|
|
|
2023-02-24 16:59:57 +08:00
|
|
|
it('should not fire twice onOk when button is pressed twice', async () => {
|
|
|
|
let resolveFn: VoidFunction;
|
|
|
|
const onOk = jest.fn(
|
|
|
|
() =>
|
|
|
|
new Promise<void>((resolve) => {
|
|
|
|
resolveFn = resolve;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
await open({
|
|
|
|
onOk,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Load will not clickable
|
|
|
|
await waitFakeTimer();
|
|
|
|
for (let i = 0; i < 10; i += 1) {
|
|
|
|
act(() => {
|
|
|
|
$$('.ant-btn-primary')[0].click();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
expect(onOk).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
// Resolve this promise
|
|
|
|
resolveFn!();
|
|
|
|
await Promise.resolve();
|
|
|
|
|
|
|
|
// Resolve still can not clickable
|
|
|
|
act(() => {
|
|
|
|
$$('.ant-btn-primary')[0].click();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(onOk).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('should not hide confirm when onOk return Promise.resolve', async () => {
|
2022-10-31 10:15:52 +08:00
|
|
|
await open({
|
2020-04-18 01:09:14 +08:00
|
|
|
onOk: () => Promise.resolve(''),
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
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 () => {
|
2023-01-08 00:27:33 +08:00
|
|
|
(global as any).injectPromise = true;
|
|
|
|
|
2019-05-09 12:24:59 +08:00
|
|
|
const error = new Error('something wrong');
|
2022-10-31 10:15:52 +08:00
|
|
|
await 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
|
|
|
|
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
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2020-09-29 19:33:37 +08:00
|
|
|
|
2023-01-08 00:27:33 +08:00
|
|
|
expect((global as any).rejectPromise instanceof Error).toBeTruthy();
|
2019-05-09 12:24:59 +08:00
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('shows animation when close', async () => {
|
2022-10-31 10:15:52 +08:00
|
|
|
await open();
|
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
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
|
|
|
|
2019-07-19 11:54:08 +08:00
|
|
|
$$('.ant-btn')[0].click();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
expect($$('.ant-modal-confirm')).toHaveLength(0);
|
|
|
|
});
|
2017-12-02 22:10:27 +08:00
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('ok only', async () => {
|
2022-10-31 10:15:52 +08:00
|
|
|
await open({ okCancel: false });
|
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-10-31 10:15:52 +08:00
|
|
|
await open({
|
2022-09-05 19:41:32 +08:00
|
|
|
okButtonProps: { disabled: true },
|
|
|
|
cancelButtonProps: { 'data-test': 'baz' } as ModalFuncProps['cancelButtonProps'],
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
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-11-19 13:47:33 +08:00
|
|
|
(['info', 'success', 'warning', 'error'] as const).forEach((type) => {
|
2022-04-06 11:07:15 +08:00
|
|
|
it(type, async () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
Modal[type]?.({ title: 'title', content: 'content' });
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
|
|
|
|
$$('.ant-btn')[0].click();
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
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
|
|
|
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,
|
|
|
|
});
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2020-08-26 16:42:47 +08:00
|
|
|
expect($$(`.ant-modal-confirm-confirm`)).toHaveLength(1);
|
|
|
|
$$('.ant-btn')[0].click();
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2020-08-26 16:42:47 +08:00
|
|
|
expect($$(`.ant-modal-confirm-confirm`)).toHaveLength(0);
|
|
|
|
expect(onCancel).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
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
|
|
|
const onCancel = jest.fn();
|
|
|
|
Modal.confirm({
|
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
|
|
|
closable: true,
|
|
|
|
closeIcon: 'X',
|
|
|
|
onCancel,
|
|
|
|
});
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2020-11-21 21:13:28 +08:00
|
|
|
expect($$(`.ant-modal-close`)).toHaveLength(1);
|
|
|
|
$$('.ant-btn')[0].click();
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2020-11-21 21:13:28 +08:00
|
|
|
expect($$(`.ant-modal-close`)).toHaveLength(0);
|
|
|
|
expect(onCancel).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
describe('should not close modals when click confirm button when onOk has argument', () => {
|
2022-11-19 13:47:33 +08:00
|
|
|
(['confirm', 'info', 'success', 'warning', 'error'] as const).forEach((type) => {
|
2022-04-06 11:07:15 +08:00
|
|
|
it(type, async () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
Modal[type]?.({
|
2022-04-06 11:07:15 +08:00
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
2022-11-19 13:47:33 +08:00
|
|
|
onOk: (_) => null, // eslint-disable-line no-unused-vars
|
2022-04-06 11:07:15 +08:00
|
|
|
});
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
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
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
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-11-19 13:47:33 +08:00
|
|
|
(['info', 'success', 'warning', 'error'] as const).forEach((type) => {
|
2022-04-06 11:07:15 +08:00
|
|
|
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',
|
|
|
|
});
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
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',
|
|
|
|
});
|
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
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-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
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-11-19 13:47:33 +08:00
|
|
|
(['info', 'success', 'warning', 'error'] as const).forEach((type) => {
|
2022-10-31 10:15:52 +08:00
|
|
|
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',
|
2022-09-05 19:41:32 +08:00
|
|
|
okButtonProps: { loading: true, style: { color: 'red' } },
|
2022-04-06 11:07:15 +08:00
|
|
|
});
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
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');
|
2022-11-19 13:47:33 +08:00
|
|
|
instance.update((prevConfig) => ({
|
2022-04-06 11:07:15 +08:00
|
|
|
...prevConfig,
|
|
|
|
okButtonProps: {
|
|
|
|
...prevConfig.okButtonProps,
|
|
|
|
loading: false,
|
|
|
|
},
|
|
|
|
}));
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
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-10-31 10:15:52 +08:00
|
|
|
|
|
|
|
await waitFakeTimer();
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
2020-10-24 14:35:00 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
describe('could be destroy', () => {
|
2022-11-19 13:47:33 +08:00
|
|
|
(['info', 'success', 'warning', 'error'] as const).forEach((type) => {
|
2022-04-06 11:07:15 +08:00
|
|
|
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',
|
|
|
|
});
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
|
|
|
|
instance.destroy();
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
2020-10-14 16:35:29 +08:00
|
|
|
});
|
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 () => {
|
|
|
|
// Show
|
2022-11-19 13:47:33 +08:00
|
|
|
(['info', 'success', 'warning', 'error'] as const).forEach((type) => {
|
2022-09-05 19:41:32 +08:00
|
|
|
Modal[type]?.({
|
2018-12-03 17:08:48 +08:00
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
});
|
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
['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
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
['info', 'success', 'warning', 'error'].forEach((type) => {
|
2018-12-03 17:08:48 +08:00
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
it('prefixCls', async () => {
|
2022-10-31 10:15:52 +08:00
|
|
|
await open({ prefixCls: 'custom-modal' });
|
2022-04-06 11:07:15 +08:00
|
|
|
|
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
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
it('should be Modal.confirm without mask', async () => {
|
|
|
|
await open({ mask: false });
|
2019-01-10 01:21:51 +08:00
|
|
|
expect($$('.ant-modal-mask')).toHaveLength(0);
|
|
|
|
});
|
2019-01-30 10:46:30 +08:00
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
it('destroyFns should reduce when instance.destroy', async () => {
|
2019-01-30 10:46:30 +08:00
|
|
|
Modal.destroyAll(); // clear destroyFns
|
2022-10-31 10:15:52 +08:00
|
|
|
|
|
|
|
await waitFakeTimer();
|
2020-09-29 19:33:37 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
const instances: ReturnType<ModalFunc>[] = [];
|
2022-11-19 13:47:33 +08:00
|
|
|
(['info', 'success', 'warning', 'error'] as const).forEach((type) => {
|
2022-09-05 19:41:32 +08:00
|
|
|
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
|
|
|
});
|
2019-01-30 10:46:30 +08:00
|
|
|
});
|
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 () => {
|
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
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
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
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
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-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,
|
|
|
|
});
|
|
|
|
|
2022-11-07 23:32:46 +08:00
|
|
|
await waitFakeTimer();
|
2022-09-13 18:09:58 +08:00
|
|
|
|
|
|
|
// 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();
|
2022-10-31 10:15:52 +08:00
|
|
|
await open({ onOk });
|
2022-04-06 11:07:15 +08:00
|
|
|
|
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();
|
2022-10-31 10:15:52 +08:00
|
|
|
await 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
|
|
|
|
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-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
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
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
|
|
|
});
|
|
|
|
|
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
|
|
|
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
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
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
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
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-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();
|
2022-10-31 10:15:52 +08:00
|
|
|
await open({
|
2020-12-07 13:44:06 +08:00
|
|
|
afterClose,
|
|
|
|
});
|
|
|
|
// first Modal
|
|
|
|
$$('.ant-btn')[0].click();
|
2020-12-17 17:59:16 +08:00
|
|
|
expect(afterClose).not.toHaveBeenCalled();
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer(500);
|
2020-12-17 17:59:16 +08:00
|
|
|
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();
|
2022-10-31 10:15:52 +08:00
|
|
|
await open({
|
2020-12-07 13:44:06 +08:00
|
|
|
afterClose,
|
|
|
|
});
|
2022-04-06 11:07:15 +08:00
|
|
|
|
2020-12-07 13:44:06 +08:00
|
|
|
// second Modal
|
|
|
|
$$('.ant-btn-primary')[0].click();
|
2020-12-17 17:59:16 +08:00
|
|
|
expect(afterClose).not.toHaveBeenCalled();
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer(500);
|
2020-12-17 17:59:16 +08:00
|
|
|
expect(afterClose).toHaveBeenCalled();
|
2020-12-07 13:44:06 +08:00
|
|
|
});
|
2021-11-18 18:29:29 +08:00
|
|
|
|
|
|
|
it('bodyStyle', async () => {
|
2022-10-31 10:15:52 +08:00
|
|
|
await open({ bodyStyle: { width: 500 } });
|
2022-04-06 11:07:15 +08:00
|
|
|
|
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-11-19 13:47:33 +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 () => {
|
|
|
|
const mock = jest.fn();
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
Modal[type]?.({
|
2022-07-23 16:04:23 +08:00
|
|
|
closable: true,
|
2022-11-19 13:47:33 +08:00
|
|
|
onCancel: (close) => mock(close),
|
2022-07-23 16:04:23 +08:00
|
|
|
});
|
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-07-23 16:04:23 +08:00
|
|
|
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
$$('.ant-modal-close')[0].click();
|
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-19 13:47:33 +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 () => {
|
|
|
|
const mock = jest.fn();
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
Modal[type]?.({
|
2022-07-23 16:04:23 +08:00
|
|
|
keyboard: true,
|
2022-11-19 13:47:33 +08:00
|
|
|
onCancel: (close) => mock(close),
|
2022-07-23 16:04:23 +08:00
|
|
|
});
|
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
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-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer(0);
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-19 13:47:33 +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 () => {
|
|
|
|
const mock = jest.fn();
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
Modal[type]?.({
|
2022-07-23 16:04:23 +08:00
|
|
|
maskClosable: true,
|
2022-11-19 13:47:33 +08:00
|
|
|
onCancel: (close) => mock(close),
|
2022-07-23 16:04:23 +08:00
|
|
|
});
|
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-07-23 16:04:23 +08:00
|
|
|
|
|
|
|
expect($$('.ant-modal-mask')).toHaveLength(1);
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
|
|
|
|
$$('.ant-modal-wrap')[0].click();
|
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('confirm modal click Cancel button close callback is a function', async () => {
|
|
|
|
const mock = jest.fn();
|
|
|
|
|
|
|
|
Modal.confirm({
|
2022-11-19 13:47:33 +08:00
|
|
|
onCancel: (close) => mock(close),
|
2022-07-23 16:04:23 +08:00
|
|
|
});
|
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-07-23 16:04:23 +08:00
|
|
|
|
|
|
|
$$('.ant-modal-confirm-btns > .ant-btn')[0].click();
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-07-23 16:04:23 +08:00
|
|
|
|
2022-08-30 10:57:13 +08:00
|
|
|
expect(mock).toHaveBeenCalledWith(expect.any(Function));
|
2022-07-23 16:04:23 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('close can close modal when onCancel has a close parameter', async () => {
|
|
|
|
Modal.confirm({
|
2022-11-19 13:47:33 +08:00
|
|
|
onCancel: (close) => close(),
|
2022-07-23 16:04:23 +08:00
|
|
|
});
|
|
|
|
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-07-23 16:04:23 +08:00
|
|
|
|
|
|
|
expect($$('.ant-modal-confirm-confirm')).toHaveLength(1);
|
|
|
|
|
|
|
|
$$('.ant-modal-confirm-btns > .ant-btn')[0].click();
|
2022-10-31 10:15:52 +08:00
|
|
|
await waitFakeTimer();
|
2022-07-23 16:04:23 +08:00
|
|
|
|
|
|
|
expect($$('.ant-modal-confirm-confirm')).toHaveLength(0);
|
|
|
|
});
|
2022-09-08 17:30:49 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/37461
|
|
|
|
it('Update should closable', async () => {
|
2022-09-18 11:22:05 +08:00
|
|
|
resetWarned();
|
2022-09-08 17:30:49 +08:00
|
|
|
jest.useFakeTimers();
|
2022-09-18 11:22:05 +08:00
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2022-09-08 17:30:49 +08:00
|
|
|
|
2022-09-18 11:22:05 +08:00
|
|
|
const modal = Modal.confirm({});
|
2022-09-08 17:30:49 +08:00
|
|
|
|
2022-11-07 23:32:46 +08:00
|
|
|
modal.update({
|
|
|
|
visible: true,
|
2022-09-08 17:30:49 +08:00
|
|
|
});
|
|
|
|
|
2022-11-07 23:32:46 +08:00
|
|
|
await waitFakeTimer();
|
2022-09-08 17:30:49 +08:00
|
|
|
|
|
|
|
expect($$('.ant-modal-confirm-confirm')).toHaveLength(1);
|
|
|
|
|
|
|
|
$$('.ant-modal-confirm-btns > .ant-btn')[0].click();
|
2022-11-07 23:32:46 +08:00
|
|
|
await waitFakeTimer();
|
2022-09-08 17:30:49 +08:00
|
|
|
|
|
|
|
expect($$('.ant-modal-confirm-confirm')).toHaveLength(0);
|
|
|
|
|
|
|
|
jest.useRealTimers();
|
2022-09-18 11:22:05 +08:00
|
|
|
errSpy.mockRestore();
|
2022-09-08 17:30:49 +08:00
|
|
|
});
|
2022-12-07 21:33:44 +08:00
|
|
|
|
|
|
|
it('null of Footer', async () => {
|
|
|
|
Modal.confirm({
|
|
|
|
footer: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitFakeTimer();
|
|
|
|
|
|
|
|
expect($$('.ant-modal-confirm-btns')).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Update Footer', async () => {
|
|
|
|
Modal.confirm({
|
|
|
|
footer: (
|
|
|
|
<div>
|
|
|
|
<button className="custom-modal-footer" type="button">
|
|
|
|
Custom Modal Footer
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
});
|
|
|
|
await waitFakeTimer();
|
|
|
|
expect($$('.custom-modal-footer')).toHaveLength(1);
|
|
|
|
});
|
2023-03-11 18:35:23 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/41170
|
|
|
|
describe('footer', () => {
|
|
|
|
(['confirm', 'info', 'success', 'warning', 'error'] as const).forEach((type) => {
|
|
|
|
it(`${type} should not render the footer in the default`, async () => {
|
|
|
|
Modal[type]({
|
|
|
|
content: 'hai',
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitFakeTimer();
|
|
|
|
|
|
|
|
expect(document.querySelector(`.ant-modal-footer`)).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('confirm should render the footer when footer is set', async () => {
|
|
|
|
Modal.confirm({
|
|
|
|
content: 'hai',
|
|
|
|
footer: 'hai',
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitFakeTimer();
|
|
|
|
|
|
|
|
expect(document.querySelector(`.ant-modal-content`)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
2017-03-17 11:47:36 +08:00
|
|
|
});
|