File size: 7,361 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
import React from 'react';
import userEvent from '@testing-library/user-event';
import { resetWarned } from 'rc-util/lib/warning';
import Alert from '..';
import { accessibilityTest } from '../../../tests/shared/accessibilityTest';
import rtlTest from '../../../tests/shared/rtlTest';
import { act, fireEvent, render, screen, waitFakeTimer } from '../../../tests/utils';
import Button from '../../button';
import Popconfirm from '../../popconfirm';
import Tooltip from '../../tooltip';
import type { AlertRef } from '../Alert';
const { ErrorBoundary } = Alert;
describe('Alert', () => {
rtlTest(Alert);
accessibilityTest(Alert);
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
it('should show close button and could be closed', async () => {
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const onClose = jest.fn();
const { container } = render(
<Alert
message="Warning Text Warning Text Warning TextW arning Text Warning Text Warning TextWarning Text"
type="warning"
closable
onClose={onClose}
/>,
);
fireEvent.click(container.querySelector('.ant-alert-close-icon')!);
expect(onClose).toHaveBeenCalledTimes(1);
expect(errSpy).not.toHaveBeenCalled();
errSpy.mockRestore();
});
it('custom action', () => {
const { container } = render(
<Alert
message="Success Tips"
type="success"
showIcon
action={
<Button size="small" type="text">
UNDO
</Button>
}
closable
/>,
);
expect(container.firstChild).toMatchSnapshot();
});
it('should sets data attributes on alert when pass attributes to props', () => {
render(
<Alert data-test="test-id" data-id="12345" aria-describedby="some-label" message={null} />,
);
const alert = screen.getByRole('alert');
expect(alert).toHaveAttribute('data-test', 'test-id');
expect(alert).toHaveAttribute('data-id', '12345');
expect(alert).toHaveAttribute('aria-describedby', 'some-label');
});
it('sets role attribute on input', () => {
render(<Alert role="status" message={null} />);
expect(screen.getByRole('status')).toBeInTheDocument();
});
it('should show error as ErrorBoundary when children have error', () => {
const warnSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
expect(warnSpy).toHaveBeenCalledTimes(0);
// @ts-expect-error
const ThrowError = () => <NotExisted />;
render(
<ErrorBoundary>
<ThrowError />
</ErrorBoundary>,
);
expect(screen.getByRole('alert')).toHaveTextContent(
'ReferenceError: NotExisted is not defined',
);
warnSpy.mockRestore();
});
it('could be used with Tooltip', async () => {
render(
<Tooltip title="xxx" mouseEnterDelay={0}>
<Alert
message="Warning Text Warning Text Warning TextW arning Text Warning Text Warning TextWarning Text"
type="warning"
/>
</Tooltip>,
);
await userEvent.hover(screen.getByRole('alert'));
await waitFakeTimer();
expect(document.querySelector<HTMLDivElement>('.ant-tooltip')).toBeInTheDocument();
});
it('could be used with Popconfirm', async () => {
render(
<Popconfirm title="xxx">
<Alert
message="Warning Text Warning Text Warning TextW arning Text Warning Text Warning TextWarning Text"
type="warning"
/>
</Popconfirm>,
);
await userEvent.click(screen.getByRole('alert'));
act(() => {
jest.runAllTimers();
});
expect(screen.getByRole('tooltip')).toBeInTheDocument();
});
it('could accept none react element icon', () => {
render(<Alert message="Success Tips" type="success" showIcon icon="icon" />);
expect(screen.getByRole('alert')).toHaveTextContent(/success tips/i);
expect(screen.getByRole('alert')).toHaveTextContent(/icon/i);
});
it('should not render message div when no message', () => {
const { container } = render(<Alert description="description" />);
expect(!!container.querySelector('.ant-alert-message')).toBe(false);
});
it('close button should be hidden when closeIcon setting to null or false', () => {
const { container, rerender } = render(<Alert closeIcon={null} />);
expect(container.querySelector('.ant-alert-close-icon')).toBeFalsy();
rerender(<Alert closeIcon={false} />);
expect(container.querySelector('.ant-alert-close-icon')).toBeFalsy();
rerender(<Alert closeIcon />);
expect(container.querySelector('.ant-alert-close-icon')).toBeTruthy();
rerender(<Alert />);
expect(container.querySelector('.ant-alert-close-icon')).toBeFalsy();
});
it('close button should be support aria-* by closable', () => {
const { container, rerender } = render(<Alert />);
expect(container.querySelector('*[aria-label]')).toBeFalsy();
rerender(<Alert closable={{ 'aria-label': 'Close' }} closeIcon="CloseIcon" />);
expect(container.querySelector('[aria-label="Close"]')).toBeTruthy();
rerender(<Alert closable={{ 'aria-label': 'Close' }} closeText="CloseText" />);
expect(container.querySelector('[aria-label="Close"]')).toBeTruthy();
rerender(<Alert closable={{ 'aria-label': 'Close', closeIcon: 'CloseIconProp' }} />);
expect(container.querySelector('[aria-label="Close"]')).toBeTruthy();
});
it('close button should be support custom icon by closable', () => {
const { container, rerender } = render(<Alert />);
expect(container.querySelector('.ant-alert-close-icon')).toBeFalsy();
rerender(<Alert closable={{ closeIcon: 'CloseBtn' }} />);
expect(container.querySelector('.ant-alert-close-icon')?.textContent).toBe('CloseBtn');
rerender(<Alert closable={{ closeIcon: 'CloseBtn' }} closeIcon="CloseBtn2" />);
expect(container.querySelector('.ant-alert-close-icon')?.textContent).toBe('CloseBtn');
rerender(<Alert closable={{ closeIcon: 'CloseBtn' }} closeText="CloseBtn3" />);
expect(container.querySelector('.ant-alert-close-icon')?.textContent).toBe('CloseBtn');
rerender(<Alert closeText="CloseBtn2" />);
expect(container.querySelector('.ant-alert-close-icon')?.textContent).toBe('CloseBtn2');
rerender(<Alert closeIcon="CloseBtn3" />);
expect(container.querySelector('.ant-alert-close-icon')?.textContent).toBe('CloseBtn3');
});
it('should warning when using closeText', () => {
resetWarned();
const warnSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const { container } = render(<Alert closeText="close" />);
expect(warnSpy).toHaveBeenCalledWith(
`Warning: [antd: Alert] \`closeText\` is deprecated. Please use \`closable.closeIcon\` instead.`,
);
expect(container.querySelector('.ant-alert-close-icon')?.textContent).toBe('close');
warnSpy.mockRestore();
});
it('should support id and ref', () => {
const alertRef = React.createRef<AlertRef>();
const { container } = render(<Alert id="test-id" ref={alertRef} />);
const element = container.querySelector<HTMLDivElement>('#test-id');
expect(element).toBeTruthy();
expect(alertRef.current?.nativeElement).toBeTruthy();
expect(alertRef.current?.nativeElement).toBe(element);
});
});
|