Skip to content

Commit 02ed21c

Browse files
committed
Remove redux-mock-store implementation so we don't have to maintain it
1 parent 8eb0532 commit 02ed21c

File tree

1 file changed

+3
-43
lines changed

1 file changed

+3
-43
lines changed

docs/recipes/WritingTests.md

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('actions', () => {
6262

6363
### Async Action Creators
6464

65-
For async action creators using [Redux Thunk](https://github.com/gaearon/redux-thunk) or other middleware, it’s best to completely mock the Redux store for tests. You can still use [`applyMiddleware()`](../api/applyMiddleware.md) with a mock store, as shown below (you can find the following code in [redux-mock-store](https://github.com/arnaudbenard/redux-mock-store)). You can also use [nock](https://github.com/pgte/nock) to mock the HTTP requests.
65+
For async action creators using [Redux Thunk](https://github.com/gaearon/redux-thunk) or other middleware, it’s best to completely mock the Redux store for tests. You can apply the middleware to a mock store using [redux-mock-store](https://github.com/arnaudbenard/redux-mock-store). You can also use [nock](https://github.com/pgte/nock) to mock the HTTP requests.
6666

6767
#### Example
6868

@@ -103,54 +103,14 @@ can be tested like:
103103
```js
104104
import expect from 'expect'
105105
import { applyMiddleware } from 'redux'
106+
import configureMockStore from 'redux-mock-store'
106107
import thunk from 'redux-thunk'
107108
import * as actions from '../../actions/counter'
108109
import * as types from '../../constants/ActionTypes'
109110
import nock from 'nock'
110111

111112
const middlewares = [ thunk ]
112-
113-
/**
114-
* Creates a mock of Redux store with middleware.
115-
*/
116-
function mockStore(getState, expectedActions, done) {
117-
if (!Array.isArray(expectedActions)) {
118-
throw new Error('expectedActions should be an array of expected actions.')
119-
}
120-
if (typeof done !== 'undefined' && typeof done !== 'function') {
121-
throw new Error('done should either be undefined or function.')
122-
}
123-
124-
function mockStoreWithoutMiddleware() {
125-
return {
126-
getState() {
127-
return typeof getState === 'function' ?
128-
getState() :
129-
getState
130-
},
131-
132-
dispatch(action) {
133-
const expectedAction = expectedActions.shift()
134-
135-
try {
136-
expect(action).toEqual(expectedAction)
137-
if (done && !expectedActions.length) {
138-
done()
139-
}
140-
return action
141-
} catch (e) {
142-
done(e)
143-
}
144-
}
145-
}
146-
}
147-
148-
const mockStoreWithMiddleware = applyMiddleware(
149-
...middlewares
150-
)(mockStoreWithoutMiddleware)
151-
152-
return mockStoreWithMiddleware()
153-
}
113+
const mockStore = configureMockStore(middlewares)
154114

155115
describe('async actions', () => {
156116
afterEach(() => {

0 commit comments

Comments
 (0)