Skip to content

Commit

Permalink
No meta by default
Browse files Browse the repository at this point in the history
  • Loading branch information
acdlite committed Jul 13, 2015
1 parent e788a34 commit 0b82c83
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
38 changes: 28 additions & 10 deletions src/__tests__/createAction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,51 @@ import isPlainObject from 'lodash.isplainobject';
describe('createAction()', () => {
describe('resulting action creator', () => {
const type = 'TYPE';
const actionCreator = createAction(type, b => b, ({ cid }) => ({cid}));
const foobar = { foo: 'bar', cid: 5 };
const action = actionCreator(foobar);

it('returns plain object', () => {
const actionCreator = createAction(type, b => b);
const foobar = { foo: 'bar' };
const action = actionCreator(foobar);
expect(isPlainObject(action)).to.be.true;
});

it('uses return value as payload', () => {
const actionCreator = createAction(type, b => b);
const foobar = { foo: 'bar' };
const action = actionCreator(foobar);
expect(action.payload).to.equal(foobar);
});

it('has no extraneous keys', () => {
const actionCreator = createAction(type, b => b);
const foobar = { foo: 'bar' };
const action = actionCreator(foobar);
expect(action).to.deep.equal({
type,
payload: foobar,
meta: {
cid: 5
}
payload: foobar
});
});

it('uses identity function if actionCreator and/or metaCreator is not a function', () => {
expect(createAction(type)(foobar)).to.deep.equal({
it('uses identity function if actionCreator is not a function', () => {
const actionCreator = createAction(type);
const foobar = { foo: 'bar' };
const action = actionCreator(foobar);
expect(action).to.deep.equal({
type,
payload: foobar
});
});

it('accepts a second parameter for adding meta to object', () => {
const actionCreator = createAction(type, null, ({ cid }) => ({ cid }));
const foobar = { foo: 'bar', cid: 5 };
const action = actionCreator(foobar);
expect(action).to.deep.equal({
type,
payload: foobar,
meta: foobar
meta: {
cid: 5
}
});
});
});
Expand Down
17 changes: 9 additions & 8 deletions src/createAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ export default function createAction(type, actionCreator, metaCreator) {
? actionCreator
: identity;

const finalMetaCreator = typeof metaCreator === 'function'
? metaCreator
: identity;
return (...args) => {
const action = {
type,
payload: finalActionCreator(...args)
};

if (typeof metaCreator === 'function') action.meta = metaCreator(...args);

return (...args) => ({
type,
payload: finalActionCreator(...args),
meta: finalMetaCreator(...args)
});
return action;
};
}

0 comments on commit 0b82c83

Please sign in to comment.