Skip to content

Commit e2e9648

Browse files
Asvaroxtimdorr
authored andcommitted
Have all arguments with which dispatch was called passed through in middleware API (#2560)
1 parent 7d1cabd commit e2e9648

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/applyMiddleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default function applyMiddleware(...middlewares) {
2424

2525
const middlewareAPI = {
2626
getState: store.getState,
27-
dispatch: (action) => dispatch(action)
27+
dispatch: (...args) => dispatch(...args)
2828
}
2929
chain = middlewares.map(middleware => middleware(middlewareAPI))
3030
dispatch = compose(...chain)(store.dispatch)

test/applyMiddleware.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,26 @@ describe('applyMiddleware', () => {
9292
})
9393
})
9494

95+
it('passes through all arguments of dispatch calls from within middleware', () => {
96+
const spy = jest.fn()
97+
const testCallArgs = ['test']
98+
function multiArgMiddleware() {
99+
return next => (action, callArgs) => {
100+
if (Array.isArray(callArgs)) {
101+
return action(...callArgs)
102+
}
103+
return next(action)
104+
}
105+
}
106+
function dummyMiddleware({ dispatch }) {
107+
return next => action => dispatch(action, testCallArgs)
108+
}
109+
110+
const store = createStore(reducers.todos, applyMiddleware(multiArgMiddleware, dummyMiddleware))
111+
store.dispatch(spy)
112+
expect(spy.mock.calls[0]).toEqual(testCallArgs)
113+
})
114+
95115
it('keeps unwrapped dispatch available while middleware is initializing', () => {
96116
// This is documenting the existing behavior in Redux 3.x.
97117
// We plan to forbid this in Redux 4.x.

0 commit comments

Comments
 (0)