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
|
|
|
});
|
2017-12-02 22:10:27 +08:00
|
|
|
|
|
|
|
if (process.env.REACT !== '15') {
|
|
|
|
it('shows animation when close', () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
open();
|
|
|
|
$$('.ant-btn')[0].click();
|
2018-10-12 14:04:26 +08:00
|
|
|
expect($$('.ant-modal-confirm')).toHaveLength(1);
|
2017-12-02 22:10:27 +08:00
|
|
|
jest.runAllTimers();
|
2018-10-12 14:04:26 +08:00
|
|
|
expect($$('.ant-modal-confirm')).toHaveLength(0);
|
2017-12-02 22:10:27 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2018-05-25 20:59:17 +08:00
|
|
|
it('trigger onCancel once when click on cancel button', () => {
|
|
|
|
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
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2017-03-17 11:47:36 +08:00
|
|
|
});
|