Skip to content

Commit 0b82c83

Browse files
committed
No meta by default
1 parent e788a34 commit 0b82c83

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

src/__tests__/createAction-test.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,51 @@ import isPlainObject from 'lodash.isplainobject';
44
describe('createAction()', () => {
55
describe('resulting action creator', () => {
66
const type = 'TYPE';
7-
const actionCreator = createAction(type, b => b, ({ cid }) => ({cid}));
8-
const foobar = { foo: 'bar', cid: 5 };
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,
22-
payload: foobar,
23-
meta: {
24-
cid: 5
25-
}
28+
payload: foobar
2629
});
2730
});
2831

29-
it('uses identity function if actionCreator and/or metaCreator is not a function', () => {
30-
expect(createAction(type)(foobar)).to.deep.equal({
32+
it('uses identity function if actionCreator is not a function', () => {
33+
const actionCreator = createAction(type);
34+
const foobar = { foo: 'bar' };
35+
const action = actionCreator(foobar);
36+
expect(action).to.deep.equal({
37+
type,
38+
payload: foobar
39+
});
40+
});
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({
3147
type,
3248
payload: foobar,
33-
meta: foobar
49+
meta: {
50+
cid: 5
51+
}
3452
});
3553
});
3654
});

src/createAction.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ export default function createAction(type, actionCreator, metaCreator) {
77
? actionCreator
88
: identity;
99

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

14-
return (...args) => ({
15-
type,
16-
payload: finalActionCreator(...args),
17-
meta: finalMetaCreator(...args)
18-
});
18+
return action;
19+
};
1920
}

0 commit comments

Comments
 (0)