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.initial,
this.#localMutations, this.#localMutations,
this.#defaultMutation, this.#defaultMutation,
this.#subduxes,
) as any as ( ) as any as (
state: undefined | typeof this.initial, state: undefined | typeof this.initial,
action: Action, action: Action,

View File

@ -56,18 +56,25 @@ test('default mutation', () => {
test('mutation of a subdux', () => { test('mutation of a subdux', () => {
const baz = createAction('baz'); const baz = createAction('baz');
const noop = createAction('noop');
const stopit = createAction('stopit');
const bar = new Updux({ const bar = new Updux({
initial: 0, initial: 0,
actions: { actions: {
baz, baz,
stopit,
}, },
}); });
bar.addMutation(baz, () => () => 1); bar.addMutation(baz, () => () => 1);
bar.addMutation(stopit, () => () => 2);
const foo = new Updux({ const foo = new Updux({
subduxes: { bar }, 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, 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'>, defaultMutation?: Omit<MutationCase, 'matcher'>,
subduxes: Record<string, Dux> = {}, subduxes: Record<string, Dux> = {},
) { ) {
// const subReducers = const subReducers = R.mapValues(subduxes, R.prop('reducer'));
// ? R.mapValues(subduxes, R.prop('reducer'));
// TODO matcherMutation // TODO matcherMutation
// TODO defaultMutation // TODO defaultMutation
@ -48,6 +47,16 @@ export function buildReducer(
)(state); )(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; return state;
}; };