import React from 'react'; import type { ModalProps } from '..'; import Modal from '..'; import mountTest from '../../../tests/shared/mountTest'; import rtlTest from '../../../tests/shared/rtlTest'; import { fireEvent, render } from '../../../tests/utils'; jest.mock('rc-util/lib/Portal'); class ModalTester extends React.Component { state = { visible: false }; componentDidMount() { this.setState({ visible: true }); // eslint-disable-line react/no-did-mount-set-state } container = React.createRef(); getContainer = () => this.container?.current!; render() { const { visible } = this.state; return (
Here is content of Modal
); } } describe('Modal', () => { mountTest(Modal); rtlTest(Modal); it('support closeIcon', () => { render(closeIcon} visible />); expect(document.body.querySelectorAll('.ant-modal-root')[0]).toMatchSnapshot(); }); it('render correctly', () => { const { asFragment } = render(); expect(asFragment().firstChild).toMatchSnapshot(); }); it('render without footer', () => { const { asFragment } = render(); expect(asFragment().firstChild).toMatchSnapshot(); }); it('onCancel should be called', () => { const onCancel = jest.fn(); render(); fireEvent.click(document.body.querySelectorAll('.ant-btn')[0]); expect(onCancel).toHaveBeenCalled(); }); it('onOk should be called', () => { const onOk = jest.fn(); render(); const btns = document.body.querySelectorAll('.ant-btn'); fireEvent.click(btns[btns.length - 1]); expect(onOk).toHaveBeenCalled(); }); it('danger type', () => { render(); const btns = document.body.querySelectorAll('.ant-btn'); expect(btns[btns.length - 1].classList.contains('ant-btn-dangerous')).toBeTruthy(); }); it('mouse position', () => { const Demo = () => { const [visible, setVisible] = React.useState(false); const containerRef = React.useRef(null); return (
setVisible(true)}> click me
containerRef.current!} />
); }; const { container } = render(); fireEvent.click(container.querySelectorAll('#trigger')[0]); expect( (container.querySelectorAll('.ant-modal')[0] as HTMLDivElement).style.transformOrigin, ).toBeTruthy(); }); });