From 2af37fff8959e376ad15b6f12ae2d832db10a50f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Kati=C4=87?= Date: Fri, 29 Jul 2016 18:18:40 +0200 Subject: [PATCH 1/3] Avoid infinite dispatching if error is a promise that rejects with itself. Fixes #33. Doing this with quick editing, without testing. Sorry, don't have much time now. --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 2ecf558..9e35a6b 100644 --- a/src/index.js +++ b/src/index.js @@ -12,7 +12,7 @@ export default function promiseMiddleware({ dispatch }) { : next(action); } - return isPromise(action.payload) + return isPromise(action.payload) && !action.error ? action.payload.then( result => dispatch({ ...action, payload: result }), error => { From a67052e4e6e1e7d36ea18ce0a2c54943096e676a Mon Sep 17 00:00:00 2001 From: Robert Katic Date: Sun, 31 Jul 2016 17:50:45 +0200 Subject: [PATCH 2/3] Add tests for #33. --- src/__tests__/promiseMiddleware-test.js | 40 +++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/__tests__/promiseMiddleware-test.js b/src/__tests__/promiseMiddleware-test.js index c8a723f..f6105a5 100644 --- a/src/__tests__/promiseMiddleware-test.js +++ b/src/__tests__/promiseMiddleware-test.js @@ -85,4 +85,44 @@ describe('promiseMiddleware', () => { 'here you go' ]); }); + + it('handles promises that reject with itself', async () => { + // Thenable that synchronously rejects with itself (like jQiery.ajax in jQuery 2) + const selfRejectingSync = { + then(_, eb) { + return Promise.resolve(eb(selfRejectingSync)); + } + }; + + await expect(dispatch({ + type: 'ACTION_TYPE', + payload: selfRejectingSync + })).to.eventually.be.rejectedWith(selfRejectingSync); + + expect(baseDispatch.calledOnce).to.be.true; + expect(baseDispatch.firstCall.args[0]).to.deep.equal({ + type: 'ACTION_TYPE', + payload: selfRejectingSync, + error: true + }); + + // Promise that rejects with itself (like jQiery.ajax in jQuery 3) + const selfRejectingAsync = new Promise((_, reject) => { + setTimeout(() => { + reject(selfRejectingAsync); + }, 1); + }); + + await expect(dispatch({ + type: 'ACTION_TYPE', + payload: selfRejectingAsync + })).to.eventually.be.rejectedWith(selfRejectingAsync); + + expect(baseDispatch.calledTwice).to.be.true; + expect(baseDispatch.secondCall.args[0]).to.deep.equal({ + type: 'ACTION_TYPE', + payload: selfRejectingAsync, + error: true + }); + }); }); From 6439bce5d8c1c46391bf0abca60f23945e9b8dc0 Mon Sep 17 00:00:00 2001 From: Robert Katic Date: Sun, 31 Jul 2016 17:58:53 +0200 Subject: [PATCH 3/3] Fix comment typos. --- src/__tests__/promiseMiddleware-test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/__tests__/promiseMiddleware-test.js b/src/__tests__/promiseMiddleware-test.js index f6105a5..b8f6289 100644 --- a/src/__tests__/promiseMiddleware-test.js +++ b/src/__tests__/promiseMiddleware-test.js @@ -87,7 +87,7 @@ describe('promiseMiddleware', () => { }); it('handles promises that reject with itself', async () => { - // Thenable that synchronously rejects with itself (like jQiery.ajax in jQuery 2) + // Thenable that synchronously rejects with itself (like jqXHR in jQuery 2) const selfRejectingSync = { then(_, eb) { return Promise.resolve(eb(selfRejectingSync)); @@ -106,7 +106,7 @@ describe('promiseMiddleware', () => { error: true }); - // Promise that rejects with itself (like jQiery.ajax in jQuery 3) + // Promise that rejects with itself (like jqXHR in jQuery 3) const selfRejectingAsync = new Promise((_, reject) => { setTimeout(() => { reject(selfRejectingAsync);