You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
4.2 KiB
141 lines
4.2 KiB
11 years ago
|
---
|
||
|
id: test-utils
|
||
|
title: Test Utilities
|
||
|
permalink: test-utils.html
|
||
|
prev: class-name-manipulation.html
|
||
11 years ago
|
next: clone-with-props.html
|
||
11 years ago
|
---
|
||
|
|
||
11 years ago
|
`React.addons.TestUtils` makes it easy to test React components in the testing framework of your choice (we use [Jest](http://facebook.github.io/jest/)).
|
||
11 years ago
|
|
||
11 years ago
|
### Simulate
|
||
|
|
||
|
```javascript
|
||
11 years ago
|
Simulate.{eventName}(DOMElement element, object eventData)
|
||
11 years ago
|
```
|
||
|
|
||
11 years ago
|
Simulate an event dispatch on a DOM node with optional `eventData` event data. **This is possibly the single most useful utility in `ReactTestUtils`.**
|
||
11 years ago
|
|
||
|
Example usage:
|
||
|
|
||
|
```javascript
|
||
11 years ago
|
var node = this.refs.input.getDOMNode();
|
||
|
React.addons.TestUtils.Simulate.click(node);
|
||
|
React.addons.TestUtils.Simulate.change(node);
|
||
|
React.addons.TestUtils.Simulate.keyDown(node, {key: "Enter"});
|
||
11 years ago
|
```
|
||
|
|
||
|
`Simulate` has a method for every event that React understands.
|
||
|
|
||
|
### renderIntoDocument
|
||
|
|
||
|
```javascript
|
||
|
ReactComponent renderIntoDocument(ReactComponent instance)
|
||
|
```
|
||
11 years ago
|
|
||
11 years ago
|
Render a component into a detached DOM node in the document. **This function requires a DOM.**
|
||
11 years ago
|
|
||
11 years ago
|
### mockComponent
|
||
|
|
||
|
```javascript
|
||
10 years ago
|
object mockComponent(function componentClass, string? mockTagName)
|
||
11 years ago
|
```
|
||
|
|
||
|
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.
|
||
|
|
||
10 years ago
|
### isDescriptorOfType
|
||
11 years ago
|
|
||
|
```javascript
|
||
10 years ago
|
boolean isDescriptorOfType(ReactDescriptor descriptor, function componentClass)
|
||
11 years ago
|
```
|
||
11 years ago
|
|
||
10 years ago
|
Returns true if `descriptor` is an instance of a React `componentClass`.
|
||
11 years ago
|
|
||
11 years ago
|
### isDOMComponent
|
||
|
|
||
|
```javascript
|
||
|
boolean isDOMComponent(ReactComponent instance)
|
||
|
```
|
||
11 years ago
|
|
||
|
Returns true if `instance` is a DOM component (such as a `<div>` or `<span>`).
|
||
|
|
||
11 years ago
|
### isCompositeComponent
|
||
|
|
||
|
```javascript
|
||
|
boolean isCompositeComponent(ReactComponent instance)`
|
||
|
```
|
||
11 years ago
|
|
||
|
Returns true if `instance` is a composite component (created with `React.createClass()`)
|
||
|
|
||
11 years ago
|
### isCompositeComponentWithType
|
||
|
|
||
|
```javascript
|
||
|
boolean isCompositeComponentWithType(ReactComponent instance, function componentClass)
|
||
|
```
|
||
11 years ago
|
|
||
|
The combination of `isComponentOfType()` and `isCompositeComponent()`.
|
||
|
|
||
11 years ago
|
### isTextComponent
|
||
|
|
||
|
```javascript
|
||
|
boolean isTextComponent(ReactComponent instance)
|
||
|
```
|
||
11 years ago
|
|
||
|
Returns true if `instance` is a plain text component.
|
||
|
|
||
11 years ago
|
### findAllInRenderedTree
|
||
11 years ago
|
|
||
11 years ago
|
```javascript
|
||
|
array findAllInRenderedTree(ReactComponent tree, function test)
|
||
|
```
|
||
11 years ago
|
|
||
11 years ago
|
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.
|
||
11 years ago
|
|
||
11 years ago
|
### scryRenderedDOMComponentsWithClass
|
||
11 years ago
|
|
||
11 years ago
|
```javascript
|
||
11 years ago
|
array scryRenderedDOMComponentsWithClass(ReactComponent tree, string className)
|
||
11 years ago
|
```
|
||
11 years ago
|
|
||
10 years ago
|
Finds all instances of components in the rendered tree that are DOM components with the class name matching `className`.
|
||
11 years ago
|
|
||
11 years ago
|
### findRenderedDOMComponentWithClass
|
||
11 years ago
|
|
||
11 years ago
|
```javascript
|
||
|
ReactComponent findRenderedDOMComponentWithClass(ReactComponent tree, string className)
|
||
|
```
|
||
11 years ago
|
|
||
11 years ago
|
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.
|
||
11 years ago
|
|
||
11 years ago
|
### scryRenderedDOMComponentsWithTag
|
||
11 years ago
|
|
||
11 years ago
|
```javascript
|
||
|
array scryRenderedDOMComponentsWithTag(ReactComponent tree, string tagName)
|
||
|
```
|
||
11 years ago
|
|
||
10 years ago
|
Finds all instances of components in the rendered tree that are DOM components with the tag name matching `tagName`.
|
||
11 years ago
|
|
||
11 years ago
|
### findRenderedDOMComponentWithTag
|
||
11 years ago
|
|
||
11 years ago
|
```javascript
|
||
|
ReactComponent findRenderedDOMComponentWithTag(ReactComponent tree, string tagName)
|
||
|
```
|
||
11 years ago
|
|
||
11 years ago
|
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.
|
||
11 years ago
|
|
||
11 years ago
|
### scryRenderedComponentsWithType
|
||
11 years ago
|
|
||
11 years ago
|
```javascript
|
||
|
array scryRenderedComponentsWithType(ReactComponent tree, function componentClass)
|
||
|
```
|
||
11 years ago
|
|
||
11 years ago
|
Finds all instances of components with type equal to `componentClass`.
|
||
11 years ago
|
|
||
11 years ago
|
### findRenderedComponentWithType
|
||
11 years ago
|
|
||
11 years ago
|
```javascript
|
||
|
ReactComponent findRenderedComponentWithType(ReactComponent tree, function componentClass)
|
||
|
```
|
||
11 years ago
|
|
||
11 years ago
|
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.
|