Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbraak committed Dec 14, 2024
1 parent 4137c14 commit 0b7beac
Showing 1 changed file with 102 additions and 3 deletions.
105 changes: 102 additions & 3 deletions src/test/dragAndDropHandler/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { DragAndDropHandler } from "../../dragAndDropHandler";
import { GetTree } from "../../jqtreeMethodTypes";
import { OnIsMoveHandle } from "../../jqtreeOptions";
import { DragMethod, OnIsMoveHandle } from "../../jqtreeOptions";
import { Node } from "../../node";
import NodeElement from "../../nodeElement";
import { Position } from "../../position";
import { generateHtmlElementsForTree } from "../support/testUtil";

interface CreateDragAndDropHandlerParams {
getTree?: GetTree;
onDragMove?: DragMethod;
onIsMoveHandle?: OnIsMoveHandle;
tree: Node;
}

const createDragAndDropHandler = ({
getTree,
onDragMove,
onIsMoveHandle,
tree,
}: CreateDragAndDropHandlerParams) => {
Expand Down Expand Up @@ -65,6 +67,7 @@ const createDragAndDropHandler = ({
getNodeElementForNode,
getScrollLeft,
getTree: getTree ?? jest.fn(() => tree),
onDragMove,
onIsMoveHandle,
openFolderDelay: false,
openNode,
Expand Down Expand Up @@ -322,7 +325,7 @@ describe(".mouseStart", () => {
});

describe(".mouseDrag", () => {
it("moves the drag element", () => {
it("moves the drag element and returns true", () => {
const tree = new Node(null, true);
const node1 = new Node({ name: "node1" });
tree.addChild(node1);
Expand All @@ -345,12 +348,13 @@ describe(".mouseDrag", () => {
expect(dragAndDropHandler.isDragging).toBeTrue();

// Move mouse
dragAndDropHandler.mouseDrag({
const dragResult = dragAndDropHandler.mouseDrag({
originalEvent: new Event("mousemove"),
pageX: 15,
pageY: 30,
target: node2.element as HTMLElement,
});
expect(dragResult).toBeTrue();

const dragElement = document.querySelector(".jqtree-dragging");
expect(dragElement).toHaveStyle({
Expand Down Expand Up @@ -400,6 +404,101 @@ describe(".mouseDrag", () => {
}),
);
});

it("returns false when dragging hasn't started", () => {
const tree = new Node(null, true);
const node1 = new Node({ name: "node1" });
tree.addChild(node1);
const node2 = new Node({ name: "node2" });
tree.addChild(node2);

const dragAndDropHandler = createDragAndDropHandler({ tree });

const dragResult = dragAndDropHandler.mouseDrag({
originalEvent: new Event("mousemove"),
pageX: 15,
pageY: 30,
target: node2.element as HTMLElement,
});
expect(dragResult).toBeFalse();
});

it("sets area to null when no area is found", () => {
const tree = new Node(null, true);
const node1 = new Node({ name: "node1" });
tree.addChild(node1);
const node2 = new Node({ name: "node2" });
tree.addChild(node2);

const dragAndDropHandler = createDragAndDropHandler({
tree,
});

// Start dragging
const positionInfo = {
originalEvent: new Event("click"),
pageX: 10,
pageY: 30,
target: node1.element as HTMLElement,
};

dragAndDropHandler.mouseCapture(positionInfo);

dragAndDropHandler.mouseStart(positionInfo);

dragAndDropHandler.mouseDrag({
originalEvent: new Event("mousemove"),
pageX: 15,
pageY: 200,
target: document.body,
});

expect(dragAndDropHandler.hoveredArea).toBeNull();
});

it("calls onDragMove when no area is found and onDragMove is defined", () => {
const tree = new Node(null, true);
const node1 = new Node({ name: "node1" });
tree.addChild(node1);
const node2 = new Node({ name: "node2" });
tree.addChild(node2);

const onDragMove = jest.fn();

const dragAndDropHandler = createDragAndDropHandler({
onDragMove,
tree,
});

// Start dragging
const positionInfo = {
originalEvent: new Event("click"),
pageX: 10,
pageY: 30,
target: node1.element as HTMLElement,
};

dragAndDropHandler.mouseCapture(positionInfo);

dragAndDropHandler.mouseStart(positionInfo);
expect(dragAndDropHandler.isDragging).toBeTrue();
expect(dragAndDropHandler.hoveredArea).toBeNull();

const positionInfoForDragging = {
originalEvent: new Event("mousemove"),
pageX: 15,
pageY: 200,
target: document.body,
};

// Move mouse
dragAndDropHandler.mouseDrag(positionInfoForDragging);

expect(onDragMove).toHaveBeenCalledWith(
node1,
positionInfoForDragging.originalEvent,
);
});
});

describe(".refresh", () => {
Expand Down

0 comments on commit 0b7beac

Please sign in to comment.