Skip to content

Commit

Permalink
getPathNodes() => map(); added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JaySunSyn committed Aug 7, 2017
1 parent 5308aae commit 20a3aad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
6 changes: 3 additions & 3 deletions cosmoz-default-tree.html
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,16 @@

var path = pathLocator.split(pathLocatorSeparator),
pathSegment = nodeObj || this._treeData,
nodes = [],
nodes,
node;

// Get the nodes on the path
path.forEach(function (nodeKey) {
nodes = path.map(function (nodeKey) {
node = pathSegment[nodeKey] !== node ? pathSegment[nodeKey] : undefined;
nodes.push(node);
if (node && this.hasChildren(node)) {
pathSegment = node[this.childProperty];
}
return node;
}, this);

// Filter out undefined items of the start
Expand Down
20 changes: 19 additions & 1 deletion test/cosmoz-tree_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,32 @@
node301 = missingAncestorTree.getNodeByProperty('3a7654f1-e3e6-49c7-b6a8-a4fb00f31245', 'id'),
node401 = missingAncestorTree.getNodeByProperty('865065da-f44c-472e-a8df-a4fb00f3124b', 'id'),
node301Path = missingAncestorTree.getPathNodes(node301.pathLocator),
node401Path = missingAncestorTree.getPathNodes(node401.pathLocator);
node401Path = missingAncestorTree.getPathNodes(node401.pathLocator),
n_1_2_3_301 = missingAncestorTree.getPathNodes('1.2.3.301'),
n_1_2 = missingAncestorTree.getPathNodes('1.2'),
n_2_301 = missingAncestorTree.getPathNodes('2.301');

assert.equal(node301Path.filter(function (n) { return n; }).length, 2);
assert.equal(node301.name, 'Node301')
assert.deepEqual(node301Path[2], node301);

assert.equal(node401Path.length, 4);
assert.equal(node401Path[2], node301);
assert.equal(node401Path[3], node401);

// 1.2.3.301
assert.equal(n_1_2_3_301[0].name, 'Node2');
assert.isNotOk(n_1_2_3_301[1]);
assert.equal(n_1_2_3_301[2].name, 'Node301');

// 1.2
assert.equal(n_1_2.length, 1);
assert.equal(n_1_2[0].name, 'Node2');

// 2.301
assert.equal(n_2_301.length, 2);
assert.equal(n_2_301[0].name, 'Node2');
assert.equal(n_2_301[1].name, 'Node301');
});
});
}());
Expand Down

0 comments on commit 20a3aad

Please sign in to comment.