|
1 |
| -# Writing tests |
| 1 | +# Writing Tests |
2 | 2 |
|
3 | 3 | Because most of the Redux code you write are functions, and many of them are pure, they are easy test without mocking.
|
4 | 4 |
|
@@ -140,55 +140,6 @@ describe('todos reducer', () => {
|
140 | 140 | });
|
141 | 141 | ```
|
142 | 142 |
|
143 |
| -### Middleware |
144 |
| -
|
145 |
| -Middleware functions wrap behavior of `dispatch` calls in Redux, so to test this modified behavior we need to mock the behavior of the `dispatch` call. |
146 |
| -
|
147 |
| -#### Example |
148 |
| -
|
149 |
| -```js |
150 |
| -import expect from 'expect'; |
151 |
| -import * as types from '../../constants/ActionTypes'; |
152 |
| -import singleDispatch from '../../middleware/singleDispatch'; |
153 |
| - |
154 |
| -const fakeStore = fakeData => ({ |
155 |
| - getState() { |
156 |
| - return fakeData; |
157 |
| - } |
158 |
| -}); |
159 |
| - |
160 |
| -const dispatchWithStoreOf = (storeData, action) => { |
161 |
| - let dispatched = null; |
162 |
| - const dispatch = singleDispatch(fakeStore(storeData))(actionAttempt => dispatched = actionAttempt); |
163 |
| - dispatch(action); |
164 |
| - return dispatched; |
165 |
| -}; |
166 |
| - |
167 |
| -describe('middleware', () => { |
168 |
| - it('should dispatch if store is empty', () => { |
169 |
| - const action = { |
170 |
| - type: types.ADD_TODO |
171 |
| - }; |
172 |
| - |
173 |
| - expect( |
174 |
| - dispatchWithStoreOf({}, action) |
175 |
| - ).toEqual(action); |
176 |
| - }); |
177 |
| - |
178 |
| - it('should not dispatch if store already has type', () => { |
179 |
| - const action = { |
180 |
| - type: types.ADD_TODO |
181 |
| - }; |
182 |
| - |
183 |
| - expect( |
184 |
| - dispatchWithStoreOf({ |
185 |
| - [types.ADD_TODO]: 'dispatched' |
186 |
| - }, action) |
187 |
| - ).toNotExist(); |
188 |
| - }); |
189 |
| -}); |
190 |
| -``` |
191 |
| -
|
192 | 143 | ### Components
|
193 | 144 |
|
194 | 145 | A nice thing about React components is that they are usually small and only rely on their props. That makes them easy to test.
|
@@ -355,10 +306,67 @@ And if you need both:
|
355 | 306 | import ConnectedApp, { App } from './App';
|
356 | 307 | ```
|
357 | 308 |
|
| 309 | +In the app itself, you would still import it normally: |
| 310 | +
|
| 311 | +```js |
| 312 | +import App from './App'; |
| 313 | +``` |
| 314 | +
|
| 315 | +You would only use the named export for tests. |
| 316 | +
|
358 | 317 | >##### A Note on Mixing ES6 Modules and CommonJS
|
359 | 318 |
|
360 | 319 | >If you are using ES6 in your application source, but write your tests in ES5, you should know that Babel handles the interchangeable use of ES6 `import` and CommonJS `require` through its [interop](http://babeljs.io/docs/usage/modules/#interop) capability to run two module formats side-by-side, but the behavior is [slightly different](https://github.com/babel/babel/issues/2047). If you add a second export beside your default export, you can no longer import the default using `require('./App')`. Instead you have to use `require('./App').default`.
|
361 | 320 |
|
| 321 | +### Middleware |
| 322 | +
|
| 323 | +Middleware functions wrap behavior of `dispatch` calls in Redux, so to test this modified behavior we need to mock the behavior of the `dispatch` call. |
| 324 | +
|
| 325 | +#### Example |
| 326 | +
|
| 327 | +```js |
| 328 | +import expect from 'expect'; |
| 329 | +import * as types from '../../constants/ActionTypes'; |
| 330 | +import singleDispatch from '../../middleware/singleDispatch'; |
| 331 | + |
| 332 | +const fakeStore = fakeData => ({ |
| 333 | + getState() { |
| 334 | + return fakeData; |
| 335 | + } |
| 336 | +}); |
| 337 | + |
| 338 | +const dispatchWithStoreOf = (storeData, action) => { |
| 339 | + let dispatched = null; |
| 340 | + const dispatch = singleDispatch(fakeStore(storeData))(actionAttempt => dispatched = actionAttempt); |
| 341 | + dispatch(action); |
| 342 | + return dispatched; |
| 343 | +}; |
| 344 | + |
| 345 | +describe('middleware', () => { |
| 346 | + it('should dispatch if store is empty', () => { |
| 347 | + const action = { |
| 348 | + type: types.ADD_TODO |
| 349 | + }; |
| 350 | + |
| 351 | + expect( |
| 352 | + dispatchWithStoreOf({}, action) |
| 353 | + ).toEqual(action); |
| 354 | + }); |
| 355 | + |
| 356 | + it('should not dispatch if store already has type', () => { |
| 357 | + const action = { |
| 358 | + type: types.ADD_TODO |
| 359 | + }; |
| 360 | + |
| 361 | + expect( |
| 362 | + dispatchWithStoreOf({ |
| 363 | + [types.ADD_TODO]: 'dispatched' |
| 364 | + }, action) |
| 365 | + ).toNotExist(); |
| 366 | + }); |
| 367 | +}); |
| 368 | +``` |
| 369 | +
|
362 | 370 | ### Glossary
|
363 | 371 |
|
364 | 372 | - [React Test Utils](http://facebook.github.io/react/docs/test-utils.html): Test utilities that ship with React.
|
|
0 commit comments