Skip to content

Commit 588692d

Browse files
John Mortlocktimche
authored andcommitted
Make payload optional (as per FSA spec) (#48)
* Make payload optional (as per FSA spec) * When payload is null also ignore it * merge in master branch * Explictly check for null or undefined, was picking up some false positives with old check
1 parent 626bf59 commit 588692d

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/__tests__/createAction-test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,41 @@ describe('createAction()', () => {
8080
meta: { foo: 'bar' }
8181
});
8282
});
83+
84+
it('sets payload only when defined', () => {
85+
const action = createAction(type)();
86+
expect(action).to.deep.equal({
87+
type
88+
});
89+
90+
const explictUndefinedAction = createAction(type)(undefined);
91+
expect(explictUndefinedAction).to.deep.equal({
92+
type
93+
});
94+
95+
const explictNullAction = createAction(type)(null);
96+
expect(explictNullAction).to.deep.equal({
97+
type
98+
});
99+
100+
const baz = '1';
101+
const actionCreator = createAction(type, null, () => ({ bar: baz }));
102+
expect(actionCreator()).to.deep.equal({
103+
type,
104+
meta: {
105+
bar: '1'
106+
}
107+
});
108+
109+
const validPayload = [false, 0, ''];
110+
for (let i = 0; i < validPayload.length; i++) {
111+
const validValue = validPayload[i];
112+
const expectPayload = createAction(type)(validValue);
113+
expect(expectPayload).to.deep.equal({
114+
type,
115+
payload: validValue
116+
});
117+
}
118+
});
83119
});
84120
});

src/createAction.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ export default function createAction(type, actionCreator, metaCreator) {
99

1010
return (...args) => {
1111
const action = {
12-
type,
13-
payload: finalActionCreator(...args)
12+
type
1413
};
1514

15+
const payload = finalActionCreator(...args);
16+
if (!(payload === null || payload === undefined)) {
17+
action.payload = payload;
18+
}
19+
1620
if (action.payload instanceof Error) {
1721
// Handle FSA errors where the payload is an Error object. Set error.
1822
action.error = true;

0 commit comments

Comments
 (0)