Skip to content

Commit b9101a3

Browse files
JasonKidyangmillstheory
authored andcommitted
Support that when reducer in actionMap is undefined or null, set it to identity function (#221)
Close #220
1 parent 06aeb21 commit b9101a3

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ const actionCreators = createActions({
148148
amount => ({ amount }),
149149
amount => ({ key: 'value', amount })
150150
],
151-
DECREMENT: amount => ({ amount: -amount })
151+
DECREMENT: amount => ({ amount: -amount }),
152+
SET: undefined // given undefined, the identity function will be used
152153
},
153154
NOTIFY: [
154155
(username, message) => ({ message: `${username}: ${message}` }),
@@ -166,6 +167,10 @@ expect(actionCreators.app.counter.decrement(1)).to.deep.equal({
166167
type: 'APP/COUNTER/DECREMENT',
167168
payload: { amount: -1 }
168169
});
170+
expect(actionCreators.app.counter.set(100)).to.deep.equal({
171+
type: 'APP/COUNTER/SET',
172+
payload: 100
173+
});
169174
expect(actionCreators.app.notify('yangmillstheory', 'Hello World')).to.deep.equal({
170175
type: 'APP/NOTIFY',
171176
payload: { message: 'yangmillstheory: Hello World' },

src/__tests__/createActions-test.js

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('createActions', () => {
1818
})
1919
).to.throw(
2020
Error,
21-
'Expected function, undefined, or array with payload and meta functions for ACTION_2'
21+
'Expected function, undefined, null, or array with payload and meta functions for ACTION_2'
2222
);
2323
});
2424

@@ -32,7 +32,7 @@ describe('createActions', () => {
3232
})
3333
).to.throw(
3434
Error,
35-
'Expected function, undefined, or array with payload and meta functions for ACTION_1'
35+
'Expected function, undefined, null, or array with payload and meta functions for ACTION_1'
3636
);
3737

3838
expect(
@@ -48,26 +48,7 @@ describe('createActions', () => {
4848
})
4949
).to.throw(
5050
Error,
51-
'Expected function, undefined, or array with payload and meta functions for ACTION_2'
52-
);
53-
});
54-
55-
it('should throw an error if the reducer value is undefined in object form', () => {
56-
expect(
57-
() => createActions({ ACTION_1: undefined }, 'ACTION_2')
58-
).to.throw(
59-
Error,
60-
'Expected function, undefined, or array with payload and meta functions for ACTION_1'
61-
);
62-
63-
expect(
64-
() => createActions({
65-
ACTION_1: () => {},
66-
ACTION_2: undefined
67-
})
68-
).to.throw(
69-
Error,
70-
'Expected function, undefined, or array with payload and meta functions for ACTION_2'
51+
'Expected function, undefined, null, or array with payload and meta functions for ACTION_2'
7152
);
7253
});
7354

@@ -78,7 +59,7 @@ describe('createActions', () => {
7859
})
7960
).to.throw(
8061
Error,
81-
'Expected function, undefined, or array with payload and meta functions for ACTION_1'
62+
'Expected function, undefined, null, or array with payload and meta functions for ACTION_1'
8263
);
8364
});
8465

@@ -211,7 +192,8 @@ describe('createActions', () => {
211192
APP: {
212193
COUNTER: {
213194
INCREMENT: amount => ({ amount }),
214-
DECREMENT: amount => ({ amount: -amount })
195+
DECREMENT: amount => ({ amount: -amount }),
196+
SET: undefined
215197
},
216198
NOTIFY: (username, message) => ({ message: `${username}: ${message}` })
217199
},
@@ -226,6 +208,10 @@ describe('createActions', () => {
226208
type: 'APP/COUNTER/DECREMENT',
227209
payload: { amount: -1 }
228210
});
211+
expect(actionCreators.app.counter.set(100)).to.deep.equal({
212+
type: 'APP/COUNTER/SET',
213+
payload: 100
214+
});
229215
expect(actionCreators.app.notify('yangmillstheory', 'Hello World')).to.deep.equal({
230216
type: 'APP/NOTIFY',
231217
payload: { message: 'yangmillstheory: Hello World' }

src/createActions.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import last from 'lodash/last';
66
import isString from 'lodash/isString';
77
import defaults from 'lodash/defaults';
88
import isFunction from 'lodash/isFunction';
9+
import isNil from 'lodash/isNil';
910
import createAction from './createAction';
1011
import invariant from 'invariant';
1112
import arrayToObject from './arrayToObject';
@@ -45,7 +46,7 @@ function actionCreatorsFromActionMap(actionMap, namespace) {
4546

4647
function actionMapToActionCreators(actionMap) {
4748
function isValidActionMapValue(actionMapValue) {
48-
if (isFunction(actionMapValue)) {
49+
if (isFunction(actionMapValue) || isNil(actionMapValue)) {
4950
return true;
5051
} else if (isArray(actionMapValue)) {
5152
const [payload = identity, meta] = actionMapValue;
@@ -58,7 +59,7 @@ function actionMapToActionCreators(actionMap) {
5859
const actionMapValue = actionMap[type];
5960
invariant(
6061
isValidActionMapValue(actionMapValue),
61-
'Expected function, undefined, or array with payload and meta ' +
62+
'Expected function, undefined, null, or array with payload and meta ' +
6263
`functions for ${type}`
6364
);
6465
const actionCreator = isArray(actionMapValue)

0 commit comments

Comments
 (0)