Skip to content

Commit

Permalink
Fix handling of falsy data and meta
Browse files Browse the repository at this point in the history
Falsy data (e.g., `data: 0`) and falsy meta properties aren't preserved
by the middleware, even though FSA allows payload and meta to be
anything, due to the use of `!!data` and `!!meta`.  This commit fixes
this by changing the handling to a more restrictive `!== undefined`.
  • Loading branch information
joshkel authored and Patrick Burtchaell committed Nov 29, 2016
1 parent c2469be commit 555d83b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function promiseMiddleware(config = {}) {
...((newPayload === null || typeof newPayload === 'undefined') ? {} : {
payload: newPayload
}),
...(!!meta ? { meta } : {}),
...(meta !== undefined ? { meta } : {}),
...(isRejected ? {
error: true
} : {})
Expand All @@ -64,7 +64,7 @@ export default function promiseMiddleware(config = {}) {
data = payload.data;
} else {
promise = payload;
data = null;
data = undefined;
}

/**
Expand All @@ -74,8 +74,8 @@ export default function promiseMiddleware(config = {}) {
*/
next({
type: `${type}_${PENDING}`,
...(!!data ? { payload: data } : {}),
...(!!meta ? { meta } : {})
...(data !== undefined ? { payload: data } : {}),
...(meta !== undefined ? { meta } : {})
});

/*
Expand Down
27 changes: 26 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,25 @@ describe('Redux promise middleware:', () => {
});
});

it('pending action optionally contains falsy optimistic update payload', () => {
store.dispatch({
type: promiseAction.type,
payload: {
promise: promiseAction.payload,
data: 0
}
});
expect(lastMiddlewareModfies.spy).to.have.been.calledWith({
...pendingAction,
payload: 0
});
});

/**
* If the promise action is dispatched with a meta property, the meta property
* and value must be included in the pending action.
*/
it('pending action does contains meta property if included', () => {
it('pending action does contain meta property if included', () => {
store.dispatch(Object.assign({}, promiseAction, {
meta: metaData
}));
Expand All @@ -199,6 +213,17 @@ describe('Redux promise middleware:', () => {
);
});

it('pending action does contain falsy meta property if included', () => {
store.dispatch(Object.assign({}, promiseAction, {
meta: 0
}));
expect(lastMiddlewareModfies.spy).to.have.been.calledWith(
Object.assign({}, pendingAction, {
meta: 0
})
);
});

/**
* The middleware should allow global custom action types included
* in the config when the middleware is constructed.
Expand Down

0 comments on commit 555d83b

Please sign in to comment.