Skip to content

Testing DnD with Cypress #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
joetidee opened this issue Dec 29, 2024 · 1 comment
Open

Testing DnD with Cypress #172

joetidee opened this issue Dec 29, 2024 · 1 comment

Comments

@joetidee
Copy link

joetidee commented Dec 29, 2024

I am migrating over from react-beautiful-dnd (RBD) to pragmatic-dnd (PD). I had some Cypress tests working with RBD, which were modelled on this example. However, now I am on PD, they no longer work.

I found this plugin, but this also doesn't seem to be dragging the draggable element.

I noticed a Cypress example on the PD website, but this also does not seem to be dragging the element, for example:

const options = {
      force: true,
      eventConstructor: 'DragEvent',
      // If you wanted to fake dragging particular data,
      // you can add it to this `DataTransfer` with `.setData()`
      // See: https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer
      dataTransfer: new DataTransfer(),
  };
  cy.get(subject).trigger('dragstart', options);
  cy.get(target).trigger('dragenter', options).trigger('drop', options);

Does PD work with Cypress at all? If so, do you have any examples of tests or plugins that should support it?

I'm using Cypress 13.17.0

@ivandevp
Copy link

ivandevp commented Jan 21, 2025

It wasn't working for me neither but I was able to simulate the drag and drop by leveraging the keyboard navigation:

  cy.get(subject).then($subject => {
    const subjectIndex = $subject.index();
    cy.get(target).then($target => {
      const targetIndex = $target.index();
      const difference = targetIndex - subjectIndex;
      const direction = difference > 0 ? '{downarrow}' : '{uparrow}';
      const steps = Math.abs(difference);

      cy.get(subject).first().focus().type(' ', { force: true });

      Array.from({ length: steps }).forEach(() => {
        cy.get(subject).first().type(direction, { force: true });
      });
      
      cy.get(subject).first().type(' ', { force: true });
    });
  });

Hope that works until there's an official way to implement it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants