From ceeb063c1effa50b2caf3e0d8ed2cc4ce9db8885 Mon Sep 17 00:00:00 2001 From: winwin <> Date: Mon, 25 May 2020 07:19:25 +0800 Subject: [PATCH] root node parent can be undefined --- dist/list-to-tree.js | 1 + spec/undefined-root-node-parent-spec.js | 80 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100755 spec/undefined-root-node-parent-spec.js diff --git a/dist/list-to-tree.js b/dist/list-to-tree.js index 95bef13..e965b3c 100644 --- a/dist/list-to-tree.js +++ b/dist/list-to-tree.js @@ -27,6 +27,7 @@ module.exports = class LTT{ constructor(list, options = {}) { const _list = list.map((item) => item); + _list.map(item=>item.parent=item.parent||0) options = Object.assign({}, defaultOptions, options); this.options = options; diff --git a/spec/undefined-root-node-parent-spec.js b/spec/undefined-root-node-parent-spec.js new file mode 100755 index 0000000..548d367 --- /dev/null +++ b/spec/undefined-root-node-parent-spec.js @@ -0,0 +1,80 @@ +const LTT = require('../dist/list-to-tree'); + +describe('Root node parent undefined:', function() { + + var tree = null; + + beforeEach(function() { + var list = [ + { + id: 1 + }, { + id: 2, + parent: 1 + }, { + id: 3, + parent: 1 + }, { + id: 4, + parent: 2 + }, { + id: 5, + parent: 2 + }, { + id: 6 + }, { + id: 7 + }, { + id: 8, + parent: 7 + }, { + id: 9, + parent: 8 + }, { + id: 10 + } + ]; + var ltt = new LTT(list); + tree = ltt.GetTree(); + }); + + + it('It is workly', function() { + expect( tree.length ).toBe(4); + + }); + + it('First node check id', function() { + var firstNode = tree[0]; + expect( firstNode.id ).toBe(1); + }); + + it('First node check parent', function() { + var firstNode = tree[0]; + expect( firstNode.parent ).toBe(0); + }); + + it('First node check child', function() { + var child = tree[0].child; + expect( child.length ).toBe(2); + }); + + it('First child - check id', function() { + var child = tree[0].child; + var node = child[0]; + expect( node.id ).toBe(2); + }); + + it('First child - check parent', function() { + var child = tree[0].child; + var node = child[0]; + expect( node.parent ).toBe(1); + }); + + it('Child node have a child key', function() { + var child = tree[0].child; + var node = child[0]; + expect( 'child' in node ).toBe(true); + }); + +});