2018-06-04 11:20:17 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount, render } from 'enzyme';
|
|
|
|
import Tree from '../index';
|
|
|
|
|
2018-11-28 15:00:03 +08:00
|
|
|
const { DirectoryTree, TreeNode } = Tree;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
|
|
describe('Directory Tree', () => {
|
|
|
|
beforeAll(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
function createTree(props) {
|
|
|
|
return (
|
|
|
|
<DirectoryTree {...props}>
|
|
|
|
<TreeNode key="0-0">
|
|
|
|
<TreeNode key="0-0-0" />
|
|
|
|
<TreeNode key="0-0-1" />
|
|
|
|
</TreeNode>
|
|
|
|
<TreeNode key="0-1">
|
|
|
|
<TreeNode key="0-1-0" />
|
|
|
|
<TreeNode key="0-1-1" />
|
|
|
|
</TreeNode>
|
|
|
|
</DirectoryTree>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('expand', () => {
|
|
|
|
it('click', () => {
|
|
|
|
const wrapper = mount(createTree());
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find(TreeNode)
|
|
|
|
.find('.ant-tree-node-content-wrapper')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
2018-06-04 11:20:17 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
jest.runAllTimers();
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find(TreeNode)
|
|
|
|
.find('.ant-tree-node-content-wrapper')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
2018-06-04 11:20:17 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('double click', () => {
|
|
|
|
const wrapper = mount(createTree({ expandAction: 'doubleClick' }));
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find(TreeNode)
|
|
|
|
.find('.ant-tree-node-content-wrapper')
|
|
|
|
.at(0)
|
|
|
|
.simulate('doubleClick');
|
2018-06-04 11:20:17 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
jest.runAllTimers();
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find(TreeNode)
|
|
|
|
.find('.ant-tree-node-content-wrapper')
|
|
|
|
.at(0)
|
|
|
|
.simulate('doubleClick');
|
2018-06-04 11:20:17 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
2018-07-20 10:50:20 +08:00
|
|
|
|
|
|
|
describe('with state control', () => {
|
|
|
|
class StateDirTree extends React.Component {
|
|
|
|
state = {
|
|
|
|
expandedKeys: [],
|
|
|
|
};
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
onExpand = expandedKeys => {
|
2018-07-20 10:50:20 +08:00
|
|
|
this.setState({ expandedKeys });
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { expandedKeys } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<DirectoryTree expandedKeys={expandedKeys} onExpand={this.onExpand} {...this.props}>
|
|
|
|
<TreeNode key="0-0" title="parent">
|
|
|
|
<TreeNode key="0-0-0" title="children" />
|
|
|
|
</TreeNode>
|
|
|
|
</DirectoryTree>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
['click', 'doubleClick'].forEach(action => {
|
2018-07-20 10:50:20 +08:00
|
|
|
it(action, () => {
|
2018-12-07 20:02:01 +08:00
|
|
|
const wrapper = mount(<StateDirTree expandAction={action} />);
|
2018-07-20 10:50:20 +08:00
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find(TreeNode)
|
|
|
|
.find('.ant-tree-node-content-wrapper')
|
|
|
|
.at(0)
|
|
|
|
.simulate(action);
|
2018-07-20 10:50:20 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-06-04 11:20:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('defaultExpandAll', () => {
|
|
|
|
const wrapper = render(createTree({ defaultExpandAll: true }));
|
|
|
|
expect(wrapper).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('defaultExpandParent', () => {
|
|
|
|
const wrapper = render(createTree({ defaultExpandParent: true }));
|
|
|
|
expect(wrapper).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('expandedKeys update', () => {
|
|
|
|
const wrapper = mount(createTree());
|
|
|
|
wrapper.setProps({ expandedKeys: ['0-1'] });
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('selectedKeys update', () => {
|
|
|
|
const wrapper = mount(createTree({ defaultExpandAll: true }));
|
|
|
|
wrapper.setProps({ selectedKeys: ['0-1-0'] });
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('group select', () => {
|
|
|
|
let nativeEventProto = null;
|
2018-12-07 20:02:01 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
createTree({
|
|
|
|
defaultExpandAll: true,
|
|
|
|
expandAction: 'doubleClick',
|
|
|
|
multiple: true,
|
|
|
|
onClick: e => {
|
|
|
|
nativeEventProto = Object.getPrototypeOf(e.nativeEvent);
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2018-06-04 11:20:17 +08:00
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find(TreeNode)
|
|
|
|
.find('.ant-tree-node-content-wrapper')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
|
|
// React not simulate full of NativeEvent. Hook it.
|
|
|
|
// Ref: https://github.com/facebook/react/blob/master/packages/react-dom/src/test-utils/ReactTestUtils.js#L360
|
|
|
|
nativeEventProto.ctrlKey = true;
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find(TreeNode)
|
|
|
|
.find('.ant-tree-node-content-wrapper')
|
|
|
|
.at(1)
|
|
|
|
.simulate('click');
|
2018-06-04 11:20:17 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
|
|
|
|
delete nativeEventProto.ctrlKey;
|
|
|
|
nativeEventProto.shiftKey = true;
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find(TreeNode)
|
|
|
|
.find('.ant-tree-node-content-wrapper')
|
|
|
|
.at(4)
|
|
|
|
.simulate('click');
|
2018-06-04 11:20:17 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
|
|
|
|
delete nativeEventProto.shiftKey;
|
|
|
|
});
|
|
|
|
});
|