2022-05-11 14:26:18 +08:00
|
|
|
import { UserOutlined } from '@ant-design/icons';
|
2023-06-07 11:54:50 +08:00
|
|
|
import React from 'react';
|
2022-05-11 14:26:18 +08:00
|
|
|
import notification, { actWrapper } from '..';
|
|
|
|
import { act, fireEvent } from '../../../tests/utils';
|
2023-06-07 11:54:50 +08:00
|
|
|
import ConfigProvider from '../../config-provider';
|
2022-05-11 14:26:18 +08:00
|
|
|
import { awaitPromise, triggerMotionEnd } from './util';
|
|
|
|
|
|
|
|
describe('notification', () => {
|
|
|
|
beforeAll(() => {
|
|
|
|
actWrapper(act);
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
2022-05-11 14:26:18 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
// Clean up
|
|
|
|
notification.destroy();
|
|
|
|
await triggerMotionEnd();
|
|
|
|
|
|
|
|
notification.config({
|
2023-08-27 11:06:04 +08:00
|
|
|
prefixCls: undefined,
|
|
|
|
getContainer: undefined,
|
2022-05-11 14:26:18 +08:00
|
|
|
});
|
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
2022-05-11 14:26:18 +08:00
|
|
|
|
|
|
|
await awaitPromise();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('not duplicate create holder', async () => {
|
|
|
|
notification.config({
|
|
|
|
prefixCls: 'additional-holder',
|
|
|
|
});
|
|
|
|
|
|
|
|
for (let i = 0; i < 5; i += 1) {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await awaitPromise();
|
|
|
|
|
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2022-05-11 14:26:18 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(document.querySelectorAll('.additional-holder')).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to hide manually', async () => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title 1',
|
|
|
|
duration: 0,
|
|
|
|
key: '1',
|
|
|
|
});
|
|
|
|
await awaitPromise();
|
|
|
|
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title 2',
|
|
|
|
duration: 0,
|
|
|
|
key: '2',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(document.querySelectorAll('.ant-notification-notice')).toHaveLength(2);
|
|
|
|
|
|
|
|
// Close 1
|
|
|
|
notification.destroy('1');
|
|
|
|
|
|
|
|
await triggerMotionEnd();
|
|
|
|
|
|
|
|
expect(document.querySelectorAll('.ant-notification-notice')).toHaveLength(1);
|
|
|
|
|
|
|
|
// Close 2
|
|
|
|
notification.destroy('2');
|
|
|
|
|
|
|
|
await triggerMotionEnd();
|
|
|
|
|
|
|
|
expect(document.querySelectorAll('.ant-notification-notice')).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to destroy globally', async () => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title 1',
|
|
|
|
duration: 0,
|
|
|
|
});
|
|
|
|
await awaitPromise();
|
|
|
|
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title 2',
|
|
|
|
duration: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(document.querySelectorAll('.ant-notification')).toHaveLength(1);
|
|
|
|
expect(document.querySelectorAll('.ant-notification-notice')).toHaveLength(2);
|
|
|
|
|
|
|
|
notification.destroy();
|
|
|
|
|
|
|
|
await triggerMotionEnd();
|
|
|
|
|
|
|
|
expect(document.querySelectorAll('.ant-notification')).toHaveLength(0);
|
|
|
|
expect(document.querySelectorAll('.ant-notification-notice')).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to destroy after config', () => {
|
|
|
|
notification.config({
|
|
|
|
bottom: 100,
|
|
|
|
});
|
|
|
|
|
|
|
|
notification.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to config rtl', async () => {
|
|
|
|
notification.config({
|
|
|
|
rtl: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
notification.open({
|
|
|
|
message: 'whatever',
|
|
|
|
});
|
|
|
|
await awaitPromise();
|
|
|
|
|
|
|
|
expect(document.querySelectorAll('.ant-notification-rtl')).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to global config rootPrefixCls', async () => {
|
|
|
|
ConfigProvider.config({ prefixCls: 'prefix-test', iconPrefixCls: 'bamboo' });
|
|
|
|
|
|
|
|
notification.success({ message: 'Notification Title', duration: 0 });
|
|
|
|
await awaitPromise();
|
|
|
|
|
|
|
|
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', async () => {
|
|
|
|
notification.config({
|
|
|
|
prefixCls: 'prefix-test',
|
|
|
|
});
|
|
|
|
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
});
|
|
|
|
await awaitPromise();
|
|
|
|
|
|
|
|
expect(document.querySelectorAll('.ant-notification-notice')).toHaveLength(0);
|
|
|
|
expect(document.querySelectorAll('.prefix-test-notice')).toHaveLength(1);
|
|
|
|
|
|
|
|
notification.config({
|
2023-08-27 11:06:04 +08:00
|
|
|
prefixCls: undefined,
|
2022-05-11 14:26:18 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to open with icon', async () => {
|
|
|
|
const iconPrefix = '.ant-notification-notice-icon';
|
|
|
|
|
|
|
|
const list = ['success', 'info', 'warning', 'error'] as const;
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
list.forEach((type) => {
|
2022-05-11 14:26:18 +08:00
|
|
|
notification[type]({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
description: 'This is the content of the notification.',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
await awaitPromise();
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
list.forEach((type) => {
|
2022-05-11 14:26:18 +08:00
|
|
|
expect(document.querySelectorAll(`${iconPrefix}-${type}`)).toHaveLength(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to add parent class for different notification types', async () => {
|
|
|
|
const list = ['success', 'info', 'warning', 'error'] as const;
|
2022-11-19 13:47:33 +08:00
|
|
|
list.forEach((type) => {
|
2022-05-11 14:26:18 +08:00
|
|
|
notification[type]({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
description: 'This is the content of the notification.',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
await awaitPromise();
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
list.forEach((type) => {
|
2022-05-11 14:26:18 +08:00
|
|
|
expect(document.querySelectorAll(`.ant-notification-notice-${type}`)).toHaveLength(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('trigger onClick', async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onClick = jest.fn();
|
2022-05-11 14:26:18 +08:00
|
|
|
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
onClick,
|
|
|
|
});
|
|
|
|
await awaitPromise();
|
|
|
|
|
|
|
|
expect(document.querySelectorAll('.ant-notification')).toHaveLength(1);
|
|
|
|
|
|
|
|
fireEvent.click(document.querySelector('.ant-notification-notice')!);
|
|
|
|
expect(onClick).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('support closeIcon', async () => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
closeIcon: <span className="test-customize-icon" />,
|
|
|
|
});
|
|
|
|
await awaitPromise();
|
|
|
|
|
|
|
|
expect(document.querySelectorAll('.test-customize-icon')).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('support config closeIcon', async () => {
|
|
|
|
notification.config({
|
|
|
|
closeIcon: <span className="test-customize-icon" />,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Global Icon
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
});
|
|
|
|
await awaitPromise();
|
|
|
|
|
|
|
|
expect(document.querySelector('.test-customize-icon')).toBeTruthy();
|
|
|
|
|
|
|
|
// Notice Icon
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
closeIcon: <span className="replace-icon" />,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(document.querySelector('.replace-icon')).toBeTruthy();
|
|
|
|
|
|
|
|
notification.config({
|
|
|
|
closeIcon: null,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-03-06 11:53:05 +08:00
|
|
|
it('support config closable', async () => {
|
|
|
|
notification.config({
|
|
|
|
closable: {
|
|
|
|
closeIcon: <span className="test-customize-icon" />,
|
|
|
|
'aria-label': 'CloseBtn',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Global Icon
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
});
|
|
|
|
await awaitPromise();
|
|
|
|
|
|
|
|
expect(document.querySelector('.test-customize-icon')).toBeTruthy();
|
|
|
|
expect(document.querySelector('*[aria-label="CloseBtn"]')).toBeTruthy();
|
|
|
|
|
|
|
|
// Notice Icon
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
closable: {
|
|
|
|
closeIcon: <span className="replace-icon" />,
|
|
|
|
'aria-label': 'CloseBtn2',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(document.querySelector('.replace-icon')).toBeTruthy();
|
|
|
|
expect(document.querySelector('*[aria-label="CloseBtn2"]')).toBeTruthy();
|
|
|
|
|
|
|
|
notification.config({
|
|
|
|
closable: undefined,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
it('closeIcon should be update', async () => {
|
|
|
|
const list = ['1', '2'];
|
2022-11-19 13:47:33 +08:00
|
|
|
list.forEach((type) => {
|
2022-05-11 14:26:18 +08:00
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
closeIcon: <span className={`test-customize-icon-${type}`} />,
|
|
|
|
duration: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
await awaitPromise();
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
list.forEach((type) => {
|
2022-05-11 14:26:18 +08:00
|
|
|
expect(document.querySelector(`.test-customize-icon-${type}`)).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('support config duration', async () => {
|
|
|
|
notification.config({
|
|
|
|
duration: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
notification.open({
|
|
|
|
message: 'whatever',
|
|
|
|
});
|
|
|
|
await awaitPromise();
|
|
|
|
|
|
|
|
expect(document.querySelector('.ant-notification')).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('support icon', async () => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
icon: <UserOutlined />,
|
|
|
|
});
|
|
|
|
await awaitPromise();
|
|
|
|
|
|
|
|
expect(document.querySelector('.anticon-user')).toBeTruthy();
|
|
|
|
});
|
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);
|
|
|
|
});
|
2023-05-19 18:21:31 +08:00
|
|
|
|
|
|
|
it('support role', async () => {
|
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
role: 'status',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(document.querySelectorAll('[role="status"]').length).toBe(1);
|
|
|
|
});
|
2023-11-06 11:29:41 +08:00
|
|
|
|
2023-06-12 14:20:10 +08:00
|
|
|
it('should hide close btn when closeIcon setting to null or false', async () => {
|
|
|
|
notification.config({
|
|
|
|
closeIcon: undefined,
|
|
|
|
});
|
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
className: 'normal',
|
|
|
|
});
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
className: 'custom',
|
|
|
|
closeIcon: <span className="custom-close-icon">Close</span>,
|
|
|
|
});
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
closeIcon: null,
|
|
|
|
className: 'with-null',
|
|
|
|
});
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
closeIcon: false,
|
|
|
|
className: 'with-false',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
await awaitPromise();
|
|
|
|
expect(document.querySelectorAll('.normal .ant-notification-notice-close').length).toBe(1);
|
|
|
|
expect(document.querySelectorAll('.custom .custom-close-icon').length).toBe(1);
|
|
|
|
expect(document.querySelectorAll('.with-null .ant-notification-notice-close').length).toBe(0);
|
|
|
|
expect(document.querySelectorAll('.with-false .ant-notification-notice-close').length).toBe(0);
|
|
|
|
});
|
2023-11-06 11:29:41 +08:00
|
|
|
|
|
|
|
it('style.width could be overrided', async () => {
|
|
|
|
act(() => {
|
|
|
|
notification.open({
|
|
|
|
message: 'Notification Title',
|
|
|
|
duration: 0,
|
|
|
|
style: {
|
|
|
|
width: 600,
|
|
|
|
},
|
|
|
|
className: 'with-style',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
await awaitPromise();
|
|
|
|
expect(document.querySelector('.with-style')).toHaveStyle({ width: '600px' });
|
|
|
|
expect(
|
|
|
|
document.querySelector('.ant-notification-notice-wrapper:has(.width-style)'),
|
|
|
|
).toHaveStyle({ width: '' });
|
|
|
|
});
|
2022-05-11 14:26:18 +08:00
|
|
|
});
|