Skip to content

Commit 8684815

Browse files
committed
Merge pull request #8 from acdlite/create-action-meta
Allow to fill `meta` prop of an action
2 parents 21cac4a + 0b82c83 commit 8684815

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/__tests__/createAction-test.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,52 @@ import isPlainObject from 'lodash.isplainobject';
44
describe('createAction()', () => {
55
describe('resulting action creator', () => {
66
const type = 'TYPE';
7-
const actionCreator = createAction(type, b => b);
8-
const foobar = { foo: 'bar' };
9-
const action = actionCreator(foobar);
107

118
it('returns plain object', () => {
9+
const actionCreator = createAction(type, b => b);
10+
const foobar = { foo: 'bar' };
11+
const action = actionCreator(foobar);
1212
expect(isPlainObject(action)).to.be.true;
1313
});
1414

1515
it('uses return value as payload', () => {
16+
const actionCreator = createAction(type, b => b);
17+
const foobar = { foo: 'bar' };
18+
const action = actionCreator(foobar);
1619
expect(action.payload).to.equal(foobar);
1720
});
1821

1922
it('has no extraneous keys', () => {
23+
const actionCreator = createAction(type, b => b);
24+
const foobar = { foo: 'bar' };
25+
const action = actionCreator(foobar);
2026
expect(action).to.deep.equal({
2127
type,
2228
payload: foobar
2329
});
2430
});
2531

2632
it('uses identity function if actionCreator is not a function', () => {
27-
expect(createAction(type)(foobar)).to.deep.equal({
33+
const actionCreator = createAction(type);
34+
const foobar = { foo: 'bar' };
35+
const action = actionCreator(foobar);
36+
expect(action).to.deep.equal({
2837
type,
2938
payload: foobar
3039
});
3140
});
41+
42+
it('accepts a second parameter for adding meta to object', () => {
43+
const actionCreator = createAction(type, null, ({ cid }) => ({ cid }));
44+
const foobar = { foo: 'bar', cid: 5 };
45+
const action = actionCreator(foobar);
46+
expect(action).to.deep.equal({
47+
type,
48+
payload: foobar,
49+
meta: {
50+
cid: 5
51+
}
52+
});
53+
});
3254
});
3355
});

src/createAction.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ function identity(t) {
22
return t;
33
}
44

5-
export default function createAction(type, actionCreator) {
5+
export default function createAction(type, actionCreator, metaCreator) {
66
const finalActionCreator = typeof actionCreator === 'function'
77
? actionCreator
88
: identity;
99

10-
return (...args) => ({
11-
type,
12-
payload: finalActionCreator(...args)
13-
});
10+
return (...args) => {
11+
const action = {
12+
type,
13+
payload: finalActionCreator(...args)
14+
};
15+
16+
if (typeof metaCreator === 'function') action.meta = metaCreator(...args);
17+
18+
return action;
19+
};
1420
}

0 commit comments

Comments
 (0)