Skip to content

Commit

Permalink
Test SelectNodeHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
mbraak committed Nov 3, 2024
1 parent ae74937 commit 89e764c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/selectNodeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,14 @@ export default class SelectNodeHandler {
return [];
}
} else {
const selectedNodes = [];

for (const id in this.selectedNodes) {
if (
Object.prototype.hasOwnProperty.call(this.selectedNodes, id)
) {
const node = this.getNodeById(id);
if (node && parent.isParentOf(node)) {
selectedNodes.push(node);
}
const selectedNodes: Node[] = [];

this.selectedNodes.forEach((id) => {
const node = this.getNodeById(id);
if (node && parent.isParentOf(node)) {
selectedNodes.push(node);
}
}
});

return selectedNodes;
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/selectNodeHandler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Node } from "../node";
import SelectNodeHandler from "../selectNodeHandler";

describe("getSelectedNodesUnder", () => {
it("returns the nodes when the nodes have an id", () => {
const node = new Node({ id: 1 });

const child = new Node({ id: 2 });
node.addChild(child);

const nodeMap = new Map<NodeId, Node>();
nodeMap.set(1, node);
nodeMap.set(2, child);

const getNodeById = (id: NodeId) => nodeMap.get(id) ?? null;

const selectNodeHandler = new SelectNodeHandler({ getNodeById });
selectNodeHandler.addToSelection(child);

expect(
selectNodeHandler.getSelectedNodesUnder(node),
).toIncludeAllMembers([child]);
});
});

0 comments on commit 89e764c

Please sign in to comment.