From bd97b50761dd8f886bf510f99956b218b679deea Mon Sep 17 00:00:00 2001 From: ironcoconut Date: Tue, 18 Jun 2019 13:42:56 -0500 Subject: [PATCH] fix: allow applyMiddleware to be called more than once closes #3451 --- src/applyMiddleware.js | 8 +++----- test/applyMiddleware.spec.js | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/applyMiddleware.js b/src/applyMiddleware.js index 3bfc647b45..e89af356cf 100644 --- a/src/applyMiddleware.js +++ b/src/applyMiddleware.js @@ -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 } } diff --git a/test/applyMiddleware.spec.js b/test/applyMiddleware.spec.js index 2efdc9f83f..9e02af5001 100644 --- a/test/applyMiddleware.spec.js +++ b/test/applyMiddleware.spec.js @@ -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' @@ -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)