Skip to content

Commit 87d6b5a

Browse files
author
Chiedo
committed
Testing docs
Adding documentation for testing react-router with jest.
1 parent 47f4a4b commit 87d6b5a

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

docs/guides/testing.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
React Router Testing With Jest
2+
====================
3+
Testing has become much easier since React Router version 1.x. For Testing prior React Router versions, see [Old testing docs](https://github.com/rackt/react-router/blob/57543eb41ce45b994a29792d77c86cc10b51eac9/docs/guides/testing.md)
4+
5+
It is recommended that you read the following two tutorials prior:
6+
- [Jest Getting Started docs](https://facebook.github.io/jest/docs/getting-started.html)
7+
- [Jest ReactJS docs](https://facebook.github.io/jest/docs/tutorial-react.html)
8+
- [ReactJS TestUtils docs](https://facebook.github.io/react/docs/test-utils.html)
9+
10+
Testing with React-Router 1.x should just work. But if you are having issues see the following. Many users had issues when upgrading prior setups from react-router 0.x.
11+
12+
Updating from testing setup from React-Router 0.x to 1.x
13+
----------------------------------------------
14+
Firstly, ensure you are using at least the following versions of each package.
15+
- "react": "^0.14.0"
16+
- "react-dom": "^0.14.0"
17+
- "react-router": "^1.0.0"
18+
- "react-addons-test-utils": "^0.14.0"
19+
- "jest-cli": "^0.5.10"
20+
- "babel-jest": "^5.3.0"
21+
22+
Also, make sure you are using node 4.x
23+
24+
In prior setups, react-tools was needed. This is not longer the case. You will need to remove it from your package.json and environment.
25+
26+
```
27+
"react-tools": "~0.13.3",
28+
```
29+
30+
Lastly, anywhere you have the following, needs to be replaced with this:
31+
32+
```
33+
var React = require('react/addons');
34+
var TestUtils = React.addons.TestUtils;
35+
```
36+
37+
with this:
38+
39+
```
40+
var TestUtils = require('react-addons-test-utils');
41+
var ReactDOM = require('react-dom');
42+
var React = require('react');
43+
```
44+
45+
Make sure you do an npm clean, install, etc. and make sure you add react-addons-test-utils and react-dom to your unmocked paths.
46+
47+
```json
48+
...
49+
"unmockedModulePathPatterns": [
50+
"./node_modules/react",
51+
"./node_modules/react-dom",
52+
"./node_modules/react-addons-test-utils",
53+
],
54+
...
55+
56+
```
57+
58+
Lastly ensure you are using babel-jest for the script preproccessor:
59+
60+
```js
61+
...
62+
"scriptPreprocessor": "./node_modules/babel-jest",
63+
...
64+
```
65+
66+
67+
Example:
68+
----------------------------------------------
69+
A component:
70+
```js
71+
//../components/BasicPage.js
72+
73+
let React = require('react');
74+
75+
import { Button } from 'react-bootstrap';
76+
import { Link } from 'react-router';
77+
78+
let BasicPage =
79+
React.createClass({
80+
propTypes: {
81+
authenticated: React.PropTypes.bool,
82+
},
83+
render:function(){
84+
let mainContent;
85+
let authenticated = this.props.authenticated;
86+
87+
if(authenticated) {
88+
mainContent = (
89+
<div>
90+
<Link to="/admin"><Button bsStyle="primary">Login</Button></Link>
91+
</div>
92+
);
93+
} else {
94+
mainContent = (
95+
<div>
96+
<Link to="/admin"><Button bsStyle="primary">Login</Button></Link>
97+
</div>
98+
);
99+
100+
}
101+
102+
return (
103+
<div>
104+
{mainContent}
105+
</div>
106+
);
107+
},
108+
});
109+
module.exports = BasicPage;
110+
```
111+
112+
The test for that component:
113+
```js
114+
//../components/__tests__/BasicPage-test.js
115+
116+
jest.dontMock('../BasicPage');
117+
118+
describe('BasicPage', function() {
119+
let BasicPage = require('../BasicPage');
120+
let TestUtils = require('react-addons-test-utils');
121+
let ReactDOM = require('react-dom');
122+
let React = require('react');
123+
124+
it('renders the Login button if not logged in', function() {
125+
let page = TestUtils.renderIntoDocument(<BasicPage />);
126+
let button = TestUtils.findRenderedDOMComponentWithTag(page, 'button');
127+
expect(ReactDOM.findDOMNode(button).textContent).toBe('Login');
128+
});
129+
130+
it('renders the Account button if logged in', function() {
131+
let page = TestUtils.renderIntoDocument(<BasicPage authenticated={true} />);
132+
let button = TestUtils.findRenderedDOMComponentWithTag(page, 'button');
133+
expect(ReactDOM.findDOMNode(button).textContent).toBe('Your Account');
134+
});
135+
});
136+
```

0 commit comments

Comments
 (0)