acknowledge the subduxes actions

This commit is contained in:
Yanick Champoux 2022-08-25 20:06:52 -04:00
parent 7494fe553e
commit 6a6bb1636a
6 changed files with 57 additions and 41 deletions

View File

@ -13,7 +13,11 @@ export class Updux {
this.#config = config; this.#config = config;
this.#localInitial = config.initial ?? {}; this.#localInitial = config.initial ?? {};
this.#subduxes = config.subduxes ?? {}; this.#subduxes = config.subduxes ?? {};
this.#actions = config.actions ?? {};
this.#actions = R.mergeAll([
config.actions ?? {},
...Object.values(this.#subduxes).map(R.prop('actions')),
]);
} }
get actions() { get actions() {
@ -35,7 +39,7 @@ export class Updux {
} }
get upreducer() { get upreducer() {
return (action) => state => { return (action) => (state) => {
const mutation = this.#mutations[action.type]; const mutation = this.#mutations[action.type];
if (mutation) { if (mutation) {

View File

@ -1,8 +1,4 @@
export function action( export function action(type, payloadFunction, transformer) {
type,
payloadFunction,
transformer,
) {
let generator = function (...payloadArg) { let generator = function (...payloadArg) {
const result = { type }; const result = { type };

View File

@ -15,23 +15,39 @@ test('basic action', () => {
}); });
}); });
test( "Updux config accepts actions", () => { test('Updux config accepts actions', () => {
const foo = new Updux({ const foo = new Updux({
actions: { actions: {
one: action('one', (x) => ({ x })), one: action('one', (x) => ({ x })),
two: action('two', x => x), two: action('two', (x) => x),
} },
}); });
expect(Object.keys(foo.actions)).toHaveLength(2); expect(Object.keys(foo.actions)).toHaveLength(2);
expect(foo.actions.one).toBeTypeOf('function'); expect(foo.actions.one).toBeTypeOf('function');
expect( foo.actions.one("potato") ).toEqual({ expect(foo.actions.one('potato')).toEqual({
type: 'one', type: 'one',
payload: { payload: {
x: 'potato' x: 'potato',
} },
});
}); });
} ) test('subduxes actions', () => {
const foo = new Updux({
actions: {
foo: null,
},
subduxes: {
beta: {
actions: {
bar: null,
},
},
},
});
expect(foo.actions).toHaveProperty('foo');
expect(foo.actions).toHaveProperty('bar');
});

View File

@ -5,7 +5,7 @@ export function buildUpreducer(
initial, initial,
mutations, mutations,
subduxes = {}, subduxes = {},
wrapper = undefined wrapper = undefined,
) { ) {
const subReducers = const subReducers =
Object.keys(subduxes).length > 0 Object.keys(subduxes).length > 0
@ -23,11 +23,11 @@ export function buildUpreducer(
newState = u.updateIn( newState = u.updateIn(
'*', '*',
subduxes['*'].upreducer(action), subduxes['*'].upreducer(action),
newState newState,
); );
} else { } else {
const update = mapValues(subReducers, (upReducer) => const update = mapValues(subReducers, (upReducer) =>
upReducer(action) upReducer(action),
); );
newState = u(update, newState); newState = u(update, newState);

View File

@ -9,22 +9,22 @@ import { Updux } from './Updux.js';
test('set a mutation', () => { test('set a mutation', () => {
const dux = new Updux({ const dux = new Updux({
initial: { initial: {
x: "potato", x: 'potato',
}, },
actions: { actions: {
foo: action('foo', (x) => ({ x })), foo: action('foo', (x) => ({ x })),
bar: action('bar'), bar: action('bar'),
} },
}); });
dux.setMutation( dux.actions.foo, (payload,action) => u({ dux.setMutation(dux.actions.foo, (payload, action) =>
x: payload.x + action.type u({
}) ); x: payload.x + action.type,
}),
);
const result = dux.reducer(undefined,dux.actions.foo("hello ")); const result = dux.reducer(undefined, dux.actions.foo('hello '));
expect(result).toEqual({ expect(result).toEqual({
x: "hello foo" x: 'hello foo',
}) });
}); });