This commit is contained in:
Yanick Champoux 2022-08-26 15:30:03 -04:00
parent 3bb9221c27
commit 43702fe096
2 changed files with 30 additions and 23 deletions

View File

@ -3,7 +3,7 @@ import R from 'remeda';
import { action } from './actions.js';
function isActionGen(action) {
return typeof action === "function" && action.type;
return typeof action === 'function' && action.type;
}
export class Updux {
@ -19,9 +19,11 @@ export class Updux {
this.#subduxes = config.subduxes ?? {};
this.#actions = R.mapValues(config.actions ?? {}, (arg, name) =>
isActionGen(arg) ? arg : action(name,arg)
isActionGen(arg) ? arg : action(name, arg),
);
Object.entries(this.#subduxes).forEach(([slice, sub]) =>
this.#addSubduxActions(slice, sub),
);
Object.entries(this.#subduxes).forEach( ([slice,sub]) => this.#addSubduxActions(slice,sub) )
}
#addSubduxActions(_slice, subdux) {

View File

@ -53,7 +53,9 @@ test('subduxes actions', () => {
});
test('throw if double action', () => {
expect( () => new Updux({
expect(
() =>
new Updux({
actions: {
foo: action('foo'),
},
@ -64,18 +66,21 @@ test('throw if double action', () => {
},
},
},
}) ).toThrow(/action 'foo' already defined/);
}),
).toThrow(/action 'foo' already defined/);
});
test('action definition shortcut', () => {
const foo = new Updux({
actions: {
foo: null,
bar: x => ({x}),
},}
);
expect( foo.actions.foo("hello") ).toEqual({ type: 'foo', payload: 'hello' });
expect( foo.actions.bar("hello") ).toEqual({ type: 'bar', payload: {x:'hello'} });
bar: (x) => ({ x }),
},
});
expect(foo.actions.foo('hello')).toEqual({ type: 'foo', payload: 'hello' });
expect(foo.actions.bar('hello')).toEqual({
type: 'bar',
payload: { x: 'hello' },
});
});