Skip to content

Commit bd97b50

Browse files
committed
fix: allow applyMiddleware to be called more than once
closes #3451
1 parent cf74935 commit bd97b50

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/applyMiddleware.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ export default function applyMiddleware(...middlewares) {
3131
dispatch: (...args) => dispatch(...args)
3232
}
3333
const chain = middlewares.map(middleware => middleware(middlewareAPI))
34-
dispatch = compose(...chain)(store.dispatch)
34+
store.dispatch = compose(...chain)(store.dispatch)
35+
dispatch = (...args) => store.dispatch(...args)
3536

36-
return {
37-
...store,
38-
dispatch
39-
}
37+
return store
4038
}
4139
}

test/applyMiddleware.spec.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createStore, applyMiddleware } from '../'
1+
import { createStore, applyMiddleware, compose } from '../'
22
import * as reducers from './helpers/reducers'
33
import { addTodo, addTodoAsync, addTodoIfEmpty } from './helpers/actionCreators'
44
import { thunk } from './helpers/middleware'
@@ -56,6 +56,26 @@ describe('applyMiddleware', () => {
5656
})
5757
})
5858

59+
it('can be applied more than once as additional enhancers', () => {
60+
function test(spyOnMethods) {
61+
return () => next => action => {
62+
spyOnMethods(action)
63+
return next(action)
64+
}
65+
}
66+
67+
const spy = jest.fn()
68+
const enhancer = compose(
69+
applyMiddleware(test(spy)),
70+
applyMiddleware(thunk)
71+
)
72+
const store = enhancer(createStore)(reducers.todos)
73+
74+
return store.dispatch(addTodoAsync('Use Redux')).then(() => {
75+
expect(spy.mock.calls.length).toEqual(2)
76+
})
77+
})
78+
5979
it('works with thunk middleware', done => {
6080
const store = applyMiddleware(thunk)(createStore)(reducers.todos)
6181

0 commit comments

Comments
 (0)