Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
fix: added unit test and switched to unshift
Browse files Browse the repository at this point in the history
  • Loading branch information
niekvanstaveren committed Feb 14, 2020
1 parent 9c81689 commit c0f80eb
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 4 deletions.
99 changes: 99 additions & 0 deletions src/tree/__tests__/render.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { hierarchy } from 'd3';
import { filterTree } from '../render';

const nodes = {
id: '1',
elemNo: 1,
children: [
{
id: '2',
elemNo: 2,
children: [
{
id: '3',
elemNo: 3,
parent: {
data: {
id: '2',
},
},
children: [
{
id: '5',
elemNo: 798,
parent: {
id: '3',
},
},
],
},
],
},
{
id: '4',
elemNo: 88,
children: [],
},
],
};

describe('render', () => {
describe('filterTree', () => {
let topId;
let isExpanded;
let expandedChildren;
let nodeTree;
beforeEach(() => {
topId = '1';
isExpanded = false;
expandedChildren = [];
nodeTree = hierarchy(nodes);
});

it('should return the only top node', () => {
const result = filterTree({ topId, isExpanded, expandedChildren }, nodeTree);
expect(result.length).to.equal(1);
expect(result[0].data.id).to.equal('1');
});

it('should return only the top node when topId not found', () => {
topId = 'someInsaneId';
const result = filterTree({ topId, isExpanded, expandedChildren }, nodeTree);
expect(result.length).to.equal(1);
expect(result[0].data.id).to.equal('1');
});

it('should return the top node and children', () => {
isExpanded = true;
const result = filterTree({ topId, isExpanded, expandedChildren }, nodeTree);
expect(result.length).to.equal(3);
expect(result.map(node => node.data.id)).to.deep.equal(['1', '2', '4']);
});

it('should return the top node and children and grandchildren of expanded', () => {
isExpanded = true;
expandedChildren = ['2'];
const result = filterTree({ topId, isExpanded, expandedChildren }, nodeTree);
expect(result.length).to.equal(4);
expect(result.map(node => node.data.id)).to.deep.equal(['1', '2', '3', '4']);
});

it('should return the top node and children with navigation mode free', () => {
nodeTree.data.navigationMode = 'free';
isExpanded = true;
topId = '2';
const result = filterTree({ topId, isExpanded, expandedChildren }, nodeTree);
expect(result.length).to.equal(4);
expect(result.map(node => node.data.id)).to.deep.equal(['1', '2', '4', '3']);
});

it('should return the top node and parents children with navigation mode free', () => {
nodeTree.data.navigationMode = 'free';
isExpanded = true;
topId = '5';
const result = filterTree({ topId, isExpanded, expandedChildren }, nodeTree);
expect(result.length).to.equal(5);
expect(result.map(node => node.data.id)).to.deep.equal(['1', '2', '4', '3', '5']);
});
});
});
8 changes: 4 additions & 4 deletions src/tree/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import box from './box';
import createPaths from './path';
import transform from './transform';

const filterTree = ({ topId, isExpanded, expandedChildren }, nodeTree) => {
export const filterTree = ({ topId, isExpanded, expandedChildren }, nodeTree) => {
const topNode = nodeTree.descendants().find(node => node.data.id === topId) || nodeTree;
const subTree = [];
if (isExpanded && topNode.children) {
Expand All @@ -22,13 +22,13 @@ const filterTree = ({ topId, isExpanded, expandedChildren }, nodeTree) => {
if (nodeTree.data.navigationMode === 'free' && topNode.parent) {
const ancestors = topNode.parent.ancestors();
ancestors.forEach(ancestor => {
subTree.push(...ancestor.children);
subTree.unshift(...ancestor.children);
if (!ancestor.parent) {
subTree.push(ancestor);
subTree.unshift(ancestor);
}
});
} else {
subTree.push(topNode); // self
subTree.unshift(topNode); // self
}
return subTree;
};
Expand Down

0 comments on commit c0f80eb

Please sign in to comment.