ReactTestUtils

Makes it easy to test React components in the testing framework of your choice.

Demonstration code

//importing

import ReactTestUtils from 'react-dom/test-utils'; // ES6
var ReactTestUtils = require('react-dom/test-utils'); // ES5

Simulate

Simulate an event dispatch on a DOM node with optional eventData event data.

Simulate has a method for every event that React understands.

Demonstration code

Simulate.{eventName}(
  element,
  [eventData]
)

renderIntoDocument()

Render a React element into a detached DOM node in the document (this function requires a DOM).

Demonstration code

renderIntoDocument(element)

mockComponent()

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.

Demonstration code

mockComponent(
  componentClass,
  [mockTagName]
)

isElement()

Returns true if element is any React element.

Demonstration code

isElement(element)

isElementOfType()

Returns true if element is a React element whose type is of a React componentClass.

Demonstration code

isElementOfType(
  element,
  componentClass
)

isDOMComponent()

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

Demonstration code

isDOMComponent(instance)

isCompositeComponent()

Returns true if instance is a user-defined component, such as a class or a function.

Demonstration code

isCompositeComponent(instance)

findAllInRenderedTree()

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.

Demonstration code

findAllInRenderedTree(
  tree,
  test
)

scryRenderedDOMComponentsWithClass()

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

Demonstration code

scryRenderedDOMComponentsWithClass(
  tree,
  className
)

findRenderedDOMComponentWithClass()

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.

Demonstration code

findRenderedDOMComponentWithClass(
  tree,
  className
)

scryRenderedDOMComponentsWithTag()

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

Demonstration code

scryRenderedDOMComponentsWithTag(
  tree,
  tagName
)

findRenderedDOMComponentWithTag()

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.

Demonstration code

findRenderedDOMComponentWithTag(
  tree,
  tagName
)

scryRenderedComponentsWithType()

Finds all instances of components with type equal to componentClass.

Demonstration code

scryRenderedComponentsWithType(
  tree,
  componentClass
)

findRenderedComponentWithType()

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.

Demonstration code

findRenderedComponentWithType(
  tree,
  componentClass
)

ReactTestUtils — Structure map

Clickable & Draggable!

ReactTestUtils — Related pages: