Coder Social home page Coder Social logo

How to test d3-drag ? about d3-drag HOT 3 CLOSED

d3 avatar d3 commented on April 28, 2024
How to test d3-drag ?

from d3-drag.

Comments (3)

scipper avatar scipper commented on April 28, 2024 4

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.

mbostock avatar mbostock commented on April 28, 2024

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.

BafS avatar BafS commented on April 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.