Dragging that actually works

Built-in drag and drop: dragTo()

Elements with the draggable attribute use the browser's own drag events. dragTo() handles them:

await page.getByTestId('drag-source').dragTo(page.getByTestId('drop-target'));

Drag libraries need real movement

Libraries such as dnd-kit and SortableJS listen to pointer moves instead. Most start a drag only after the pointer has moved a few pixels, then track where it is on later moves. dragTo() jumps from source to target in one move, so the library starts the drag and never sees where it went. Drive the mouse yourself, in steps:

const list = page.getByRole('list', { name: 'Priorities' });
const handle = list.getByRole('button', { name: 'Reorder Deploy' });
const target = list.getByRole('listitem').first();

await handle.hover();
await page.mouse.down();
const box = (await target.boundingBox())!;
await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2, { steps: 10 });
await page.mouse.up();

await expect(list.getByRole('listitem')).toHaveText(['Deploy', 'Write tests', /* … */]);

Or use the keyboard

Accessible drag libraries also support the keyboard: Space picks an item up, the arrow keys move it, Space drops it. It tests the same feature without any coordinates:

const handle = list.getByRole('button', { name: 'Reorder Deploy' });

await handle.press('Space');
await handle.press('ArrowUp');
await page.keyboard.press('Space');
Assert the result, not the movement: the order of the items, a card's column, a status message. That is what the user cares about.

Built-in drag and drop

Drag me
Drop here

Drag and drop not completed

Sortable list

Drag a row to reorder it. Or Tab to its handle and press Space, the arrow keys, then Space again.

  1. Write tests
  2. Review PR
  3. Fix flaky test
  4. Update docs
  5. Deploy

Nothing moved yet

Kanban board

To do (2)

  • Design login page
  • Write release notes

In progress (1)

  • Set up CI

Done (1)

  • Plan sprint