Repository navigation
How can I test the drag events with react testing library and vitest? #2095
Replies: 2 comments 1 reply
|
Tracked this down in vitest + jsdom. The default PointerSensor just ignores your fireEvent.pointerDown, so nothing fires. Its activator bails early:
RTL's pointerDown doesn't set isPrimary, so the sensor never starts. Two things that worked for me on @dnd-kit/core 6.3.1:
Caveat: actual sortable reordering still won't resolve, since jsdom returns 0s from getBoundingClientRect and the position math collapses. For that, mock getBoundingClientRect or run in a real browser (Playwright / vitest browser mode). |
|
The new exception is a separate jsdom gap, and its stack trace identifies it precisely. In For a callback/state unit test, add a small Pointer Events shim in the Vitest setup file: class MockPointerEvent extends MouseEvent {
readonly pointerId: number
readonly pointerType: string
readonly isPrimary: boolean
constructor(type: string, init: PointerEventInit = {}) {
super(type, init)
this.pointerId = init.pointerId ?? 1
this.pointerType = init.pointerType ?? 'mouse'
this.isPrimary = init.isPrimary ?? true
}
}
Object.defineProperty(window, 'PointerEvent', {
configurable: true,
writable: true,
value: MockPointerEvent,
})
Object.defineProperty(globalThis, 'PointerEvent', {
configurable: true,
writable: true,
value: MockPointerEvent,
})
Object.defineProperties(HTMLElement.prototype, {
setPointerCapture: {
configurable: true,
value: () => {},
},
releasePointerCapture: {
configurable: true,
value: () => {},
},
hasPointerCapture: {
configurable: true,
value: () => false,
},
})The pointer-capture shim matters too. After activation, this sensor calls Then dispatch a consistent pointer sequence: fireEvent.pointerDown(handle, {
button: 0,
isPrimary: true,
pointerId: 1,
pointerType: 'mouse',
clientX: 0,
clientY: 0,
})
fireEvent.pointerMove(document, {
button: 0,
isPrimary: true,
pointerId: 1,
pointerType: 'mouse',
clientX: 20,
clientY: 0,
})
fireEvent.pointerUp(document, {
button: 0,
isPrimary: true,
pointerId: 1,
pointerType: 'mouse',
clientX: 20,
clientY: 0,
})The primary-pointer and left-button values are required by the same sensor, and the movement also clears the default distance constraint when one applies. There is no need to mock the entire dnd-kit package. This is suitable for asserting callbacks and state transitions. It is not a realistic sortable-layout test: jsdom does not calculate layout, so its element rectangles collapse to zero unless explicitly mocked. For collision detection, final item positions, and actual pointer capture, use Vitest Browser Mode or Playwright; Vitest explains the simulated-browser limitation here. |
Uh oh!
There was an error while loading. Please reload this page.
Possibly a duplicate of #1265
I'm having a hard time triggering the drag events in unit tests with no success up to this point. Should I mock the whole package or is there any other easier way? It would also be really helpful to have a couple of unit test examples in the library docs.
All reactions