Test Renderer

import TestRenderer from 'react-test-renderer';

function Link(props) {
  return <a href={props.page}>{props.children}</a>;
}

const testRenderer = TestRenderer.create(
  <Link page="https://www.facebook.com/">Facebook</Link>
);

console.log(testRenderer.toJSON());
// { type: 'a',
//   props: { href: 'https://www.facebook.com/' },
//   children: [ 'Facebook' ] }
import TestRenderer from 'react-test-renderer';

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.input = null;
  }
  componentDidMount() {
    this.input.focus();
  }
  render() {
    return <input type="text" ref={el => this.input = el} />
  }
}

let focused = false;
TestRenderer.create(
  <MyComponent />,
  {
    createNodeMock: (element) => {
      if (element.type === 'input') {
        // mock a focus function
        return {
          focus: () => {
            focused = true;
          }
        };
      }
      return null;
    }
  }
);
expect(focused).toBe(true);

This package provides a React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment.

TestRenderer.create()

Create a TestRenderer instance with the passed React element. It doesn’t use the real DOM, but it still fully renders the component tree into memory so you can make assertions about it. The returned instance has the following methods and properties.

testRenderer.toJSON()

Return an object representing the rendered tree. This tree only contains the platform-specific nodes like <div> or <View> and their props, but doesn’t contain any user-written components. This is handy for snapshot testing.

testRenderer.toTree()

Return an object representing the rendered tree. Unlike toJSON(), the representation is more detailed than the one provided by toJSON(), and includes the user-written components. You probably don’t need this method unless you’re writing your own assertion library on top of the test rendererer.

testRenderer.update()

Re-render the in-memory tree with a new root element. This simulates a React update at the root. If the new element has the same type and key as the previous element, the tree will be updated; otherwise, it will re-mount a new tree.

testRenderer.unmount()

Unmount the in-memory tree, triggering the appropriate lifecycle events.

testRenderer.getInstance()

Return the instance corresponding to the root element, if available. This will not work if the root element is a function component because they don’t have instances.

testRenderer.root

Returns the root “test instance” object that is useful for making assertions about specific nodes in the tree. You can use it to find other “test instances” deeper below.

testInstance.find()

Find a single descendant test instance for which test(testInstance) returns true. If test(testInstance) does not return true for exactly one test instance, it will throw an error.

testInstance.findByType()

Find a single descendant test instance with the provided type. If there is not exactly one test instance with the provided type, it will throw an error.

testInstance.findByProps()

Find a single descendant test instance with the provided props. If there is not exactly one test instance with the provided props, it will throw an error.

testInstance.findAll()

Find all descendant test instances for which test(testInstance) returns true.

TestInstance.findAllByType()

Find all descendant test instances with the provided type.

testInstance.findAllByProps()

Find all descendant test instances with the provided props.

testInstance.instance

The component instance corresponding to this test instance. It is only available for class components, as function components don’t have instances. It matches the this value inside the given component.

testInstance.type

The component type corresponding to this test instance. For example, a <Button /> component has a type of Button.

testInstance.props

The props corresponding to this test instance. For example, a <Button size="small" /> component has {size: 'small'} as props.

testInstance.parent

The parent test instance of this test instance.

testInstance.children

The children test instances of this test instance.

Test Renderer — Structure map

Clickable & Draggable!

Test Renderer — Related pages: