Skip to content

Commit

Permalink
Merge branch 'dev' into update-js-packages
Browse files Browse the repository at this point in the history
  • Loading branch information
mbraak committed Nov 3, 2024
2 parents cafe9f8 + ef330ce commit cff52ad
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 22 deletions.
7 changes: 2 additions & 5 deletions src/keyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
} from "./jqtreeMethodTypes";
import { Node } from "./node";

type KeyboardEventHandler = (event: KeyboardEvent) => boolean;

interface KeyHandlerParams {
closeNode: CloseNode;
getSelectedNode: GetSelectedNode;
Expand Down Expand Up @@ -55,7 +53,6 @@ export default class KeyHandler {
}
};

private handleKeyDownHandler?: KeyboardEventHandler;
private isFocusOnTree: IsFocusOnTree;
private keyboardSupport: boolean;
private openNode: OpenNode;
Expand Down Expand Up @@ -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);
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/nodeElement/folderElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
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
66 changes: 66 additions & 0 deletions src/test/dataLoader.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
37 changes: 37 additions & 0 deletions src/test/dragAndDropHandler/dragElement.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
14 changes: 14 additions & 0 deletions src/test/jqTree/methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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 cff52ad

Please sign in to comment.