Skip to content

Commit

Permalink
Merge pull request #559 from idolize/patch-2
Browse files Browse the repository at this point in the history
Add section on unit testing middleware to docs
  • Loading branch information
gaearon committed Aug 26, 2015
2 parents 0b99c45 + b8d831d commit e1faa7a
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions docs/recipes/WritingTests.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,55 @@ describe('todos reducer', () => {
});
```
### Middleware
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.
#### Example
```js
import expect from 'expect';
import * as types from '../../constants/ActionTypes';
import singleDispatch from '../../middleware/singleDispatch';

const fakeStore = fakeData => ({
getState() {
return fakeData;
}
});

const dispatchWithStoreOf = (storeData, action) => {
let dispatched = null;
const dispatch = singleDispatch(fakeStore(storeData))(actionAttempt => dispatched = actionAttempt);
dispatch(action);
return dispatched;
};

describe('middleware', () => {
it('should dispatch if store is empty', () => {
const action = {
type: types.ADD_TODO
};

expect(
dispatchWithStoreOf({}, action)
).toEqual(action);
});

it('should not dispatch if store already has type', () => {
const action = {
type: types.ADD_TODO
};

expect(
dispatchWithStoreOf({
[types.ADD_TODO]: 'dispatched'
}, action)
).toNotExist();
});
});
```
### Components
A nice thing about React components is that they are usually small and only rely on their props. That makes them easy to test.
Expand Down

0 comments on commit e1faa7a

Please sign in to comment.