• Test Utilities
    • Simulate
    • renderIntoDocument
    • mockComponent
    • isElement
    • isElementOfType
    • isDOMComponent
    • isCompositeComponent
    • isCompositeComponentWithType
    • findAllInRenderedTree
    • scryRenderedDOMComponentsWithClass
    • findRenderedDOMComponentWithClass
    • scryRenderedDOMComponentsWithTag
    • findRenderedDOMComponentWithTag
    • scryRenderedComponentsWithType
    • findRenderedComponentWithType
  • Shallow rendering

    Test Utilities

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

    1. var ReactTestUtils = require('react-addons-test-utils');

    Simulate

    1. Simulate.{eventName}(
    2. DOMElement element,
    3. [object eventData]
    4. )

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

    Clicking an element

    1. // <button ref="button">...</button>
    2. var node = this.refs.button;
    3. ReactTestUtils.Simulate.click(node);

    Changing the value of an input field and then pressing ENTER.

    1. // <input ref="input" />
    2. var node = this.refs.input;
    3. node.value = 'giraffe'
    4. ReactTestUtils.Simulate.change(node);
    5. ReactTestUtils.Simulate.keyDown(node, {key: "Enter", keyCode: 13, which: 13});

    Note that you will have to provide any event property that you’re using in your component (e.g. keyCode, which, etc…) as React is not creating any of these for you.

    Simulate has a method for every event that React understands.

    renderIntoDocument

    1. ReactComponent renderIntoDocument(
    2. ReactElement instance
    3. )

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

    Note:

    You will need to have window, window.document and window.document.createElement
    globally available before you import React. Otherwise React will think it can’t access the DOM and methods like setState won’t work.

    mockComponent

    1. object mockComponent(
    2. function componentClass,
    3. [string mockTagName]
    4. )

    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.

    isElement

    1. boolean isElement(
    2. ReactElement element
    3. )

    Returns true if element is any ReactElement.

    isElementOfType

    1. boolean isElementOfType(
    2. ReactElement element,
    3. function componentClass
    4. )

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

    isDOMComponent

    1. boolean isDOMComponent(
    2. ReactComponent instance
    3. )

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

    isCompositeComponent

    1. boolean isCompositeComponent(
    2. ReactComponent instance
    3. )

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

    isCompositeComponentWithType

    1. boolean isCompositeComponentWithType(
    2. ReactComponent instance,
    3. function componentClass
    4. )

    Returns true if instance is a composite component (created with React.createClass()) whose type is of a React componentClass.

    findAllInRenderedTree

    1. array findAllInRenderedTree(
    2. ReactComponent tree,
    3. function test
    4. )

    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

    1. array scryRenderedDOMComponentsWithClass(
    2. ReactComponent tree, string className
    3. )

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

    findRenderedDOMComponentWithClass

    1. ReactComponent findRenderedDOMComponentWithClass(
    2. ReactComponent tree,
    3. string className
    4. )

    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

    1. array scryRenderedDOMComponentsWithTag(
    2. ReactComponent tree,
    3. string tagName
    4. )

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

    findRenderedDOMComponentWithTag

    1. ReactComponent findRenderedDOMComponentWithTag(
    2. ReactComponent tree,
    3. string tagName
    4. )

    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

    1. array scryRenderedComponentsWithType(
    2. ReactComponent tree,
    3. function componentClass
    4. )

    Finds all instances of components with type equal to componentClass.

    findRenderedComponentWithType

    1. ReactComponent findRenderedComponentWithType(
    2. ReactComponent tree, function componentClass
    3. )

    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.

    Shallow rendering

    Shallow rendering is an experimental feature that lets you render a component “one level deep” and assert facts about what its render method returns, without worrying about the behavior of child components, which are not instantiated or rendered. This does not require a DOM.

    1. ReactShallowRenderer createRenderer()

    Call this in your tests to create a shallow renderer. You can think of this as a “place” to render the component you’re testing, where it can respond to events and update itself.

    1. shallowRenderer.render(
    2. ReactElement element
    3. )

    Similar to ReactDOM.render.

    1. ReactElement shallowRenderer.getRenderOutput()

    After render has been called, returns shallowly rendered output. You can then begin to assert facts about the output. For example, if your component’s render method returns:

    1. <div>
    2. <span className="heading">Title</span>
    3. <Subcomponent foo="bar" />
    4. </div>

    Then you can assert:

    1. var renderer = ReactTestUtils.createRenderer();
    2. result = renderer.getRenderOutput();
    3. expect(result.type).toBe('div');
    4. expect(result.props.children).toEqual([
    5. <span className="heading">Title</span>,
    6. <Subcomponent foo="bar" />
    7. ]);

    Shallow testing currently has some limitations, namely not supporting refs. We’re releasing this feature early and would appreciate the React community’s feedback on how it should evolve.