ant-design/components/notification/__tests__/index.test.js

228 lines
6.6 KiB
JavaScript
Raw Normal View History

import React from 'react';
import ReactDOM from 'react-dom';
import { UserOutlined } from '@ant-design/icons';
import notification, { getInstance } from '..';
import ConfigProvider from '../../config-provider';
2017-03-19 02:04:32 +08:00
describe('notification', () => {
beforeEach(() => {
2017-11-13 10:46:22 +08:00
jest.useFakeTimers();
});
2017-03-19 02:04:32 +08:00
afterEach(() => {
jest.useRealTimers();
2017-03-19 02:04:32 +08:00
notification.destroy();
});
it('not duplicate create holder', () => {
const originRender = ReactDOM.render;
const argsList = [];
const spyRender = jest.spyOn(ReactDOM, 'render').mockImplementation((...args) => {
argsList.push(args);
});
for (let i = 0; i < 5; i += 1) {
notification.open({
message: 'Notification Title',
duration: 0,
prefixCls: 'additional-holder',
});
}
argsList.forEach(args => {
originRender(...args);
});
const count = document.querySelectorAll('.additional-holder').length;
expect(count).toEqual(1);
spyRender.mockRestore();
});
it('should be able to hide manually', async () => {
2017-03-19 02:04:32 +08:00
notification.open({
message: 'Notification Title',
duration: 0,
key: '1',
});
notification.open({
message: 'Notification Title',
duration: 0,
key: '2',
});
await Promise.resolve();
2017-03-19 02:04:32 +08:00
expect(document.querySelectorAll('.ant-notification-notice').length).toBe(2);
2017-03-19 02:04:32 +08:00
notification.close('1');
2017-10-20 15:10:53 +08:00
jest.runAllTimers();
expect((await getInstance('ant-notification-topRight')).component.state.notices).toHaveLength(
1,
);
2017-03-19 02:04:32 +08:00
notification.close('2');
2017-10-20 15:10:53 +08:00
jest.runAllTimers();
expect((await getInstance('ant-notification-topRight')).component.state.notices).toHaveLength(
0,
);
2017-03-19 02:04:32 +08:00
});
it('should be able to destroy globally', async () => {
2017-03-19 02:04:32 +08:00
notification.open({
message: 'Notification Title',
duration: 0,
});
notification.open({
message: 'Notification Title',
duration: 0,
});
await Promise.resolve();
2017-03-19 02:04:32 +08:00
expect(document.querySelectorAll('.ant-notification').length).toBe(1);
expect(document.querySelectorAll('.ant-notification-notice').length).toBe(2);
notification.destroy();
await Promise.resolve();
2017-03-19 02:04:32 +08:00
expect(document.querySelectorAll('.ant-notification').length).toBe(0);
expect(document.querySelectorAll('.ant-notification-notice').length).toBe(0);
});
it('should be able to destroy after config', () => {
notification.config({
bottom: 100,
});
notification.destroy();
});
it('should be able to config rtl', () => {
notification.config({
rtl: true,
});
notification.open({
message: 'whatever',
});
expect(document.querySelectorAll('.ant-notification-rtl').length).toBe(1);
});
it('should be able to global config rootPrefixCls', () => {
ConfigProvider.config({ prefixCls: 'prefix-test', iconPrefixCls: 'bamboo' });
notification.success({ message: 'Notification Title', duration: 0 });
expect(document.querySelectorAll('.ant-notification-notice')).toHaveLength(0);
expect(document.querySelectorAll('.prefix-test-notification-notice')).toHaveLength(1);
expect(document.querySelectorAll('.bamboo-check-circle')).toHaveLength(1);
ConfigProvider.config({ prefixCls: 'ant', iconPrefixCls: null });
});
it('should be able to config prefixCls', () => {
notification.config({
prefixCls: 'prefix-test',
});
notification.open({
message: 'Notification Title',
duration: 0,
});
expect(document.querySelectorAll('.ant-notification-notice')).toHaveLength(0);
expect(document.querySelectorAll('.prefix-test-notice')).toHaveLength(1);
notification.config({
prefixCls: '',
});
});
it('should be able to open with icon', async () => {
const openNotificationWithIcon = async type => {
const iconPrefix = '.ant-notification-notice-icon';
notification[type]({
message: 'Notification Title',
duration: 0,
description: 'This is the content of the notification.',
});
await Promise.resolve();
expect(document.querySelectorAll(`${iconPrefix}-${type}`).length).toBe(1);
};
const promises = ['success', 'info', 'warning', 'error'].map(type =>
openNotificationWithIcon(type),
);
await Promise.all(promises);
});
it('should be able to add parent class for different notification types', async () => {
const openNotificationWithIcon = async type => {
notification[type]({
message: 'Notification Title',
duration: 0,
description: 'This is the content of the notification.',
});
await Promise.resolve();
expect(document.querySelectorAll(`.ant-notification-notice-${type}`).length).toBe(1);
};
const promises = ['success', 'info', 'warning', 'error'].map(type =>
openNotificationWithIcon(type),
);
await Promise.all(promises);
});
it('trigger onClick', () => {
notification.open({
message: 'Notification Title',
duration: 0,
});
expect(document.querySelectorAll('.ant-notification').length).toBe(1);
});
it('support closeIcon', () => {
notification.open({
message: 'Notification Title',
duration: 0,
2019-12-01 21:46:17 +08:00
closeIcon: <span className="test-customize-icon" />,
});
2019-12-01 21:46:17 +08:00
expect(document.querySelectorAll('.test-customize-icon').length).toBe(1);
});
it('support config closeIcon', () => {
notification.config({
2019-12-01 21:46:17 +08:00
closeIcon: <span className="test-customize-icon" />,
});
notification.open({
message: 'Notification Title',
duration: 0,
2019-12-01 21:46:17 +08:00
closeIcon: <span className="test-customize-icon" />,
});
2019-12-01 21:46:17 +08:00
expect(document.querySelectorAll('.test-customize-icon').length).toBe(1);
2019-12-01 15:29:53 +08:00
});
it('closeIcon should be update', async () => {
const openNotificationWithCloseIcon = async type => {
notification.open({
message: 'Notification Title',
closeIcon: <span className={`test-customize-icon-${type}`} />,
});
await Promise.resolve();
expect(document.querySelectorAll(`.test-customize-icon-${type}`).length).toBe(1);
};
const promises = ['1', '2'].map(type => openNotificationWithCloseIcon(type));
await Promise.all(promises);
});
it('support config duration', () => {
notification.config({
duration: 0,
});
notification.open({
message: 'whatever',
});
expect(document.querySelectorAll('.ant-notification').length).toBe(1);
});
it('support icon', () => {
notification.open({
message: 'Notification Title',
duration: 0,
icon: <UserOutlined />,
});
expect(document.querySelectorAll('.anticon-user').length).toBe(1);
});
2017-03-19 02:04:32 +08:00
});