diff --git a/src/__tests__/promiseMiddleware-test.js b/src/__tests__/promiseMiddleware-test.js index 8226475..c6f4022 100644 --- a/src/__tests__/promiseMiddleware-test.js +++ b/src/__tests__/promiseMiddleware-test.js @@ -82,31 +82,83 @@ describe('promiseMiddleware', () => { type: resolve('ACTION_TYPE_RESOLVE'), payload: foobar, meta: { - foo2: 'bar2' + payload: { + foo2: 'bar2' + } } }); }); it('dispatches reject action with arguments', async () => { + try { + await dispatch({ + type: 'ACTION_TYPE_REJECT', + payload: { + promise: Promise.reject(err), + foo3: 'bar3', + foo4: 'bar4' + } + }); + } catch (e) { + // We're not interested in the rejection. We just need to wait until all + // dispatching is done. + true; + } + + expect(baseDispatch.calledTwice).to.be.true; + + expect(baseDispatch.secondCall.args[0]).to.deep.equal({ + type: reject('ACTION_TYPE_REJECT'), + payload: err, + meta: { + payload: { + foo3: 'bar3', + foo4: 'bar4' + } + } + }); + }); + + it('does not overwrite any meta arguments', async () => { await dispatch({ - type: 'ACTION_TYPE_REJECT', + type: 'ACTION_TYPE_RESOLVE', payload: { - promise: Promise.reject(err), - foo3: 'bar3', - foo4: 'bar4' + promise: Promise.resolve(foobar), + foo2: 'bar2' + }, + meta: { + foo3: 'bar3' } }); expect(baseDispatch.calledTwice).to.be.true; expect(baseDispatch.secondCall.args[0]).to.deep.equal({ - type: reject('ACTION_TYPE_REJECT'), - payload: err, + type: resolve('ACTION_TYPE_RESOLVE'), + payload: foobar, meta: { foo3: 'bar3', - foo4: 'bar4' + payload: { + foo2: 'bar2' + } + } + }); + }); + + it('does not include empty meta payload attribute', async () => { + await dispatch({ + type: 'ACTION_TYPE_RESOLVE', + payload: { + promise: Promise.resolve(foobar) } }); + + expect(baseDispatch.calledTwice).to.be.true; + + expect(baseDispatch.secondCall.args[0]).to.deep.equal({ + type: resolve('ACTION_TYPE_RESOLVE'), + payload: foobar + }); }); it('returns the original promise from dispatch', () => { @@ -133,7 +185,7 @@ describe('promiseMiddleware', () => { foo2: 'bar2' } }); - expect(dispatchedResult).to.eventually.equal(foobar); + return expect(dispatchedResult).to.eventually.equal(foobar); }); it('reject the original promise from dispatch', () => { @@ -146,7 +198,7 @@ describe('promiseMiddleware', () => { foo2: 'bar2' } }); - expect(dispatchedResult).to.eventually.be.rejectedWith(err); + return expect(dispatchedResult).to.eventually.be.rejectedWith(err); }); it('returns the reject and resolve strings with default values', () => { diff --git a/src/index.js b/src/index.js index 571d332..772b6ac 100644 --- a/src/index.js +++ b/src/index.js @@ -43,13 +43,33 @@ export default function promiseMiddleware(resolvedName, rejectedName) { dispatch(newAction); + // Create a base for the next action containing the metadata. + let nextActionBase = { + meta: { + ...action.meta, + payload: { + ...newAction.payload + } + } + }; + + if (Object.keys(nextActionBase.meta.payload).length === 0) { + // No arguments were given beside the promise, no need to include them + // in the meta. + delete nextActionBase.meta.payload; + } + if (Object.keys(nextActionBase.meta).length === 0) { + // No meta was included either, remove all meta. + delete nextActionBase.meta; + } + // (2) Listen to promise and dispatch payload with new actionName return action.payload.promise.then( (result) => { dispatch({ type: resolve(action.type, resolvedName), payload: result, - meta: newAction.payload + ...nextActionBase }); return result; }, @@ -57,9 +77,9 @@ export default function promiseMiddleware(resolvedName, rejectedName) { dispatch({ type: reject(action.type, rejectedName), payload: error, - meta: newAction.payload + ...nextActionBase }); - return error; + throw error; } ); };