|
| 1 | +--- |
| 2 | +id: test-utils |
| 3 | +title: Test Utilities |
| 4 | +layout: docs |
| 5 | +permalink: test-utils.html |
| 6 | +prev: class-name-manipulation.html |
| 7 | +next: examples.html |
| 8 | +--- |
| 9 | + |
| 10 | +`React.addons.TestUtils` makes it easy to test React components in the testing framework of your choice (we use [Jasmine](http://pivotal.github.io/jasmine/) with [jsdom](https://github.com/tmpvar/jsdom)). |
| 11 | + |
| 12 | +#### ReactComponent renderIntoDocument(ReactComponent instance) |
| 13 | + |
| 14 | +Render a component into a detached DOM node in the document. **This function requires a DOM.** |
| 15 | + |
| 16 | +#### boolean isComponentOfType(ReactComponent instance, function componentClass) |
| 17 | + |
| 18 | +Returns true if `instance` is an instance of a React `componentClass`. |
| 19 | + |
| 20 | +#### boolean isDOMComponent(ReactComponent instance) |
| 21 | + |
| 22 | +Returns true if `instance` is a DOM component (such as a `<div>` or `<span>`). |
| 23 | + |
| 24 | +#### boolean isCompositeComponent(ReactComponent instance)` |
| 25 | + |
| 26 | +Returns true if `instance` is a composite component (created with `React.createClass()`) |
| 27 | + |
| 28 | +#### boolean isCompositeComponentWithType(ReactComponent instance, function componentClass) |
| 29 | + |
| 30 | +The combination of `isComponentOfType()` and `isCompositeComponent()`. |
| 31 | + |
| 32 | +#### boolean isTextComponent(ReactComponent instance) |
| 33 | + |
| 34 | +Returns true if `instance` is a plain text component. |
| 35 | + |
| 36 | +#### array findAllInRenderedTree(ReactComponent tree, function test) |
| 37 | + |
| 38 | +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. |
| 39 | + |
| 40 | +#### array scryRenderedDOMComponentsWithClass(ReactCompoennt tree, string className) |
| 41 | + |
| 42 | +Finds all instance of components in the rendered tree that are DOM components with the class name matching `className`. |
| 43 | + |
| 44 | +#### ReactComponent findRenderedDOMComponentWithClass(ReactComponent tree, string className) |
| 45 | + |
| 46 | +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. |
| 47 | + |
| 48 | +#### array scryRenderedDOMComponentsWithTag(ReactComponent tree, string tagName) |
| 49 | + |
| 50 | +Finds all instance of components in the rendered tree that are DOM components with the tag name matching `tagName`. |
| 51 | + |
| 52 | +#### ReactComponent findRenderedDOMComponentWithTag(ReactComponent tree, string tagName) |
| 53 | + |
| 54 | +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. |
| 55 | + |
| 56 | +#### array scryRenderedComponentsWithType(ReactComponent tree, function componentClass) |
| 57 | + |
| 58 | +Finds all instances of components with type equal to `componentClass`. |
| 59 | + |
| 60 | +#### ReactComponent findRenderedComponentWithType(ReactComponent tree, function componentClass) |
| 61 | + |
| 62 | +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. |
| 63 | + |
| 64 | +#### object mockComponent(function componentClass, string? tagName) |
| 65 | + |
| 66 | +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. |
| 67 | + |
| 68 | +#### Simulate.{eventName}({ReactComponent|DOMElement} element, object nativeEventData) |
| 69 | + |
| 70 | +Simulate an event dispatch on a React component instance or browser DOM node with optional `nativeEventData` event data. **This is possibly the single most useful utility in `ReactTestUtils`.** |
| 71 | + |
| 72 | +> Note: |
| 73 | +> |
| 74 | +> This helper is used to simulate browser events, so synthetic React events like `change` are not available. If you want to test `change`, simulate the underlying `input` browser event. |
| 75 | +
|
| 76 | +Example usage: `React.addons.TestUtils.Simulate.click(myComponent)` |
| 77 | + |
| 78 | +`Simulate` has a method for every event that React understands. |
0 commit comments