2019-01-12 11:33:27 +08:00
|
|
|
import Modal from '..';
|
|
|
|
import { asyncExpect } from '@/tests/utils';
|
|
|
|
const { confirm } = Modal;
|
2018-05-24 23:04:41 +08:00
|
|
|
|
|
|
|
describe('Modal.confirm triggers callbacks correctly', () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2018-05-24 23:04:41 +08:00
|
|
|
|
|
|
|
afterEach(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
errorSpy.mockReset();
|
|
|
|
document.body.innerHTML = '';
|
|
|
|
});
|
2018-05-24 23:04:41 +08:00
|
|
|
|
|
|
|
afterAll(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
2018-05-24 23:04:41 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function $$(className) {
|
|
|
|
return document.body.querySelectorAll(className);
|
2018-05-24 23:04:41 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function open(args) {
|
2018-05-24 23:04:41 +08:00
|
|
|
confirm({
|
|
|
|
title: 'Want to delete these items?',
|
|
|
|
content: 'some descriptions',
|
|
|
|
...args,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-05-24 23:04:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
it('trigger onCancel once when click on cancel button', () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const onCancel = jest.fn();
|
|
|
|
const onOk = jest.fn();
|
2018-05-24 23:04:41 +08:00
|
|
|
open({
|
|
|
|
onCancel,
|
|
|
|
onOk,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-05-24 23:04:41 +08:00
|
|
|
// first Modal
|
2019-01-12 11:33:27 +08:00
|
|
|
$$('.ant-btn')[0].click();
|
|
|
|
expect(onCancel.mock.calls.length).toBe(1);
|
|
|
|
expect(onOk.mock.calls.length).toBe(0);
|
|
|
|
});
|
2018-05-24 23:04:41 +08:00
|
|
|
|
|
|
|
it('trigger onOk once when click on ok button', () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const onCancel = jest.fn();
|
|
|
|
const onOk = jest.fn();
|
2018-05-24 23:04:41 +08:00
|
|
|
open({
|
|
|
|
onCancel,
|
|
|
|
onOk,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-05-24 23:04:41 +08:00
|
|
|
// second Modal
|
2019-01-12 11:33:27 +08:00
|
|
|
$$('.ant-btn-primary')[0].click();
|
|
|
|
expect(onCancel.mock.calls.length).toBe(0);
|
|
|
|
expect(onOk.mock.calls.length).toBe(1);
|
|
|
|
});
|
2018-05-24 23:04:41 +08:00
|
|
|
|
|
|
|
it('should allow Modal.comfirm without onCancel been set', () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
open();
|
2018-05-24 23:04:41 +08:00
|
|
|
// Third Modal
|
2019-01-12 11:33:27 +08:00
|
|
|
$$('.ant-btn')[0].click();
|
|
|
|
expect(errorSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
2018-05-24 23:04:41 +08:00
|
|
|
|
|
|
|
it('should allow Modal.comfirm without onOk been set', () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
open();
|
2018-05-24 23:04:41 +08:00
|
|
|
// Fourth Modal
|
2019-01-12 11:33:27 +08:00
|
|
|
$$('.ant-btn-primary')[0].click();
|
|
|
|
expect(errorSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
2018-05-24 23:04:41 +08:00
|
|
|
|
|
|
|
it('ok only', () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
open({ okCancel: false });
|
|
|
|
expect($$('.ant-btn')).toHaveLength(1);
|
|
|
|
expect($$('.ant-btn')[0].innerHTML).toContain('OK');
|
|
|
|
});
|
2018-12-09 17:34:27 +08:00
|
|
|
|
|
|
|
it('allows extra props on buttons', () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
open({
|
|
|
|
okButtonProps: { props: { disabled: true } },
|
|
|
|
cancelButtonProps: { attrs: { '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);
|
|
|
|
});
|
2018-12-09 17:34:27 +08:00
|
|
|
|
2018-09-05 21:28:54 +08:00
|
|
|
it('trigger onCancel once when click on cancel button', () => {
|
|
|
|
jest.useFakeTimers();
|
2019-01-12 11:33:27 +08:00
|
|
|
['info', 'success', 'warning', 'error'].forEach(async type => {
|
2018-09-05 21:28:54 +08:00
|
|
|
Modal[type]({
|
|
|
|
title: 'title',
|
|
|
|
content: 'content',
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
|
|
|
$$('.ant-btn')[0].click();
|
|
|
|
jest.runAllTimers();
|
2018-09-05 21:28:54 +08:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
});
|