Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
omichelsen committed Aug 15, 2018
1 parent f58b0f5 commit 5803775
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"homepage": "https://github.com/omichelsen/redux-promise-middleware-actions#readme",
"devDependencies": {
"@types/mocha": "^5.2.5",
"@types/node": "^10.5.2",
"@types/node": "^10.7.0",
"mocha": "^5.2.0",
"nyc": "^12.0.2",
"ts-node": "^7.0.0",
"ts-node": "^7.0.1",
"tslint": "^5.11.0",
"typescript": "^3.0.1"
},
Expand Down
13 changes: 9 additions & 4 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ export function createAsyncAction<Payload, U extends any[]>(
payloadCreator?: (...args: U) => Payload
) {
return Object.assign(
(...args: U) => ({
type,
...(payloadCreator && { payload: payloadCreator(...args) }),
}),
(...args: U) => {
const payload = (payloadCreator && payloadCreator(...args));

if (typeof payload === 'function') {
return payload;
}

return { type, ...(payload && { payload }) };
},
{
toString: () => {
throw new Error(`Async action ${type} must be handled with pending, fulfilled or rejected`);
Expand Down
10 changes: 10 additions & 0 deletions test/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,15 @@ describe('actions', () => {
});
});
});

describe('thunk', () => {
it('should return a thunk function', () => {
const thunk = createAsyncAction(
'THUNK',
(): any => (_: any, getState: any) => Promise.resolve(getState())
);
assert(typeof thunk('test') === 'function');
});
});
});
});

0 comments on commit 5803775

Please sign in to comment.