import { fireEvent, render } from '@testing-library/react';
import React from 'react';
import { act } from 'react-dom/test-utils';
import type { NotificationAPI, NotificationConfig, NotificationProgressProps } from '../src';
import { Notification, useNotification } from '../src';
require('../assets/index.less');
// 🔥 Note: In latest version. We remove static function.
// This only test for hooks usage.
describe('Notification.Basic', () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
function renderDemo(config?: NotificationConfig) {
let instance: NotificationAPI;
const Demo = () => {
const [api, holder] = useNotification(config);
instance = api;
return holder;
};
const renderResult = render();
return { ...renderResult, instance };
}
function step(time: number, slice = 16) {
let current = 0;
while (current < time) {
vi.advanceTimersByTime(slice);
current += slice;
}
}
function expectContentStyle(selector: string, content: string) {
expect((document.querySelector(selector) as HTMLElement).style.content).toBe(`"${content}"`);
}
it('works', () => {
const { instance, unmount } = renderDemo();
act(() => {
instance.open({
description:
1
,
duration: 0.1,
});
});
expect(document.querySelector('.test')).toBeTruthy();
act(() => {
vi.runAllTimers();
});
expect(document.querySelector('.test')).toBeFalsy();
unmount();
});
it('works with custom close icon', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
description: 1
,
closable: {
closeIcon: test-close-icon,
},
duration: 0,
});
});
expect(document.querySelectorAll('.test')).toHaveLength(1);
expect(document.querySelector('.test-icon').textContent).toEqual('test-close-icon');
});
it('works with disabled close button', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
description: 1
,
closable: false,
duration: 0,
});
});
expect(document.querySelector('.test')).toBeTruthy();
expect(document.querySelector('.rc-notification-notice-close')).toBeFalsy();
});
it('works with multi instance', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
description: 1
,
duration: 0.1,
});
});
act(() => {
instance.open({
description: 2
,
duration: 0.1,
});
});
expect(document.querySelectorAll('.test')).toHaveLength(2);
act(() => {
vi.runAllTimers();
});
expect(document.querySelectorAll('.test')).toHaveLength(0);
});
it('destroy works', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
description: (
222222
),
duration: 0.1,
});
});
expect(document.querySelector('.test')).toBeTruthy();
act(() => {
instance.destroy();
});
expect(document.querySelector('.test')).toBeFalsy();
});
it('getContainer works', () => {
const id = 'get-container-test';
const div = document.createElement('div');
div.id = id;
div.innerHTML = 'test';
document.body.appendChild(div);
const { instance } = renderDemo({
getContainer: () => document.getElementById('get-container-test'),
});
act(() => {
instance.open({
description: (
222222
),
duration: 1,
});
});
expect(document.getElementById(id).children).toHaveLength(2);
act(() => {
instance.destroy();
});
expect(document.getElementById(id).children).toHaveLength(1);
document.body.removeChild(div);
});
it('remove notify works', () => {
const { instance, unmount } = renderDemo();
const key = Math.random();
const close = (k: React.Key) => {
instance.close(k);
};
act(() => {
instance.open({
description: (
),
key,
duration: false,
});
});
expect(document.querySelectorAll('.test')).toHaveLength(1);
fireEvent.click(document.querySelector('#closeButton'));
act(() => {
vi.runAllTimers();
});
expect(document.querySelectorAll('.test')).toHaveLength(0);
unmount();
});
it('update notification by key with multi instance', () => {
const { instance } = renderDemo();
const key = 'updatable';
const value = 'value';
const newValue = `new-${value}`;
const notUpdatableValue = 'not-updatable-value';
act(() => {
instance.open({
description: (
{notUpdatableValue}
),
duration: false,
});
});
act(() => {
instance.open({
description: (
{value}
),
key,
duration: false,
});
});
expect(document.querySelectorAll('.updatable')).toHaveLength(1);
expect(document.querySelector('.updatable').textContent).toEqual(value);
act(() => {
instance.open({
description: (
{newValue}
),
key,
duration: 0.1,
});
});
// Text updated successfully
expect(document.querySelectorAll('.updatable')).toHaveLength(1);
expect(document.querySelector('.updatable').textContent).toEqual(newValue);
act(() => {
vi.runAllTimers();
});
// Other notices are not affected
expect(document.querySelectorAll('.not-updatable')).toHaveLength(1);
expect(document.querySelector('.not-updatable').textContent).toEqual(notUpdatableValue);
// Duration updated successfully
expect(document.querySelectorAll('.updatable')).toHaveLength(0);
});
it('keeps notification open when updated from positive duration to 0', () => {
const onClose = vi.fn();
const { instance } = renderDemo();
act(() => {
instance.open({
key: 'keep-open',
description: initial
,
duration: 1,
onClose,
});
});
expect(document.querySelector('.keep-open')).toHaveTextContent('initial');
act(() => {
instance.open({
key: 'keep-open',
description: persistent
,
duration: 0,
onClose,
});
});
act(() => {
vi.advanceTimersByTime(5000);
});
expect(onClose).not.toHaveBeenCalled();
expect(document.querySelector('.keep-open')).toHaveTextContent('persistent');
});
it('freeze notification layer when mouse over', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
description: (
freeze
),
duration: 0.3,
});
});
expect(document.querySelectorAll('.freeze')).toHaveLength(1);
// Mouse in should not remove
fireEvent.mouseEnter(document.querySelector('.rc-notification-notice'));
act(() => {
vi.runAllTimers();
});
expect(document.querySelectorAll('.freeze')).toHaveLength(1);
// Mouse out will remove
fireEvent.mouseLeave(document.querySelector('.rc-notification-notice'));
act(() => {
vi.runAllTimers();
});
expect(document.querySelectorAll('.freeze')).toHaveLength(0);
});
it('continue timing after hover', () => {
const { instance } = renderDemo({
duration: 1,
});
act(() => {
instance.open({
description: 1
,
});
});
expect(document.querySelector('.test')).toBeTruthy();
// Wait for 500ms
act(() => {
step(500);
});
expect(document.querySelector('.test')).toBeTruthy();
// Mouse in should not remove
fireEvent.mouseEnter(document.querySelector('.rc-notification-notice'));
act(() => {
// Elapsed time should not advance while hovering.
step(1000);
});
expect(document.querySelector('.test')).toBeTruthy();
// Mouse out should not remove until 500ms later
fireEvent.mouseLeave(document.querySelector('.rc-notification-notice'));
act(() => {
step(450);
});
expect(document.querySelector('.test')).toBeTruthy();
act(() => {
step(100);
});
expect(document.querySelector('.test')).toBeFalsy();
});
describe('pauseOnHover is false', () => {
it('does not freeze when pauseOnHover is false', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
description: (
not freeze
),
duration: 0.3,
pauseOnHover: false,
});
});
expect(document.querySelectorAll('.not-freeze')).toHaveLength(1);
// Mouse in should remove
fireEvent.mouseEnter(document.querySelector('.rc-notification-notice'));
act(() => {
vi.runAllTimers();
});
expect(document.querySelectorAll('.not-freeze')).toHaveLength(0);
});
it('continue timing after hover', () => {
const { instance } = renderDemo({
duration: 1,
pauseOnHover: false,
});
act(() => {
instance.open({
description: 1
,
});
});
expect(document.querySelector('.test')).toBeTruthy();
// Wait for 500ms
act(() => {
step(500);
});
expect(document.querySelector('.test')).toBeTruthy();
// Mouse in should not remove
fireEvent.mouseEnter(document.querySelector('.rc-notification-notice'));
act(() => {
step(200);
});
expect(document.querySelector('.test')).toBeTruthy();
// Mouse out should not remove until 500ms later
fireEvent.mouseLeave(document.querySelector('.rc-notification-notice'));
act(() => {
step(200);
});
expect(document.querySelector('.test')).toBeTruthy();
//
act(() => {
step(100);
});
expect(document.querySelector('.test')).toBeFalsy();
});
});
describe('maxCount', () => {
it('remove work when maxCount set', () => {
const { instance } = renderDemo({
maxCount: 1,
});
// First
act(() => {
instance.open({
description: bamboo
,
key: 'bamboo',
duration: 0,
});
});
// Next
act(() => {
instance.open({
description: bamboo
,
key: 'bamboo',
duration: 0,
});
});
expect(document.querySelectorAll('.max-count')).toHaveLength(1);
act(() => {
instance.close('bamboo');
});
expect(document.querySelectorAll('.max-count')).toHaveLength(0);
});
it('drop first notice when items limit exceeds', () => {
const { instance } = renderDemo({
maxCount: 1,
});
const value = 'updated last';
act(() => {
instance.open({
description: simple show,
duration: 0,
});
});
act(() => {
instance.open({
description: simple show,
duration: 0,
});
});
act(() => {
instance.open({
description: {value},
duration: 0,
});
});
act(() => {
vi.runAllTimers();
});
expect(document.querySelectorAll('.test-maxcount')).toHaveLength(1);
expect(document.querySelector('.test-maxcount').textContent).toEqual(value);
});
it('duration should work', () => {
const { instance } = renderDemo({
maxCount: 1,
});
act(() => {
instance.open({
description: bamboo,
duration: 99,
});
});
expect(document.querySelector('.auto-remove').textContent).toEqual('bamboo');
act(() => {
instance.open({
description: light,
duration: 0.5,
});
});
expect(document.querySelector('.auto-remove').textContent).toEqual('light');
act(() => {
vi.runAllTimers();
});
expect(document.querySelectorAll('.auto-remove')).toHaveLength(0);
});
});
it('onClick trigger', () => {
const { instance } = renderDemo();
let clicked = 0;
const key = Date.now();
const close = (k: React.Key) => {
instance.close(k);
};
act(() => {
instance.open({
description: (
),
key,
duration: false,
onClick: () => {
clicked += 1;
},
});
});
fireEvent.click(document.querySelector('.rc-notification-notice')); // origin latest
expect(clicked).toEqual(1);
});
it('Close Notification only trigger onClose', () => {
const { instance } = renderDemo();
let clickCount = 0;
let closeCount = 0;
act(() => {
instance.open({
description: 1
,
closable: true,
onClick: () => {
clickCount += 1;
},
onClose: () => {
closeCount += 1;
},
});
});
fireEvent.click(document.querySelector('.rc-notification-notice-close')); // origin latest
expect(clickCount).toEqual(0);
expect(closeCount).toEqual(1);
});
it('sets data attributes', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
description: simple show,
duration: 3,
className: 'notice-class',
props: {
'data-test': 'data-test-value',
'data-testid': 'data-testid-value',
},
});
});
const notice = document.querySelectorAll('.notice-class');
expect(notice.length).toBe(1);
expect(notice[0].getAttribute('data-test')).toBe('data-test-value');
expect(notice[0].getAttribute('data-testid')).toBe('data-testid-value');
});
it('sets aria attributes', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
description: simple show,
duration: 3,
className: 'notice-class',
props: {
'aria-describedby': 'aria-describedby-value',
'aria-labelledby': 'aria-labelledby-value',
},
});
});
const notice = document.querySelectorAll('.notice-class');
expect(notice.length).toBe(1);
expect(notice[0].getAttribute('aria-describedby')).toBe('aria-describedby-value');
expect(notice[0].getAttribute('aria-labelledby')).toBe('aria-labelledby-value');
});
it('sets role attribute', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
description: simple show,
duration: 3,
className: 'notice-class',
props: { role: 'alert' },
});
});
const notice = document.querySelectorAll('.notice-class');
expect(notice.length).toBe(1);
expect(notice[0].getAttribute('role')).toBe('alert');
});
it('sets role attribute from notification config', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
title: 'bamboo',
description: simple show,
icon: ,
actions: ,
role: 'status',
closable: true,
showProgress: true,
duration: 3,
});
});
const notice = document.querySelector('.rc-notification-notice');
expect(notice).toHaveAttribute('role', 'status');
expect(notice.querySelector('.rc-notification-notice-content')).toBeFalsy();
expect(notice.querySelector('.rc-notification-notice-title')).toBeTruthy();
expect(notice.querySelector('.rc-notification-notice-description')).toBeTruthy();
expect(notice.querySelector('.rc-notification-notice-icon')).toBeTruthy();
expect(notice.querySelector('.rc-notification-notice-actions')).toBeTruthy();
expect(notice.querySelector('.rc-notification-notice-close')).toBeTruthy();
expect(notice.querySelector('.rc-notification-notice-progress')).toBeTruthy();
});
it('sets default role attribute', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
description: simple show,
});
});
expect(document.querySelector('.rc-notification-notice')).toHaveAttribute('role', 'alert');
});
it('should style work', () => {
const { instance } = renderDemo({
style: () => ({
content: '"little"',
}),
});
act(() => {
instance.open({});
});
expectContentStyle('.rc-notification', 'little');
});
it('should open style and className work', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
style: {
content: '"little"',
},
className: 'bamboo',
});
});
expectContentStyle('.rc-notification-notice', 'little');
expect(document.querySelector('.rc-notification-notice')).toHaveClass('bamboo');
});
it('should support zero offset', () => {
const { container, rerender } = render(
,
);
expect(container.querySelector('.rc-notification-notice')).toHaveStyle({
'--notification-y': '10px',
});
rerender();
expect(container.querySelector('.rc-notification-notice')).toHaveStyle({
'--notification-y': '0px',
});
});
it('should not render section for single content node', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
description: 'little',
});
});
expect(document.querySelector('.rc-notification-notice-section')).toBeFalsy();
expect(document.querySelector('.rc-notification-notice-description')).toBeTruthy();
});
it('should render section when title and description both exist', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
title: 'bamboo',
description: 'little',
styles: {
section: {
content: '"light"',
},
},
classNames: {
section: 'section-class',
},
});
});
expectContentStyle('.rc-notification-notice-section', 'light');
expect(document.querySelector('.rc-notification-notice-section')).toHaveClass('section-class');
});
it('should open styles and classNames work', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
icon: ,
styles: {
wrapper: {
content: '"little"',
},
},
classNames: {
wrapper: 'bamboo',
},
});
});
expectContentStyle('.rc-notification-notice-wrapper', 'little');
expect(document.querySelector('.rc-notification-notice-wrapper')).toHaveClass('bamboo');
});
it('should keep default classNames for semantic nodes', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
title: 'bamboo',
description: 'little',
icon: ,
actions: ,
closable: true,
showProgress: true,
});
});
expect(document.querySelector('.rc-notification-notice')).toBeTruthy();
['wrapper', 'icon', 'section', 'title', 'description', 'actions', 'close', 'progress'].forEach(
(slot) => {
expect(document.querySelector(`.rc-notification-notice-${slot}`)).toBeTruthy();
},
);
});
it('should support semantic content styles and classNames', () => {
const { instance } = renderDemo({
classNames: {
title: 'global-title',
description: 'global-description',
actions: 'global-actions',
icon: 'global-icon',
},
styles: {
title: {
content: '"global-title"',
},
description: {
content: '"global-description"',
},
actions: {
content: '"global-actions"',
},
icon: {
content: '"global-icon"',
},
},
});
act(() => {
instance.open({
title: 'bamboo',
description: 'little',
icon: ,
actions: ,
classNames: {
title: 'notice-title',
description: 'notice-description',
actions: 'notice-actions',
icon: 'notice-icon',
},
styles: {
title: {
marginTop: 1,
},
description: {
marginRight: 2,
},
actions: {
marginBottom: 3,
},
icon: {
marginLeft: 4,
},
},
});
});
expect(document.querySelector('.rc-notification-notice-title')).toHaveClass(
'global-title',
'notice-title',
);
expectContentStyle('.rc-notification-notice-title', 'global-title');
expect(document.querySelector('.rc-notification-notice-title')).toHaveStyle({
marginTop: '1px',
});
expect(document.querySelector('.rc-notification-notice-description')).toHaveClass(
'global-description',
'notice-description',
);
expectContentStyle('.rc-notification-notice-description', 'global-description');
expect(document.querySelector('.rc-notification-notice-description')).toHaveStyle({
marginRight: '2px',
});
expect(document.querySelector('.rc-notification-notice-actions')).toHaveClass(
'global-actions',
'notice-actions',
);
expectContentStyle('.rc-notification-notice-actions', 'global-actions');
expect(document.querySelector('.rc-notification-notice-actions')).toHaveStyle({
marginBottom: '3px',
});
expect(document.querySelector('.actions')).toBeFalsy();
expect(document.querySelector('.rc-notification-notice-icon')).toHaveClass(
'global-icon',
'notice-icon',
);
expectContentStyle('.rc-notification-notice-icon', 'global-icon');
expect(document.querySelector('.rc-notification-notice-icon')).toHaveStyle({
marginLeft: '4px',
});
});
it('should className work', () => {
const { instance } = renderDemo({
className: (placement) => `bamboo-${placement}`,
});
act(() => {
instance.open({});
});
expect(document.querySelector('.bamboo-topRight')).toBeTruthy();
});
it('should list and listContent styles and classNames work', () => {
const { instance } = renderDemo({
classNames: {
list: 'root-list',
listContent: 'bamboo',
},
styles: {
list: {
content: '"root-list"',
},
listContent: {
content: '"little"',
},
},
});
act(() => {
instance.open({});
});
expectContentStyle('.rc-notification-list', 'root-list');
expect(document.querySelector('.rc-notification-list')).toHaveClass('root-list');
expectContentStyle('.rc-notification-list-content', 'little');
expect(document.querySelector('.rc-notification-list-content')).toHaveClass('bamboo');
});
it('should expose top notice size on listContent', () => {
const offsetHeightSpy = vi
.spyOn(HTMLElement.prototype, 'offsetHeight', 'get')
.mockImplementation(function offsetHeight(this: HTMLElement) {
if (this.classList.contains('rc-notification-notice')) {
return this.textContent === 'second' ? 18 : 10;
}
return 0;
});
const offsetWidthSpy = vi
.spyOn(HTMLElement.prototype, 'offsetWidth', 'get')
.mockImplementation(function offsetWidth(this: HTMLElement) {
if (this.classList.contains('rc-notification-notice')) {
return this.textContent === 'second' ? 28 : 20;
}
return 0;
});
const { instance } = renderDemo();
act(() => {
instance.open({
description: 'first',
duration: false,
});
instance.open({
description: 'second',
duration: false,
});
});
expect(document.querySelector('.rc-notification-list-content')).toHaveStyle({
'--top-notificiation-height': '18px',
'--top-notificiation-width': '28px',
});
offsetHeightSpy.mockRestore();
offsetWidthSpy.mockRestore();
});
it('placement', () => {
const { instance } = renderDemo();
act(() => {
instance.open({
placement: 'bottomLeft',
});
});
expect(document.querySelector('.rc-notification')).toHaveClass('rc-notification-bottomLeft');
});
it('motion as function', () => {
const motionFn = vi.fn();
const { instance } = renderDemo({
motion: motionFn,
});
act(() => {
instance.open({
placement: 'bottomLeft',
});
});
expect(motionFn).toHaveBeenCalledWith('bottomLeft');
});
it('notice when empty', () => {
const onAllRemoved = vi.fn();
const { instance } = renderDemo({
onAllRemoved,
});
expect(onAllRemoved).not.toHaveBeenCalled();
// Open!
act(() => {
instance.open({
duration: 0.1,
});
});
expect(onAllRemoved).not.toHaveBeenCalled();
// Hide
act(() => {
vi.runAllTimers();
});
expect(onAllRemoved).toHaveBeenCalled();
// Open again
onAllRemoved.mockReset();
act(() => {
instance.open({
duration: 0,
key: 'first',
});
});
act(() => {
instance.open({
duration: 0,
key: 'second',
});
});
expect(onAllRemoved).not.toHaveBeenCalled();
// Close first
act(() => {
instance.close('first');
});
expect(onAllRemoved).not.toHaveBeenCalled();
// Close second
act(() => {
instance.close('second');
});
expect(onAllRemoved).toHaveBeenCalled();
});
it('when the same key message is closing, dont open new until it closed', () => {
const onClose = vi.fn();
const Demo = () => {
const [api, holder] = useNotification();
return (
<>