Skip to content

Commit 93e7778

Browse files
committed
Merge pull request #1112 from petehunt/testutils-addons
Add ReactTestUtils to addons
2 parents b7836c4 + c2904b9 commit 93e7778

File tree

5 files changed

+89
-4
lines changed

5 files changed

+89
-4
lines changed

docs/_data/nav_docs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
title: Two-Way Binding Helpers
4646
- id: class-name-manipulation
4747
title: Class Name Manipulation
48+
- id: test-utils
49+
title: Test Utilities
4850
- id: examples
4951
title: Examples
5052
- title: Reference

docs/docs/09-addons.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ next: animation.html
99

1010
`React.addons` is where we park some useful utilities for building React apps. **These should be considered experimental** but will eventually be rolled into core or a blessed utilities library:
1111

12-
- `ReactTransitions`, for dealing with animations and transitions that are usually not simple to implement, such as before a component's removal.
13-
- `ReactLink`, to simplify the coordination between user's form input data and and the component's state.
14-
- `classSet`, for manipulating the DOM `class` string a bit more cleanly.
12+
- [`ReactTransitions`](animation.html), for dealing with animations and transitions that are usually not simple to implement, such as before a component's removal.
13+
- [`ReactLink`](two-way-binding-helpers.html), to simplify the coordination between user's form input data and and the component's state.
14+
- [`classSet`](class-name-manipulation.html), for manipulating the DOM `class` string a bit more cleanly.
15+
- [`ReactTestUtils`](test-utils.html), simple helpers for writing test cases (unminified build only).
1516

1617
To get the add-ons, use `react-with-addons.js` (and its minified counterpart) rather than the common `react.js`.

docs/docs/09.3-class-name-manipulation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Class Name Manipulation
44
layout: docs
55
permalink: class-name-manipulation.html
66
prev: two-way-binding-helpers.html
7-
next: examples.html
7+
next: test-utils.html
88
---
99

1010
`classSet()` is a neat utility for easily manipulating the DOM `class` string.

docs/docs/09.4-test-utils.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.

src/browser/ReactWithAddons.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,9 @@ React.addons = {
4343
cloneWithProps: cloneWithProps
4444
};
4545

46+
if (__DEV__) {
47+
React.addons.TestUtils = require('ReactTestUtils');
48+
}
49+
4650
module.exports = React;
4751

0 commit comments

Comments
 (0)