2017-03-17 11:47:36 +08:00
|
|
|
import Modal from '..';
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|