What is react-test-renderer?
The react-test-renderer package is designed for testing React components in a more efficient and lightweight manner. It provides a renderer that can be used to render React components into pure JavaScript objects without depending on the DOM or a native mobile environment. This makes it ideal for testing components in isolation from the browser or any other external environment.
What are react-test-renderer's main functionalities?
Rendering components to JSON
This feature allows you to render a React component and generate a JSON output of the rendered component. This is useful for snapshot testing, where you can compare the current version of the component with a previously stored snapshot.
import React from 'react';
import renderer from 'react-test-renderer';
const component = renderer.create(<MyComponent />);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
Testing component output
This feature enables testing of the component's output. You can inspect the rendered component to verify that it has the expected props, state, or behavior. This is useful for ensuring that your component behaves correctly under various conditions.
import React from 'react';
import renderer from 'react-test-renderer';
const component = renderer.create(<MyComponent prop='value' />);
const instance = component.root;
expect(instance.props.prop).toBe('value');
Interacting with component instances
This feature allows you to interact with the component instances directly. You can simulate events or call methods on the component instance and then re-render. This is useful for testing the component's behavior in response to user interactions or lifecycle events.
import React from 'react';
import renderer from 'react-test-renderer';
class MyComponent extends React.Component {
state = { clicked: false };
handleClick = () => {
this.setState({ clicked: true });
};
render() {
return (<button onClick={this.handleClick}>Click me</button>);
}
}
const component = renderer.create(<MyComponent />);
const button = component.root.findByType('button');
button.props.onClick();
expect(component.toJSON()).toMatchSnapshot();
Other packages similar to react-test-renderer
enzyme
Enzyme is a popular testing utility for React that makes it easier to assert, manipulate, and traverse your React Components' output. Unlike react-test-renderer, Enzyme allows you to simulate events and provides a more jQuery-like API for selecting elements. However, it requires an adapter to work with different versions of React.
react-testing-library
React Testing Library is a set of helpers that let you test React components without relying on their implementation details. This approach encourages better testing practices by focusing on the behavior of components rather than their internal structure. It's more aligned with user interaction compared to react-test-renderer, which is more about rendering components to JSON for snapshot testing.
react-test-renderer
A lightweight solution to testing fully-rendered React Components
Installation
$ npm install react-test-renderer
Usage
const render = require('react-test-renderer');
const stub = createStub();
render(<MyComponent onClick={stub}/>)
.find(element => element.type === 'button')
.simulate('click');
assert.ok(stub.called);
API
interface Searchable {
find(predicate: (element: RenderedElement) => boolean): RenderedElement;
findAll(predicate: (element: RenderedElement) => boolean): Array<RenderedElement>;
findComponent(component: ReactComponent): RenderedComponent;
findAllComponent(component: ReactComponent): RenderedComponent;
}
interface RenderedElement mixins Searchable {
type: string;
props: Object;
simulate(eventName: string, eventOpts?: Object): void;
}
interface RenderedComponent mixins Searchable {
root: RenderedElement;
refs: { [name: string]: RenderedElement };
}
interface RenderedTree mixins Searchable {}
function render(ReactElement): RenderedTree
Examples
Find an element:
var tree = render(
<section>
<h1>Hello World</h1>
<p>...</p>
</section>
);
var heading = tree.find(element => element.type === 'h1');
assert.equal(heading.type, 'h1');
assert.deepEqual(heading.props, {
children: 'Hello World'
});
Simulate an event on a component:
var stub = createStub();
var tree = render(
<section>
<button onClick={stub}>Click Me!</button>
</section>
);
tree.find(element => element.type === 'button')
.simulate('click');
assert.ok(stub.called);
Usage with jsdom
Using jsdom you can run this test renderer
entirely in Node. Just set this up before you run your tests:
$ npm install --save-dev jsdom
var jsdom = require('jsdom').jsdom;
global.window = jsdom(undefined, { url: 'about:blank' }).defaultView;
global.document = global.window.document;
Note: This was tested using jsdom@9