Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#19132): fix component tree defaultExpandAll does not work when u… #19148

Merged
merged 9 commits into from Oct 12, 2019
13 changes: 11 additions & 2 deletions components/tree/DirectoryTree.tsx
Expand Up @@ -13,7 +13,12 @@ import Tree, {
AntTreeNodeSelectedEvent,
AntTreeNode,
} from './Tree';
import { calcRangeKeys, getFullKeyList, convertDirectoryKeysToNodes } from './util';
import {
calcRangeKeys,
getFullKeyList,
convertDirectoryKeysToNodes,
getFullKeyListByTreeData,
} from './util';
import Icon from '../icon';

export type ExpandAction = false | 'click' | 'doubleClick';
Expand Down Expand Up @@ -82,7 +87,11 @@ class DirectoryTree extends React.Component<DirectoryTreeProps, DirectoryTreeSta

// Expanded keys
if (defaultExpandAll) {
this.state.expandedKeys = getFullKeyList(props.children);
if (props.treeData) {
this.state.expandedKeys = getFullKeyListByTreeData(props.treeData);
} else {
this.state.expandedKeys = getFullKeyList(props.children);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to:

if (treeData) {
  this.state.expandedKeys = ...
} else {
  this.state.expandedKeys = ...
}

treeData priority should be higher than children.

} else if (defaultExpandParent) {
this.state.expandedKeys = conductExpandParent(
expandedKeys || defaultExpandedKeys,
Expand Down
49 changes: 48 additions & 1 deletion components/tree/__tests__/util.test.js
@@ -1,7 +1,7 @@
import React from 'react';
import { mount } from 'enzyme';
import Tree from '../index';
import { calcRangeKeys } from '../util';
import { calcRangeKeys, getFullKeyListByTreeData } from '../util';

const { TreeNode } = Tree;

Expand Down Expand Up @@ -32,4 +32,51 @@ describe('Tree util', () => {
const target = ['0-0-0', '0-0-1', '0-1', '0-2', '0-2-0', '0-2-0-0', '0-2-0-1'];
expect(keys.sort()).toEqual(target.sort());
});

it('calc range keys by treeData', () => {
const treeData = [
{
key: '0-0-0',
title: 'Folder',
children: [
{
title: 'Folder2',
key: '0-0-1',
children: [
{
title: 'File',
key: '0-0-2',
isLeaf: true,
},
],
},
],
},
{
key: '0-0-3',
title: 'Folder',
children: [
{
title: 'File',
key: '0-0-4',
isLeaf: true,
},
{
title: 'File',
key: '0-0-5',
isLeaf: true,
},
{
title: 'File',
key: '0-0-6',
isLeaf: true,
},
],
},
];

const keys = getFullKeyListByTreeData(treeData);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should test Tree component behavior instead of testing inner function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry, this is my first contribution for ant-design, I have see documents but I still don't understand how to write this test, can you help me ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I white test case in directory.test.js ? like this:


it('defaultExpandAll when use treeData', () => {
    const treeData = [
      {
        key: '0-0-0',
        title: 'Folder',
        children: [
          {
            title: 'Folder2',
            key: '0-0-1',
            children: [
              {
                title: 'File',
                key: '0-0-2',
                isLeaf: true,
              },
            ],
          },
        ],
      },
      {
        key: '0-0-3',
        title: 'Folder',
        children: [
          {
            title: 'File',
            key: '0-0-4',
            isLeaf: true,
          },
          {
            title: 'File',
            key: '0-0-5',
            isLeaf: true,
          },
          {
            title: 'File',
            key: '0-0-6',
            isLeaf: true,
          },
        ],
      },
    ];
    const wrapper = render(createTree({ defaultExpandAll: true, treeData }));
    expect(wrapper).toMatchSnapshot();
  });

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just simulate the exact problem in test case, like test case in https://github.com/ant-design/ant-design/blob/master/components/tree/__tests__/directory.test.js or

it('inlineCollapsed should works well when specify a not existed default openKeys', () => {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is right ? I don't sure...

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,
              },
            ],
          },
        ],
      },
    ];
    const wrapper = render(createTree({ defaultExpandAll: true, treeData }));
    expect(wrapper).toMatchSnapshot();
  });

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this test case can reflect the problem you meet, then it is right.

const target = ['0-0-0', '0-0-1', '0-0-2', '0-0-3', '0-0-4', '0-0-5', '0-0-6'];
expect(keys.sort()).toEqual(target.sort());
});
});
12 changes: 12 additions & 0 deletions components/tree/util.ts
Expand Up @@ -101,3 +101,15 @@ export function convertDirectoryKeysToNodes(
});
return nodes;
}

export function getFullKeyListByTreeData(treeData: any[], keys: any = []): any[] {
(treeData || []).forEach(item => {
if (item.children) {
keys.push(item.key);
keys.concat(getFullKeyListByTreeData(item.children, keys));
} else {
keys.push(item.key);
}
});
return keys;
}