2017-11-18 15:26:02 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
2017-03-19 02:07:39 +08:00
|
|
|
import message from '..';
|
2018-08-31 12:28:09 +08:00
|
|
|
import Icon from '../../icon';
|
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(() => {
|
|
|
|
message.destroy();
|
2017-11-18 15:26:02 +08:00
|
|
|
jest.useRealTimers();
|
2017-03-19 02:07:39 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to config top', () => {
|
|
|
|
message.config({
|
|
|
|
top: 100,
|
|
|
|
});
|
|
|
|
message.info('whatever');
|
|
|
|
expect(document.querySelectorAll('.ant-message')[0].style.top).toBe('100px');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to config getContainer', () => {
|
|
|
|
message.config({
|
|
|
|
getContainer: () => {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.className = 'custom-container';
|
|
|
|
document.body.appendChild(div);
|
|
|
|
return div;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
message.info('whatever');
|
|
|
|
expect(document.querySelectorAll('.custom-container').length).toBe(1);
|
|
|
|
});
|
|
|
|
|
2018-04-20 15:40:43 +08:00
|
|
|
it('should be able to config maxCount', () => {
|
|
|
|
message.config({
|
|
|
|
maxCount: 5,
|
|
|
|
});
|
|
|
|
for (let i = 0; i < 10; i += 1) {
|
|
|
|
message.info('test');
|
|
|
|
}
|
|
|
|
message.info('last');
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(5);
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice')[4].textContent).toBe('last');
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0);
|
|
|
|
});
|
|
|
|
|
2017-11-16 17:12:36 +08:00
|
|
|
it('should be able to hide manually', () => {
|
2017-03-19 02:07:39 +08:00
|
|
|
const hide1 = message.info('whatever', 0);
|
|
|
|
const hide2 = message.info('whatever', 0);
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(2);
|
|
|
|
hide1();
|
2017-11-16 17:12:36 +08:00
|
|
|
jest.runAllTimers();
|
2017-03-19 02:07:39 +08:00
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(1);
|
|
|
|
hide2();
|
2017-11-16 17:12:36 +08:00
|
|
|
jest.runAllTimers();
|
2017-03-19 02:07:39 +08:00
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to destroy globally', () => {
|
|
|
|
message.info('whatever', 0);
|
|
|
|
message.info('whatever', 0);
|
|
|
|
expect(document.querySelectorAll('.ant-message').length).toBe(1);
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(2);
|
|
|
|
message.destroy();
|
|
|
|
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', () => {});
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-05-25 20:41:07 +08:00
|
|
|
it('should be called like promise', () => {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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() {
|
|
|
|
hide = message.loading('Action in progress..', 0);
|
|
|
|
}
|
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);
|
|
|
|
hide();
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0);
|
|
|
|
});
|
2017-11-22 14:43:30 +08:00
|
|
|
|
2018-08-31 12:28:09 +08:00
|
|
|
it('should allow custom icon', () => {
|
|
|
|
message.open({ content: 'Message', icon: <Icon type="smile-o" /> });
|
|
|
|
expect(document.querySelectorAll('.anticon-smile-o').length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have no icon', () => {
|
|
|
|
message.open({ content: 'Message' });
|
|
|
|
expect(document.querySelectorAll('.ant-message-notice .anticon').length).toBe(0);
|
|
|
|
});
|
|
|
|
|
2017-11-22 14:43:30 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/8201
|
|
|
|
it('should destroy messages correctly', () => {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
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);
|
|
|
|
});
|
2017-03-19 02:07:39 +08:00
|
|
|
});
|