basic action test

This commit is contained in:
Yanick Champoux 2023-03-06 13:02:40 -05:00
parent f5cdc4207a
commit 13eeb86e05
3 changed files with 26 additions and 14 deletions

View File

@ -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({

View File

@ -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<number>('bar');

View File

@ -2,6 +2,14 @@ import { createAction } from '@reduxjs/toolkit';
export { createAction } from '@reduxjs/toolkit';
export const withPayload =
<P>() =>
(payload: P) => ({ payload });
interface WithPayload {
(): <P>(input: P) => { payload: P };
<P, A extends any[]>(prepare: (...args: A) => P): (...input: A) => {
payload: P;
};
}
export const withPayload: WithPayload = ((prepare) =>
(...input) => ({
payload: prepare ? prepare(...input) : input,
})) as any;