Skip to content

Commit

Permalink
Test mouseDrag
Browse files Browse the repository at this point in the history
  • Loading branch information
mbraak committed Dec 14, 2024
1 parent dc1c977 commit 4137c14
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/dragAndDropHandler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ export class DragAndDropHandler {
};
}

/* Move the dragged node to the selected position in the tree. */
private moveItem(positionInfo: PositionInfo): void {
if (
this.currentItem &&
Expand Down
49 changes: 48 additions & 1 deletion src/test/dragAndDropHandler/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ const createDragAndDropHandler = ({
});
};

beforeEach(() => {
document.body.innerHTML = "";
});

describe(".mouseCapture", () => {
it("sets the current item and returns true when a node can be moved", () => {
const tree = new Node(null, true);
Expand Down Expand Up @@ -279,6 +283,7 @@ describe(".mouseStart", () => {
tree.addChild(node2);

const dragAndDropHandler = createDragAndDropHandler({ tree });

// Set current item
const positionInfo = {
originalEvent: new Event("click"),
Expand Down Expand Up @@ -335,10 +340,11 @@ describe(".mouseDrag", () => {
};

dragAndDropHandler.mouseCapture(positionInfo);

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

// mouse start
// Move mouse
dragAndDropHandler.mouseDrag({
originalEvent: new Event("mousemove"),
pageX: 15,
Expand All @@ -353,6 +359,47 @@ describe(".mouseDrag", () => {
top: "20px",
});
});

it("changes the hovered area", () => {
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: 10,
target: node1.element as HTMLElement,
};

dragAndDropHandler.mouseCapture(positionInfo);

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

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

expect(dragAndDropHandler.hoveredArea).toEqual(
expect.objectContaining({
bottom: 38,
node: node2,
position: Position.Inside,
top: 20,
}),
);
});
});

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

0 comments on commit 4137c14

Please sign in to comment.