Registry / testing / react-beautiful-dnd-test-utils

react-beautiful-dnd-test-utils

JSON →
library4.1.1jsnpmunverified

react-beautiful-dnd-test-utils is a testing utility library designed to facilitate unit and integration testing of components that integrate with `react-beautiful-dnd` (rbd). It leverages `@testing-library/react` for DOM interaction and assertions, making it suitable for testing rbd applications in a JSDOM environment like Jest. The current stable version is 4.1.1, released in late 2021. While not frequently updated, its purpose as a testing helper for a stable library means updates are typically driven by major changes in rbd or testing-library. Its primary differentiator is its specific focus on simplifying drag-and-drop interactions within tests for rbd components, abstracting away complex simulated events.

npm install react-beautiful-dnd-test-utils
INSTALL
IMPORT
SIG · REACT-BEAUTIFUL-DN
R
react-beautiful-dnd-test-utils
testingjavascriptv4.1.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

makeDnd
import { makeDnd } from 'react-beautiful-dnd-test-utils'
import makeDnd from 'react-beautiful-dnd-test-utils'
This is a named export. It creates a function to simulate drag and drop events for an element found by text or custom getter.
mockDndSpacing
import { mockDndSpacing } from 'react-beautiful-dnd-test-utils'
const { mockDndSpacing } = require('react-beautiful-dnd-test-utils')
Used to mock CSS properties like `getComputedStyle` which `react-beautiful-dnd` relies on. Essential for JSDOM environments. Renamed from `mockDndElSpacing` in v4.0.0.
mockGetComputedStyle
import { mockGetComputedStyle } from 'react-beautiful-dnd-test-utils'
import { mockGetComputedSpacing } from 'react-beautiful-dnd-test-utils'
Renamed from `mockGetComputedSpacing` in v4.0.0. Provides a more generic mock for `window.getComputedStyle` if `mockDndSpacing` is insufficient.

This quickstart demonstrates how to set up a basic test for a `react-beautiful-dnd` component using `makeDnd` and `mockDndSpacing`. It shows how to render a list of draggable items, mock necessary DOM properties for `rbd` to function correctly in JSDOM, and then simulate a drag-and-drop event to verify item reordering and state updates.

import React from 'react'; import { render, screen } from '@testing-library/react'; import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd'; import { makeDnd, mockDndSpacing } from 'react-beautiful-dnd-test-utils'; // A simple component using react-beautiful-dnd function DraggableList({ items, onDragEnd }) { return ( <DragDropContext onDragEnd={onDragEnd}> <Droppable droppableId="list"> {(provided) => ( <div {...provided.droppableProps} ref={provided.innerRef} data-testid="droppable-list"> {items.map((item, index) => ( <Draggable key={item.id} draggableId={item.id} index={index}> {(provided) => ( <div ref={provided.innerRef} {...provided.draggableProps} {...provided.dragHandleProps} data-testid={`draggable-item-${item.id}`} > {item.content} </div> )} </Draggable> ))} {provided.placeholder} </div> )} </Droppable> </DragDropContext> ); } describe('DraggableList', () => { const initialItems = [ { id: 'item-1', content: 'Item 1' }, { id: 'item-2', content: 'Item 2' }, { id: 'item-3', content: 'Item 3' }, ]; let itemsInState = [...initialItems]; const handleDragEnd = (result) => { if (!result.destination) return; const [removed] = itemsInState.splice(result.source.index, 1); itemsInState.splice(result.destination.index, 0, removed); }; beforeEach(() => { // Reset items for each test itemsInState = [...initialItems]; // Mock getComputedStyle for rbd in JSDOM mockDndSpacing(document.body); }); it('should be able to drag "Item 1" down by one position', async () => { render(<DraggableList items={itemsInState} onDragEnd={handleDragEnd} />); const dnd = makeDnd({ getDragElement: () => screen.getByText('Item 1'), }); // Simulate dragging 'Item 1' down by one position await dnd.dragBy(1); // Verify the order has changed in the mocked state and rendered DOM expect(itemsInState[0].content).toBe('Item 2'); expect(itemsInState[1].content).toBe('Item 1'); expect(itemsInState[2].content).toBe('Item 3'); // Re-render to show updated state (if you were testing the UI after state change) // Or assert directly on the `itemsInState` for stateful components. }); });
Debug
Known issues
breakingIn v4.0.0, the `mockGetComputedSpacing` function was renamed to `mockGetComputedStyle` to better reflect its broader mocking capabilities for CSS computed styles.
fix
Update imports and calls from `mockGetComputedSpacing` to `mockGetComputedStyle`.
affects: >=4.0.0
breakingThe `mockDndElSpacing` utility was changed in v4.0.0. Its signature changed from `mockDndElSpacing(rtlUtils: RenderResult)` to `mockDndSpacing(container: HTMLElement)`.
fix
Replace `mockDndElSpacing(renderResult)` with `mockDndSpacing(container)` where `container` is typically `document.body` or `renderResult.container`.
affects: >=4.0.0
breakingThe `makeDnd` utility's constructor argument changed in v4.0.0. Instead of `makeDnd({ getDragEl }: { getDragEl: () => Element })`, it now typically accepts `makeDnd({ text }: { text: string })` or `makeDnd({ getDragElement }: { getDragElement: () => HTMLElement })`.
fix
Adjust the `makeDnd` call to use either the `text` option to find the draggable element by its text content, or the `getDragElement` option to provide a function that returns the draggable element.
affects: >=4.0.0
gotchaThis library has specific compatibility requirements with `react-beautiful-dnd`. Versions 3+ of `react-beautiful-dnd-test-utils` are designed to work with `react-beautiful-dnd` version 12+. For `react-beautiful-dnd` version 11, you must use version 2 of this library.
fix
Ensure your `react-beautiful-dnd` version aligns with the supported version for `react-beautiful-dnd-test-utils`. Downgrade `react-beautiful-dnd-test-utils` to v2 if using `rbd` v11, or upgrade `rbd` to v12+.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'getComputedStyle')
`react-beautiful-dnd` relies on `window.getComputedStyle` which is not available in a JSDOM environment by default.
fix
Call `mockDndSpacing(document.body)` or `mockGetComputedStyle()` in your `beforeEach` hook to mock the necessary DOM APIs for `react-beautiful-dnd`.
Error: Could not find drag handle with text: 'My Draggable Item'
The `makeDnd({ text: '...' })` function failed to locate an element with the specified text content to initiate the drag.
fix
Ensure the text provided to `makeDnd` exactly matches the visible text content of the draggable element, or use the `getDragElement` option to provide a custom selector function.
Property 'dragHandleProps' does not exist on type 'DraggableProvided'.
This is a TypeScript error when `dragHandleProps` is not correctly spread onto the element that should serve as the drag handle for the draggable item, or incorrect types are inferred.
fix
Ensure that `provided.dragHandleProps` is correctly spread onto the intended drag handle element within your `<Draggable>` component, e.g., `<div {...provided.dragHandleProps}>...</div>`. Verify `react-beautiful-dnd` and `@types/react-beautiful-dnd` versions match.
Upgrade
Version history
4.1.1latest on npm
Audit
Dependencies
@testing-library/jest-domrequiredRequired for Jest DOM matchers and extending assertions.
@testing-library/reactrequiredCore library for rendering React components and interacting with the DOM in tests.
@testing-library/user-eventrequiredSimulates user interactions more closely than fireEvent, including drag and drop actions.
jestrequiredPrimary test runner for which this library's utilities are designed.
Agent activity
7 hits · last 30 days
node
6
Amazon
1
Resources
react-beautiful-dnd-test-utils — npm install react-beautiful-dnd-test-utils · libregistry