Skip to content

Commit b8c3cc4

Browse files
committed
Update WritingTests.md
1 parent e1faa7a commit b8c3cc4

File tree

1 file changed

+58
-50
lines changed

1 file changed

+58
-50
lines changed

docs/recipes/WritingTests.md

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Writing tests
1+
# Writing Tests
22

33
Because most of the Redux code you write are functions, and many of them are pure, they are easy test without mocking.
44

@@ -140,55 +140,6 @@ describe('todos reducer', () => {
140140
});
141141
```
142142
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-
192143
### Components
193144
194145
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:
355306
import ConnectedApp, { App } from './App';
356307
```
357308
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+
358317
>##### A Note on Mixing ES6 Modules and CommonJS
359318
360319
>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`.
361320
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+
362370
### Glossary
363371
364372
- [React Test Utils](http://facebook.github.io/react/docs/test-utils.html): Test utilities that ship with React.

0 commit comments

Comments
 (0)