Skip to content

Commit

Permalink
fix: ts types error fix
Browse files Browse the repository at this point in the history
ts types error fix
  • Loading branch information
zhangtengjin authored and wewoor committed Mar 11, 2021
1 parent 883de0f commit e2fb056
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/controller/explorer/explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ export class ExplorerController
data: explorerState.folderTree?.data,
contextMenu: explorerState.folderTree?.contextMenu,
};
return <FolderTreeView {...folderProps} {...folderTreeController} />;
return (
<FolderTreeView
{...folderProps}
{...folderTreeController}
/>
);
},
};

Expand Down
5 changes: 2 additions & 3 deletions src/services/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export function searchById(id) {
return (item) => item.id === id;
}


export interface IIndex<T> {
id?: number;
node?: T;
Expand Down Expand Up @@ -43,7 +42,7 @@ export class TreeViewUtil<T = any> implements ITreeInterface<T> {
...
}
*/
constructor(obj, childNodeName = 'children') {
constructor(obj?, childNodeName = 'children') {
this.count = 1; // nodes count
this.obj = obj || { [childNodeName]: [] };
this.indexes = {};
Expand Down Expand Up @@ -212,4 +211,4 @@ export class TreeViewUtil<T = any> implements ITreeInterface<T> {
destIndex[this.childNodeName] = destIndex[this.childNodeName] || [];
return this.insert(obj, destId, destIndex[this.childNodeName].length);
}
}
}
261 changes: 261 additions & 0 deletions src/services/workbench/__tests__/explorerService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
import 'reflect-metadata';
import { TreeViewUtil } from '../../helper';
// import { container } from 'tsyringe';
// import { IExplorerService, ExplorerService } from '../explorerService';
describe('Test panelService', () => {
// TODO: error: Attempted to construct an undefined constructor. Could mean a circular dependency problem. Try using `delay` function...
// const explorerService = container.resolve<IExplorerService>(ExplorerService);
// console.log('explorerService', explorerService)
});

describe('Test TreeViewUtil Class', () => {
const createTree = () => {
return new TreeViewUtil({
id: 1,
module: 'root',
children: [
{
id: 2,
module: 'a',
children: [{ id: 3, module: 'c' }],
},
{
id: 4,
module: 'b',
},
],
});
};

test('empty Tree', () => {
const tree = new TreeViewUtil();
expect(tree.obj).toEqual({ children: [] });
});

test('custom TreeViewUtil childNodeName', () => {
let tree = new TreeViewUtil(null, 'properties');
expect(tree.obj).toEqual({ properties: [] });
tree = new TreeViewUtil(
{
id: 10,
propName: 'root',
properties: [
{
id: 11,
propName: 'a',
properties: [{ id: 12, propName: 'c' }],
},
{
id: 13,
propName: 'b',
},
],
},
'properties'
);

const { obj, indexes } = tree;
expect(indexes['10']).toEqual({
id: 10,
node: obj,
properties: [11, 13],
});

expect(indexes['11']).toEqual({
id: 11,
parent: 10,
properties: [12],
node: obj.properties[0],
next: 13,
});

expect(indexes['12']).toEqual({
id: 12,
parent: 11,
node: obj.properties[0].properties[0],
});

expect(indexes['13']).toEqual({
id: 13,
parent: 10,
node: obj.properties[1],
prev: 11,
});
});

test('Test TreeViewUtil generate method', () => {
const tree = createTree();
const { obj, indexes } = tree;

expect(indexes['1']).toEqual({
id: 1,
node: obj,
children: [2, 4],
});

expect(indexes['2']).toEqual({
id: 2,
parent: 1,
children: [3],
node: obj.children[0],
next: 4,
});

expect(indexes['3']).toEqual({
id: 3,
parent: 2,
node: obj.children[0].children[0],
});

expect(indexes['4']).toEqual({
id: 4,
parent: 1,
node: obj.children[1],
prev: 2,
});
});

test('Test TreeViewUtil get method', () => {
const tree = createTree();
const { obj } = tree;

expect(tree.get(1)).toEqual(obj);
expect(tree.get(100)).toBeNull();
});

test('Test TreeViewUtil remove method', () => {
const tree = createTree();
const { obj } = tree;

const node = tree.remove(2);

expect(node).toEqual({
id: 2,
module: 'a',
children: [{ id: 3, module: 'c' }],
});
expect(obj).toEqual({
id: 1,
module: 'root',
children: [{ id: 4, module: 'b' }],
});
expect(tree.getIndex(2)).toBeUndefined();
expect(tree.getIndex(3)).toBeUndefined();
});

test('Test TreeViewUtil insert method', () => {
const tree = createTree();
const { obj } = tree;

tree.insert({ id: 5, module: 'd' }, 3, 0);

expect(obj).toEqual({
id: 1,
module: 'root',
children: [
{
id: 2,
module: 'a',
children: [
{
id: 3,
module: 'c',
children: [{ id: 5, module: 'd' }],
},
],
},
{ id: 4, module: 'b' },
],
});
});

test('Test TreeViewUtil insertBefore method', () => {
const tree = createTree();
const { obj } = tree;
tree.insertBefore({ id: 5, module: 'd' }, 3);

expect(obj).toEqual({
id: 1,
module: 'root',
children: [
{
id: 2,
module: 'a',
children: [
{ id: 5, module: 'd' },
{ id: 3, module: 'c' },
],
},
{ id: 4, module: 'b' },
],
});
});

test('Test TreeViewUtil insertAfter method', () => {
const tree = createTree();
const { obj } = tree;
tree.insertAfter({ id: 5, module: 'd' }, 3);

expect(obj).toEqual({
id: 1,
module: 'root',
children: [
{
id: 2,
module: 'a',
children: [
{ id: 3, module: 'c' },
{ id: 5, module: 'd' },
],
},
{ id: 4, module: 'b' },
],
});
});

test('Test TreeViewUtil prepend method', () => {
const tree = createTree();
const { obj } = tree;
tree.prepend({ id: 5, module: 'd' }, 1);

expect(obj).toEqual({
id: 1,
module: 'root',
children: [
{
id: 5,
module: 'd',
},
{
id: 2,
module: 'a',
children: [{ id: 3, module: 'c' }],
},
{ id: 4, module: 'b' },
],
});
});

test('Test TreeViewUtil append method', () => {
const tree = createTree();
const { obj } = tree;
tree.append({ id: 5, module: 'd' }, 1);

expect(obj).toEqual({
id: 1,
module: 'root',
children: [
{
id: 2,
module: 'a',
children: [{ id: 3, module: 'c' }],
},
{ id: 4, module: 'b' },
{
id: 5,
module: 'd',
},
],
});
});
});
4 changes: 2 additions & 2 deletions src/workbench/sidebar/explore/folderTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ const FolderTree: React.FunctionComponent<IFolderTree> = (
onBlur={handleInputBlur}
/>
) : (
name
);
name
);
};

const renderByData = (
Expand Down

0 comments on commit e2fb056

Please sign in to comment.