ant-design/components/drawer/__tests__/DrawerEvent.test.tsx
thinkasany 2c3c5d169c
Some checks are pending
Publish Any Commit / build (push) Waiting to run
✅ test v6 / lint (push) Waiting to run
✅ test v6 / test-react-legacy (18, 1/2) (push) Waiting to run
✅ test v6 / test-react-legacy (18, 2/2) (push) Waiting to run
✅ test v6 / test-node (push) Waiting to run
✅ test v6 / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test v6 / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test v6 / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test v6 / test-coverage (push) Blocked by required conditions
✅ test v6 / build (push) Waiting to run
✅ test v6 / test lib/es module (es, 1/2) (push) Waiting to run
✅ test v6 / test lib/es module (es, 2/2) (push) Waiting to run
✅ test v6 / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test v6 / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
feat: improve Semantic for Drawer (#52247)
* feat: improve Semantic for Drawer

* update content to section

* fix

* update snap

* sort
2025-01-08 15:31:57 +08:00

155 lines
4.7 KiB
TypeScript

import React from 'react';
import type { DrawerProps } from '..';
import Drawer from '..';
import { act, fireEvent, render } from '../../../tests/utils';
const DrawerTest: React.FC<DrawerProps> = (props) => (
<Drawer open getContainer={false} {...props}>
Here is content of Drawer
</Drawer>
);
describe('Drawer', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
it('render correctly', () => {
const { container, asFragment, rerender } = render(<DrawerTest />);
expect(container.querySelector('.ant-drawer-body')).toBeTruthy();
rerender(<DrawerTest open={false} />);
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(<DrawerTest onClose={onClose} />);
fireEvent.click(container.querySelector('.ant-drawer-mask')!);
expect(onClose).toHaveBeenCalled();
});
it('close button trigger onClose', () => {
const onClose = jest.fn();
const { container } = render(<DrawerTest onClose={onClose} />);
fireEvent.click(container.querySelector('.ant-drawer-close')!);
expect(onClose).toHaveBeenCalled();
});
it('maskClosable no trigger onClose', () => {
const onClose = jest.fn();
const { container } = render(<DrawerTest onClose={onClose} maskClosable={false} />);
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(<DrawerTest destroyOnClose />);
expect(container.querySelector('.ant-drawer')).toBeTruthy();
rerender(<DrawerTest destroyOnClose open={false} />);
act(() => {
jest.runAllTimers();
});
expect(container.querySelector('.ant-drawer')).toBeFalsy();
});
it('dom should be existed after close when destroyOnClose is false', () => {
const { container, rerender } = render(<DrawerTest />);
expect(container.querySelector('.ant-drawer')).toBeTruthy();
rerender(<DrawerTest open={false} />);
act(() => {
jest.runAllTimers();
});
fireEvent.animationEnd(container.querySelector('.ant-drawer-section')!);
expect(container.querySelector('.ant-drawer')).toBeTruthy();
});
it('dom should be existed after close twice when getContainer is false', () => {
const { container, rerender } = render(<DrawerTest open getContainer={false} />);
expect(container.querySelector('.ant-drawer-section')).toBeTruthy();
// Hide
rerender(<DrawerTest open={false} getContainer={false} />);
act(() => {
jest.runAllTimers();
});
fireEvent.animationEnd(container.querySelector('.ant-drawer-content-wrapper')!);
expect(container.querySelector('.ant-drawer-content-wrapper-hidden')).toBeTruthy();
// Show
rerender(<DrawerTest open getContainer={false} />);
expect(container.querySelector('.ant-drawer-content-wrapper')).toBeTruthy();
expect(container.querySelector('.ant-drawer-content-wrapper-hidden')).toBeFalsy();
// Hide
rerender(<DrawerTest open={false} getContainer={false} />);
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(<DrawerTest open afterOpenChange={afterOpenChange} />);
rerender(<DrawerTest open={false} afterOpenChange={afterOpenChange} />);
act(() => {
jest.runAllTimers();
});
fireEvent.animationEnd(container.querySelector('.ant-drawer-content-wrapper')!);
expect(afterOpenChange).toHaveBeenCalledTimes(1);
});
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<HTMLDivElement>(null);
const [open, setOpen] = React.useState(false);
React.useEffect(() => {
if (open) {
refCallback(ref.current!);
}
}, [open]);
return (
<>
<a onClick={() => setOpen(true)}>open</a>
<Drawer open={open}>
<div ref={ref} />
</Drawer>
</>
);
};
const { container } = render(<RefDemo />);
fireEvent.click(container.querySelector('a')!);
expect(fn).toHaveBeenCalled();
});
});