test is passing

This commit is contained in:
Yanick Champoux 2023-03-09 15:13:13 -05:00
parent 1555e4d289
commit 769b70dfce
3 changed files with 19 additions and 2 deletions

View File

@ -116,6 +116,7 @@ export default class Updux<
this.initial,
this.#localMutations,
this.#defaultMutation,
this.#subduxes,
) as any as (
state: undefined | typeof this.initial,
action: Action,

View File

@ -56,18 +56,25 @@ test('default mutation', () => {
test('mutation of a subdux', () => {
const baz = createAction('baz');
const noop = createAction('noop');
const stopit = createAction('stopit');
const bar = new Updux({
initial: 0,
actions: {
baz,
stopit,
},
});
bar.addMutation(baz, () => () => 1);
bar.addMutation(stopit, () => () => 2);
const foo = new Updux({
subduxes: { bar },
});
foo.addMutation(stopit, () => (state) => state, true);
expect(foo.reducer(undefined, noop())).toHaveProperty('bar', 0);
expect(foo.reducer(undefined, baz())).toHaveProperty('bar', 1);
expect(foo.reducer(undefined, stopit())).toHaveProperty('bar', 0);
});

View File

@ -16,8 +16,7 @@ export function buildReducer(
defaultMutation?: Omit<MutationCase, 'matcher'>,
subduxes: Record<string, Dux> = {},
) {
// const subReducers =
// ? R.mapValues(subduxes, R.prop('reducer'));
const subReducers = R.mapValues(subduxes, R.prop('reducer'));
// TODO matcherMutation
// TODO defaultMutation
@ -48,6 +47,16 @@ export function buildReducer(
)(state);
}
if (!terminal && Object.keys(subduxes).length > 0) {
// subduxes
state = R.merge(
state,
R.mapValues(subReducers, (reducer, slice) =>
(reducer as any)(state[slice], action),
),
);
}
return state;
};