Skip to content

Commit

Permalink
Added support for synchronous actions with transform prop but no promise
Browse files Browse the repository at this point in the history
  • Loading branch information
dolsem committed Apr 15, 2019
1 parent 8a303a0 commit 8b0aac5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
25 changes: 24 additions & 1 deletion __tests__/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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');
});
5 changes: 4 additions & 1 deletion src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8b0aac5

Please sign in to comment.