How to test react-draggable? Jest + Enzyme
See original GitHub issueHello, How to test react-draggable? I try to do it by following code but it is not working (spy is not called):
describe('<MyComponent /> component:', () => {
let wrapper;
const props = {
dragStart: jest.fn(),
dragExit: jest.fn(),
dragMove: jest.fn()
};
beforeEach(() => {
wrapper = shallow(<MyComponent {...props}/>);
});
describe('Interaction:', () => {
it('should call dragStart()', () => {
wrapper.find('Dragabble').simulate('dragStart');
expect(props.dragStart).toHaveBeenCalled(); // FAIL
});
it('should call dragExit()', () => {
wrapper.find('div.bar').simulate('dragExit');
expect(props.dragExit).toHaveBeenCalled(); // FAIL
});
});
});
My draggable component looks like this:
<Draggable
onStart={this.props.dragStart.bind(this)}
onStop={this.props.dragExit.bind(this)}
onDrag={this.props.dragMove.bind(this)}>
(...)
</Draggable>
Issue Analytics
- State:
- Created 6 years ago
- Comments:7
Top Results From Across the Web
How to test react-draggable? Jest + Enzyme #313 - GitHub
Hello, How to test react-draggable? I try to do it by following code but it is not working (spy is not called): describe('...
Read more >Testing React Drag and Drop using Enzyme - Stack Overflow
I am having trouble simulating and testing the drag and drop in Enzyme. Here is my code (I removed some handle functions).
Read more >Unit Testing in React with Jest and Enzyme Frameworks
Follow these steps to write a test case in your React application. Step 1: We are going to render a simple button named...
Read more >How to test react-draggable? Jest + Enzyme - Bountysource
Hello, How to test react-draggable? I try to do it by following code but it is not working (spy is not called):
Read more >Testing - React DnD
You can test drag and drop interactions using the HTML 5 backend or touch backend with jsdom in your testing library. Many testing...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

For the lazy. I implemented a solution based on the tests link @STRML provided.
These are my current methods I pass as props for the Draggable (onDragStop ommited for brevity):
In my tests I implemented a function based on the tests example. It is very similar, with the difference I removed the code I don’t need. Namely, the
node.ownerDocument, because I have a single document on my environment.To actually simulate a mouse move, you need to call this function more than once. So I did, in my tests:
And…
Tests pass perfectly. 😃
@Artimal If you are passing the event name as a string then I think you have to use
mousedown,mousemoveandmouseup(lowercase).That is what you pass into
addEventListeneranyway.