From 2c5e4306a5e147ee561bd8a73d222527d1a75e3c Mon Sep 17 00:00:00 2001 From: Patrick Burtchaell Date: Sun, 26 Jun 2016 18:03:06 -0700 Subject: [PATCH] Return original promise --- package.json | 1 + src/index.js | 58 ++++++++++++++++++++++++++++------------------ test/index.spec.js | 16 +++++++++++++ 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 233d9bb7..94290e61 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.js b/src/index.js index fa5d5aa8..43ef893e 100644 --- a/src/index.js +++ b/src/index.js @@ -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 @@ -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); }; }; } diff --git a/test/index.spec.js b/test/index.spec.js index 6b746866..53dff400 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -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'; @@ -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,