2017-03-17 11:47:36 +08:00
|
|
|
import Modal from '..';
|
2019-02-19 11:42:05 +08:00
|
|
|
import { destroyFns } from '../Modal';
|
2017-03-17 11:47:36 +08:00
|
|
|
|
2017-10-09 13:23:20 +08:00
|
|
|
const { confirm } = Modal;
|
2017-03-17 11:47:36 +08:00
|
|
|
|
|
|
|
describe('Modal.confirm triggers callbacks correctly', () => {
|
2017-03-17 18:56:30 +08:00
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
errorSpy.mockReset();
|
2017-11-11 00:07:03 +08:00
|
|
|
document.body.innerHTML = '';
|
2017-03-17 18:56:30 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
2017-03-17 11:47:36 +08:00
|
|
|
function $$(className) {
|
|
|
|
return document.body.querySelectorAll(className);
|
|
|
|
}
|
|
|
|
|
|
|
|
function open(args) {
|
|
|
|
confirm({
|
|
|
|
title: 'Want to delete these items?',
|
|
|
|
content: 'some descriptions',
|
|
|
|
...args,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-26 10:31:58 +08:00
|
|
|
it('should not render title when title not defined', () => {
|
|
|
|
confirm({
|
|
|
|
content: 'some descriptions',
|
|
|
|
});
|
|
|
|
expect(document.querySelector('.ant-modal-confirm-title')).toBe(null);
|
|
|
|
});
|
|
|
|
|
2017-03-17 11:47:36 +08:00
|
|
|
it('trigger onCancel once when click on cancel button', () => {
|
|
|
|
const onCancel = jest.fn();
|
|
|
|
const onOk = jest.fn();
|
|
|
|
open({
|
|
|
|
onCancel,
|
|
|
|
onOk,
|
|
|
|
});
|
|
|
|
// first Modal
|
|
|
|
$$('.ant-btn')[0].click();
|
|
|
|
expect(onCancel.mock.calls.length).toBe(1);
|
|
|
|
expect(onOk.mock.calls.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('trigger onOk once when click on ok button', () => {
|
|
|
|
const onCancel = jest.fn();
|
|
|
|
const onOk = jest.fn();
|
|
|
|
open({
|
|
|
|
onCancel,
|
|
|
|
onOk,
|
|
|
|
});
|
|
|
|
// second Modal
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow Modal.comfirm without onCancel been set', () => {
|
|
|
|
open();
|
|
|
|
// 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
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow Modal.comfirm without onOk been set', () => {
|
|
|
|
open();
|
|
|
|
// 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
|
|
|
|
2019-05-09 12:24:59 +08:00
|
|
|
it('should emit error when onOk return Promise.reject', () => {
|
|
|
|
const error = new Error('something wrong');
|
|
|
|
open({
|
|
|
|
onOk: () => Promise.reject(error),
|
|
|
|
});
|
|
|
|
// Fifth Modal
|
|
|
|
$$('.ant-btn-primary')[0].click();
|
|
|
|
// wait promise
|
|
|
|
return Promise.resolve().then(() => {
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
it('shows animation when close', () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
open();
|
|
|
|
expect($$('.ant-modal-confirm')).toHaveLength(1);
|
2019-07-19 11:54:08 +08:00
|
|
|
$$('.ant-btn')[0].click();
|
2019-07-03 20:14:39 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
expect($$('.ant-modal-confirm')).toHaveLength(0);
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2017-12-02 22:10:27 +08:00
|
|
|
|
|
|
|
it('ok only', () => {
|
|
|
|
open({ okCancel: false });
|
|
|
|
expect($$('.ant-btn')).toHaveLength(1);
|
|
|
|
expect($$('.ant-btn')[0].innerHTML).toContain('OK');
|
|
|
|
});
|
2018-05-25 20:59:17 +08:00
|
|
|
|
2018-09-30 14:50:03 +08:00
|
|
|
it('allows extra props on buttons', () => {
|
|
|
|
open({ okButtonProps: { disabled: true }, cancelButtonProps: { 'data-test': 'baz' } });
|
|
|
|
expect($$('.ant-btn')).toHaveLength(2);
|
|
|
|
expect($$('.ant-btn')[0].attributes['data-test'].value).toBe('baz');
|
|
|
|
expect($$('.ant-btn')[1].disabled).toBe(true);
|
|
|
|
});
|
|
|
|
|
2019-04-03 15:54:26 +08:00
|
|
|
it('should close modals when click confirm button', () => {
|
2018-05-25 20:59:17 +08:00
|
|
|
jest.useFakeTimers();
|
2018-12-07 20:02:01 +08:00
|
|
|
['info', 'success', 'warning', 'error'].forEach(type => {
|
2018-05-25 20:59:17 +08:00
|
|
|
Modal[type]({
|
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
|
|
|
});
|
2018-10-12 14:04:26 +08:00
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
2018-05-25 20:59:17 +08:00
|
|
|
$$('.ant-btn')[0].click();
|
|
|
|
jest.runAllTimers();
|
2018-10-12 14:04:26 +08:00
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
2018-05-25 20:59:17 +08:00
|
|
|
});
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2018-08-25 21:58:40 +08:00
|
|
|
|
|
|
|
it('could be update', () => {
|
|
|
|
jest.useFakeTimers();
|
2018-12-07 20:02:01 +08:00
|
|
|
['info', 'success', 'warning', 'error'].forEach(type => {
|
2018-08-25 21:58:40 +08:00
|
|
|
const instance = Modal[type]({
|
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
|
|
|
});
|
2018-10-12 14:04:26 +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');
|
2018-08-25 21:58:40 +08:00
|
|
|
instance.update({
|
|
|
|
title: 'new title',
|
|
|
|
content: 'new content',
|
|
|
|
});
|
2018-10-12 14:04:26 +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');
|
2018-08-25 21:58:40 +08:00
|
|
|
instance.destroy();
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('could be destroy', () => {
|
|
|
|
jest.useFakeTimers();
|
2018-12-07 20:02:01 +08:00
|
|
|
['info', 'success', 'warning', 'error'].forEach(type => {
|
2018-08-25 21:58:40 +08:00
|
|
|
const instance = Modal[type]({
|
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
|
|
|
});
|
2018-10-12 14:04:26 +08:00
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
2018-08-25 21:58:40 +08:00
|
|
|
instance.destroy();
|
|
|
|
jest.runAllTimers();
|
2018-10-12 14:04:26 +08:00
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
2018-08-25 21:58:40 +08:00
|
|
|
});
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2018-10-12 14:04:26 +08:00
|
|
|
|
2018-12-03 21:49:00 +08:00
|
|
|
it('could be Modal.destroyAll', () => {
|
2018-12-03 17:08:48 +08:00
|
|
|
jest.useFakeTimers();
|
2019-01-07 09:40:55 +08:00
|
|
|
['info', 'success', 'warning', 'error'].forEach(type => {
|
2018-12-03 17:08:48 +08:00
|
|
|
Modal[type]({
|
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
|
|
|
});
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
});
|
2018-12-03 21:49:00 +08:00
|
|
|
Modal.destroyAll();
|
2019-01-07 09:40:55 +08:00
|
|
|
['info', 'success', 'warning', 'error'].forEach(type => {
|
2018-12-03 17:08:48 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
|
|
|
});
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2018-10-12 14:04:26 +08:00
|
|
|
it('prefixCls', () => {
|
|
|
|
open({ prefixCls: 'custom-modal' });
|
|
|
|
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();
|
|
|
|
Modal.destroyAll(); // clear destroyFns
|
|
|
|
jest.runAllTimers();
|
|
|
|
const instances = [];
|
|
|
|
['info', 'success', 'warning', 'error'].forEach(type => {
|
|
|
|
const instance = Modal[type]({
|
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
|
|
|
});
|
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);
|
|
|
|
instance.destroy();
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(destroyFns.length).toBe(length - index - 1);
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|
2019-01-30 10:46:30 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2017-03-17 11:47:36 +08:00
|
|
|
});
|