2018-05-25 21:56:57 +08:00
|
|
|
import React from 'react';
|
2021-12-04 17:06:36 +08:00
|
|
|
import { act } from 'react-dom/test-utils';
|
2022-05-25 20:31:36 +08:00
|
|
|
import Alert from '..';
|
|
|
|
import accessibilityTest from '../../../tests/shared/accessibilityTest';
|
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
|
|
|
import { fireEvent, render, sleep } from '../../../tests/utils';
|
2020-11-30 11:04:56 +08:00
|
|
|
import Button from '../../button';
|
2020-03-25 16:50:43 +08:00
|
|
|
import Popconfirm from '../../popconfirm';
|
2022-05-25 20:31:36 +08:00
|
|
|
import Tooltip from '../../tooltip';
|
2018-05-25 21:56:57 +08:00
|
|
|
|
2019-12-11 14:15:45 +08:00
|
|
|
const { ErrorBoundary } = Alert;
|
|
|
|
|
2018-05-25 21:56:57 +08:00
|
|
|
describe('Alert', () => {
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Alert);
|
2021-12-02 20:25:31 +08:00
|
|
|
accessibilityTest(Alert);
|
2020-01-02 19:10:16 +08:00
|
|
|
|
2018-05-25 21:56:57 +08:00
|
|
|
beforeAll(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('could be closed', () => {
|
|
|
|
const onClose = jest.fn();
|
2022-05-25 20:31:36 +08:00
|
|
|
const { container } = render(
|
2018-05-25 21:56:57 +08:00
|
|
|
<Alert
|
|
|
|
message="Warning Text Warning Text Warning TextW arning Text Warning Text Warning TextWarning Text"
|
|
|
|
type="warning"
|
|
|
|
closable
|
|
|
|
onClose={onClose}
|
2018-12-07 16:17:45 +08:00
|
|
|
/>,
|
2018-05-25 21:56:57 +08:00
|
|
|
);
|
2022-05-25 20:31:36 +08:00
|
|
|
|
2021-12-04 17:06:36 +08:00
|
|
|
act(() => {
|
|
|
|
jest.useFakeTimers();
|
2022-05-25 20:31:36 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-alert-close-icon')!);
|
2021-12-04 17:06:36 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2020-08-11 17:04:42 +08:00
|
|
|
expect(onClose).toHaveBeenCalled();
|
2018-05-25 21:56:57 +08:00
|
|
|
});
|
2018-06-15 03:08:51 +08:00
|
|
|
|
2020-11-30 11:04:56 +08:00
|
|
|
describe('action of Alert', () => {
|
|
|
|
it('custom action', () => {
|
2022-05-25 20:31:36 +08:00
|
|
|
const { container } = render(
|
2020-11-30 11:04:56 +08:00
|
|
|
<Alert
|
|
|
|
message="Success Tips"
|
|
|
|
type="success"
|
|
|
|
showIcon
|
|
|
|
action={
|
|
|
|
<Button size="small" type="text">
|
|
|
|
UNDO
|
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
closable
|
|
|
|
/>,
|
|
|
|
);
|
2022-05-25 20:31:36 +08:00
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2020-11-30 11:04:56 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-10-13 18:58:30 +08:00
|
|
|
it('support closeIcon', () => {
|
2022-05-25 20:31:36 +08:00
|
|
|
const { container } = render(
|
2021-10-13 18:58:30 +08:00
|
|
|
<Alert
|
|
|
|
closable
|
|
|
|
closeIcon={<span>close</span>}
|
|
|
|
message="Warning Text Warning Text Warning TextW arning Text Warning Text Warning TextWarning Text"
|
|
|
|
type="warning"
|
|
|
|
/>,
|
|
|
|
);
|
2022-05-25 20:31:36 +08:00
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2021-10-13 18:58:30 +08:00
|
|
|
});
|
|
|
|
|
2018-06-15 03:08:51 +08:00
|
|
|
describe('data and aria props', () => {
|
|
|
|
it('sets data attributes on input', () => {
|
2022-05-25 20:31:36 +08:00
|
|
|
const { container } = render(<Alert data-test="test-id" data-id="12345" message={null} />);
|
|
|
|
const input = container.querySelector('.ant-alert')!;
|
2018-06-15 03:08:51 +08:00
|
|
|
expect(input.getAttribute('data-test')).toBe('test-id');
|
|
|
|
expect(input.getAttribute('data-id')).toBe('12345');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets aria attributes on input', () => {
|
2022-05-25 20:31:36 +08:00
|
|
|
const { container } = render(<Alert aria-describedby="some-label" message={null} />);
|
|
|
|
const input = container.querySelector('.ant-alert')!;
|
2018-06-15 03:08:51 +08:00
|
|
|
expect(input.getAttribute('aria-describedby')).toBe('some-label');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets role attribute on input', () => {
|
2022-05-25 20:31:36 +08:00
|
|
|
const { container } = render(<Alert role="status" message={null} />);
|
|
|
|
const input = container.querySelector('.ant-alert')!;
|
2018-06-15 03:08:51 +08:00
|
|
|
expect(input.getAttribute('role')).toBe('status');
|
|
|
|
});
|
|
|
|
});
|
2019-12-11 14:15:45 +08:00
|
|
|
|
2020-12-01 18:28:35 +08:00
|
|
|
it('ErrorBoundary', () => {
|
2021-09-07 13:07:41 +08:00
|
|
|
jest.spyOn(console, 'error').mockImplementation(() => undefined);
|
2021-10-12 13:14:26 +08:00
|
|
|
// eslint-disable-next-line no-console
|
2021-09-07 13:07:41 +08:00
|
|
|
expect(console.error).toBeCalledTimes(0);
|
2020-05-22 10:42:55 +08:00
|
|
|
// @ts-expect-error
|
2020-04-03 15:02:11 +08:00
|
|
|
// eslint-disable-next-line react/jsx-no-undef
|
|
|
|
const ThrowError = () => <NotExisted />;
|
2022-05-25 20:31:36 +08:00
|
|
|
const { container } = render(
|
2019-12-11 14:15:45 +08:00
|
|
|
<ErrorBoundary>
|
|
|
|
<ThrowError />
|
|
|
|
</ErrorBoundary>,
|
|
|
|
);
|
|
|
|
// eslint-disable-next-line jest/no-standalone-expect
|
2022-05-25 20:31:36 +08:00
|
|
|
expect(container.textContent).toContain('ReferenceError: NotExisted is not defined');
|
2021-09-07 13:07:41 +08:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
(console.error as any).mockRestore();
|
2019-12-11 14:15:45 +08:00
|
|
|
});
|
2020-03-25 16:50:43 +08:00
|
|
|
|
|
|
|
it('could be used with Tooltip', async () => {
|
2020-05-15 10:47:03 +08:00
|
|
|
const ref = React.createRef<any>();
|
2020-03-25 16:50:43 +08:00
|
|
|
jest.useRealTimers();
|
2022-05-25 20:31:36 +08:00
|
|
|
const { container } = render(
|
2020-05-15 10:47:03 +08:00
|
|
|
<Tooltip title="xxx" mouseEnterDelay={0} ref={ref}>
|
2020-03-25 16:50:43 +08:00
|
|
|
<Alert
|
|
|
|
message="Warning Text Warning Text Warning TextW arning Text Warning Text Warning TextWarning Text"
|
|
|
|
type="warning"
|
|
|
|
/>
|
|
|
|
</Tooltip>,
|
|
|
|
);
|
2022-05-25 20:31:36 +08:00
|
|
|
// wrapper.find('.ant-alert').simulate('mouseenter');
|
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-alert')!);
|
2020-03-25 16:50:43 +08:00
|
|
|
await sleep(0);
|
2020-05-15 10:47:03 +08:00
|
|
|
expect(ref.current.getPopupDomNode()).toBeTruthy();
|
2020-03-25 16:50:43 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('could be used with Popconfirm', async () => {
|
2020-04-26 22:24:13 +08:00
|
|
|
const ref = React.createRef<any>();
|
2020-03-25 16:50:43 +08:00
|
|
|
jest.useRealTimers();
|
2022-05-25 20:31:36 +08:00
|
|
|
const { container } = render(
|
2020-04-26 22:24:13 +08:00
|
|
|
<Popconfirm ref={ref} title="xxx">
|
2020-03-25 16:50:43 +08:00
|
|
|
<Alert
|
|
|
|
message="Warning Text Warning Text Warning TextW arning Text Warning Text Warning TextWarning Text"
|
|
|
|
type="warning"
|
|
|
|
/>
|
|
|
|
</Popconfirm>,
|
|
|
|
);
|
2022-05-25 20:31:36 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-alert')!);
|
2020-03-25 16:50:43 +08:00
|
|
|
await sleep(0);
|
2020-04-26 22:24:13 +08:00
|
|
|
expect(ref.current.getPopupDomNode()).toBeTruthy();
|
2020-03-25 16:50:43 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
2020-05-09 19:32:02 +08:00
|
|
|
|
|
|
|
it('could accept none react element icon', () => {
|
2022-05-25 20:31:36 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Alert message="Success Tips" type="success" showIcon icon="icon" />,
|
|
|
|
);
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2020-05-09 19:32:02 +08:00
|
|
|
});
|
2021-09-07 13:07:41 +08:00
|
|
|
|
|
|
|
it('should not render message div when no message', () => {
|
2022-05-25 20:31:36 +08:00
|
|
|
const { container } = render(<Alert description="description" />);
|
|
|
|
expect(!!container.querySelector('.ant-alert-message')).toBe(false);
|
2021-09-07 13:07:41 +08:00
|
|
|
});
|
2018-05-25 21:56:57 +08:00
|
|
|
});
|