import React from 'react';
import Alert from '..';
import accessibilityTest from '../../../tests/shared/accessibilityTest';
import rtlTest from '../../../tests/shared/rtlTest';
import { fireEvent, render, sleep, act } from '../../../tests/utils';
import Button from '../../button';
import Popconfirm from '../../popconfirm';
import Tooltip from '../../tooltip';
const { ErrorBoundary } = Alert;
describe('Alert', () => {
rtlTest(Alert);
accessibilityTest(Alert);
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
it('could be closed', () => {
const onClose = jest.fn();
const { container } = render(
,
);
jest.useFakeTimers();
fireEvent.click(container.querySelector('.ant-alert-close-icon')!);
act(() => {
jest.runAllTimers();
});
expect(onClose).toHaveBeenCalled();
jest.useRealTimers();
});
describe('action of Alert', () => {
it('custom action', () => {
const { container } = render(
UNDO
}
closable
/>,
);
expect(container.firstChild).toMatchSnapshot();
});
});
it('support closeIcon', () => {
const { container } = render(
close}
message="Warning Text Warning Text Warning TextW arning Text Warning Text Warning TextWarning Text"
type="warning"
/>,
);
expect(container.firstChild).toMatchSnapshot();
});
describe('data and aria props', () => {
it('sets data attributes on input', () => {
const { container } = render();
const input = container.querySelector('.ant-alert')!;
expect(input.getAttribute('data-test')).toBe('test-id');
expect(input.getAttribute('data-id')).toBe('12345');
});
it('sets aria attributes on input', () => {
const { container } = render();
const input = container.querySelector('.ant-alert')!;
expect(input.getAttribute('aria-describedby')).toBe('some-label');
});
it('sets role attribute on input', () => {
const { container } = render();
const input = container.querySelector('.ant-alert')!;
expect(input.getAttribute('role')).toBe('status');
});
});
it('ErrorBoundary', () => {
jest.spyOn(console, 'error').mockImplementation(() => undefined);
// eslint-disable-next-line no-console
expect(console.error).toHaveBeenCalledTimes(0);
// @ts-expect-error
// eslint-disable-next-line react/jsx-no-undef
const ThrowError = () => ;
const { container } = render(
,
);
// eslint-disable-next-line jest/no-standalone-expect
expect(container.textContent).toContain('ReferenceError: NotExisted is not defined');
// eslint-disable-next-line no-console
(console.error as any).mockRestore();
});
it('could be used with Tooltip', async () => {
const ref = React.createRef();
jest.useRealTimers();
const { container } = render(
,
);
// wrapper.find('.ant-alert').simulate('mouseenter');
fireEvent.mouseEnter(container.querySelector('.ant-alert')!);
await sleep(0);
expect(ref.current.getPopupDomNode()).toBeTruthy();
jest.useFakeTimers();
});
it('could be used with Popconfirm', async () => {
const ref = React.createRef();
jest.useRealTimers();
const { container } = render(
,
);
fireEvent.click(container.querySelector('.ant-alert')!);
await sleep(0);
expect(ref.current.getPopupDomNode()).toBeTruthy();
jest.useFakeTimers();
});
it('could accept none react element icon', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
it('should not render message div when no message', () => {
const { container } = render();
expect(!!container.querySelector('.ant-alert-message')).toBe(false);
});
});