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

redux #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-promise",
"version": "0.5.0",
"version": "0.6.0-alpha",
"description": "FSA-compliant promise middleware for Redux.",
"main": "lib/index.js",
"scripts": {
Expand Down Expand Up @@ -34,6 +34,7 @@
"sinon": "^1.15.4"
},
"dependencies": {
"flux-standard-action": "0.6.0"
"flux-standard-action": "0.6.0",
"lodash": "^3.10.0"
}
}
44 changes: 31 additions & 13 deletions src/__tests__/promiseMiddleware-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,46 @@ describe('promiseMiddleware', () => {
});

it('handles Flux standard actions', async () => {
const meta = { 'do': 'wop' };

await dispatch({
type: 'ACTION_TYPE',
payload: Promise.resolve(foobar)
payload: Promise.resolve(foobar),
meta
});

expect(baseDispatch.calledOnce).to.be.true;
expect(baseDispatch.firstCall.args[0]).to.deep.equal({
type: 'ACTION_TYPE',
payload: foobar
});
expect(baseDispatch.calledTwice).to.be.true;
const action1 = baseDispatch.firstCall.args[0];
expect(action1.type).to.equal('ACTION_TYPE');
expect(action1.payload).to.be.undefined;
expect(action1.meta).to.eql(meta);
expect(action1.sequence.type).to.equal('start');
const action2 = baseDispatch.secondCall.args[0];
expect(action2.type).to.equal('ACTION_TYPE');
expect(action2.payload).to.eql(foobar);
expect(action2.meta).to.eql(meta);
expect(action2.sequence.type).to.equal('next');
expect(action1.sequence.id).to.equal(action2.sequence.id);

await dispatch({
type: 'ACTION_TYPE',
payload: Promise.reject(err)
payload: Promise.reject(err),
meta
}).catch(noop);

expect(baseDispatch.calledTwice).to.be.true;
expect(baseDispatch.secondCall.args[0]).to.deep.equal({
type: 'ACTION_TYPE',
payload: err,
error: true
});
expect(baseDispatch.callCount).to.equal(4);
const action3 = baseDispatch.args[2][0];
expect(action3.type).to.equal('ACTION_TYPE');
expect(action3.payload).to.be.undefined;
expect(action3.meta).to.eql(meta);
expect(action3.sequence.type).to.equal('start');
const action4 = baseDispatch.args[3][0];
expect(action4.type).to.equal('ACTION_TYPE');
expect(action4.payload).to.eql(err);
expect(action4.error).to.be.true;
expect(action4.meta).to.eql(meta);
expect(action4.sequence.type).to.equal('next');
expect(action3.sequence.id).to.equal(action4.sequence.id);
});

it('handles promises', async () => {
Expand Down
41 changes: 35 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isFSA } from 'flux-standard-action';
import uniqueId from 'lodash/utility/uniqueId';

function isPromise(val) {
return val && typeof val.then === 'function';
Expand All @@ -12,11 +13,39 @@ export default function promiseMiddleware({ dispatch }) {
: next(action);
}

return isPromise(action.payload)
? action.payload.then(
result => dispatch({ ...action, payload: result }),
error => dispatch({ ...action, payload: error, error: true })
)
: next(action);
if (isPromise(action.payload)) {
const sequenceId = uniqueId();

dispatch({
...action,
payload: undefined,
sequence: {
type: 'start',
id: sequenceId
}
});

return action.payload.then(
result => dispatch({
...action,
payload: result,
sequence: {
type: 'next',
id: sequenceId
}
}),
error => dispatch({
...action,
payload: error,
error: true,
sequence: {
type: 'next',
id: sequenceId
}
})
);
}

return next(action);
};
}