Skip to content
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

Trying to drag and drop with Cypress #208

Closed
damien-monni opened this issue Apr 19, 2021 · 9 comments
Closed

Trying to drag and drop with Cypress #208

damien-monni opened this issue Apr 19, 2021 · 9 comments

Comments

@damien-monni
Copy link

Hi !

I would like to test my application with Cypress but I can't find a way to simulate a drag and drop. I tried to use the same mouseMoveBy command you created in draggable_spec.ts, but it has no effect on my application. Nothing seems to move.

I just have a basic drag and drop setup from the DndKit getting started documentation, nothing fancy. Did you have to setup something special to make it work with Cypress?

I'm not sure if I can use Cypress with Code Sandbox so I can link a reproductive example. I'll investigate.

Thank you

@damien-monni damien-monni changed the title Trying to drad and drop with Cypress Trying to drag and drop with Cypress Apr 19, 2021
@clauderic
Copy link
Owner

@damien-monni the custom commands used for testing in Cypress can be found here: https://github.com/clauderic/dnd-kit/blob/master/cypress/support/commands.ts

Did you also add these in your Cypress support folder?

@damien-monni
Copy link
Author

Yes I used the exact same commands

@damien-monni
Copy link
Author

damien-monni commented Apr 19, 2021

@clauderic I could not find a way to put an example online so I just created minimal repo reproducing the issue: https://github.com/damien-monni/dnd-kit-issue

To reproduce:

git clone git@github.com:damien-monni/dnd-kit-issue.git

cd dnd-kit-issue

npm install

npm start

npm run cypress:open

Launch the only test, you will see that nothing seems to be dragged and dropped.

@iguto
Copy link

iguto commented Apr 22, 2021

hi, @damien-monni

I had similar issue and found a solution.
in your dnd-kit-issue repository, try this:

diff --git a/src/App.js b/src/App.js
index 0eee1ee..766a14c 100644
--- a/src/App.js
+++ b/src/App.js
@@ -1,10 +1,13 @@
-import { useDroppable, useDraggable, DndContext } from "@dnd-kit/core";
+import { useDroppable, useDraggable, DndContext, MouseSensor, useSensor, useSensors } from "@dnd-kit/core";
 import { useState } from "react";

 const Draggable = () => {
   const { attributes, listeners, setNodeRef, transform } = useDraggable({
     id: "draggable",
   });
+  const sensors = useSensors(
+    useSensor(MouseSensor),
+  )

   return (
     <div style={{ display: "flex" }}>
@@ -62,9 +65,13 @@ const Root = ({ isDropped }) => {

 function App() {
   const [isDropped, setIsDropped] = useState(false);
+  const sensors = useSensors(
+    useSensor(MouseSensor),
+  )

   return (
     <DndContext
+      sensors={sensors}
       onDragEnd={(event) => {
         if (event.over?.id) {
           setIsDropped(true);

It seems like MouseSensor is necessary. I hope this helps.

@damien-monni
Copy link
Author

@iguto It's working! Thank you so much!

@clauderic
Copy link
Owner

Right. The MouseSensor is necessary because the commands trigger mouse events. If you would like to use the Pointer sensor, trigger pointer events instead (for example, pointerdown instead of mousedown)

@chestozo
Copy link
Contributor

@damien-monni any luck with testing?
I am also trying to make them work with pointer events with no luck as of now.

@chestozo
Copy link
Contributor

#437

@AriPerkkio
Copy link

Couple of years old issue but just in case others run into same issues with pointer events:

It's not just as simple as replacing mouse events with pointer ones. You'll also have to add couple of parameters to get through these checks:

if (!event.isPrimary || event.button !== 0) {
return false;
}

This works with latest Cypress and dnd-kit:

cy.wrap(button)
    .trigger('pointerdown', {
        force: true,
        isPrimary: true,
        button: 0,
    })
    .wait(1_000)
    .trigger('pointermove', {
        clientX: 100,
        clientY: 100,
        force: true,
        isPrimary: true,
        button: 0,
    })
    .wait(1000)
    .trigger('pointerup', {
        force: true,
        isPrimary: true,
        button: 0,
    });

markbastian added a commit to metabase/metabase that referenced this issue Jan 19, 2024
The primary change needed to support rearranging of tabs with super long names is the to shorten the names only when dragging. This was achieved by exposing the `isDragging` prop of the `useSortable` hook in the `RenameableTabButtonStyled` component. The label of this component is dynamically shortened if it is very long on a drag operation by the following logic and function:

```
// Update the label prop when dragging
label={isDragging ? dragLabel(label) : label}
```

The `dragLabel` function is as follows:
```
  const dragLabel = (s: string) => {
    if (s.length < 20) {
      return s;
    } else {
      return `${s.slice(0, 17)}...`;
    }
  };
```

The other change needed to support e2e testing for this operation was the addition of the `MouseSensor` hook to the DnDContext. See [this issue comment](clauderic/dnd-kit#208 (comment)) for explanation.

Once that change was in place it was possible to do a drag and drop rearrange test in the "should allow me to rearrange long tabs (#34970)" spec.

This test:
- Creates a dashboard with 3 tabs
- Ensures their order
- Gives the last tab a super long name that would not be rearrangeable before fix
- Drags that tab to the first position
- Saves the dashboard
- Asserts the new tab order is saved

Fixes #34970
markbastian added a commit to metabase/metabase that referenced this issue Jan 19, 2024
* Enabling dragging of long-named TabButtons

The primary change needed to support rearranging of tabs with super long names is the to shorten the names only when dragging. This was achieved by exposing the `isDragging` prop of the `useSortable` hook in the `RenameableTabButtonStyled` component. The label of this component is dynamically shortened if it is very long on a drag operation by the following logic and function:

```
// Update the label prop when dragging
label={isDragging ? dragLabel(label) : label}
```

The `dragLabel` function is as follows:
```
  const dragLabel = (s: string) => {
    if (s.length < 20) {
      return s;
    } else {
      return `${s.slice(0, 17)}...`;
    }
  };
```

The other change needed to support e2e testing for this operation was the addition of the `MouseSensor` hook to the DnDContext. See [this issue comment](clauderic/dnd-kit#208 (comment)) for explanation.

Once that change was in place it was possible to do a drag and drop rearrange test in the "should allow me to rearrange long tabs (#34970)" spec.

This test:
- Creates a dashboard with 3 tabs
- Ensures their order
- Gives the last tab a super long name that would not be rearrangeable before fix
- Drags that tab to the first position
- Saves the dashboard
- Asserts the new tab order is saved

Fixes #34970

* Fixing type from T to unknown to satisfy the type of the render function in TabButton.unit.spec.ts

* Fixing type from T to any to satisfy the type of the render function in TabButton.unit.spec.ts

* Fixing divergent tests

The tests to ensure DnD works required the addition of the `mouseSensor` in TabRow.tsx. This, in turn, broke the tests in DashboardTabs.unit.spec.tsx. Adding in the `activationConstraint: { distance: 10 }` prop to the mouseSensor fixed the DashboardTabs.unit.spec.tsx tests, but broke the DnD tests. Based on the docs [here](https://docs.dndkit.com/api-documentation/sensors/mouse) it looks like you need to move a "distance, in pixels, by which the mouse needs to be moved before a drag start event is emitted." So, the DnD test in tabs.cy.spec.js added an addtional mousemove trigger of 11 pixels to activate the mouseSensor. Yeah, it totally makes sense. 😜🤯💢😡😥

* prettier
github-actions bot pushed a commit to metabase/metabase that referenced this issue Jan 19, 2024
* Enabling dragging of long-named TabButtons

The primary change needed to support rearranging of tabs with super long names is the to shorten the names only when dragging. This was achieved by exposing the `isDragging` prop of the `useSortable` hook in the `RenameableTabButtonStyled` component. The label of this component is dynamically shortened if it is very long on a drag operation by the following logic and function:

```
// Update the label prop when dragging
label={isDragging ? dragLabel(label) : label}
```

The `dragLabel` function is as follows:
```
  const dragLabel = (s: string) => {
    if (s.length < 20) {
      return s;
    } else {
      return `${s.slice(0, 17)}...`;
    }
  };
```

The other change needed to support e2e testing for this operation was the addition of the `MouseSensor` hook to the DnDContext. See [this issue comment](clauderic/dnd-kit#208 (comment)) for explanation.

Once that change was in place it was possible to do a drag and drop rearrange test in the "should allow me to rearrange long tabs (#34970)" spec.

This test:
- Creates a dashboard with 3 tabs
- Ensures their order
- Gives the last tab a super long name that would not be rearrangeable before fix
- Drags that tab to the first position
- Saves the dashboard
- Asserts the new tab order is saved

Fixes #34970

* Fixing type from T to unknown to satisfy the type of the render function in TabButton.unit.spec.ts

* Fixing type from T to any to satisfy the type of the render function in TabButton.unit.spec.ts

* Fixing divergent tests

The tests to ensure DnD works required the addition of the `mouseSensor` in TabRow.tsx. This, in turn, broke the tests in DashboardTabs.unit.spec.tsx. Adding in the `activationConstraint: { distance: 10 }` prop to the mouseSensor fixed the DashboardTabs.unit.spec.tsx tests, but broke the DnD tests. Based on the docs [here](https://docs.dndkit.com/api-documentation/sensors/mouse) it looks like you need to move a "distance, in pixels, by which the mouse needs to be moved before a drag start event is emitted." So, the DnD test in tabs.cy.spec.js added an addtional mousemove trigger of 11 pixels to activate the mouseSensor. Yeah, it totally makes sense. 😜🤯💢😡😥

* prettier
metabase-bot bot added a commit to metabase/metabase that referenced this issue Jan 22, 2024
* Enabling dragging of long-named TabButtons

The primary change needed to support rearranging of tabs with super long names is the to shorten the names only when dragging. This was achieved by exposing the `isDragging` prop of the `useSortable` hook in the `RenameableTabButtonStyled` component. The label of this component is dynamically shortened if it is very long on a drag operation by the following logic and function:

```
// Update the label prop when dragging
label={isDragging ? dragLabel(label) : label}
```

The `dragLabel` function is as follows:
```
  const dragLabel = (s: string) => {
    if (s.length < 20) {
      return s;
    } else {
      return `${s.slice(0, 17)}...`;
    }
  };
```

The other change needed to support e2e testing for this operation was the addition of the `MouseSensor` hook to the DnDContext. See [this issue comment](clauderic/dnd-kit#208 (comment)) for explanation.

Once that change was in place it was possible to do a drag and drop rearrange test in the "should allow me to rearrange long tabs (#34970)" spec.

This test:
- Creates a dashboard with 3 tabs
- Ensures their order
- Gives the last tab a super long name that would not be rearrangeable before fix
- Drags that tab to the first position
- Saves the dashboard
- Asserts the new tab order is saved

Fixes #34970

* Fixing type from T to unknown to satisfy the type of the render function in TabButton.unit.spec.ts

* Fixing type from T to any to satisfy the type of the render function in TabButton.unit.spec.ts

* Fixing divergent tests

The tests to ensure DnD works required the addition of the `mouseSensor` in TabRow.tsx. This, in turn, broke the tests in DashboardTabs.unit.spec.tsx. Adding in the `activationConstraint: { distance: 10 }` prop to the mouseSensor fixed the DashboardTabs.unit.spec.tsx tests, but broke the DnD tests. Based on the docs [here](https://docs.dndkit.com/api-documentation/sensors/mouse) it looks like you need to move a "distance, in pixels, by which the mouse needs to be moved before a drag start event is emitted." So, the DnD test in tabs.cy.spec.js added an addtional mousemove trigger of 11 pixels to activate the mouseSensor. Yeah, it totally makes sense. 😜🤯💢😡😥

* prettier

Co-authored-by: Mark Bastian <markbastian@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants