list-to-tree
Version:
List to tree data structure
121 lines (104 loc) • 3.3 kB
JavaScript
const LTT = require('../dist/list-to-tree');
describe('Invalid list:', function() {
let tree = null;
const key_id = 'id';
const key_parent = 'parent';
const key_child = 'child';
const depthKeyName = '__depth'
const consoleLogOfSilence = (func) => {
const log = console.log;
console.log = () => {};
func();
console.log = log;
}
beforeEach(function() {
const list = [
{
id: 1,
parent: 0
}, {
id: 20,
parent: 1
}, {
id: 30,
parent: 1
}, {
id: 14,
parent: 20
}, {
id: 15,
parent: 20
}, {
id: 6,
parent: 14
}, {
id: 7,
parent: 14
}, {
id: 8,
parent: 100 // bad id
}, {
id: 9,
parent: 101 // bad id
}, {
id: 10,
parent: 102 // bad id
}
];
// hide Warning massages
consoleLogOfSilence(function() {
const ltt = new LTT(list, {
key_id: key_id,
key_parent: key_parent,
key_child: key_child
});
tree = ltt.GetTree();
})
});
it('It is workly', function() {
expect( tree.length ).toBe(1);
});
it('1st node', function() {
const node = tree[0];
expect( node[key_id] ).toBe(1);
expect( node[key_parent] ).toBe(0);
expect( key_child in node ).toBe(true);
expect( node[key_child].length ).toBe(2);
expect( node[depthKeyName] ).toBe(0);
});
it('2nd depth node', function() {
const node = tree[0][key_child][0];
expect( node[key_id] ).toBe(20);
expect( node[key_parent] ).toBe(1);
expect( key_child in node ).toBe(true);
expect( node[depthKeyName] ).toBe(1);
});
it('3rd depth node 1', function() {
const node = tree[0][key_child][0][key_child][0];
expect( node[key_id] ).toBe(14);
expect( node[key_parent] ).toBe(20);
expect( key_child in node ).toBe(true);
expect( node[depthKeyName] ).toBe(2);
});
it('3rd depth node 2', function() {
const node = tree[0][key_child][0][key_child][1];
expect( node[key_id] ).toBe(15);
expect( node[key_parent] ).toBe(20);
expect( key_child in node ).toBe(false);
expect( node[depthKeyName] ).toBe(2);
});
it('4th depth node 1', function() {
const node = tree[0][key_child][0][key_child][0][key_child][0];
expect( node[key_id] ).toBe(6);
expect( node[key_parent] ).toBe(14);
expect( key_child in node ).toBe(false);
expect( node[depthKeyName] ).toBe(3);
});
it('4th depth node 2', function() {
const node = tree[0][key_child][0][key_child][0][key_child][1];
expect( node[key_id] ).toBe(7);
expect( node[key_parent] ).toBe(14);
expect( key_child in node ).toBe(false);
expect( node[depthKeyName] ).toBe(3);
});
});