From 9dce78de49064394615811304fb6eeabaa8ecb03 Mon Sep 17 00:00:00 2001 From: Marco Braak Date: Sun, 10 Nov 2024 16:20:45 +0100 Subject: [PATCH] Add tests --- src/test/saveStateHandler.test.ts | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/test/saveStateHandler.test.ts diff --git a/src/test/saveStateHandler.test.ts b/src/test/saveStateHandler.test.ts new file mode 100644 index 00000000..9afd24ed --- /dev/null +++ b/src/test/saveStateHandler.test.ts @@ -0,0 +1,44 @@ +import SaveStateHandler from "../saveStateHandler"; + +const createSaveStateHandler = () => { + const addToSelection = jest.fn(); + const getNodeById = jest.fn(); + const getSelectedNodes = jest.fn(); + const getTree = jest.fn(); + const openNode = jest.fn(); + const refreshElements = jest.fn(); + const removeFromSelection = jest.fn(); + + return new SaveStateHandler({ + addToSelection, + getNodeById, + getSelectedNodes, + getTree, + openNode, + refreshElements, + removeFromSelection, + saveState: true, + }); +}; + +describe("getStateFromStorage", () => { + afterEach(() => { + localStorage.clear(); + }); + + it("returns null when the state is not in local storage", () => { + localStorage.clear(); + + const saveStateHandler = createSaveStateHandler(); + expect(saveStateHandler.getStateFromStorage()).toBeNull(); + }); + + it("returns an array of selected nodes when 'selected_node' in the states is a number", () => { + localStorage.setItem("tree", JSON.stringify({ selected_node: 123 })); + + const saveStateHandler = createSaveStateHandler(); + expect(saveStateHandler.getStateFromStorage()).toEqual({ + selected_node: [123], + }); + }); +});