d3 / d3-drag

Drag and drop SVG, HTML or Canvas using mouse or touch input.

Home Page:https://d3js.org/d3-drag

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to test d3-drag ?

BafS opened this issue · comments

Hi,

I want to unit test my library made with d3 and d3-drag but I'm not able to test the drag.

I tried something like d3.selectAll(document.querySelectorAll('*')).dispatch('drag') or .dispatch('mousedown') but I'm not able to trigger anything and the dispatch doesn't seem to work with the drag (it does with the other events like click).

Thank you for your clarification on the issue

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! 🤗

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 :)

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.