2022-06-22 14:57:09 +08:00
|
|
|
import { SmileOutlined } from '@ant-design/icons';
|
2017-11-18 15:26:02 +08:00
|
|
|
import { mount } from 'enzyme';
|
2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
2022-04-14 18:09:19 +08:00
|
|
|
import { act } from 'react-dom/test-utils';
|
2020-09-28 00:41:16 +08:00
|
|
|
import message, { getInstance } from '..';
|
2017-03-19 02:07:39 +08:00
|
|
|
|
2022-04-14 18:09:19 +08:00
|
|
|
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
|
|
|
|
|
2017-03-19 02:07:39 +08:00
|
|
|
describe('message', () => {
|
2017-11-18 15:26:02 +08:00
|
|
|
beforeEach(() => {
|
2017-11-16 17:12:36 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
2017-03-19 02:07:39 +08:00
|
|
|
afterEach(() => {
|
2017-11-18 15:26:02 +08:00
|
|
|
jest.useRealTimers();
|
2022-04-14 18:09:19 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
message.destroy();
|
|
|
|
});
|
2017-03-19 02:07:39 +08:00
|
|
|
});
|
|
|
|
|
2022-04-14 18:09:19 +08:00
|
|
|
it('should be able to hide manually', async () => {
|
|
|
|
let hide1;
|
|
|
|
let hide2;
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
hide1 = message.info('whatever', 0);
|
|
|
|
});
|
|
|
|
act(() => {
|
|
|
|
hide2 = message.info('whatever', 0);
|
|
|
|
});
|
|
|
|
|
2017-03-19 02:07:39 +08:00
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(2);
|
2022-04-14 18:09:19 +08:00
|
|
|
|
2017-03-19 02:07:39 +08:00
|
|
|
hide1();
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2020-09-28 00:41:16 +08:00
|
|
|
expect(getInstance().component.state.notices).toHaveLength(1);
|
2022-04-14 18:09:19 +08:00
|
|
|
|
2017-03-19 02:07:39 +08:00
|
|
|
hide2();
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2020-09-28 00:41:16 +08:00
|
|
|
expect(getInstance().component.state.notices).toHaveLength(0);
|
2017-03-19 02:07:39 +08:00
|
|
|
});
|
|
|
|
|
2020-08-06 13:34:18 +08:00
|
|
|
it('should be able to remove manually with a unique key', () => {
|
|
|
|
const key1 = 'key1';
|
|
|
|
const key2 = 'key2';
|
2022-04-14 18:09:19 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
message.info({ content: 'Message1', key: 'key1', duration: 0 });
|
|
|
|
});
|
|
|
|
act(() => {
|
|
|
|
message.info({ content: 'Message2', key: 'key2', duration: 0 });
|
|
|
|
});
|
|
|
|
|
2020-08-06 13:34:18 +08:00
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(2);
|
2022-04-14 18:09:19 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
message.destroy(key1);
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2020-09-28 00:41:16 +08:00
|
|
|
expect(getInstance().component.state.notices).toHaveLength(1);
|
2022-04-14 18:09:19 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
message.destroy(key2);
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2020-09-28 00:41:16 +08:00
|
|
|
expect(getInstance().component.state.notices).toHaveLength(0);
|
2020-08-06 13:34:18 +08:00
|
|
|
});
|
|
|
|
|
2017-03-19 02:07:39 +08:00
|
|
|
it('should be able to destroy globally', () => {
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
message.info('whatever', 0);
|
|
|
|
});
|
|
|
|
act(() => {
|
|
|
|
message.info('whatever', 0);
|
|
|
|
});
|
|
|
|
|
2017-03-19 02:07:39 +08:00
|
|
|
expect(document.querySelectorAll('.ant-message').length).toBe(1);
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(2);
|
2022-04-14 18:09:19 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
message.destroy();
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2017-03-19 02:07:39 +08:00
|
|
|
expect(document.querySelectorAll('.ant-message').length).toBe(0);
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0);
|
|
|
|
});
|
2017-10-14 14:28:57 +08:00
|
|
|
|
|
|
|
it('should not need to use duration argument when using the onClose arguments', () => {
|
|
|
|
message.info('whatever', () => {});
|
|
|
|
});
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
it('should have the default duration when using the onClose arguments', done => {
|
2017-11-16 19:58:20 +08:00
|
|
|
jest.useRealTimers();
|
2017-10-14 14:28:57 +08:00
|
|
|
const defaultDuration = 3;
|
|
|
|
const now = Date.now();
|
|
|
|
message.info('whatever', () => {
|
|
|
|
// calculate the approximately duration value
|
|
|
|
const aboutDuration = parseInt((Date.now() - now) / 1000, 10);
|
|
|
|
expect(aboutDuration).toBe(defaultDuration);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-11-18 15:26:02 +08:00
|
|
|
|
2020-12-03 10:58:11 +08:00
|
|
|
it('trigger onClick method', () => {
|
|
|
|
const onClick = jest.fn();
|
|
|
|
class Test extends React.Component {
|
|
|
|
componentDidMount() {
|
|
|
|
message.info({
|
2020-12-07 17:08:48 +08:00
|
|
|
onClick,
|
2020-12-03 10:58:11 +08:00
|
|
|
duration: 0,
|
|
|
|
content: 'message info',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <div>test message onClick method</div>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mount(<Test />);
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(1);
|
|
|
|
document.querySelectorAll('.ant-message-notice')[0].click();
|
|
|
|
expect(onClick).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2019-04-03 15:54:26 +08:00
|
|
|
it('should be called like promise', done => {
|
2018-05-25 20:41:07 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
const defaultDuration = 3;
|
|
|
|
const now = Date.now();
|
|
|
|
message.info('whatever').then(() => {
|
|
|
|
// calculate the approximately duration value
|
|
|
|
const aboutDuration = parseInt((Date.now() - now) / 1000, 10);
|
|
|
|
expect(aboutDuration).toBe(defaultDuration);
|
2019-04-03 15:54:26 +08:00
|
|
|
done();
|
2018-05-25 20:41:07 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-18 15:26:02 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/8201
|
|
|
|
it('should hide message correctly', () => {
|
|
|
|
let hide;
|
|
|
|
class Test extends React.Component {
|
|
|
|
componentDidMount() {
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
hide = message.loading('Action in progress..', 0);
|
|
|
|
});
|
2017-11-18 15:26:02 +08:00
|
|
|
}
|
2018-06-22 21:05:13 +08:00
|
|
|
|
2017-11-18 15:26:02 +08:00
|
|
|
render() {
|
|
|
|
return <div>test</div>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mount(<Test />);
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(1);
|
2022-04-14 18:09:19 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
hide();
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2020-09-28 00:41:16 +08:00
|
|
|
expect(getInstance().component.state.notices).toHaveLength(0);
|
2017-11-18 15:26:02 +08:00
|
|
|
});
|
2017-11-22 14:43:30 +08:00
|
|
|
|
2018-08-31 12:28:09 +08:00
|
|
|
it('should allow custom icon', () => {
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
message.open({ content: 'Message', icon: <SmileOutlined /> });
|
|
|
|
});
|
2019-08-13 14:07:17 +08:00
|
|
|
expect(document.querySelectorAll('.anticon-smile').length).toBe(1);
|
2018-08-31 12:28:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should have no icon', () => {
|
2020-02-24 16:58:09 +08:00
|
|
|
message.open({ content: 'Message', icon: <span /> });
|
2018-08-31 12:28:09 +08:00
|
|
|
expect(document.querySelectorAll('.ant-message-notice .anticon').length).toBe(0);
|
|
|
|
});
|
2020-03-02 10:26:26 +08:00
|
|
|
it('should have no icon when not pass icon props', () => {
|
|
|
|
message.open({ content: 'Message' });
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice .anticon').length).toBe(0);
|
|
|
|
});
|
2018-08-31 12:28:09 +08:00
|
|
|
|
2017-11-22 14:43:30 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/8201
|
|
|
|
it('should destroy messages correctly', () => {
|
|
|
|
class Test extends React.Component {
|
|
|
|
componentDidMount() {
|
|
|
|
message.loading('Action in progress1..', 0);
|
|
|
|
message.loading('Action in progress2..', 0);
|
|
|
|
setTimeout(() => message.destroy(), 1000);
|
|
|
|
}
|
2018-06-22 21:05:13 +08:00
|
|
|
|
2017-11-22 14:43:30 +08:00
|
|
|
render() {
|
|
|
|
return <div>test</div>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mount(<Test />);
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(2);
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0);
|
|
|
|
});
|
2019-09-05 10:15:14 +08:00
|
|
|
|
|
|
|
it('should support update message content with a unique key', () => {
|
|
|
|
const key = 'updatable';
|
|
|
|
class Test extends React.Component {
|
|
|
|
componentDidMount() {
|
|
|
|
message.loading({ content: 'Loading...', key });
|
|
|
|
// Testing that content of the message should be updated.
|
|
|
|
setTimeout(() => message.success({ content: 'Loaded', key }), 1000);
|
|
|
|
setTimeout(() => message.destroy(), 3000);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <div>test</div>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mount(<Test />);
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(1);
|
|
|
|
jest.advanceTimersByTime(1500);
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(1);
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0);
|
|
|
|
});
|
2019-11-28 10:01:43 +08:00
|
|
|
|
|
|
|
it('update message content with a unique key and cancel manually', () => {
|
|
|
|
const key = 'updatable';
|
|
|
|
class Test extends React.Component {
|
|
|
|
componentDidMount() {
|
2022-04-14 18:09:19 +08:00
|
|
|
let hideLoading;
|
|
|
|
act(() => {
|
|
|
|
hideLoading = message.loading({ content: 'Loading...', key, duration: 0 });
|
|
|
|
});
|
2019-11-28 10:01:43 +08:00
|
|
|
// Testing that content of the message should be cancel manually.
|
2022-04-14 18:09:19 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
act(() => {
|
|
|
|
hideLoading();
|
|
|
|
});
|
|
|
|
}, 1000);
|
2019-11-28 10:01:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <div>test</div>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mount(<Test />);
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(1);
|
2022-04-14 18:09:19 +08:00
|
|
|
|
2019-11-28 10:01:43 +08:00
|
|
|
jest.advanceTimersByTime(1500);
|
2020-09-28 00:41:16 +08:00
|
|
|
expect(getInstance().component.state.notices).toHaveLength(0);
|
2019-11-28 10:01:43 +08:00
|
|
|
});
|
2019-12-30 22:14:17 +08:00
|
|
|
|
|
|
|
it('should not throw error when pass null', () => {
|
|
|
|
message.error(null);
|
|
|
|
});
|
2017-03-19 02:07:39 +08:00
|
|
|
});
|