2019-11-08 20:03:27 +08:00
|
|
|
import React from 'react';
|
2022-08-08 22:43:39 +08:00
|
|
|
import { UserOutlined } from '@ant-design/icons';
|
|
|
|
import notification, { getInstance, type NotificationInstance } from '..';
|
2022-08-08 13:31:55 +08:00
|
|
|
import { sleep, act } from '../../../tests/utils';
|
2022-06-22 14:57:09 +08:00
|
|
|
import ConfigProvider from '../../config-provider';
|
2022-04-14 18:09:19 +08:00
|
|
|
|
2022-08-08 22:43:39 +08:00
|
|
|
Object.defineProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT', {
|
|
|
|
writable: true,
|
|
|
|
value: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
type NotificationWithIconType = keyof Omit<NotificationInstance, 'open'>;
|
2017-03-19 02:04:32 +08:00
|
|
|
|
|
|
|
describe('notification', () => {
|
2020-09-28 00:41:16 +08:00
|
|
|
beforeEach(() => {
|
2017-11-13 10:46:22 +08:00
|
|
|
jest.useFakeTimers();
|
2022-04-14 18:09:19 +08:00
|
|
|
jest.clearAllTimers();
|
2017-11-13 10:46:22 +08:00
|
|
|
});
|
|
|
|
|
2017-03-19 02:04:32 +08:00
|
|
|
afterEach(() => {
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2020-09-28 00:41:16 +08:00
|
|
|
jest.useRealTimers();
|
2017-03-19 02:04:32 +08:00
|
|
|
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
notification.destroy();
|
2020-02-23 00:02:48 +08:00
|
|
|
});
|
2022-04-14 18:09:19 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('not duplicate create holder', async () => {
|
2020-02-23 00:02:48 +08:00
|
|
|
for (let i = 0; i < 5; i += 1) {
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
prefixCls: 'additional-holder',
|
|
|
|
});
|
2020-02-23 00:02:48 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-14 18:09:19 +08:00
|
|
|
await sleep();
|
2020-02-23 00:02:48 +08:00
|
|
|
|
|
|
|
const count = document.querySelectorAll('.additional-holder').length;
|
|
|
|
expect(count).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to hide manually', async () => {
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title 1',
|
|
|
|
duration: 0,
|
|
|
|
key: '1',
|
|
|
|
});
|
|
|
|
jest.runAllTimers();
|
2017-03-19 02:04:32 +08:00
|
|
|
});
|
2022-04-14 18:09:19 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
2017-03-19 02:04:32 +08:00
|
|
|
});
|
2020-02-23 00:02:48 +08:00
|
|
|
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title 2',
|
|
|
|
duration: 0,
|
|
|
|
key: '2',
|
|
|
|
});
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
await Promise.resolve();
|
|
|
|
});
|
2017-03-19 02:04:32 +08:00
|
|
|
expect(document.querySelectorAll('.ant-notification-notice').length).toBe(2);
|
2020-09-28 00:41:16 +08:00
|
|
|
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
notification.close('1');
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
await Promise.resolve();
|
|
|
|
});
|
2022-08-08 22:43:39 +08:00
|
|
|
expect((await getInstance('ant-notification-topRight'))!.component.state.notices).toHaveLength(
|
2020-09-28 00:41:16 +08:00
|
|
|
1,
|
|
|
|
);
|
|
|
|
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
notification.close('2');
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
await Promise.resolve();
|
|
|
|
});
|
|
|
|
|
2022-08-08 22:43:39 +08:00
|
|
|
expect((await getInstance('ant-notification-topRight'))!.component.state.notices).toHaveLength(
|
2020-09-28 00:41:16 +08:00
|
|
|
0,
|
|
|
|
);
|
2017-03-19 02:04:32 +08:00
|
|
|
});
|
|
|
|
|
2020-02-23 00:02:48 +08:00
|
|
|
it('should be able to destroy globally', async () => {
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
});
|
2017-03-19 02:04:32 +08:00
|
|
|
});
|
2022-04-14 18:09:19 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
await Promise.resolve();
|
2017-03-19 02:04:32 +08:00
|
|
|
});
|
2022-04-14 18:09:19 +08:00
|
|
|
|
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);
|
2022-04-14 18:09:19 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
notification.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
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);
|
|
|
|
});
|
2017-05-08 16:57:43 +08:00
|
|
|
|
2017-05-17 10:24:16 +08:00
|
|
|
it('should be able to destroy after config', () => {
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
notification.config({
|
|
|
|
bottom: 100,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
notification.destroy();
|
2017-05-17 10:24:16 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-13 15:45:49 +08:00
|
|
|
it('should be able to config rtl', () => {
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
notification.config({
|
|
|
|
rtl: true,
|
|
|
|
});
|
2020-04-13 15:45:49 +08:00
|
|
|
});
|
2022-04-14 18:09:19 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'whatever',
|
|
|
|
});
|
2020-04-13 15:45:49 +08:00
|
|
|
});
|
2022-04-14 18:09:19 +08:00
|
|
|
|
2020-04-13 15:45:49 +08:00
|
|
|
expect(document.querySelectorAll('.ant-notification-rtl').length).toBe(1);
|
|
|
|
});
|
|
|
|
|
2021-02-09 21:49:15 +08:00
|
|
|
it('should be able to global config rootPrefixCls', () => {
|
2021-06-09 15:36:59 +08:00
|
|
|
ConfigProvider.config({ prefixCls: 'prefix-test', iconPrefixCls: 'bamboo' });
|
2022-04-14 18:09:19 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
notification.success({ message: 'Notification Title', duration: 0 });
|
|
|
|
});
|
|
|
|
|
2021-06-09 15:36:59 +08:00
|
|
|
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);
|
2022-08-08 22:43:39 +08:00
|
|
|
ConfigProvider.config({ prefixCls: 'ant', iconPrefixCls: '' });
|
2021-02-09 21:49:15 +08:00
|
|
|
});
|
|
|
|
|
2020-05-21 21:15:53 +08:00
|
|
|
it('should be able to config prefixCls', () => {
|
|
|
|
notification.config({
|
|
|
|
prefixCls: 'prefix-test',
|
|
|
|
});
|
2022-04-14 18:09:19 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
});
|
2020-05-21 21:15:53 +08:00
|
|
|
});
|
2022-04-14 18:09:19 +08:00
|
|
|
|
2021-06-09 15:36:59 +08:00
|
|
|
expect(document.querySelectorAll('.ant-notification-notice')).toHaveLength(0);
|
|
|
|
expect(document.querySelectorAll('.prefix-test-notice')).toHaveLength(1);
|
2022-04-14 18:09:19 +08:00
|
|
|
|
2020-05-21 21:15:53 +08:00
|
|
|
notification.config({
|
2021-02-09 21:49:15 +08:00
|
|
|
prefixCls: '',
|
2020-05-21 21:15:53 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-02-23 00:02:48 +08:00
|
|
|
it('should be able to open with icon', async () => {
|
2022-04-14 18:09:19 +08:00
|
|
|
const iconPrefix = '.ant-notification-notice-icon';
|
|
|
|
|
2022-08-08 22:43:39 +08:00
|
|
|
const openNotificationWithIcon = async (type: NotificationWithIconType) => {
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
notification[type]({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
description: 'This is the content of the notification.',
|
|
|
|
});
|
|
|
|
jest.runAllTimers();
|
2017-05-08 16:57:43 +08:00
|
|
|
});
|
|
|
|
};
|
2020-02-23 00:02:48 +08:00
|
|
|
|
2022-08-08 22:43:39 +08:00
|
|
|
const list: Array<NotificationWithIconType> = ['success', 'info', 'warning', 'error'];
|
2022-04-14 18:09:19 +08:00
|
|
|
|
|
|
|
const promises = list.map(type => openNotificationWithIcon(type));
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
await Promise.all(promises);
|
|
|
|
});
|
2020-02-23 00:02:48 +08:00
|
|
|
|
2022-04-14 18:09:19 +08:00
|
|
|
list.forEach(type => {
|
|
|
|
expect(document.querySelectorAll(`${iconPrefix}-${type}`).length).toBe(1);
|
|
|
|
});
|
2017-05-08 16:57:43 +08:00
|
|
|
});
|
2018-10-18 10:16:56 +08:00
|
|
|
|
2021-03-07 20:48:00 +08:00
|
|
|
it('should be able to add parent class for different notification types', async () => {
|
2022-08-08 22:43:39 +08:00
|
|
|
const openNotificationWithIcon = async (type: NotificationWithIconType) => {
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
notification[type]({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
description: 'This is the content of the notification.',
|
|
|
|
});
|
|
|
|
jest.runAllTimers();
|
2021-03-07 20:48:00 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-08-08 22:43:39 +08:00
|
|
|
const list: Array<NotificationWithIconType> = ['success', 'info', 'warning', 'error'];
|
2022-04-14 18:09:19 +08:00
|
|
|
const promises = list.map(type => openNotificationWithIcon(type));
|
2021-03-07 20:48:00 +08:00
|
|
|
|
2022-04-14 18:09:19 +08:00
|
|
|
await act(async () => {
|
|
|
|
await Promise.all(promises);
|
|
|
|
});
|
|
|
|
|
|
|
|
list.forEach(type => {
|
|
|
|
expect(document.querySelectorAll(`.ant-notification-notice-${type}`).length).toBe(1);
|
|
|
|
});
|
2021-03-07 20:48:00 +08:00
|
|
|
});
|
|
|
|
|
2018-10-18 10:16:56 +08:00
|
|
|
it('trigger onClick', () => {
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
});
|
2018-10-18 10:16:56 +08:00
|
|
|
});
|
2022-04-14 18:09:19 +08:00
|
|
|
|
2018-10-18 10:16:56 +08:00
|
|
|
expect(document.querySelectorAll('.ant-notification').length).toBe(1);
|
|
|
|
});
|
2019-11-08 20:03:27 +08:00
|
|
|
|
|
|
|
it('support closeIcon', () => {
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
closeIcon: <span className="test-customize-icon" />,
|
|
|
|
});
|
2019-11-08 20:03:27 +08:00
|
|
|
});
|
2022-04-14 18:09:19 +08:00
|
|
|
|
2019-12-01 21:46:17 +08:00
|
|
|
expect(document.querySelectorAll('.test-customize-icon').length).toBe(1);
|
2019-11-08 20:03:27 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('support config closeIcon', () => {
|
|
|
|
notification.config({
|
2019-12-01 21:46:17 +08:00
|
|
|
closeIcon: <span className="test-customize-icon" />,
|
2019-11-08 20:03:27 +08:00
|
|
|
});
|
2022-04-14 18:09:19 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
closeIcon: <span className="test-customize-icon" />,
|
|
|
|
});
|
2019-11-08 20:03:27 +08:00
|
|
|
});
|
2022-04-14 18:09:19 +08:00
|
|
|
|
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
|
|
|
});
|
2020-06-07 21:47:07 +08:00
|
|
|
|
2021-10-02 14:14:31 +08:00
|
|
|
it('closeIcon should be update', async () => {
|
2022-08-08 22:43:39 +08:00
|
|
|
const openNotificationWithCloseIcon = async (type: '1' | '2') => {
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
closeIcon: <span className={`test-customize-icon-${type}`} />,
|
|
|
|
});
|
|
|
|
jest.runAllTimers();
|
2021-10-02 14:14:31 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-08-08 22:43:39 +08:00
|
|
|
const list: Array<'1' | '2'> = ['1', '2'];
|
2022-04-14 18:09:19 +08:00
|
|
|
const promises = list.map(type => openNotificationWithCloseIcon(type));
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
await Promise.all(promises);
|
|
|
|
});
|
2021-10-02 14:14:31 +08:00
|
|
|
|
2022-04-14 18:09:19 +08:00
|
|
|
list.forEach(type => {
|
|
|
|
expect(document.querySelectorAll(`.test-customize-icon-${type}`).length).toBe(1);
|
|
|
|
});
|
2021-10-02 14:14:31 +08:00
|
|
|
});
|
|
|
|
|
2020-06-07 21:47:07 +08:00
|
|
|
it('support config duration', () => {
|
|
|
|
notification.config({
|
|
|
|
duration: 0,
|
|
|
|
});
|
2022-04-14 18:09:19 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'whatever',
|
|
|
|
});
|
2020-06-07 21:47:07 +08:00
|
|
|
});
|
2022-04-14 18:09:19 +08:00
|
|
|
|
2020-06-07 21:47:07 +08:00
|
|
|
expect(document.querySelectorAll('.ant-notification').length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('support icon', () => {
|
2022-04-14 18:09:19 +08:00
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
icon: <UserOutlined />,
|
|
|
|
});
|
2020-06-07 21:47:07 +08:00
|
|
|
});
|
2022-04-14 18:09:19 +08:00
|
|
|
|
2020-06-07 21:47:07 +08:00
|
|
|
expect(document.querySelectorAll('.anticon-user').length).toBe(1);
|
|
|
|
});
|
2022-10-06 19:16:57 +08:00
|
|
|
|
|
|
|
it('support props', () => {
|
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
props: { 'data-testid': 'test-notification' },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(document.querySelectorAll("[data-testid='test-notification']").length).toBe(1);
|
|
|
|
});
|
2017-03-19 02:04:32 +08:00
|
|
|
});
|