ant-design/components/message/__tests__/index.test.js
2017-11-16 19:58:20 +08:00

75 lines
2.2 KiB
JavaScript

import message from '..';
describe('message', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
afterEach(() => {
message.destroy();
});
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);
});
it('should be able to hide manually', () => {
const hide1 = message.info('whatever', 0);
const hide2 = message.info('whatever', 0);
expect(document.querySelectorAll('.ant-message-notice').length).toBe(2);
hide1();
jest.runAllTimers();
expect(document.querySelectorAll('.ant-message-notice').length).toBe(1);
hide2();
jest.runAllTimers();
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);
});
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) => {
jest.useRealTimers();
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();
});
});
});