Test Utilities Edit on GitHub

React.addons.TestUtils makes it easy to test React components in the testing framework of your choice (we use Jest).

Simulate #

Simulate.{eventName}(DOMElement element, object eventData)

Simulate an event dispatch on a DOM node with optional eventData event data. This is possibly the single most useful utility in ReactTestUtils.

Example usage:

var node = this.refs.input.getDOMNode();
React.addons.TestUtils.Simulate.click(node);
React.addons.TestUtils.Simulate.change(node);
React.addons.TestUtils.Simulate.keyDown(node, {key: "Enter"});

Simulate has a method for every event that React understands.

renderIntoDocument #

ReactComponent renderIntoDocument(ReactComponent instance)

Render a component into a detached DOM node in the document. This function requires a DOM.

mockComponent #

object mockComponent(function componentClass, string? mockTagName)

Pass a mocked component module to this method to augment it with useful methods that allow it to be used as a dummy React component. Instead of rendering as usual, the component will become a simple <div> (or other tag if mockTagName is provided) containing any provided children.

isDescriptorOfType #

boolean isDescriptorOfType(ReactDescriptor descriptor, function componentClass)

Returns true if descriptor is an instance of a React componentClass.

isDOMComponent #

boolean isDOMComponent(ReactComponent instance)

Returns true if instance is a DOM component (such as a <div> or <span>).

isCompositeComponent #

boolean isCompositeComponent(ReactComponent instance)`

Returns true if instance is a composite component (created with React.createClass())

isCompositeComponentWithType #

boolean isCompositeComponentWithType(ReactComponent instance, function componentClass)

The combination of isComponentOfType() and isCompositeComponent().

isTextComponent #

boolean isTextComponent(ReactComponent instance)

Returns true if instance is a plain text component.

findAllInRenderedTree #

array findAllInRenderedTree(ReactComponent tree, function test)

Traverse all components in tree and accumulate all components where test(component) is true. This is not that useful on its own, but it's used as a primitive for other test utils.

scryRenderedDOMComponentsWithClass #

array scryRenderedDOMComponentsWithClass(ReactComponent tree, string className)

Finds all instances of components in the rendered tree that are DOM components with the class name matching className.

findRenderedDOMComponentWithClass #

ReactComponent findRenderedDOMComponentWithClass(ReactComponent tree, string className)

Like scryRenderedDOMComponentsWithClass() but expects there to be one result, and returns that one result, or throws exception if there is any other number of matches besides one.

scryRenderedDOMComponentsWithTag #

array scryRenderedDOMComponentsWithTag(ReactComponent tree, string tagName)

Finds all instances of components in the rendered tree that are DOM components with the tag name matching tagName.

findRenderedDOMComponentWithTag #

ReactComponent findRenderedDOMComponentWithTag(ReactComponent tree, string tagName)

Like scryRenderedDOMComponentsWithTag() but expects there to be one result, and returns that one result, or throws exception if there is any other number of matches besides one.

scryRenderedComponentsWithType #

array scryRenderedComponentsWithType(ReactComponent tree, function componentClass)

Finds all instances of components with type equal to componentClass.

findRenderedComponentWithType #

ReactComponent findRenderedComponentWithType(ReactComponent tree, function componentClass)

Same as scryRenderedComponentsWithType() but expects there to be one result and returns that one result, or throws exception if there is any other number of matches besides one.