Skip to content

Commit

Permalink
fix: allow applyMiddleware to be called more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
ironcoconut committed Jun 18, 2019
1 parent cf74935 commit bd97b50
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
8 changes: 3 additions & 5 deletions src/applyMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ export default function applyMiddleware(...middlewares) {
dispatch: (...args) => dispatch(...args)
}
const chain = middlewares.map(middleware => middleware(middlewareAPI))
dispatch = compose(...chain)(store.dispatch)
store.dispatch = compose(...chain)(store.dispatch)
dispatch = (...args) => store.dispatch(...args)

return {
...store,
dispatch
}
return store
}
}
22 changes: 21 additions & 1 deletion test/applyMiddleware.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createStore, applyMiddleware } from '../'
import { createStore, applyMiddleware, compose } from '../'
import * as reducers from './helpers/reducers'
import { addTodo, addTodoAsync, addTodoIfEmpty } from './helpers/actionCreators'
import { thunk } from './helpers/middleware'
Expand Down Expand Up @@ -56,6 +56,26 @@ describe('applyMiddleware', () => {
})
})

it('can be applied more than once as additional enhancers', () => {
function test(spyOnMethods) {
return () => next => action => {
spyOnMethods(action)
return next(action)
}
}

const spy = jest.fn()
const enhancer = compose(
applyMiddleware(test(spy)),
applyMiddleware(thunk)
)
const store = enhancer(createStore)(reducers.todos)

return store.dispatch(addTodoAsync('Use Redux')).then(() => {
expect(spy.mock.calls.length).toEqual(2)
})
})

it('works with thunk middleware', done => {
const store = applyMiddleware(thunk)(createStore)(reducers.todos)

Expand Down

0 comments on commit bd97b50

Please sign in to comment.