import { expect } from 'chai'; import { Tree, TreeNode, TreeRaw } from '../src'; function getIDList(nodes: TreeNode[]): string[] { return nodes.map(node => node.ID); } describe('NodeCtx', () => { const rawData: TreeRaw[] = [ { ID: '1', parentID: '-1', seq: 1 }, { ID: '2', parentID: '-1', seq: 3 }, { ID: '3', parentID: '-1', seq: 2 }, { ID: '4', parentID: '-1', seq: 4 }, { ID: '5', parentID: '-1', seq: 5 }, { ID: '6', parentID: '1', seq: 2 }, { ID: '7', parentID: '1', seq: 1 }, { ID: '8', parentID: '7', seq: 1 }, { ID: '9', parentID: '2', seq: 1 }, ]; const tree = new Tree(rawData); it('row', () => { const node = tree.find('4'); if (node) { const row = node.ctx.row(); expect(row).to.equal(7); } }); it('depth', () => { const node = tree.find('1'); if (node) { const depth = node.ctx.depth(); expect(depth).to.equal(0); } const deepNode = tree.find('8'); if (deepNode) { const depth = deepNode.ctx.depth(); expect(depth).to.equal(2); } }); it('visible', () => { const node = tree.find('1'); if (node) { node.ctx.expanded = true; const testNode = tree.find('8'); if (testNode) { const visibleA = testNode.ctx.visible(); expect(visibleA).to.equal(false); node.ctx.expanded = false; const visibleB = testNode.ctx.visible(); expect(visibleB).to.equal(true); } } }); it('parent', () => { const node = tree.find('6'); if (node) { const parent = node.ctx.parent(); expect(parent).to.have.property('ID', '1'); } }); it('parent', () => { const node = tree.find('6'); if (node) { const parent = node.ctx.parent(); expect(parent).to.have.property('ID', '1'); } }); it('next', () => { const node = tree.find('1'); if (node) { const next = node.ctx.next(); expect(next).to.have.property('ID', '3'); } }); it('prev', () => { const node = tree.find('3'); if (node) { const prev = node.ctx.prev(); expect(prev).to.have.property('ID', '1'); } }); it('children', () => { const node = tree.find('1'); if (node) { const children = node.ctx.chilren(); const IDList = getIDList(children); expect(IDList).to.have.ordered.members(['7', '6']); } }); it('firstChild', () => { const node = tree.find('1'); if (node) { const firstChild = node.ctx.firstChild(); expect(firstChild).to.have.property('ID', '7'); } }); it('lastChild', () => { const node = tree.find('1'); if (node) { const lastChild = node.ctx.lastChild(); expect(lastChild).to.have.property('ID', '6'); } }); it('posterity', () => { const node = tree.find('1'); if (node) { const posterity = node.ctx.posterity(); const IDList = getIDList(posterity); expect(IDList).to.have.ordered.members(['7', '8', '6']); } }); it('progenitor', () => { const node = tree.find('8'); if (node) { const progenitor = node.ctx.progenitor(); if (progenitor) { expect(progenitor).to.have.property('ID', '1'); } } }); it('ancestor', () => { const node = tree.find('8'); if (node) { const ancestor = node.ctx.ancestor(); const IDList = getIDList(ancestor); expect(IDList).to.have.ordered.members(['7', '1']); } }); it('brothers', () => { const node = tree.find('6'); if (node) { const brothers = node.ctx.brothers(); const IDList = getIDList(brothers); expect(IDList).to.have.ordered.members(['7', '6']); const excludeBrothers = node.ctx.brothers(false); const exIDList = getIDList(excludeBrothers); expect(exIDList).to.have.ordered.members(['7']); } }); it('nextBrothers', () => { const node = tree.find('2'); if (node) { const nextBrothers = node.ctx.nextBrothers(); const IDList = getIDList(nextBrothers); expect(IDList).to.have.ordered.members(['4', '5']); } }); it('prevBrothers', () => { const node = tree.find('2'); if (node) { const prevBrothers = node.ctx.prevBrothers(); const IDList = getIDList(prevBrothers); expect(IDList).to.have.ordered.members(['1', '3']); } }); });