question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to test draggable object programmatically

See original GitHub issue

I’m trying to unit test some functionality related to dragging on my site; I use interact like this:

interact(element).draggable({ onstart: functionToTest })

I’m trying to simulate a drag by manually triggering events with jQuery:

$(element).mousedown(); ...

But it seems that interact doesn’t respond to these manual triggers at all. Am I doing something wrong here?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
tayecommented, Dec 11, 2014

$(element).mousedown() doesn’t fire event listeners that were added with Node.addEventListener which is used by interact.js. You would have to create and trigger events manually.

But you can just use interactable.fire(eventObject) to call the listeners for eventObject.type.

interact(element).fire({
  type: 'dragstart',
  target: element,
  pageX: 10,
  ...
});
0reactions
dmo-odoocommented, Apr 24, 2019

@taye and others who might be trying the same thing, I found out how to do it. Interact.js actually listens to PointerEvents, not MouseEvents, and it listens for them on document, not the element itself, so it needs to bubble.

var pointerDownEvent = new PointerEvent("pointerdown", {
    bubbles: true,
    // any mouseEvent contructor value
});
el.dispatchEvent(pointerDownEvent);
var pointerMoveEvent = new PointerEvent("pointermove", {
    bubbles: true,
    // any mouseEvent contructor value
});
el.dispatchEvent(pointerMoveEvent);
var pointerUpEvent = new PointerEvent("pointerup", {
    bubbles: true,
    // any mouseEvent contructor value
});
el.dispatchEvent(pointerUpEvent);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Drag object programmatically - javascript - Stack Overflow
This should get you fairly close, if I understood you correctly. Click anywhere inside the black square of the canvas and outside the...
Read more >
How to write better tests for drag-and-drop operations in the ...
We need to set the clientX and clientY properties of the event, because they are used to determine the direction of the dragging....
Read more >
Draggable - How to start dragging programmatically
Hi team, We need to be able to drag an item from a dev-ex scheduler (scheduler2) (which is in a separate browser window)...
Read more >
Controlling Draggable programmatically - GSAP - GreenSock
Draggable.create() returns an Array of Draggable instances (remember, you may pass in selector text that'd match many elements, thus a Draggable ...
Read more >
How to test drag-and-drop interactions in Playwright - Reflect.run
To make the script works, you have to call the dragTo() function on the same source and target sliderHandle element. Also, the flag...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found