Skip to content

Commit

Permalink
Return original promise
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Burtchaell authored Jun 27, 2016
1 parent a917fec commit 2c5e430
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"bluebird": "^3.4.0",
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"eslint": "^2.5.1",
Expand Down
58 changes: 35 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,40 @@ export default function promiseMiddleware(config = {}) {
...!!meta ? { meta } : {}
});

/*
* @function handleReject
* @description Dispatch the rejected action and return
* an error object. The error object should contain the
* reason and the dispatched action.
* @params reason The reason the promise was rejected
* @returns {object}
*/
const handleReject = (reason = null) => {
const rejectedAction = getAction(reason, true);
dispatch(rejectedAction);

const error = new Error();
error.reason = reason;
error.action = rejectedAction;

return error;
};

/*
* @function handleFulfill
* @description Dispatch the fulfilled action and
* return the success object. The success object should
* contain the value and the dispatched action.
* @param value The value the promise was resloved with
* @returns {object}
*/
const handleFulfill = (value = null) => {
const resolvedAction = getAction(value, false);
dispatch(resolvedAction);

return { value, action: resolvedAction };
};

/**
* Second, dispatch a rejected or fulfilled action. This flux standard
* action object will describe the resolved state of the promise. In
Expand Down Expand Up @@ -107,29 +141,7 @@ export default function promiseMiddleware(config = {}) {
* }
* }
*/
return new Promise((resolve, reject) => {
promise.then(
(value = null) => {
const resolvedAction = getAction(value, false);
dispatch(resolvedAction);
resolve({ value, action: resolvedAction });

return;
},
(reason = null) => {
const rejectedAction = getAction(reason, true);
dispatch(rejectedAction);

const error = new Error();
error.reason = reason;
error.action = rejectedAction;

reject(error);

return;
}
);
});
return promise.then(handleFulfill, handleReject);
};
};
}
16 changes: 16 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import chai, { expect } from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import Bluebird from 'bluebird';
import { createStore, applyMiddleware } from 'redux';
import configureStore from 'redux-mock-store';
import promiseMiddleware from '../src/index';
Expand Down Expand Up @@ -217,6 +218,21 @@ describe('Redux Promise Middleware:', () => {
fulfilledAction = defaultFulfilledAction;
});

it('propagates the original promise', async () => {
const actionDispatched = store.dispatch({
type: defaultPromiseAction.type,
payload: Bluebird.resolve(promiseValue)
});

// Expect that the promise returned has bluebird functions available
expect(actionDispatched.any).to.be.a('function');

await actionDispatched.then(({ value, action }) => {
expect(value).to.eql(promiseValue);
expect(action).to.eql(fulfilledAction);
});
});

context('When resolve reason is null:', () => {
const nullResolveAction = {
type: defaultPromiseAction.type,
Expand Down

0 comments on commit 2c5e430

Please sign in to comment.