Comments (3)
A little old, but I found a (IMO) good solution, to test D3 drag events without using D3 itself. I will share my experiences here, because I don't know, what's your question on SO @BafS ;)
Assuming that you registered your start
, drag
and end
events to an element, you can trigger them by dispatching an MouseEvent
with the view
option set to window
manually. The option is the relevant part here. My jasmine tests look like this:
it("registers event listener for start", () => {
setupElements();
const startListener = jasmine.createSpy();
service.drag(rect, {
start: startListener, // do not mind this, just my service registering the drag event
});
rect.dispatchEvent(new MouseEvent("mousedown", {
view: window // <- without this, I get the error: Cannot read property 'document' of null thrown,
// because event.view is undefined and has to be window
}));
expect(startListener).toHaveBeenCalledWith(rect, jasmine.anything());
});
it("registers event listener for drag", () => {
setupElements();
const dragListener = jasmine.createSpy();
service.drag(rect, {
drag: dragListener, // do not mind this, just my service registering the drag event
});
rect.dispatchEvent(new MouseEvent("mousedown", {
view: window
}));
rect.dispatchEvent(new MouseEvent("mousemove", {
view: window
}));
expect(dragListener).toHaveBeenCalledWith(rect, jasmine.anything());
});
it("registers event listener for end", () => {
setupElements();
const endListener = jasmine.createSpy();
service.drag(rect, {
end: endListener, // do not mind this, just my service registering the drag event
});
rect.dispatchEvent(new MouseEvent("mousedown", {
view: window
}));
rect.dispatchEvent(new MouseEvent("mousemove", {
view: window
}));
rect.dispatchEvent(new MouseEvent("mouseup", {
view: window
}));
expect(endListener).toHaveBeenCalledWith(rect, jasmine.anything());
});`
Maybe this helps someone.
from d3-drag.
You might be able to use Selenium or another browser-driver to test input gestures, but personally I haven’t found a great solution. You could theoretically dispatch the appropriate low-level input events, but you’ll need more than selection.dispatch, since that only dispatches custom events and mouse/touch events have many more fields that need to be populated to drive the drag behavior appropriately.
Please use Stack Overflow tag d3.js to ask for help. Although I make an effort to assist everyone that asks, I am not always available to provide help promptly or directly. Stack Overflow provides a better collaborative forum for self-help: tens of thousands of D3-related questions have already been asked there, and some answered questions may be relevant to you.
When asking for help, please include a link to a live example that demonstrates the issue, preferably on bl.ocks.org. It is often impossible to debug from code snippets alone. Isolate the issue and reduce your code as much as possible before asking for help. The less code you post, the easier it is for someone to debug, and the more likely you are to get a helpful response.
If you have a question about D3’s behavior and want to discuss it with other users, also consider the d3-js Google Group or joining the d3-js Slack.
Thank you! 🤗
from d3-drag.
Thanks a lot for your answer, sorry I should have checked stackoverflow before creating the issue on here, I will do it the next time :)
from d3-drag.
Related Issues (20)
- Possible typo in README HOT 2
- What are the perposes of functions yesdrag and nodrag?
- Drag event on Chrome 72 HOT 3
- d3 mouseup event not firing HOT 1
- Drag Event Issue on Windows Chrome 74, related to solution for #47 HOT 1
- The default filter should return false if event.ctrlKey. HOT 5
- drag start event is don't send when Ctrl-key is press HOT 1
- drag.on("init") ? HOT 7
- Click event not firing if drag enables with simulation HOT 1
- Non-cancelable event HOT 3
- remove drag.container? HOT 1
- Use native events (*element*.dispatchEvent) instead of d3-dispatch? HOT 1
- remove dragEvent.on?
- d3.event is not available in version 6.1.1 HOT 1
- DragEvent missing event.currentTarget for arrow function node selection HOT 1
- Make event listeners non-passive. HOT 2
- d3.drag() causes error in testing mousedown event via jsdom HOT 4
- d3.pointer() could use event.touches[0] HOT 2
- Add event.currentTarget
- Support d3.drag in testing environments
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from d3-drag.