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

View File

@ -1,8 +1,4 @@
export function action(
type,
payloadFunction,
transformer,
) {
export function action(type, payloadFunction, transformer) {
let generator = function (...payloadArg) {
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({
actions: {
one: action('one', (x) => ({ x })),
two: action('two', x => x),
}
two: action('two', (x) => x),
},
});
expect(Object.keys(foo.actions)).toHaveLength(2);
expect(foo.actions.one).toBeTypeOf('function');
expect( foo.actions.one("potato") ).toEqual({
expect(foo.actions.one('potato')).toEqual({
type: 'one',
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,
mutations,
subduxes = {},
wrapper = undefined
wrapper = undefined,
) {
const subReducers =
Object.keys(subduxes).length > 0
@ -23,11 +23,11 @@ export function buildUpreducer(
newState = u.updateIn(
'*',
subduxes['*'].upreducer(action),
newState
newState,
);
} else {
const update = mapValues(subReducers, (upReducer) =>
upReducer(action)
upReducer(action),
);
newState = u(update, newState);

View File

@ -9,22 +9,22 @@ import { Updux } from './Updux.js';
test('set a mutation', () => {
const dux = new Updux({
initial: {
x: "potato",
x: 'potato',
},
actions: {
foo: action('foo', (x) => ({ x })),
bar: action('bar'),
}
},
});
dux.setMutation( dux.actions.foo, (payload,action) => u({
x: payload.x + action.type
}) );
dux.setMutation(dux.actions.foo, (payload, action) =>
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({
x: "hello foo"
})
x: 'hello foo',
});
});