From 8b0aac529303be9388c528b20aa19423c0ad06f6 Mon Sep 17 00:00:00 2001 From: Denis Semenenko Date: Mon, 15 Apr 2019 10:47:26 -0400 Subject: [PATCH] Added support for synchronous actions with transform prop but no promise --- __tests__/middleware.test.js | 25 ++++++++++++++++++++++++- src/middleware.js | 5 ++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/__tests__/middleware.test.js b/__tests__/middleware.test.js index 9d02647..b857ad3 100644 --- a/__tests__/middleware.test.js +++ b/__tests__/middleware.test.js @@ -23,7 +23,7 @@ afterEach(() => { store.getState.mockClear(); }); -it('ignores error actions or actions without promise prop', () => { +it('ignores error actions or actions without promise and transform props', () => { const action1 = { type: 'SOME_ACTION_TYPE', payload: 'some payload' }; const action2 = { type: 'SOME_ACTION_TYPE', @@ -224,3 +224,26 @@ it('supports transform prop', () => { expect(next.mock.calls[1][0]).toHaveProperty('type', types.SUCCESS); expect(next.mock.calls[1][0]).toHaveProperty('meta.useDifferentRoute', true); }); + +it('handles actions with transform prop but without promise', () => { + const action = { + type: 'TAKE_ITEMS', + payload: ['Backpack'], + transform: (action, state) => (state.isSunny + ? { ...action, payload: action.payload.concat('Sunglasses') } + : action + ), + } + + store.getState.mockReturnValueOnce({ isSunny: true }); + middleware(action); + expect(next).toHaveBeenCalledTimes(1); + expect(resolve).not.toHaveBeenCalled(); + expect(next.mock.calls[0][0]).toHaveProperty('payload.1', 'Sunglasses'); + + store.getState.mockReturnValueOnce({ isSunny: false }); + middleware(action); + expect(next).toHaveBeenCalledTimes(2); + expect(resolve).not.toHaveBeenCalled(); + expect(next.mock.calls[1][0]).not.toHaveProperty('payload.1'); +}); diff --git a/src/middleware.js b/src/middleware.js index 1735c88..7fec96d 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -4,7 +4,10 @@ import createPromiseResolver from './promiseResolver'; const HOP = Object.prototype.hasOwnProperty; const createMiddleware = (store, next, resolve) => (action) => { - if (!HOP.call(action, 'promise')) return next(action); + if (!HOP.call(action, 'promise')) { + if (!HOP.call(action, 'transform')) return next(action); + return next(action.transform(action, store.getState())); + } const { // FSA props