import React from 'react'; import type { DrawerProps } from '..'; import Drawer from '..'; import { act, fireEvent, render } from '../../../tests/utils'; const DrawerTest: React.FC = (props) => ( Here is content of Drawer ); describe('Drawer', () => { beforeEach(() => { jest.useFakeTimers(); }); afterEach(() => { jest.useRealTimers(); }); it('render correctly', () => { const { container, asFragment, rerender } = render(); expect(container.querySelector('.ant-drawer-body')).toBeTruthy(); rerender(); expect(container.querySelector('.ant-drawer-body')?.textContent).toEqual( 'Here is content of Drawer', ); expect(asFragment().firstChild).toMatchSnapshot(); }); it('mask trigger onClose', () => { const onClose = jest.fn(); const { container } = render(); fireEvent.click(container.querySelector('.ant-drawer-mask')!); expect(onClose).toHaveBeenCalled(); }); it('close button trigger onClose', () => { const onClose = jest.fn(); const { container } = render(); fireEvent.click(container.querySelector('.ant-drawer-close')!); expect(onClose).toHaveBeenCalled(); }); it('maskClosable no trigger onClose', () => { const onClose = jest.fn(); const { container } = render(); fireEvent.click(container.querySelector('.ant-drawer-mask')!); expect(onClose).not.toHaveBeenCalled(); }); it('dom should be removed after close when destroyOnClose is true', () => { const { container, rerender } = render(); expect(container.querySelector('.ant-drawer')).toBeTruthy(); rerender(); act(() => { jest.runAllTimers(); }); expect(container.querySelector('.ant-drawer')).toBeFalsy(); }); it('dom should be existed after close when destroyOnClose is false', () => { const { container, rerender } = render(); expect(container.querySelector('.ant-drawer')).toBeTruthy(); rerender(); act(() => { jest.runAllTimers(); }); fireEvent.animationEnd(container.querySelector('.ant-drawer-content')!); expect(container.querySelector('.ant-drawer')).toBeTruthy(); }); it('dom should be existed after close twice when getContainer is false', () => { const { container, rerender } = render(); expect(container.querySelector('.ant-drawer-content')).toBeTruthy(); // Hide rerender(); act(() => { jest.runAllTimers(); }); fireEvent.animationEnd(container.querySelector('.ant-drawer-content-wrapper')!); expect(container.querySelector('.ant-drawer-content-wrapper-hidden')).toBeTruthy(); // Show rerender(); expect(container.querySelector('.ant-drawer-content-wrapper')).toBeTruthy(); expect(container.querySelector('.ant-drawer-content-wrapper-hidden')).toBeFalsy(); // Hide rerender(); act(() => { jest.runAllTimers(); }); fireEvent.animationEnd(container.querySelector('.ant-drawer-content-wrapper')!); expect(container.querySelector('.ant-drawer-content-wrapper-hidden')).toBeTruthy(); }); it('test afterOpenChange', async () => { const afterOpenChange = jest.fn(); const { container, rerender } = render(); rerender(); act(() => { jest.runAllTimers(); }); fireEvent.animationEnd(container.querySelector('.ant-drawer-content-wrapper')!); expect(afterOpenChange).toHaveBeenCalledTimes(1); }); it('test legacy afterVisibleChange', async () => { const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); const afterVisibleChange = jest.fn(); const { container, rerender } = render( , ); rerender(); act(() => { jest.runAllTimers(); }); fireEvent.animationEnd(container.querySelector('.ant-drawer-content-wrapper')!); expect(afterVisibleChange).toHaveBeenCalledTimes(1); expect(errorSpy).toHaveBeenCalledWith( 'Warning: [antd: Drawer] `visible` is deprecated. Please use `open` instead.', ); expect(errorSpy).toHaveBeenCalledWith( 'Warning: [antd: Drawer] `afterVisibleChange` is deprecated. Please use `afterOpenChange` instead.', ); errorSpy.mockRestore(); }); it('should support children ref', () => { const fn = jest.fn(); const refCallback = (ref: HTMLDivElement | null) => { expect(typeof ref).toBe('object'); fn(); }; const RefDemo: React.FC = () => { const ref = React.useRef(null); const [open, setOpen] = React.useState(false); React.useEffect(() => { if (open) { refCallback(ref.current!); } }, [open]); return ( <> setOpen(true)}>open
); }; const { container } = render(); fireEvent.click(container.querySelector('a')!); expect(fn).toHaveBeenCalled(); }); });