Skip to content

Commit

Permalink
Add test for drag and drop handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mbraak committed Dec 7, 2024
1 parent 407353e commit 24c1f83
Showing 1 changed file with 131 additions and 0 deletions.
131 changes: 131 additions & 0 deletions src/test/dragAndDropHandler/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { mockElementBoundingClientRect } from "jsdom-testing-mocks";

Check failure on line 1 in src/test/dragAndDropHandler/index.test.ts

View workflow job for this annotation

GitHub Actions / runner-job

Unable to resolve path to module 'jsdom-testing-mocks'

import { DragAndDropHandler } from "../../dragAndDropHandler";
import { Node } from "../../node";
import NodeElement from "../../nodeElement";
import { Position } from "../../position";

describe(".refresh", () => {
it("generates hit areas if there is a current item", () => {
const getNodeElementForNode = jest.fn();
const getScrollLeft = jest.fn();
const openNode = jest.fn();
const refreshElements = jest.fn();
const triggerEvent = jest.fn();

const elementForTree = document.createElement("div");
document.body.append(elementForTree);

jest.spyOn(elementForTree, "clientHeight", "get").mockReturnValue(50);
jest.spyOn(elementForTree, "clientWidth", "get").mockReturnValue(40);

const elementForNode1 = document.createElement("div");
elementForTree.append(elementForNode1);
const elementForNode2 = document.createElement("div");
elementForTree.append(elementForNode2);

jest.spyOn(elementForNode1, "offsetParent", "get").mockReturnValue(
elementForTree,
);
jest.spyOn(elementForNode2, "offsetParent", "get").mockReturnValue(
elementForTree,
);

mockElementBoundingClientRect(elementForTree, {

Check failure on line 34 in src/test/dragAndDropHandler/index.test.ts

View workflow job for this annotation

GitHub Actions / runner-job

Unsafe call of a(n) `error` type typed value
height: 40,
width: 50,
x: 0,
y: 0,
});

mockElementBoundingClientRect(elementForNode1, {

Check failure on line 41 in src/test/dragAndDropHandler/index.test.ts

View workflow job for this annotation

GitHub Actions / runner-job

Unsafe call of a(n) `error` type typed value
height: 20,
width: 50,
x: 0,
y: 0,
});

mockElementBoundingClientRect(elementForNode2, {

Check failure on line 48 in src/test/dragAndDropHandler/index.test.ts

View workflow job for this annotation

GitHub Actions / runner-job

Unsafe call of a(n) `error` type typed value
height: 20,
width: 50,
x: 0,
y: 20,
});

const tree = new Node({ isRoot: true });

const node1 = new Node({ name: "node1" });
node1.element = elementForNode1;
tree.addChild(node1);

const node2 = new Node({ name: "node2" });
node2.element = elementForNode2;
tree.addChild(node2);

const nodeElement1 = new NodeElement({
getScrollLeft,
node: node1,
treeElement: elementForTree,
});

const nodeElement2 = new NodeElement({
getScrollLeft,
node: node2,
treeElement: elementForTree,
});

const getNodeElement = jest.fn((element) => {
if (element == elementForNode1) {
return nodeElement1;
} else if (element == elementForNode2) {
return nodeElement2;
} else {
return null;
}
});

const getTree = jest.fn(() => tree);

const dragAndDropHandler = new DragAndDropHandler({
getNodeElement,
getNodeElementForNode,
getScrollLeft,
getTree,
openFolderDelay: false,
openNode,
refreshElements,
slide: false,
treeElement: elementForTree,
triggerEvent,
});

// Set current item
const positionInfo = {
originalEvent: new Event("click"),
pageX: 10,
pageY: 10,
target: elementForNode1,
};

dragAndDropHandler.mouseCapture(positionInfo);
expect(dragAndDropHandler.currentItem).toBe(nodeElement1);

// Call refresh
dragAndDropHandler.refresh();

expect(dragAndDropHandler.hitAreas).toMatchObject([
expect.objectContaining({
bottom: 43,
node: node2,
position: Position.Inside,
top: 20,
}),
expect.objectContaining({
bottom: 66,
node: node2,
position: Position.After,
top: 43,
}),
]);
});
});

0 comments on commit 24c1f83

Please sign in to comment.