2018-05-25 21:56:57 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
|
|
|
import Alert from '..';
|
2020-03-25 16:50:43 +08:00
|
|
|
import Tooltip from '../../tooltip';
|
|
|
|
import Popconfirm from '../../popconfirm';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2020-03-25 16:50:43 +08:00
|
|
|
import { sleep } from '../../../tests/utils';
|
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);
|
|
|
|
|
2018-05-25 21:56:57 +08:00
|
|
|
beforeAll(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('could be closed', () => {
|
|
|
|
const onClose = jest.fn();
|
|
|
|
const afterClose = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Alert
|
|
|
|
message="Warning Text Warning Text Warning TextW arning Text Warning Text Warning TextWarning Text"
|
|
|
|
type="warning"
|
|
|
|
closable
|
|
|
|
onClose={onClose}
|
|
|
|
afterClose={afterClose}
|
2018-12-07 16:17:45 +08:00
|
|
|
/>,
|
2018-05-25 21:56:57 +08:00
|
|
|
);
|
|
|
|
wrapper.find('.ant-alert-close-icon').simulate('click');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onClose).toHaveBeenCalled();
|
2018-05-25 21:56:57 +08:00
|
|
|
jest.runAllTimers();
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(afterClose).toHaveBeenCalled();
|
2018-05-25 21:56:57 +08:00
|
|
|
});
|
2018-06-15 03:08:51 +08:00
|
|
|
|
|
|
|
describe('data and aria props', () => {
|
|
|
|
it('sets data attributes on input', () => {
|
2020-04-22 18:16:03 +08:00
|
|
|
const wrapper = mount(<Alert data-test="test-id" data-id="12345" message={null} />);
|
2018-06-15 03:08:51 +08:00
|
|
|
const input = wrapper.find('.ant-alert').getDOMNode();
|
|
|
|
expect(input.getAttribute('data-test')).toBe('test-id');
|
|
|
|
expect(input.getAttribute('data-id')).toBe('12345');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets aria attributes on input', () => {
|
2020-04-22 18:16:03 +08:00
|
|
|
const wrapper = mount(<Alert aria-describedby="some-label" message={null} />);
|
2018-06-15 03:08:51 +08:00
|
|
|
const input = wrapper.find('.ant-alert').getDOMNode();
|
|
|
|
expect(input.getAttribute('aria-describedby')).toBe('some-label');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets role attribute on input', () => {
|
2020-04-22 18:16:03 +08:00
|
|
|
const wrapper = mount(<Alert role="status" message={null} />);
|
2018-06-15 03:08:51 +08:00
|
|
|
const input = wrapper.find('.ant-alert').getDOMNode();
|
|
|
|
expect(input.getAttribute('role')).toBe('status');
|
|
|
|
});
|
|
|
|
});
|
2019-12-11 14:15:45 +08:00
|
|
|
|
|
|
|
const testIt = process.env.REACT === '15' ? it.skip : it;
|
|
|
|
testIt('ErrorBoundary', () => {
|
2020-04-22 18:16:03 +08:00
|
|
|
// TODO: Change to @ts-expect-error once typescript is at 3.9
|
|
|
|
// @ts-ignore
|
2020-04-03 15:02:11 +08:00
|
|
|
// eslint-disable-next-line react/jsx-no-undef
|
|
|
|
const ThrowError = () => <NotExisted />;
|
2019-12-11 14:15:45 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<ErrorBoundary>
|
|
|
|
<ThrowError />
|
|
|
|
</ErrorBoundary>,
|
|
|
|
);
|
|
|
|
// eslint-disable-next-line jest/no-standalone-expect
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
2020-03-25 16:50:43 +08:00
|
|
|
|
|
|
|
it('could be used with Tooltip', async () => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Tooltip title="xxx" mouseEnterDelay={0}>
|
|
|
|
<Alert
|
|
|
|
message="Warning Text Warning Text Warning TextW arning Text Warning Text Warning TextWarning Text"
|
|
|
|
type="warning"
|
|
|
|
/>
|
|
|
|
</Tooltip>,
|
|
|
|
);
|
|
|
|
wrapper.find('.ant-alert').simulate('mouseenter');
|
|
|
|
await sleep(0);
|
2020-04-22 18:16:03 +08:00
|
|
|
expect(wrapper.find<Tooltip>(Tooltip).instance().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();
|
|
|
|
const wrapper = mount(
|
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>,
|
|
|
|
);
|
|
|
|
wrapper.find('.ant-alert').simulate('click');
|
|
|
|
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();
|
|
|
|
});
|
2018-05-25 21:56:57 +08:00
|
|
|
});
|