2023-12-27 11:53:19 +08:00
|
|
|
import React from 'react';
|
2022-09-05 19:41:32 +08:00
|
|
|
import debounce from 'lodash/debounce';
|
|
|
|
import type RcTree from 'rc-tree';
|
2023-09-20 11:01:49 +08:00
|
|
|
import type { Key } from 'rc-tree/lib/interface';
|
2023-12-27 11:53:19 +08:00
|
|
|
|
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';
|
2023-06-07 21:59:21 +08:00
|
|
|
import { act, fireEvent, render, waitFakeTimer } from '../../../tests/utils';
|
2022-09-05 19:41:32 +08:00
|
|
|
import type { TreeProps } from '../index';
|
2023-05-24 22:03:26 +08:00
|
|
|
import Tree from '../index';
|
2018-06-04 11:20:17 +08:00
|
|
|
|
2018-11-28 15:00:03 +08:00
|
|
|
const { DirectoryTree, TreeNode } = Tree;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.mock('lodash/debounce');
|
2019-04-07 19:39:04 +08:00
|
|
|
|
2018-06-04 11:20:17 +08:00
|
|
|
describe('Directory Tree', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Tree);
|
|
|
|
mountTest(DirectoryTree);
|
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Tree);
|
|
|
|
rtlTest(DirectoryTree);
|
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
(debounce as any).mockImplementation((fn: () => void) => fn);
|
2019-04-15 14:56:21 +08:00
|
|
|
|
2018-06-04 11:20:17 +08:00
|
|
|
beforeAll(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
2018-06-04 11:20:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
(debounce as any).mockRestore();
|
2018-06-04 11:20:17 +08:00
|
|
|
});
|
|
|
|
|
2023-12-27 11:53:19 +08:00
|
|
|
function createTree(props?: TreeProps & React.RefAttributes<RcTree>) {
|
2018-06-04 11:20:17 +08:00
|
|
|
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', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onExpand = jest.fn();
|
2022-06-07 21:04:47 +08:00
|
|
|
const { container } = render(createTree({ onExpand }));
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-tree-node-content-wrapper')!);
|
2022-06-07 21:04:47 +08:00
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2022-06-07 21:04:47 +08:00
|
|
|
});
|
|
|
|
expect(onExpand).toHaveBeenCalledWith(['0-0'], expect.anything());
|
|
|
|
onExpand.mockReset();
|
2018-06-04 11:20:17 +08:00
|
|
|
|
2022-06-07 21:04:47 +08:00
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2022-06-07 21:04:47 +08:00
|
|
|
});
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-tree-node-content-wrapper')!);
|
2022-06-07 21:04:47 +08:00
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2022-06-07 21:04:47 +08:00
|
|
|
});
|
|
|
|
expect(onExpand).toHaveBeenCalledWith([], expect.anything());
|
2018-06-04 11:20:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('double click', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onExpand = jest.fn();
|
2022-06-07 21:04:47 +08:00
|
|
|
const { container } = render(createTree({ expandAction: 'doubleClick', onExpand }));
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.doubleClick(container.querySelector('.ant-tree-node-content-wrapper')!);
|
2022-06-07 21:04:47 +08:00
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2022-06-07 21:04:47 +08:00
|
|
|
});
|
|
|
|
expect(onExpand).toHaveBeenCalledWith(['0-0'], expect.anything());
|
|
|
|
onExpand.mockReset();
|
2018-06-04 11:20:17 +08:00
|
|
|
|
2022-06-07 21:04:47 +08:00
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2022-06-07 21:04:47 +08:00
|
|
|
});
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.doubleClick(container.querySelector('.ant-tree-node-content-wrapper')!);
|
2022-06-07 21:04:47 +08:00
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2022-06-07 21:04:47 +08:00
|
|
|
});
|
|
|
|
expect(onExpand).toHaveBeenCalledWith([], expect.anything());
|
2018-06-04 11:20:17 +08:00
|
|
|
});
|
2018-07-20 10:50:20 +08:00
|
|
|
|
|
|
|
describe('with state control', () => {
|
2023-01-06 16:48:44 +08:00
|
|
|
const StateDirTree: React.FC<TreeProps> = (props) => {
|
|
|
|
const [expandedKeys, setExpandedKeys] = React.useState<Key[]>([]);
|
|
|
|
return (
|
|
|
|
<DirectoryTree expandedKeys={expandedKeys} onExpand={setExpandedKeys} {...props}>
|
|
|
|
<TreeNode key="0-0" title="parent">
|
|
|
|
<TreeNode key="0-0-0" title="children" />
|
|
|
|
</TreeNode>
|
|
|
|
</DirectoryTree>
|
|
|
|
);
|
|
|
|
};
|
2018-07-20 10:50:20 +08:00
|
|
|
|
2023-05-24 22:03:26 +08:00
|
|
|
it('click', async () => {
|
2022-06-07 21:04:47 +08:00
|
|
|
const { container, asFragment } = render(<StateDirTree expandAction="click" />);
|
2018-07-20 10:50:20 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-tree-node-content-wrapper')!);
|
2023-06-07 21:59:21 +08:00
|
|
|
await waitFakeTimer();
|
2022-06-07 21:04:47 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
|
|
|
});
|
2023-05-24 22:03:26 +08:00
|
|
|
it('doubleClick', async () => {
|
2022-06-07 21:04:47 +08:00
|
|
|
const { container, asFragment } = render(<StateDirTree expandAction="doubleClick" />);
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.doubleClick(container.querySelector('.ant-tree-node-content-wrapper')!);
|
2023-06-07 21:59:21 +08:00
|
|
|
await waitFakeTimer();
|
2022-06-07 21:04:47 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2018-07-20 10:50:20 +08:00
|
|
|
});
|
|
|
|
});
|
2018-06-04 11:20:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('defaultExpandAll', () => {
|
2022-06-07 21:04:47 +08:00
|
|
|
const { asFragment } = render(createTree({ defaultExpandAll: true }));
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2018-06-04 11:20:17 +08:00
|
|
|
});
|
|
|
|
|
2024-02-24 12:42:21 +08:00
|
|
|
it('select multi nodes when shift key down', () => {
|
|
|
|
const treeData = [
|
|
|
|
{ title: 'leaf 0-0', key: '0-0-0', isLeaf: true },
|
|
|
|
{ title: 'leaf 0-1', key: '0-0-1', isLeaf: true },
|
|
|
|
{ title: 'leaf 1-0', key: '0-1-0', isLeaf: true },
|
|
|
|
{ title: 'leaf 1-1', key: '0-1-1', isLeaf: true },
|
|
|
|
];
|
|
|
|
const { container } = render(
|
|
|
|
<DirectoryTree multiple defaultExpandAll={false} treeData={treeData} />,
|
|
|
|
);
|
|
|
|
expect(container.querySelectorAll('.ant-tree-node-content-wrapper').length).toBe(4);
|
|
|
|
expect(container.querySelectorAll('.ant-tree-node-selected').length).toBe(0);
|
|
|
|
const leaf0 = container.querySelectorAll('.ant-tree-node-content-wrapper')[0];
|
|
|
|
const leaf1 = container.querySelectorAll('.ant-tree-node-content-wrapper')[1];
|
|
|
|
const leaf2 = container.querySelectorAll('.ant-tree-node-content-wrapper')[2];
|
|
|
|
const leaf3 = container.querySelectorAll('.ant-tree-node-content-wrapper')[3];
|
|
|
|
fireEvent.click(leaf2);
|
|
|
|
fireEvent.click(leaf0, { shiftKey: true });
|
|
|
|
expect(leaf0).toHaveClass('ant-tree-node-selected');
|
|
|
|
expect(leaf1).toHaveClass('ant-tree-node-selected');
|
|
|
|
expect(leaf2).toHaveClass('ant-tree-node-selected');
|
|
|
|
expect(leaf3).not.toHaveClass('ant-tree-node-selected');
|
|
|
|
});
|
|
|
|
|
2019-10-12 09:24:48 +08:00
|
|
|
it('DirectoryTree should expend all when use treeData and defaultExpandAll is true', () => {
|
|
|
|
const treeData = [
|
|
|
|
{
|
|
|
|
key: '0-0-0',
|
|
|
|
title: 'Folder',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
title: 'Folder2',
|
|
|
|
key: '0-0-1',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
title: 'File',
|
|
|
|
key: '0-0-2',
|
|
|
|
isLeaf: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
2022-06-07 21:04:47 +08:00
|
|
|
const { asFragment } = render(createTree({ defaultExpandAll: true, treeData }));
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2019-10-12 09:24:48 +08:00
|
|
|
});
|
|
|
|
|
2018-06-04 11:20:17 +08:00
|
|
|
it('defaultExpandParent', () => {
|
2022-06-07 21:04:47 +08:00
|
|
|
const { asFragment } = render(createTree({ defaultExpandParent: true }));
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2018-06-04 11:20:17 +08:00
|
|
|
});
|
|
|
|
|
2023-05-24 22:03:26 +08:00
|
|
|
it('expandedKeys update', async () => {
|
2022-06-07 21:04:47 +08:00
|
|
|
const { rerender, asFragment } = render(createTree());
|
|
|
|
rerender(createTree({ expandedKeys: ['0-1'] }));
|
2023-06-07 21:59:21 +08:00
|
|
|
await waitFakeTimer();
|
2022-06-07 21:04:47 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2018-06-04 11:20:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('selectedKeys update', () => {
|
2022-06-07 21:04:47 +08:00
|
|
|
const { rerender, asFragment } = render(createTree({ defaultExpandAll: true }));
|
|
|
|
rerender(createTree({ selectedKeys: ['0-1-0'] }));
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2018-06-04 11:20:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('group select', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onSelect = jest.fn();
|
2022-06-07 21:04:47 +08:00
|
|
|
const { container, asFragment } = render(
|
2018-12-07 16:17:45 +08:00
|
|
|
createTree({
|
|
|
|
defaultExpandAll: true,
|
|
|
|
expandAction: 'doubleClick',
|
|
|
|
multiple: true,
|
2019-04-09 14:20:59 +08:00
|
|
|
onSelect,
|
2018-12-07 16:17:45 +08:00
|
|
|
}),
|
|
|
|
);
|
2018-06-04 11:20:17 +08:00
|
|
|
|
2022-06-07 21:04:47 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-tree-node-content-wrapper')[0]);
|
2019-04-09 14:20:59 +08:00
|
|
|
expect(onSelect.mock.calls[0][1].selected).toBeTruthy();
|
|
|
|
expect(onSelect.mock.calls[0][1].selectedNodes.length).toBe(1);
|
|
|
|
|
|
|
|
// Click twice should keep selected
|
2022-06-07 21:04:47 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-tree-node-content-wrapper')[0]);
|
2019-04-09 14:20:59 +08:00
|
|
|
expect(onSelect.mock.calls[1][1].selected).toBeTruthy();
|
|
|
|
expect(onSelect.mock.calls[0][0]).toEqual(onSelect.mock.calls[1][0]);
|
|
|
|
expect(onSelect.mock.calls[1][1].selectedNodes.length).toBe(1);
|
2018-06-04 11:20:17 +08:00
|
|
|
|
2022-06-07 21:04:47 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-tree-node-content-wrapper')[1], {
|
|
|
|
ctrlKey: true,
|
|
|
|
});
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2019-04-09 14:20:59 +08:00
|
|
|
expect(onSelect.mock.calls[2][0].length).toBe(2);
|
|
|
|
expect(onSelect.mock.calls[2][1].selected).toBeTruthy();
|
|
|
|
expect(onSelect.mock.calls[2][1].selectedNodes.length).toBe(2);
|
2018-06-04 11:20:17 +08:00
|
|
|
|
2022-06-07 21:04:47 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-tree-node-content-wrapper')[4], {
|
|
|
|
shiftKey: true,
|
|
|
|
});
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2019-04-09 14:20:59 +08:00
|
|
|
expect(onSelect.mock.calls[3][0].length).toBe(5);
|
|
|
|
expect(onSelect.mock.calls[3][1].selected).toBeTruthy();
|
|
|
|
expect(onSelect.mock.calls[3][1].selectedNodes.length).toBe(5);
|
2018-06-04 11:20:17 +08:00
|
|
|
});
|
2020-04-25 15:30:13 +08:00
|
|
|
|
|
|
|
it('onDoubleClick', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onDoubleClick = jest.fn();
|
2022-06-07 21:04:47 +08:00
|
|
|
const { container } = render(createTree({ onDoubleClick }));
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.doubleClick(container.querySelector('.ant-tree-node-content-wrapper')!);
|
2022-08-30 10:57:13 +08:00
|
|
|
expect(onDoubleClick).toHaveBeenCalled();
|
2020-04-25 15:30:13 +08:00
|
|
|
});
|
2020-06-19 15:45:52 +08:00
|
|
|
|
|
|
|
it('should not expand tree now when pressing ctrl', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onExpand = jest.fn();
|
|
|
|
const onSelect = jest.fn();
|
2022-06-07 21:04:47 +08:00
|
|
|
const { container } = render(createTree({ onExpand, onSelect }));
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-tree-node-content-wrapper')!, { ctrlKey: true });
|
2020-06-19 15:45:52 +08:00
|
|
|
expect(onExpand).not.toHaveBeenCalled();
|
|
|
|
expect(onSelect).toHaveBeenCalledWith(
|
|
|
|
['0-0'],
|
|
|
|
expect.objectContaining({ event: 'select', nativeEvent: expect.anything() }),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not expand tree now when click leaf node', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onExpand = jest.fn();
|
|
|
|
const onSelect = jest.fn();
|
2022-06-07 21:04:47 +08:00
|
|
|
const { container } = render(
|
2020-06-19 15:45:52 +08:00
|
|
|
createTree({
|
|
|
|
onExpand,
|
|
|
|
onSelect,
|
|
|
|
defaultExpandAll: true,
|
|
|
|
treeData: [
|
|
|
|
{
|
|
|
|
key: '0-0-0',
|
|
|
|
title: 'Folder',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
title: 'Folder2',
|
|
|
|
key: '0-0-1',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
title: 'File',
|
|
|
|
key: '0-0-2',
|
|
|
|
isLeaf: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
2022-06-07 21:04:47 +08:00
|
|
|
const nodeList = container.querySelectorAll('.ant-tree-node-content-wrapper');
|
|
|
|
fireEvent.click(nodeList[nodeList.length - 1]);
|
2020-06-19 15:45:52 +08:00
|
|
|
expect(onExpand).not.toHaveBeenCalled();
|
|
|
|
expect(onSelect).toHaveBeenCalledWith(
|
|
|
|
['0-0-2'],
|
|
|
|
expect.objectContaining({ event: 'select', nativeEvent: expect.anything() }),
|
|
|
|
);
|
|
|
|
});
|
2020-08-11 14:22:16 +08:00
|
|
|
|
|
|
|
it('ref support', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
const treeRef = React.createRef<RcTree>();
|
2022-06-07 21:04:47 +08:00
|
|
|
render(createTree({ ref: treeRef }));
|
2022-09-05 19:41:32 +08:00
|
|
|
expect('scrollTo' in treeRef.current!).toBeTruthy();
|
2020-08-11 14:22:16 +08:00
|
|
|
});
|
2023-10-11 17:03:15 +08:00
|
|
|
|
|
|
|
it('fieldNames support', () => {
|
|
|
|
const treeData = [
|
|
|
|
{
|
|
|
|
id: '0-0-0',
|
|
|
|
label: 'Folder',
|
|
|
|
child: [
|
|
|
|
{
|
|
|
|
label: 'Folder2',
|
|
|
|
id: '0-0-1',
|
|
|
|
child: [
|
|
|
|
{
|
|
|
|
label: 'File',
|
|
|
|
id: '0-0-2',
|
|
|
|
isLeaf: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const onSelect = jest.fn();
|
|
|
|
const { container } = render(
|
|
|
|
createTree({
|
|
|
|
defaultExpandAll: true,
|
|
|
|
// @ts-ignore
|
|
|
|
treeData,
|
|
|
|
onSelect,
|
|
|
|
fieldNames: { key: 'id', title: 'label', children: 'child' },
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
fireEvent.click(container.querySelectorAll('.ant-tree-node-content-wrapper')[0]);
|
|
|
|
expect(onSelect.mock.calls[0][1].selectedNodes.length).toBe(1);
|
|
|
|
});
|
2018-06-04 11:20:17 +08:00
|
|
|
});
|