question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to test react-draggable? Jest + Enzyme

See original GitHub issue

Hello, 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:open
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
andreyluizcommented, Apr 24, 2018

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):

onDragStart() {
  this.setState({ dragging: true });
}

onDrag(e, data) {
  if (this.state.dragging) {
    this.setState({
      x: data.x,
      y: data.y
    });
  }
}

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.

function mouseMove(x, y, node) {
  const evt = document.createEvent('MouseEvents');
  evt.initMouseEvent("mousemove", true, true, window, 0, 0, 0, x, y, false, false, false, false, 0, null);
  document.dispatchEvent(evt);
  return evt;
}

To actually simulate a mouse move, you need to call this function more than once. So I did, in my tests:

expect(component.state().dragging).toBe(false);

expect(component.find("Draggable").at(1).prop("position")).toEqual({ x: 0, y: 0 });

component.find("Draggable").at(1).simulate("mousedown");

expect(component.state().dragging).toBe(true);

mouseMove(115, 319);
mouseMove(214, 325);
mouseMove(225, 356);
mouseMove(255, 375);

expect(component.find("Draggable").at(1).prop("position")).toEqual({ x: 140, y: 56 });

And…

 PASS  src\components\liveStream\liveStream.test.jsx
 PASS  src\components\liveStream\mediaPlayer.test.jsx
 PASS  src\components\liveStream\containers\liveStream.test.jsx

Test Suites: 3 passed, 3 total
Tests:       30 passed, 30 total
Snapshots:   0 total
Time:        7.293s
Ran all test suites related to changed files.

Watch Usage: Press w to show more.

Tests pass perfectly. 😃

0reactions
cstrocommented, Jan 24, 2018

@Artimal If you are passing the event name as a string then I think you have to use mousedown, mousemove and mouseup (lowercase).

That is what you pass into addEventListener anyway.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found