-
-
Notifications
You must be signed in to change notification settings - Fork 639
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
Comments
@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 |
Yes I used the exact same commands |
@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:
Launch the only test, you will see that nothing seems to be dragged and dropped. |
hi, @damien-monni I had similar issue and found a solution.
It seems like MouseSensor is necessary. I hope this helps. |
@iguto It's working! Thank you so much! |
Right. The |
@damien-monni any luck with testing? |
Couple of years old issue but just in case others run into same issues with It's not just as simple as replacing dnd-kit/packages/core/src/sensors/pointer/PointerSensor.ts Lines 37 to 39 in 694dcc2
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,
}); |
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
* 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
* 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
* 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>
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 indraggable_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
The text was updated successfully, but these errors were encountered: