2018-05-25 21:56:57 +08:00
|
|
|
import React from 'react';
|
2022-09-19 13:41:00 +08:00
|
|
|
import userEvent from '@testing-library/user-event';
|
2022-05-25 20:31:36 +08:00
|
|
|
import Alert from '..';
|
|
|
|
import accessibilityTest from '../../../tests/shared/accessibilityTest';
|
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2022-09-19 13:41:00 +08:00
|
|
|
import { render, act, screen } 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();
|
|
|
|
});
|
|
|
|
|
2022-09-19 13:41:00 +08:00
|
|
|
it('should show close button and could be closed', async () => {
|
2018-05-25 21:56:57 +08:00
|
|
|
const onClose = jest.fn();
|
2022-09-19 13:41:00 +08:00
|
|
|
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
|
|
|
|
2022-09-19 13:41:00 +08:00
|
|
|
await userEvent.click(screen.getByRole('button', { name: /close/i }));
|
|
|
|
|
2021-12-04 17:06:36 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2018-06-15 03:08:51 +08:00
|
|
|
|
2022-09-19 16:30:44 +08:00
|
|
|
expect(onClose).toHaveBeenCalledTimes(1);
|
2020-11-30 11:04:56 +08:00
|
|
|
});
|
|
|
|
|
2022-09-19 13:41:00 +08:00
|
|
|
it('custom action', () => {
|
2022-05-25 20:31:36 +08:00
|
|
|
const { container } = render(
|
2021-10-13 18:58:30 +08:00
|
|
|
<Alert
|
2022-09-19 13:41:00 +08:00
|
|
|
message="Success Tips"
|
|
|
|
type="success"
|
|
|
|
showIcon
|
|
|
|
action={
|
|
|
|
<Button size="small" type="text">
|
|
|
|
UNDO
|
|
|
|
</Button>
|
|
|
|
}
|
2021-10-13 18:58:30 +08:00
|
|
|
closable
|
|
|
|
/>,
|
|
|
|
);
|
2022-05-25 20:31:36 +08:00
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2021-10-13 18:58:30 +08:00
|
|
|
});
|
|
|
|
|
2022-09-19 13:41:00 +08:00
|
|
|
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');
|
|
|
|
});
|
2018-06-15 03:08:51 +08:00
|
|
|
|
2022-09-19 13:41:00 +08:00
|
|
|
it('sets role attribute on input', () => {
|
|
|
|
render(<Alert role="status" message={null} />);
|
2018-06-15 03:08:51 +08:00
|
|
|
|
2022-09-19 13:41:00 +08:00
|
|
|
expect(screen.getByRole('status')).toBeInTheDocument();
|
2018-06-15 03:08:51 +08:00
|
|
|
});
|
2019-12-11 14:15:45 +08:00
|
|
|
|
2022-09-19 13:41:00 +08:00
|
|
|
it('should show error as ErrorBoundary when children have error', () => {
|
2022-11-28 14:19:25 +08:00
|
|
|
const warnSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
expect(warnSpy).toHaveBeenCalledTimes(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-09-19 13:41:00 +08:00
|
|
|
render(
|
2019-12-11 14:15:45 +08:00
|
|
|
<ErrorBoundary>
|
|
|
|
<ThrowError />
|
|
|
|
</ErrorBoundary>,
|
|
|
|
);
|
2022-09-19 13:41:00 +08:00
|
|
|
|
|
|
|
expect(screen.getByRole('alert')).toHaveTextContent(
|
|
|
|
'ReferenceError: NotExisted is not defined',
|
|
|
|
);
|
2022-11-28 14:19:25 +08:00
|
|
|
warnSpy.mockRestore();
|
2019-12-11 14:15:45 +08:00
|
|
|
});
|
2020-03-25 16:50:43 +08:00
|
|
|
|
|
|
|
it('could be used with Tooltip', async () => {
|
2022-09-19 13:41:00 +08:00
|
|
|
render(
|
|
|
|
<Tooltip title="xxx" mouseEnterDelay={0}>
|
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-09-19 13:41:00 +08:00
|
|
|
|
|
|
|
await userEvent.hover(screen.getByRole('alert'));
|
|
|
|
|
|
|
|
expect(screen.getByRole('tooltip')).toBeInTheDocument();
|
2020-03-25 16:50:43 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('could be used with Popconfirm', async () => {
|
2022-09-19 13:41:00 +08:00
|
|
|
render(
|
|
|
|
<Popconfirm 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-09-19 13:41:00 +08:00
|
|
|
await userEvent.click(screen.getByRole('alert'));
|
|
|
|
|
|
|
|
expect(screen.getByRole('tooltip')).toBeInTheDocument();
|
2020-03-25 16:50:43 +08:00
|
|
|
});
|
2020-05-09 19:32:02 +08:00
|
|
|
|
|
|
|
it('could accept none react element icon', () => {
|
2022-09-19 13:41:00 +08:00
|
|
|
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);
|
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
|
|
|
});
|