Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mbraak committed Nov 20, 2024
1 parent 906e65d commit 7fd2bf0
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/test/saveStateHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { Node } from "../node";
import SaveStateHandler from "../saveStateHandler";

const createSaveStateHandler = ({
addToSelection = jest.fn(),
getNodeById = jest.fn(),
getSelectedNodes = jest.fn(),
openNode = jest.fn(),
refreshElements = jest.fn(),
removeFromSelection = jest.fn(),
}) => {
const addToSelection = jest.fn();
const getTree = jest.fn();
const refreshElements = jest.fn();

return new SaveStateHandler({
addToSelection,
Expand Down Expand Up @@ -72,16 +72,16 @@ describe("setInitialStateOnDemand", () => {
expect(openNode).not.toHaveBeenCalled();
});

it("opens a node when it's in open_nodes in the state", () => {
it("opens a node when the node id is in open_nodes in the state", () => {
const node = new Node({ id: 123 });
const openNode = jest.fn();
const getNodeById = jest.fn((nodeId) => {
if (nodeId === 123) {
return node;
} else {
return null;
}
});
const openNode = jest.fn();

const saveStateHandler = createSaveStateHandler({
getNodeById,
Expand All @@ -94,4 +94,31 @@ describe("setInitialStateOnDemand", () => {

expect(openNode).toHaveBeenCalledWith(node, false);
});

it("selects a node and redraws the tree when the node is id in selected_node in the state", () => {
const node = new Node({ id: 123 });
const getNodeById = jest.fn((nodeId) => {
if (nodeId === 123) {
return node;
} else {
return null;
}
});
const addToSelection = jest.fn();
const refreshElements = jest.fn();

const saveStateHandler = createSaveStateHandler({
addToSelection,
getNodeById,
refreshElements,
});

saveStateHandler.setInitialStateOnDemand(
{ open_nodes: [123], selected_node: [123] },
jest.fn(),
);

expect(addToSelection).toHaveBeenCalledWith(node);
expect(refreshElements).toHaveBeenCalledWith(null);
});
});

0 comments on commit 7fd2bf0

Please sign in to comment.