2022-05-31 10:29:13 +08:00
|
|
|
import React from 'react';
|
|
|
|
import type { ModalProps } from '..';
|
2022-06-22 14:57:09 +08:00
|
|
|
import Modal from '..';
|
2022-05-31 10:29:13 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
2022-08-23 16:55:57 +08:00
|
|
|
import { resetWarned } from '../../_util/warning';
|
2022-05-31 10:29:13 +08:00
|
|
|
|
|
|
|
jest.mock('rc-util/lib/Portal');
|
|
|
|
|
2022-08-23 16:55:57 +08:00
|
|
|
class ModalTester extends React.Component<ModalProps, { open: boolean }> {
|
|
|
|
state = { open: false };
|
2022-05-31 10:29:13 +08:00
|
|
|
|
|
|
|
componentDidMount() {
|
2022-08-23 16:55:57 +08:00
|
|
|
this.setState({ open: true }); // eslint-disable-line react/no-did-mount-set-state
|
2022-05-31 10:29:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
container = React.createRef<HTMLDivElement>();
|
|
|
|
|
|
|
|
getContainer = () => this.container?.current!;
|
|
|
|
|
|
|
|
render() {
|
2022-08-23 16:55:57 +08:00
|
|
|
const { open } = this.state;
|
2022-05-31 10:29:13 +08:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div ref={this.container} />
|
2022-08-23 16:55:57 +08:00
|
|
|
<Modal {...this.props} open={open} getContainer={this.getContainer}>
|
2022-05-31 10:29:13 +08:00
|
|
|
Here is content of Modal
|
|
|
|
</Modal>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('Modal', () => {
|
|
|
|
mountTest(Modal);
|
|
|
|
rtlTest(Modal);
|
|
|
|
|
|
|
|
it('support closeIcon', () => {
|
2022-08-23 16:55:57 +08:00
|
|
|
render(<Modal closeIcon={<a>closeIcon</a>} open />);
|
2022-05-31 10:29:13 +08:00
|
|
|
expect(document.body.querySelectorAll('.ant-modal-root')[0]).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('render correctly', () => {
|
|
|
|
const { asFragment } = render(<ModalTester />);
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('render without footer', () => {
|
|
|
|
const { asFragment } = render(<ModalTester footer={null} />);
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('onCancel should be called', () => {
|
|
|
|
const onCancel = jest.fn();
|
2022-08-23 16:55:57 +08:00
|
|
|
render(<Modal open onCancel={onCancel} />);
|
2022-05-31 10:29:13 +08:00
|
|
|
fireEvent.click(document.body.querySelectorAll('.ant-btn')[0]);
|
|
|
|
expect(onCancel).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('onOk should be called', () => {
|
|
|
|
const onOk = jest.fn();
|
2022-08-23 16:55:57 +08:00
|
|
|
render(<Modal open onOk={onOk} />);
|
2022-05-31 10:29:13 +08:00
|
|
|
const btns = document.body.querySelectorAll('.ant-btn');
|
|
|
|
fireEvent.click(btns[btns.length - 1]);
|
|
|
|
expect(onOk).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('danger type', () => {
|
2022-08-23 16:55:57 +08:00
|
|
|
render(<Modal okType="danger" okText="123" open />);
|
2022-05-31 10:29:13 +08:00
|
|
|
const btns = document.body.querySelectorAll('.ant-btn');
|
|
|
|
expect(btns[btns.length - 1].classList.contains('ant-btn-dangerous')).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('mouse position', () => {
|
|
|
|
const Demo = () => {
|
2022-08-23 16:55:57 +08:00
|
|
|
const [open, setOpen] = React.useState(false);
|
2022-05-31 10:29:13 +08:00
|
|
|
const containerRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
return (
|
|
|
|
<div ref={containerRef}>
|
2022-08-23 16:55:57 +08:00
|
|
|
<div id="trigger" onClick={() => setOpen(true)}>
|
2022-05-31 10:29:13 +08:00
|
|
|
click me
|
|
|
|
</div>
|
2022-08-23 16:55:57 +08:00
|
|
|
<Modal open={open} getContainer={() => containerRef.current!} />
|
2022-05-31 10:29:13 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
const { container } = render(<Demo />);
|
|
|
|
fireEvent.click(container.querySelectorAll('#trigger')[0]);
|
|
|
|
expect(
|
|
|
|
(container.querySelectorAll('.ant-modal')[0] as HTMLDivElement).style.transformOrigin,
|
|
|
|
).toBeTruthy();
|
|
|
|
});
|
2022-08-23 16:55:57 +08:00
|
|
|
|
|
|
|
it('deprecated warning', () => {
|
|
|
|
resetWarned();
|
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
|
|
|
render(<Modal visible />);
|
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
2022-08-23 22:41:01 +08:00
|
|
|
'Warning: [antd: Modal] `visible` is removed in v5, please use `open` instead.',
|
2022-08-23 16:55:57 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
expect(document.querySelector('.ant-modal')).toBeTruthy();
|
|
|
|
|
|
|
|
errSpy.mockRestore();
|
|
|
|
});
|
2022-05-31 10:29:13 +08:00
|
|
|
});
|