ant-design/components/drawer/__tests__/DrawerEvent.test.js

119 lines
3.5 KiB
JavaScript
Raw Normal View History

2018-06-21 12:32:13 +08:00
import React from 'react';
import { mount } from 'enzyme';
import Drawer from '..';
import Button from '../../button';
class DrawerEventTester extends React.Component {
constructor(props) {
super(props);
this.state = { visible: false };
}
2018-06-26 00:22:50 +08:00
2018-06-21 12:32:13 +08:00
componentDidMount() {
this.setState({ visible: true }); // eslint-disable-line react/no-did-mount-set-state
}
2018-06-26 00:22:50 +08:00
2018-06-21 12:32:13 +08:00
onClose = () => {
this.setState({
visible: false,
});
};
2018-06-26 00:22:50 +08:00
2018-06-21 12:32:13 +08:00
open = () => {
this.setState({
visible: true,
});
2018-12-07 16:17:45 +08:00
};
2018-06-26 00:22:50 +08:00
2018-06-21 12:32:13 +08:00
render() {
2018-06-26 00:22:50 +08:00
const { visible } = this.state;
2018-06-21 12:32:13 +08:00
return (
<div>
<Button onClick={this.open}>open</Button>
2018-12-07 16:17:45 +08:00
<Drawer visible={visible} onClose={this.onClose} getContainer={false} {...this.props}>
2018-06-21 12:32:13 +08:00
Here is content of Drawer
</Drawer>
</div>
);
}
}
describe('Drawer', () => {
it('render correctly', () => {
const wrapper = mount(<DrawerEventTester />);
const body = wrapper.find('.ant-drawer-body').exists();
expect(body).toBe(true);
wrapper.find('button.ant-btn').simulate('click');
const content = wrapper.find('.ant-drawer-body').getDOMNode().innerHTML;
expect(content).toBe('Here is content of Drawer');
expect(wrapper.render()).toMatchSnapshot();
});
it('mask trigger onClose', () => {
const wrapper = mount(<DrawerEventTester />);
wrapper.find('button.ant-btn').simulate('click');
expect(wrapper.instance().state.visible).toBe(true);
wrapper.find('.ant-drawer-mask').simulate('click');
expect(wrapper.instance().state.visible).toBe(false);
});
it('close button trigger onClose', () => {
const wrapper = mount(<DrawerEventTester />);
wrapper.find('button.ant-btn').simulate('click');
expect(wrapper.instance().state.visible).toBe(true);
wrapper.find('.ant-drawer-close').simulate('click');
expect(wrapper.instance().state.visible).toBe(false);
});
it('maskClosable no trigger onClose', () => {
const wrapper = mount(<DrawerEventTester maskClosable={false} />);
wrapper.find('button.ant-btn').simulate('click');
expect(wrapper.instance().state.visible).toBe(true);
wrapper.find('.ant-drawer-mask').simulate('click');
expect(wrapper.instance().state.visible).toBe(true);
});
2018-07-20 18:51:10 +08:00
it('dom should be removed after close when destroyOnClose is true', () => {
2018-07-20 18:51:10 +08:00
const wrapper = mount(<DrawerEventTester destroyOnClose />);
2018-07-22 01:01:07 +08:00
wrapper.find('button.ant-btn').simulate('click');
expect(wrapper.find('.ant-drawer-wrapper-body').exists()).toBe(true);
2018-07-20 18:51:10 +08:00
wrapper.setState({
visible: false,
});
wrapper.find('.ant-drawer-wrapper-body').simulate('transitionend');
2018-07-22 01:01:07 +08:00
expect(wrapper.find('.ant-drawer-wrapper-body').exists()).toBe(false);
2018-07-20 18:51:10 +08:00
});
it('dom should be existed after close when destroyOnClose is false', () => {
const wrapper = mount(<DrawerEventTester />);
wrapper.find('button.ant-btn').simulate('click');
expect(wrapper.find('.ant-drawer-wrapper-body').exists()).toBe(true);
wrapper.setState({
visible: false,
});
wrapper.find('.ant-drawer-wrapper-body').simulate('transitionend');
expect(wrapper.find('.ant-drawer-wrapper-body').exists()).toBe(true);
});
2018-07-20 18:51:10 +08:00
it('no mask and no closable', () => {
const wrapper = mount(<DrawerEventTester destroyOnClose />);
wrapper.find('button.ant-btn').simulate('click');
expect(wrapper.instance().state.visible).toBe(true);
wrapper.find('.ant-drawer-close').simulate('click');
expect(wrapper.instance().state.visible).toBe(false);
});
2018-06-21 12:32:13 +08:00
});