Skip to content

Commit

Permalink
Add withExtraArgument()
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed May 10, 2016
1 parent e0773ff commit 41aefcc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/index.js
@@ -1,9 +1,14 @@
export default function thunkMiddleware({ dispatch, getState }) {
return next => action => {
function createThunkMiddleware(extraArgument) {
return ({ dispatch, getState }) => next => action => {
if (typeof action === 'function') {
return action(dispatch, getState);
return action(dispatch, getState, extraArgument);
}

return next(action);
};
}

const thunk = createThunkMiddleware();
thunk.withExtraArgument = createThunkMiddleware;

export default thunk;
15 changes: 15 additions & 0 deletions test/index.js
Expand Up @@ -76,4 +76,19 @@ describe('thunk middleware', () => {
}
});
});

describe('withExtraArgument', () => {
it('must pass the third argument', done => {
const extraArg = { lol: true };
thunkMiddleware.withExtraArgument(extraArg)({
dispatch: doDispatch,
getState: doGetState,
})()((dispatch, getState, arg) => {
chai.assert.strictEqual(dispatch, doDispatch);
chai.assert.strictEqual(getState, doGetState);
chai.assert.strictEqual(arg, extraArg);
done();
});
});
});
});

4 comments on commit 41aefcc

@QuotableWater7
Copy link

@QuotableWater7 QuotableWater7 commented on 41aefcc Sep 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaearon I've been studying the redux source to learn various functional programming techniques and was curious - may I ask why this particular code change happened (or what it enabled)? Thanks!

@TrySound
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@QuotableWater7 It's just an extension to work with external API and be able to mock that in tests.

@QuotableWater7
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TrySound thanks!

@dezhizhang
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

Please sign in to comment.