Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatch sends action through entire middleware chain #250

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/utils/applyMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ export default function applyMiddleware(...middlewares) {

return next => (...args) => {
const store = next(...args);
const methods = {
dispatch: store.dispatch,
getState: store.getState
};
const middleware = composeMiddleware(...finalMiddlewares);

return {
...store,
dispatch: compose(
composeMiddleware(...finalMiddlewares)(methods),
store.dispatch
)
dispatch: function dispatch(action) {
const methods = { dispatch, getState: store.getState };

return compose(
middleware(methods),
store.dispatch
)(action);
}
};
};
}
19 changes: 19 additions & 0 deletions test/applyMiddleware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('applyMiddleware', () => {

const spy = expect.createSpy(() => {});
const store = applyMiddleware(test(spy), thunk)(createStore)(reducers.todos);

store.dispatch(addTodo('Use Redux'));

expect(Object.keys(spy.calls[0].arguments[0])).toEqual([
Expand All @@ -24,6 +25,24 @@ describe('applyMiddleware', () => {
expect(store.getState()).toEqual([ { id: 1, text: 'Use Redux' } ]);
});

it('should pass recursive dispatches through the middleware chain', () => {
function test(spyOnMethods) {
return () => next => action => {
spyOnMethods(action);
return next(action);
};
}

const spy = expect.createSpy(() => {});

const store = applyMiddleware(test(spy), thunk)(createStore)(reducers.todos);

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

});

it('uses thunk middleware by default', done => {
const store = applyMiddleware()(createStore)(reducers.todos);

Expand Down