diff --git a/src/actions.test.todo b/src/actions.test.todo index c442f36..d1e45ee 100644 --- a/src/actions.test.todo +++ b/src/actions.test.todo @@ -4,16 +4,6 @@ import { action } from './actions.js'; import { Updux } from './Updux.js'; -test('basic action', () => { - const foo = action('foo', (thing) => ({ thing })); - - expect(foo('bar')).toEqual({ - type: 'foo', - payload: { - thing: 'bar', - }, - }); -}); test('Updux config accepts actions', () => { const foo = new Updux({ diff --git a/src/actions.test.ts b/src/actions.test.ts index 76cdb07..c051266 100644 --- a/src/actions.test.ts +++ b/src/actions.test.ts @@ -1,4 +1,18 @@ -import Updux, { createAction } from './index.js'; +import Updux, { createAction, withPayload } from './index.js'; + +test('basic action', () => { + const foo = createAction( + 'foo', + withPayload((thing: string) => ({ thing })), + ); + + expect(foo('bar')).toEqual({ + type: 'foo', + payload: { + thing: 'bar', + }, + }); +}); test('subduxes actions', () => { const bar = createAction('bar'); diff --git a/src/actions.ts b/src/actions.ts index d36171b..9bd5453 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -2,6 +2,14 @@ import { createAction } from '@reduxjs/toolkit'; export { createAction } from '@reduxjs/toolkit'; -export const withPayload = -

() => - (payload: P) => ({ payload }); +interface WithPayload { + ():

(input: P) => { payload: P }; + (prepare: (...args: A) => P): (...input: A) => { + payload: P; + }; +} + +export const withPayload: WithPayload = ((prepare) => + (...input) => ({ + payload: prepare ? prepare(...input) : input, + })) as any;