Skip to content

Commit ea1f39c

Browse files
authored
feat(createAction): add parameter payloadCreator, metaCreators (#12)
1 parent 0c19233 commit ea1f39c

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ declare module 'redux-fluent' {
1717
P extends object = object,
1818
M extends object = object,
1919
T extends string = string
20-
>(type: T): I.ActionCreator<P, M, T>;
20+
>(type: T, payloadCreator?: (p: any) => P, metaCreator?: (m: any) => M): I.ActionCreator<P, M, T>;
2121
}
2222

2323
declare namespace /* Interfaces */ I {

src/Action/Action.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
export default function ActionCreatorFactory(type) {
2-
function actionCreator(payload, meta) {
1+
const identity = arg => arg;
2+
3+
export default function ActionCreatorFactory(type, payloadCreator, metaCreator) {
4+
function actionCreator(rawPayload, rawMeta) {
35
const action = Object.create(null);
46

7+
const payload = (payloadCreator || identity)(rawPayload) || null;
8+
const meta = (metaCreator || identity)(rawMeta) || null;
9+
510
action.type = type;
611
action.error = payload instanceof Error;
7-
action.meta = meta || null;
8-
action.payload = payload || null;
12+
action.meta = meta;
13+
action.payload = payload;
914

1015
return Object.freeze(action);
1116
}

src/Action/Action.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ describe('createAction', () => {
99
expect(createAction('test')).toEqual(jasmine.any(Function));
1010
});
1111

12+
it('Factory should accept a payloadCreator', () => {
13+
const payloadCreator = jasmine.createSpy('payloadCreator')
14+
.and.callFake(todoId => ({ id: todoId }));
15+
16+
const deleteTodo = createAction('@@todos/:id | delete', payloadCreator);
17+
const todoId = 12;
18+
19+
expect(deleteTodo(todoId).payload).toEqual({ id: todoId });
20+
expect(payloadCreator).toHaveBeenCalledWith(todoId);
21+
});
22+
23+
it('Factory should accept a metaCreator', () => {
24+
const metaCreator = jasmine.createSpy('metaCreator')
25+
.and.callFake(metaThing => ({ id: metaThing }));
26+
27+
const deleteTodo = createAction('@@todos/:id | delete', null, metaCreator);
28+
const metaThing = 'Hello World';
29+
30+
expect(deleteTodo(null, metaThing).meta).toEqual({ id: metaThing });
31+
expect(metaCreator).toHaveBeenCalledWith(metaThing);
32+
});
33+
1234
describe('actionCreator', () => {
1335
it('should have a property type', () => {
1436
expect(createAction(type)).toHaveProperty('type', type);

0 commit comments

Comments
 (0)