Skip to content

Commit

Permalink
Have all arguments with which dispatch was called passed through in m…
Browse files Browse the repository at this point in the history
…iddleware API (reduxjs#2560)
  • Loading branch information
Asvarox authored and seantcoyote committed Jan 14, 2018
1 parent 12ab122 commit 4e88836
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/applyMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function applyMiddleware(...middlewares) {

const middlewareAPI = {
getState: store.getState,
dispatch: (action) => dispatch(action)
dispatch: (...args) => dispatch(...args)
}
chain = middlewares.map(middleware => middleware(middlewareAPI))
dispatch = compose(...chain)(store.dispatch)
Expand Down
20 changes: 20 additions & 0 deletions test/applyMiddleware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ describe('applyMiddleware', () => {
})
})

it('passes through all arguments of dispatch calls from within middleware', () => {
const spy = jest.fn()
const testCallArgs = ['test']
function multiArgMiddleware() {
return next => (action, callArgs) => {
if (Array.isArray(callArgs)) {
return action(...callArgs)
}
return next(action)
}
}
function dummyMiddleware({ dispatch }) {
return next => action => dispatch(action, testCallArgs)
}

const store = createStore(reducers.todos, applyMiddleware(multiArgMiddleware, dummyMiddleware))
store.dispatch(spy)
expect(spy.mock.calls[0]).toEqual(testCallArgs)
})

it('keeps unwrapped dispatch available while middleware is initializing', () => {
// This is documenting the existing behavior in Redux 3.x.
// We plan to forbid this in Redux 4.x.
Expand Down

0 comments on commit 4e88836

Please sign in to comment.