How do I simulate browser events when writing tests for React js?

Viewed 5963

How do I simulate a click on a div element? Or a mouse move? Or text input?

How do I do it in a server side nodejs environment, like mocha? And how do I do it in a browser environment, with a runner like karma?

2 Answers

Since version React 0.9, we've included ReactTestUtils which is a small bundle of tools to help you test your components. The most useful part of it is event simulation -- you can run ReactTestUtils.Simulate.click(node) in order to simulate a click event using React's synthetic event system.

There's also a few other useful utilities there for making assertions about the DOM structure. Just download the development addons build (react-with-addons.js) and pull it out like so:

var ReactTestUtils = React.addons.TestUtils;
ReactTestUtils.Simulate.click(node);

Let me know if anything's unclear here.

Related