diff --git a/src/keyHandler.ts b/src/keyHandler.ts index 11feb139..8a686e12 100644 --- a/src/keyHandler.ts +++ b/src/keyHandler.ts @@ -7,8 +7,6 @@ import { } from "./jqtreeMethodTypes"; import { Node } from "./node"; -type KeyboardEventHandler = (event: KeyboardEvent) => boolean; - interface KeyHandlerParams { closeNode: CloseNode; getSelectedNode: GetSelectedNode; @@ -55,7 +53,6 @@ export default class KeyHandler { } }; - private handleKeyDownHandler?: KeyboardEventHandler; private isFocusOnTree: IsFocusOnTree; private keyboardSupport: boolean; private openNode: OpenNode; @@ -127,8 +124,8 @@ export default class KeyHandler { } public deinit(): void { - if (this.handleKeyDownHandler) { - document.removeEventListener("keydown", this.handleKeyDownHandler); + if (this.keyboardSupport) { + document.removeEventListener("keydown", this.handleKeyDown); } } diff --git a/src/nodeElement/folderElement.ts b/src/nodeElement/folderElement.ts index 6e51596e..0ac2cc56 100644 --- a/src/nodeElement/folderElement.ts +++ b/src/nodeElement/folderElement.ts @@ -44,10 +44,7 @@ class FolderElement extends NodeElement { ) as HTMLLinkElement; } - public close( - slide = true, - animationSpeed: JQuery.Duration | undefined = "fast", - ): void { + public close(slide: boolean, animationSpeed: JQuery.Duration): void { if (!this.node.is_open) { return; } @@ -86,8 +83,8 @@ class FolderElement extends NodeElement { public open( onFinished: OnFinishOpenNode | undefined, - slide = true, - animationSpeed: JQuery.Duration = "fast", + slide: boolean, + animationSpeed: JQuery.Duration, ): void { if (this.node.is_open) { return; diff --git a/src/selectNodeHandler.ts b/src/selectNodeHandler.ts index adbedc83..4510000d 100644 --- a/src/selectNodeHandler.ts +++ b/src/selectNodeHandler.ts @@ -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; } diff --git a/src/test/dataLoader.test.ts b/src/test/dataLoader.test.ts new file mode 100644 index 00000000..7ade4a3f --- /dev/null +++ b/src/test/dataLoader.test.ts @@ -0,0 +1,66 @@ +import { waitFor } from "@testing-library/dom"; +import { http, HttpResponse } from "msw"; +import { setupServer } from "msw/node"; + +import DataLoader from "../dataLoader"; + +const server = setupServer(); + +afterAll(() => { + server.close(); +}); + +afterEach(() => { + server.resetHandlers(); +}); + +beforeAll(() => { + server.listen(); +}); + +describe("loadFromUrl", () => { + it("does nothing when urlInfo is empty", () => { + const loadData = () => null; + const treeElement = document.createElement("div"); + const triggerEvent = jest.fn(); + + const dataLoader = new DataLoader({ + loadData, + treeElement, + triggerEvent, + }); + + dataLoader.loadFromUrl(null, null, null); + expect(triggerEvent).not.toHaveBeenCalled(); + }); + + it("parses json when the response is a string", async () => { + server.use( + http.get( + "/test", + () => + new HttpResponse('{ "key1": "value1" }', { + headers: { + "Content-Type": "text/plain", + }, + }), + {}, + ), + ); + + const loadData = jest.fn(); + const treeElement = document.createElement("div"); + const triggerEvent = jest.fn(); + + const dataLoader = new DataLoader({ + loadData, + treeElement, + triggerEvent, + }); + dataLoader.loadFromUrl({ dataType: "text", url: "/test" }, null, null); + + await waitFor(() => { + expect(loadData).toHaveBeenCalledWith({ key1: "value1" }, null); + }); + }); +}); diff --git a/src/test/dragAndDropHandler/dragElement.test.ts b/src/test/dragAndDropHandler/dragElement.test.ts new file mode 100644 index 00000000..0dcfb4b2 --- /dev/null +++ b/src/test/dragAndDropHandler/dragElement.test.ts @@ -0,0 +1,37 @@ +import DragElement from "../../dragAndDropHandler/dragElement"; + +it("creates an element with autoEscape is true", () => { + const treeElement = document.createElement("div"); + + new DragElement({ + autoEscape: true, + nodeName: "abc & def", + offsetX: 0, + offsetY: 0, + treeElement, + }); + + expect(treeElement.children.length).toBe(1); + + const childElement = treeElement.children[0]; + expect(childElement).toHaveClass("jqtree-title"); + expect(childElement).toHaveClass("jqtree-dragging"); + expect(childElement).toHaveTextContent("abc & def"); +}); + +it("creates an element with autoEscape is false", () => { + const treeElement = document.createElement("div"); + + new DragElement({ + autoEscape: false, + nodeName: "abc & def", + offsetX: 0, + offsetY: 0, + treeElement, + }); + + expect(treeElement.children.length).toBe(1); + + const childElement = treeElement.children[0]; + expect(childElement).toHaveTextContent("abc & def"); +}); diff --git a/src/test/jqTree/methods.test.ts b/src/test/jqTree/methods.test.ts index cd26b662..f68846e4 100644 --- a/src/test/jqTree/methods.test.ts +++ b/src/test/jqTree/methods.test.ts @@ -300,6 +300,20 @@ describe("closeNode", () => { }); }); +describe("destroy", () => { + it("clears the tree element", () => { + const $tree = $("#tree1"); + + $tree.tree({ + data: exampleData, + }); + + $tree.tree("destroy"); + + expect($tree.get(0)).toBeEmptyDOMElement(); + }); +}); + describe("getNodeByCallback", () => { interface Vars { $tree: JQuery; diff --git a/src/test/selectNodeHandler.test.ts b/src/test/selectNodeHandler.test.ts new file mode 100644 index 00000000..843b7d7c --- /dev/null +++ b/src/test/selectNodeHandler.test.ts @@ -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(); + 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]); + }); +});