ant-design-vue/components/modal/__tests__/confirm.test.js

115 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-01-12 03:33:27 +00:00
import Modal from '..';
2020-05-13 10:36:53 +00:00
import { sleep } from '../../../tests/utils';
2019-01-12 03:33:27 +00:00
const { confirm } = Modal;
jest.mock('../../_util/Portal');
2018-05-24 15:04:41 +00:00
describe('Modal.confirm triggers callbacks correctly', () => {
2019-01-12 03:33:27 +00:00
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
document.createDocumentFragment = () => {
const container = document.createElement('div');
document.body.appendChild(container);
return container;
};
2018-05-24 15:04:41 +00:00
afterEach(() => {
2019-01-12 03:33:27 +00:00
errorSpy.mockReset();
document.body.innerHTML = '';
Modal.destroyAll();
2019-01-12 03:33:27 +00:00
});
2018-05-24 15:04:41 +00:00
afterAll(() => {
2019-01-12 03:33:27 +00:00
errorSpy.mockRestore();
});
2018-05-24 15:04:41 +00:00
2019-01-12 03:33:27 +00:00
function $$(className) {
return document.body.querySelectorAll(className);
2018-05-24 15:04:41 +00:00
}
2019-01-12 03:33:27 +00:00
function open(args) {
jest.useFakeTimers();
2018-05-24 15:04:41 +00:00
confirm({
title: 'Want to delete these items?',
content: 'some descriptions',
...args,
2019-01-12 03:33:27 +00:00
});
jest.runAllTimers();
jest.useRealTimers();
2018-05-24 15:04:41 +00:00
}
2020-05-13 10:36:53 +00:00
it('trigger onCancel once when click on cancel button', async () => {
2019-01-12 03:33:27 +00:00
const onCancel = jest.fn();
const onOk = jest.fn();
2018-05-24 15:04:41 +00:00
open({
onCancel,
onOk,
2019-01-12 03:33:27 +00:00
});
2020-05-13 10:36:53 +00:00
await sleep();
2018-05-24 15:04:41 +00:00
// first Modal
2019-01-12 03:33:27 +00:00
$$('.ant-btn')[0].click();
expect(onCancel.mock.calls.length).toBe(1);
expect(onOk.mock.calls.length).toBe(0);
});
2018-05-24 15:04:41 +00:00
2020-05-13 10:36:53 +00:00
it('trigger onOk once when click on ok button', async () => {
2019-01-12 03:33:27 +00:00
const onCancel = jest.fn();
const onOk = jest.fn();
2018-05-24 15:04:41 +00:00
open({
onCancel,
onOk,
2019-01-12 03:33:27 +00:00
});
2020-05-13 10:36:53 +00:00
await sleep();
2018-05-24 15:04:41 +00:00
// second Modal
2019-01-12 03:33:27 +00:00
$$('.ant-btn-primary')[0].click();
expect(onCancel.mock.calls.length).toBe(0);
expect(onOk.mock.calls.length).toBe(1);
});
2018-05-24 15:04:41 +00:00
2020-05-13 10:36:53 +00:00
it('should allow Modal.comfirm without onCancel been set', async () => {
2019-01-12 03:33:27 +00:00
open();
2020-05-13 10:36:53 +00:00
await sleep();
2018-05-24 15:04:41 +00:00
// Third Modal
2019-01-12 03:33:27 +00:00
$$('.ant-btn')[0].click();
expect(errorSpy).not.toHaveBeenCalled();
});
2018-05-24 15:04:41 +00:00
2020-05-13 10:36:53 +00:00
it('should allow Modal.comfirm without onOk been set', async () => {
2019-01-12 03:33:27 +00:00
open();
2020-05-13 10:36:53 +00:00
await sleep();
2018-05-24 15:04:41 +00:00
// Fourth Modal
2019-01-12 03:33:27 +00:00
$$('.ant-btn-primary')[0].click();
expect(errorSpy).not.toHaveBeenCalled();
});
2018-05-24 15:04:41 +00:00
2020-05-13 10:36:53 +00:00
it('ok only', async () => {
2019-01-12 03:33:27 +00:00
open({ okCancel: false });
2020-05-13 10:36:53 +00:00
await sleep();
2019-01-12 03:33:27 +00:00
expect($$('.ant-btn')).toHaveLength(1);
expect($$('.ant-btn')[0].innerHTML).toContain('OK');
});
2020-05-13 10:36:53 +00:00
it('allows extra props on buttons', async () => {
2019-01-12 03:33:27 +00:00
open({
2020-08-14 03:15:10 +00:00
okButtonProps: { disabled: true },
cancelButtonProps: { 'data-test': 'baz' },
2019-01-12 03:33:27 +00:00
});
2020-05-13 10:36:53 +00:00
await sleep();
2019-01-12 03:33:27 +00:00
expect($$('.ant-btn')).toHaveLength(2);
expect($$('.ant-btn')[0].attributes['data-test'].value).toBe('baz');
expect($$('.ant-btn')[1].disabled).toBe(true);
});
2020-08-14 03:15:10 +00:00
it('trigger onCancel once when click on cancel button', async () => {
2023-05-03 07:09:01 +00:00
const onCancel = jest.fn();
const onOk = jest.fn();
await open({
title: 'title',
content: 'content',
onCancel,
onOk,
});
await sleep();
$$('.ant-btn')[0].click();
expect(onCancel.mock.calls.length).toBe(1);
expect(onOk.mock.calls.length).toBe(0);
2019-01-12 03:33:27 +00:00
});
});