From 6a6bb1636ae123acbabae0f8eaa4e72210b851a0 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 25 Aug 2022 20:06:52 -0400 Subject: [PATCH] acknowledge the subduxes actions --- src/Updux.js | 20 ++++++++++++-------- src/actions.js | 6 +----- src/actions.test.js | 36 ++++++++++++++++++++++++++---------- src/buildUpreducer.js | 6 +++--- src/mutations.test.js | 24 ++++++++++++------------ src/reducer.test.js | 6 +++--- 6 files changed, 57 insertions(+), 41 deletions(-) diff --git a/src/Updux.js b/src/Updux.js index 2b33866..5524aaf 100644 --- a/src/Updux.js +++ b/src/Updux.js @@ -5,7 +5,7 @@ import { Action, ActionGenerator } from './actions.js'; export class Updux { #localInitial = {}; #subduxes = {}; - #actions ; + #actions; #mutations = {}; #config = {}; @@ -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() { @@ -31,22 +35,22 @@ export class Updux { } get reducer() { - return (state,action) => this.upreducer(action)(state); + return (state, action) => this.upreducer(action)(state); } get upreducer() { - return (action) => state => { + return (action) => (state) => { const mutation = this.#mutations[action.type]; - if( mutation ) { - state = mutation(action.payload,action)(state); + if (mutation) { + state = mutation(action.payload, action)(state); } return state; }; } - setMutation( action, mutation ) { - this.#mutations[ action.type ] = mutation; + setMutation(action, mutation) { + this.#mutations[action.type] = mutation; } } diff --git a/src/actions.js b/src/actions.js index d64a464..e5a838b 100644 --- a/src/actions.js +++ b/src/actions.js @@ -1,8 +1,4 @@ -export function action( - type, - payloadFunction, - transformer, -) { +export function action(type, payloadFunction, transformer) { let generator = function (...payloadArg) { const result = { type }; diff --git a/src/actions.test.js b/src/actions.test.js index b42ae1f..b81d8e1 100644 --- a/src/actions.test.js +++ b/src/actions.test.js @@ -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), - } + one: action('one', (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).toBeTypeOf('function'); + 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'); +}); diff --git a/src/buildUpreducer.js b/src/buildUpreducer.js index bbf788b..39672ac 100644 --- a/src/buildUpreducer.js +++ b/src/buildUpreducer.js @@ -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); diff --git a/src/mutations.test.js b/src/mutations.test.js index 7c103e1..f71c041 100644 --- a/src/mutations.test.js +++ b/src/mutations.test.js @@ -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})), + foo: action('foo', (x) => ({ x })), bar: action('bar'), - } + }, }); - dux.setMutation( dux.actions.foo, (payload,action) => u({ - x: payload.x + action.type - }) ); - - const result = dux.reducer(undefined,dux.actions.foo("hello ")); - expect( result ).toEqual({ - x: "hello foo" - }) - + dux.setMutation(dux.actions.foo, (payload, action) => + u({ + x: payload.x + action.type, + }), + ); + const result = dux.reducer(undefined, dux.actions.foo('hello ')); + expect(result).toEqual({ + x: 'hello foo', + }); }); diff --git a/src/reducer.test.js b/src/reducer.test.js index c668a1e..2b5ff18 100644 --- a/src/reducer.test.js +++ b/src/reducer.test.js @@ -3,7 +3,7 @@ import { test, expect } from 'vitest'; import { Updux } from './Updux.js'; test('basic reducer', () => { - const dux = new Updux({ initial: {a: 3} }); + const dux = new Updux({ initial: { a: 3 } }); expect(dux.reducer).toBeTypeOf('function'); @@ -11,9 +11,9 @@ test('basic reducer', () => { }); test('basic upreducer', () => { - const dux = new Updux({ initial: {a: 3} }); + const dux = new Updux({ initial: { a: 3 } }); expect(dux.upreducer).toBeTypeOf('function'); - expect(dux.upreducer({ type: 'foo' })({a:1})).toMatchObject({ a: 1 }); // noop + expect(dux.upreducer({ type: 'foo' })({ a: 1 })).toMatchObject({ a: 1 }); // noop });