2018-06-19 16:47:52 +08:00
|
|
|
import React from 'react';
|
2022-07-25 21:04:43 +08:00
|
|
|
import { act } from 'react-dom/test-utils';
|
2018-06-19 16:47:52 +08:00
|
|
|
import Drawer from '..';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2022-07-25 21:04:43 +08:00
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
2022-06-22 14:57:09 +08:00
|
|
|
import ConfigProvider from '../../config-provider';
|
2018-06-19 16:47:52 +08:00
|
|
|
|
2020-12-09 17:12:32 +08:00
|
|
|
const DrawerTest = ({ getContainer }) => (
|
|
|
|
<div>
|
|
|
|
<Drawer visible width={400} getContainer={getContainer}>
|
|
|
|
Here is content of Drawer
|
|
|
|
</Drawer>
|
|
|
|
</div>
|
|
|
|
);
|
2020-06-07 19:05:44 +08:00
|
|
|
|
2018-06-19 16:47:52 +08:00
|
|
|
describe('Drawer', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Drawer);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Drawer);
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2022-07-25 21:04:43 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
function triggerMotion() {
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
const mask = document.querySelector('.ant-drawer-mask');
|
|
|
|
if (mask) {
|
|
|
|
fireEvent.animationEnd(mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
const panel = document.querySelector('.ant-drawer-content');
|
|
|
|
if (panel) {
|
|
|
|
fireEvent.animationEnd(panel);
|
|
|
|
}
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-20 14:40:03 +08:00
|
|
|
it('render correctly', () => {
|
2022-06-01 09:40:25 +08:00
|
|
|
const { container: wrapper } = render(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Drawer visible width={400} getContainer={false}>
|
2018-07-20 14:40:03 +08:00
|
|
|
Here is content of Drawer
|
2018-12-07 16:17:45 +08:00
|
|
|
</Drawer>,
|
2018-07-20 14:40:03 +08:00
|
|
|
);
|
2022-07-25 21:04:43 +08:00
|
|
|
|
|
|
|
triggerMotion();
|
|
|
|
|
2022-06-01 09:40:25 +08:00
|
|
|
expect(wrapper.firstChild).toMatchSnapshot();
|
2018-07-20 14:40:03 +08:00
|
|
|
});
|
|
|
|
|
2020-06-07 19:05:44 +08:00
|
|
|
it('getContainer return undefined', () => {
|
2022-06-01 09:40:25 +08:00
|
|
|
const { container: wrapper, rerender } = render(<DrawerTest getContainer={() => undefined} />);
|
2022-07-25 21:04:43 +08:00
|
|
|
triggerMotion();
|
2022-06-01 09:40:25 +08:00
|
|
|
expect(wrapper.firstChild).toMatchSnapshot();
|
2022-07-25 21:04:43 +08:00
|
|
|
|
2022-06-01 09:40:25 +08:00
|
|
|
rerender(<DrawerTest getContainer={false} />);
|
2022-07-25 21:04:43 +08:00
|
|
|
triggerMotion();
|
2022-06-01 09:40:25 +08:00
|
|
|
expect(wrapper.firstChild).toMatchSnapshot();
|
2020-06-07 19:05:44 +08:00
|
|
|
});
|
|
|
|
|
2018-08-10 13:32:33 +08:00
|
|
|
it('render top drawer', () => {
|
2022-06-01 09:40:25 +08:00
|
|
|
const { container: wrapper } = render(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Drawer visible height={400} placement="top" getContainer={false}>
|
2018-08-10 13:32:33 +08:00
|
|
|
Here is content of Drawer
|
2018-12-07 16:17:45 +08:00
|
|
|
</Drawer>,
|
2018-08-10 13:32:33 +08:00
|
|
|
);
|
2022-07-25 21:04:43 +08:00
|
|
|
|
|
|
|
triggerMotion();
|
2022-06-01 09:40:25 +08:00
|
|
|
expect(wrapper.firstChild).toMatchSnapshot();
|
2018-08-10 13:32:33 +08:00
|
|
|
});
|
|
|
|
|
2018-07-20 14:40:03 +08:00
|
|
|
it('have a title', () => {
|
2022-06-01 09:40:25 +08:00
|
|
|
const { container: wrapper } = render(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Drawer visible title="Test Title" getContainer={false}>
|
2018-07-20 14:40:03 +08:00
|
|
|
Here is content of Drawer
|
2018-12-07 16:17:45 +08:00
|
|
|
</Drawer>,
|
2018-07-20 14:40:03 +08:00
|
|
|
);
|
2022-07-25 21:04:43 +08:00
|
|
|
|
|
|
|
triggerMotion();
|
2022-06-01 09:40:25 +08:00
|
|
|
expect(wrapper.firstChild).toMatchSnapshot();
|
2018-07-20 14:40:03 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('closable is false', () => {
|
2022-06-01 09:40:25 +08:00
|
|
|
const { container: wrapper } = render(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Drawer visible closable={false} getContainer={false}>
|
2018-07-20 14:40:03 +08:00
|
|
|
Here is content of Drawer
|
2018-12-07 16:17:45 +08:00
|
|
|
</Drawer>,
|
2018-07-20 14:40:03 +08:00
|
|
|
);
|
2022-07-25 21:04:43 +08:00
|
|
|
|
|
|
|
triggerMotion();
|
2022-06-01 09:40:25 +08:00
|
|
|
expect(wrapper.firstChild).toMatchSnapshot();
|
2018-07-20 14:40:03 +08:00
|
|
|
});
|
|
|
|
|
2018-06-21 12:32:13 +08:00
|
|
|
it('destroyOnClose is true', () => {
|
2022-06-01 09:40:25 +08:00
|
|
|
const { container: wrapper } = render(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Drawer destroyOnClose visible={false} getContainer={false}>
|
2018-06-26 11:19:49 +08:00
|
|
|
Here is content of Drawer
|
2018-12-07 16:17:45 +08:00
|
|
|
</Drawer>,
|
2018-06-26 11:19:49 +08:00
|
|
|
);
|
2022-07-25 21:04:43 +08:00
|
|
|
|
|
|
|
triggerMotion();
|
2022-06-01 09:40:25 +08:00
|
|
|
expect(wrapper.firstChild).toMatchSnapshot();
|
2018-06-19 16:47:52 +08:00
|
|
|
});
|
2018-07-22 11:27:48 +08:00
|
|
|
|
2018-08-05 14:16:08 +08:00
|
|
|
it('className is test_drawer', () => {
|
2022-06-01 09:40:25 +08:00
|
|
|
const { container: wrapper } = render(
|
2022-07-25 21:04:43 +08:00
|
|
|
<Drawer destroyOnClose visible className="test_drawer" getContainer={false}>
|
2018-07-22 11:27:48 +08:00
|
|
|
Here is content of Drawer
|
2018-12-07 16:17:45 +08:00
|
|
|
</Drawer>,
|
2018-07-22 11:27:48 +08:00
|
|
|
);
|
2022-07-25 21:04:43 +08:00
|
|
|
|
|
|
|
triggerMotion();
|
2022-06-01 09:40:25 +08:00
|
|
|
expect(wrapper.firstChild).toMatchSnapshot();
|
2018-07-22 11:27:48 +08:00
|
|
|
});
|
2019-10-07 18:58:10 +08:00
|
|
|
|
|
|
|
it('style/drawerStyle/headerStyle/bodyStyle should work', () => {
|
|
|
|
const style = {
|
|
|
|
backgroundColor: '#08c',
|
|
|
|
};
|
2022-06-01 09:40:25 +08:00
|
|
|
const { container: wrapper } = render(
|
2019-10-07 18:58:10 +08:00
|
|
|
<Drawer
|
|
|
|
visible
|
|
|
|
style={style}
|
|
|
|
drawerStyle={style}
|
|
|
|
headerStyle={style}
|
|
|
|
bodyStyle={style}
|
|
|
|
getContainer={false}
|
|
|
|
>
|
|
|
|
Here is content of Drawer
|
|
|
|
</Drawer>,
|
|
|
|
);
|
2022-07-25 21:04:43 +08:00
|
|
|
|
|
|
|
triggerMotion();
|
2022-06-01 09:40:25 +08:00
|
|
|
expect(wrapper.firstChild).toMatchSnapshot();
|
2019-10-07 18:58:10 +08:00
|
|
|
});
|
2020-01-06 14:56:00 +08:00
|
|
|
|
|
|
|
it('have a footer', () => {
|
2022-06-01 09:40:25 +08:00
|
|
|
const { container: wrapper } = render(
|
2020-01-06 14:56:00 +08:00
|
|
|
<Drawer visible footer="Test Footer" getContainer={false}>
|
|
|
|
Here is content of Drawer
|
|
|
|
</Drawer>,
|
|
|
|
);
|
2022-07-25 21:04:43 +08:00
|
|
|
|
|
|
|
triggerMotion();
|
2022-06-01 09:40:25 +08:00
|
|
|
expect(wrapper.firstChild).toMatchSnapshot();
|
2020-01-06 14:56:00 +08:00
|
|
|
});
|
2020-03-02 17:28:45 +08:00
|
|
|
|
|
|
|
it('forceRender works', () => {
|
2022-06-01 09:40:25 +08:00
|
|
|
const { baseElement, rerender } = render(
|
2020-03-02 17:28:45 +08:00
|
|
|
<Drawer>
|
2020-06-07 19:05:44 +08:00
|
|
|
<button type="button" className="forceRender">
|
|
|
|
should not be rendered
|
|
|
|
</button>
|
2020-03-02 17:28:45 +08:00
|
|
|
</Drawer>,
|
|
|
|
);
|
2022-06-01 09:40:25 +08:00
|
|
|
expect(baseElement.querySelectorAll('button.forceRender').length).toBe(0);
|
|
|
|
rerender(
|
2020-03-02 17:28:45 +08:00
|
|
|
<Drawer forceRender>
|
2020-06-07 19:05:44 +08:00
|
|
|
<button type="button" className="forceRender">
|
|
|
|
should be rendered
|
|
|
|
</button>
|
2020-03-02 17:28:45 +08:00
|
|
|
</Drawer>,
|
|
|
|
);
|
2022-06-01 09:40:25 +08:00
|
|
|
expect(baseElement.querySelectorAll('button.forceRender').length).toBe(1);
|
2020-06-07 19:05:44 +08:00
|
|
|
});
|
2020-06-28 22:41:59 +08:00
|
|
|
|
|
|
|
it('support closeIcon', () => {
|
2022-06-01 09:40:25 +08:00
|
|
|
const { container: wrapper } = render(
|
2020-06-28 22:41:59 +08:00
|
|
|
<Drawer visible closable closeIcon={<span>close</span>} width={400} getContainer={false}>
|
|
|
|
Here is content of Drawer
|
|
|
|
</Drawer>,
|
|
|
|
);
|
2022-07-25 21:04:43 +08:00
|
|
|
|
|
|
|
triggerMotion();
|
2022-06-01 09:40:25 +08:00
|
|
|
expect(wrapper.firstChild).toMatchSnapshot();
|
2020-06-28 22:41:59 +08:00
|
|
|
});
|
2020-10-24 14:47:44 +08:00
|
|
|
|
|
|
|
it('ConfigProvider should not warning', () => {
|
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
render(
|
2020-10-24 14:47:44 +08:00
|
|
|
<ConfigProvider virtual>
|
|
|
|
<Drawer visible>Bamboo is Light</Drawer>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(errorSpy).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
2022-08-08 22:09:51 +08:00
|
|
|
|
|
|
|
it('zIndex should work', () => {
|
|
|
|
const { container } = render(<Drawer getContainer={false} visible zIndex={903} />);
|
2022-08-09 14:31:53 +08:00
|
|
|
expect(container.querySelector('.ant-drawer')).toHaveStyle({
|
2022-08-08 22:09:51 +08:00
|
|
|
zIndex: 903,
|
|
|
|
});
|
|
|
|
});
|
2018-06-19 16:47:52 +08:00
|
|
|
});
|