Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ const actionCreators = createActions({
amount => ({ amount }),
amount => ({ key: 'value', amount })
],
DECREMENT: amount => ({ amount: -amount })
DECREMENT: amount => ({ amount: -amount }),
SET: undefined // given undefined, the identity function will be used
},
NOTIFY: [
(username, message) => ({ message: `${username}: ${message}` }),
Expand All @@ -166,6 +167,10 @@ expect(actionCreators.app.counter.decrement(1)).to.deep.equal({
type: 'APP/COUNTER/DECREMENT',
payload: { amount: -1 }
});
expect(actionCreators.app.counter.set(100)).to.deep.equal({
type: 'APP/COUNTER/SET',
payload: 100
});
expect(actionCreators.app.notify('yangmillstheory', 'Hello World')).to.deep.equal({
type: 'APP/NOTIFY',
payload: { message: 'yangmillstheory: Hello World' },
Expand Down
34 changes: 10 additions & 24 deletions src/__tests__/createActions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('createActions', () => {
})
).to.throw(
Error,
'Expected function, undefined, or array with payload and meta functions for ACTION_2'
'Expected function, undefined, null, or array with payload and meta functions for ACTION_2'
);
});

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

expect(
Expand All @@ -48,26 +48,7 @@ describe('createActions', () => {
})
).to.throw(
Error,
'Expected function, undefined, or array with payload and meta functions for ACTION_2'
);
});

it('should throw an error if the reducer value is undefined in object form', () => {
expect(
() => createActions({ ACTION_1: undefined }, 'ACTION_2')
).to.throw(
Error,
'Expected function, undefined, or array with payload and meta functions for ACTION_1'
);

expect(
() => createActions({
ACTION_1: () => {},
ACTION_2: undefined
})
).to.throw(
Error,
'Expected function, undefined, or array with payload and meta functions for ACTION_2'
'Expected function, undefined, null, or array with payload and meta functions for ACTION_2'
);
});

Expand All @@ -78,7 +59,7 @@ describe('createActions', () => {
})
).to.throw(
Error,
'Expected function, undefined, or array with payload and meta functions for ACTION_1'
'Expected function, undefined, null, or array with payload and meta functions for ACTION_1'
);
});

Expand Down Expand Up @@ -211,7 +192,8 @@ describe('createActions', () => {
APP: {
COUNTER: {
INCREMENT: amount => ({ amount }),
DECREMENT: amount => ({ amount: -amount })
DECREMENT: amount => ({ amount: -amount }),
SET: undefined
},
NOTIFY: (username, message) => ({ message: `${username}: ${message}` })
},
Expand All @@ -226,6 +208,10 @@ describe('createActions', () => {
type: 'APP/COUNTER/DECREMENT',
payload: { amount: -1 }
});
expect(actionCreators.app.counter.set(100)).to.deep.equal({
type: 'APP/COUNTER/SET',
payload: 100
});
expect(actionCreators.app.notify('yangmillstheory', 'Hello World')).to.deep.equal({
type: 'APP/NOTIFY',
payload: { message: 'yangmillstheory: Hello World' }
Expand Down
5 changes: 3 additions & 2 deletions src/createActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import last from 'lodash/last';
import isString from 'lodash/isString';
import defaults from 'lodash/defaults';
import isFunction from 'lodash/isFunction';
import isNil from 'lodash/isNil';
import createAction from './createAction';
import invariant from 'invariant';
import arrayToObject from './arrayToObject';
Expand Down Expand Up @@ -45,7 +46,7 @@ function actionCreatorsFromActionMap(actionMap, namespace) {

function actionMapToActionCreators(actionMap) {
function isValidActionMapValue(actionMapValue) {
if (isFunction(actionMapValue)) {
if (isFunction(actionMapValue) || isNil(actionMapValue)) {
return true;
} else if (isArray(actionMapValue)) {
const [payload = identity, meta] = actionMapValue;
Expand All @@ -58,7 +59,7 @@ function actionMapToActionCreators(actionMap) {
const actionMapValue = actionMap[type];
invariant(
isValidActionMapValue(actionMapValue),
'Expected function, undefined, or array with payload and meta ' +
'Expected function, undefined, null, or array with payload and meta ' +
`functions for ${type}`
);
const actionCreator = isArray(actionMapValue)
Expand Down