--- id: test-utils title: Test Utilities permalink: test-utils.html prev: two-way-binding-helpers.html next: clone-with-props.html --- `React.addons.TestUtils` makes it easy to test React components in the testing framework of your choice (we use [Jest](https://facebook.github.io/jest/)). ### Simulate ```javascript 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: ```javascript var node = React.findDOMNode(this.refs.input); React.addons.TestUtils.Simulate.click(node); React.addons.TestUtils.Simulate.change(node, {target: {value: 'Hello, world'}}); React.addons.TestUtils.Simulate.keyDown(node, {key: "Enter"}); ``` `Simulate` has a method for every event that React understands. ### renderIntoDocument ```javascript ReactComponent renderIntoDocument(ReactElement instance) ``` Render a component into a detached DOM node in the document. **This function requires a DOM.** ### mockComponent ```javascript 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 `
` (or other tag if `mockTagName` is provided) containing any provided children. ### isElement ```javascript boolean isElement(ReactElement element) ``` Returns `true` if `element` is any ReactElement. ### isElementOfType ```javascript boolean isElementOfType(ReactElement element, function componentClass) ``` Returns `true` if `element` is a ReactElement whose type is of a React `componentClass`. ### isDOMComponent ```javascript boolean isDOMComponent(ReactComponent instance) ``` Returns `true` if `instance` is a DOM component (such as a `
` or ``). ### isCompositeComponent ```javascript boolean isCompositeComponent(ReactComponent instance)` ``` Returns `true` if `instance` is a composite component (created with `React.createClass()`). ### isCompositeComponentWithType ```javascript boolean isCompositeComponentWithType(ReactComponent instance, function componentClass) ``` Returns `true` if `instance` is a composite component (created with `React.createClass()`) whose type is of a React `componentClass`. ### findAllInRenderedTree ```javascript 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 ```javascript 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 ```javascript 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 ```javascript 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 ```javascript 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 ```javascript array scryRenderedComponentsWithType(ReactComponent tree, function componentClass) ``` Finds all instances of components with type equal to `componentClass`. ### findRenderedComponentWithType ```javascript 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. ## 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. ```javascript 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. ```javascript shallowRenderer.render(ReactElement element) ``` Similar to `React.render`. ```javascript ReactComponent 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: ```javascript
Title
``` Then you can assert: ```javascript result = renderer.getRenderOutput(); expect(result.type).toBe('div'); expect(result.props.children).toEqual([ Title, ]); ``` 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.