ant-design/components/modal/__tests__/confirm.test.js
C.J. Winslow b9a86b2ea1 Pass the ok and cancel button props in the confirm API (#12425)
This PR adds the `okButtonProps` and `cancelButtonProps` API to the `Modal.confirm` and associated methods.


* [x] Make sure that you propose PR to right branch: bugfix for `master`, feature for branch `feature`.
* [x] Make sure that you follow antd's [code convention](https://github.com/ant-design/ant-design/wiki/Code-convention-for-antd).
* [x] Run `npm run lint` and fix those errors before submitting in order to keep consistent code style.
* [x] Rebase before creating a PR to keep commit history clear.
* [x] Add some descriptions and refer relative issues for you PR.

Extra checklist:

*isNewFeature* **:**

  * [x] Update API docs for the component.
  * [x] Update/Add demo to demonstrate new feature.
  * [x] Update TypeScript definition for the component.
  * [x] Add unit tests for the feature.
2018-09-30 14:50:03 +08:00

147 lines
4.0 KiB
JavaScript

import Modal from '..';
const { confirm } = Modal;
describe('Modal.confirm triggers callbacks correctly', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
afterEach(() => {
errorSpy.mockReset();
document.body.innerHTML = '';
});
afterAll(() => {
errorSpy.mockRestore();
});
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
$$('.ant-btn-primary')[0].click();
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
$$('.ant-btn')[0].click();
expect(errorSpy).not.toHaveBeenCalled();
});
it('should allow Modal.comfirm without onOk been set', () => {
open();
// Fourth Modal
$$('.ant-btn-primary')[0].click();
expect(errorSpy).not.toHaveBeenCalled();
});
if (process.env.REACT !== '15') {
it('shows animation when close', () => {
jest.useFakeTimers();
open();
$$('.ant-btn')[0].click();
expect($$('.ant-confirm')).toHaveLength(1);
jest.runAllTimers();
expect($$('.ant-confirm')).toHaveLength(0);
jest.useRealTimers();
});
}
it('ok only', () => {
open({ okCancel: false });
expect($$('.ant-btn')).toHaveLength(1);
expect($$('.ant-btn')[0].innerHTML).toContain('OK');
});
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);
});
it('trigger onCancel once when click on cancel button', () => {
jest.useFakeTimers();
['info', 'success', 'warning', 'error'].forEach((type) => {
Modal[type]({
title: 'title',
content: 'content',
});
expect($$(`.ant-confirm-${type}`)).toHaveLength(1);
$$('.ant-btn')[0].click();
jest.runAllTimers();
expect($$(`.ant-confirm-${type}`)).toHaveLength(0);
});
jest.useRealTimers();
});
it('could be update', () => {
jest.useFakeTimers();
['info', 'success', 'warning', 'error'].forEach((type) => {
const instance = Modal[type]({
title: 'title',
content: 'content',
});
expect($$(`.ant-confirm-${type}`)).toHaveLength(1);
expect($$('.ant-confirm-title')[0].innerHTML).toBe('title');
expect($$('.ant-confirm-content')[0].innerHTML).toBe('content');
instance.update({
title: 'new title',
content: 'new content',
});
expect($$(`.ant-confirm-${type}`)).toHaveLength(1);
expect($$('.ant-confirm-title')[0].innerHTML).toBe('new title');
expect($$('.ant-confirm-content')[0].innerHTML).toBe('new content');
instance.destroy();
jest.runAllTimers();
});
jest.useRealTimers();
});
it('could be destroy', () => {
jest.useFakeTimers();
['info', 'success', 'warning', 'error'].forEach((type) => {
const instance = Modal[type]({
title: 'title',
content: 'content',
});
expect($$(`.ant-confirm-${type}`)).toHaveLength(1);
instance.destroy();
jest.runAllTimers();
expect($$(`.ant-confirm-${type}`)).toHaveLength(0);
});
jest.useRealTimers();
});
});