2018-06-21 12:32:13 +08:00
|
|
|
import React from 'react';
|
2022-09-05 19:41:32 +08:00
|
|
|
import type { DrawerProps } from '..';
|
2018-06-21 12:32:13 +08:00
|
|
|
import Drawer from '..';
|
2022-07-25 21:04:43 +08:00
|
|
|
import { act, fireEvent, render } from '../../../tests/utils';
|
2018-06-21 12:32:13 +08:00
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const DrawerTest: React.FC<DrawerProps> = (props) => (
|
2022-09-05 19:41:32 +08:00
|
|
|
<Drawer open getContainer={false} {...props}>
|
|
|
|
Here is content of Drawer
|
|
|
|
</Drawer>
|
|
|
|
);
|
2022-04-18 21:02:11 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
describe('Drawer', () => {
|
2022-07-25 21:04:43 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2018-06-21 12:32:13 +08:00
|
|
|
it('render correctly', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
const { container, asFragment, rerender } = render(<DrawerTest />);
|
2022-04-18 21:02:11 +08:00
|
|
|
expect(container.querySelector('.ant-drawer-body')).toBeTruthy();
|
2018-06-21 12:32:13 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
rerender(<DrawerTest open={false} />);
|
2018-06-21 12:32:13 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
expect(container.querySelector('.ant-drawer-body')?.textContent).toEqual(
|
2022-04-18 21:02:11 +08:00
|
|
|
'Here is content of Drawer',
|
|
|
|
);
|
2018-06-21 12:32:13 +08:00
|
|
|
|
2022-04-18 21:02:11 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2018-06-21 12:32:13 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('mask trigger onClose', () => {
|
2022-04-18 21:02:11 +08:00
|
|
|
const onClose = jest.fn();
|
2022-09-05 19:41:32 +08:00
|
|
|
const { container } = render(<DrawerTest onClose={onClose} />);
|
2018-06-21 12:32:13 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-drawer-mask')!);
|
2022-04-18 21:02:11 +08:00
|
|
|
expect(onClose).toHaveBeenCalled();
|
2018-06-21 12:32:13 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('close button trigger onClose', () => {
|
2022-04-18 21:02:11 +08:00
|
|
|
const onClose = jest.fn();
|
2022-09-05 19:41:32 +08:00
|
|
|
const { container } = render(<DrawerTest onClose={onClose} />);
|
2018-06-21 12:32:13 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-drawer-close')!);
|
2022-04-18 21:02:11 +08:00
|
|
|
expect(onClose).toHaveBeenCalled();
|
2018-06-21 12:32:13 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('maskClosable no trigger onClose', () => {
|
2022-04-18 21:02:11 +08:00
|
|
|
const onClose = jest.fn();
|
2022-09-05 19:41:32 +08:00
|
|
|
const { container } = render(<DrawerTest onClose={onClose} maskClosable={false} />);
|
2018-06-21 12:32:13 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-drawer-mask')!);
|
2022-04-18 21:02:11 +08:00
|
|
|
expect(onClose).not.toHaveBeenCalled();
|
2018-06-21 12:32:13 +08:00
|
|
|
});
|
2018-07-20 18:51:10 +08:00
|
|
|
|
2020-06-08 19:24:29 +08:00
|
|
|
it('dom should be removed after close when destroyOnClose is true', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
const { container, rerender } = render(<DrawerTest destroyOnClose />);
|
2022-07-25 21:04:43 +08:00
|
|
|
expect(container.querySelector('.ant-drawer')).toBeTruthy();
|
2018-07-20 18:51:10 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
rerender(<DrawerTest destroyOnClose open={false} />);
|
2022-07-25 21:04:43 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-04-18 21:02:11 +08:00
|
|
|
|
2022-07-25 21:04:43 +08:00
|
|
|
expect(container.querySelector('.ant-drawer')).toBeFalsy();
|
2020-06-08 19:24:29 +08:00
|
|
|
});
|
|
|
|
|
2022-04-18 21:02:11 +08:00
|
|
|
it('dom should be existed after close when destroyOnClose is false', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
const { container, rerender } = render(<DrawerTest />);
|
2022-07-25 21:04:43 +08:00
|
|
|
expect(container.querySelector('.ant-drawer')).toBeTruthy();
|
2018-07-20 18:51:10 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
rerender(<DrawerTest open={false} />);
|
2022-07-25 21:04:43 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.animationEnd(container.querySelector('.ant-drawer-content')!);
|
2018-07-20 18:51:10 +08:00
|
|
|
|
2022-07-25 21:04:43 +08:00
|
|
|
expect(container.querySelector('.ant-drawer')).toBeTruthy();
|
2018-07-20 18:51:10 +08:00
|
|
|
});
|
2022-07-25 21:04:43 +08:00
|
|
|
|
2022-06-01 16:46:55 +08:00
|
|
|
it('dom should be existed after close twice when getContainer is false', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
const { container, rerender } = render(<DrawerTest open getContainer={false} />);
|
2022-07-25 21:04:43 +08:00
|
|
|
expect(container.querySelector('.ant-drawer-content')).toBeTruthy();
|
|
|
|
|
|
|
|
// Hide
|
2022-09-05 19:41:32 +08:00
|
|
|
rerender(<DrawerTest open={false} getContainer={false} />);
|
2022-07-25 21:04:43 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.animationEnd(container.querySelector('.ant-drawer-content-wrapper')!);
|
2022-07-27 23:44:45 +08:00
|
|
|
expect(container.querySelector('.ant-drawer-content-wrapper-hidden')).toBeTruthy();
|
2022-06-01 16:46:55 +08:00
|
|
|
|
2022-07-25 21:04:43 +08:00
|
|
|
// Show
|
2022-09-05 19:41:32 +08:00
|
|
|
rerender(<DrawerTest open getContainer={false} />);
|
2022-07-27 23:44:45 +08:00
|
|
|
expect(container.querySelector('.ant-drawer-content-wrapper')).toBeTruthy();
|
|
|
|
expect(container.querySelector('.ant-drawer-content-wrapper-hidden')).toBeFalsy();
|
2022-06-01 16:46:55 +08:00
|
|
|
|
2022-07-25 21:04:43 +08:00
|
|
|
// Hide
|
2022-09-05 19:41:32 +08:00
|
|
|
rerender(<DrawerTest open={false} getContainer={false} />);
|
2022-07-25 21:04:43 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.animationEnd(container.querySelector('.ant-drawer-content-wrapper')!);
|
2022-07-27 23:44:45 +08:00
|
|
|
expect(container.querySelector('.ant-drawer-content-wrapper-hidden')).toBeTruthy();
|
2022-06-01 16:46:55 +08:00
|
|
|
});
|
2022-07-25 21:04:43 +08:00
|
|
|
|
2022-09-06 21:46:49 +08:00
|
|
|
it('test afterOpenChange', async () => {
|
2022-08-23 16:22:00 +08:00
|
|
|
const afterOpenChange = jest.fn();
|
2022-09-05 21:26:04 +08:00
|
|
|
const { container, rerender } = render(<DrawerTest open afterOpenChange={afterOpenChange} />);
|
|
|
|
rerender(<DrawerTest open={false} afterOpenChange={afterOpenChange} />);
|
2022-07-25 21:04:43 +08:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.animationEnd(container.querySelector('.ant-drawer-content-wrapper')!);
|
2022-07-25 21:04:43 +08:00
|
|
|
|
2022-08-30 14:22:15 +08:00
|
|
|
expect(afterOpenChange).toHaveBeenCalledTimes(1);
|
2022-05-16 10:14:26 +08:00
|
|
|
});
|
2022-07-25 21:04:43 +08:00
|
|
|
|
2022-09-06 21:46:49 +08:00
|
|
|
it('test legacy afterVisibleChange', async () => {
|
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
|
|
|
const afterVisibleChange = jest.fn();
|
|
|
|
const { container, rerender } = render(
|
|
|
|
<DrawerTest open afterVisibleChange={afterVisibleChange} />,
|
|
|
|
);
|
|
|
|
rerender(<DrawerTest visible={false} afterVisibleChange={afterVisibleChange} />);
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2022-05-25 10:50:12 +08:00
|
|
|
it('should support children ref', () => {
|
|
|
|
const fn = jest.fn();
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
const refCallback = (ref: HTMLDivElement | null) => {
|
2022-05-25 10:50:12 +08:00
|
|
|
expect(typeof ref).toBe('object');
|
|
|
|
fn();
|
|
|
|
};
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
const RefDemo: React.FC = () => {
|
|
|
|
const ref = React.useRef<HTMLDivElement>(null);
|
2022-08-23 16:22:00 +08:00
|
|
|
const [open, setOpen] = React.useState(false);
|
2022-05-25 10:50:12 +08:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2022-08-23 16:22:00 +08:00
|
|
|
if (open) {
|
2022-09-05 19:41:32 +08:00
|
|
|
refCallback(ref.current!);
|
2022-05-25 10:50:12 +08:00
|
|
|
}
|
2022-08-23 16:22:00 +08:00
|
|
|
}, [open]);
|
2022-05-25 10:50:12 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-08-23 16:22:00 +08:00
|
|
|
<a onClick={() => setOpen(true)}>open</a>
|
|
|
|
<Drawer open={open}>
|
2022-05-25 10:50:12 +08:00
|
|
|
<div ref={ref} />
|
|
|
|
</Drawer>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
const { container } = render(<RefDemo />);
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.click(container.querySelector('a')!);
|
2022-08-30 10:57:13 +08:00
|
|
|
expect(fn).toHaveBeenCalled();
|
2022-05-25 10:50:12 +08:00
|
|
|
});
|
2018-06-21 12:32:13 +08:00
|
|
|
});
|