diff --git a/src/Updux.js b/src/Updux.js index 36f2455..ad7a9c7 100644 --- a/src/Updux.js +++ b/src/Updux.js @@ -116,8 +116,11 @@ export class Updux { return this; } - createStore() { + addEffect(action, effect) { + this.#effects = [...this.#effects, [action, effect]]; + } + createStore() { const store = reduxCreateStore( this.reducer, this.initial, diff --git a/src/Updux.test.js b/src/Updux.test.js index 7d30994..c2b552a 100644 --- a/src/Updux.test.js +++ b/src/Updux.test.js @@ -56,7 +56,7 @@ test('addAction', async (t) => { t.same(Object.keys(dux.actions).sort(), ['bar', 'foo']); }); -test('basic selectors', { todo: true }, async (t) => { +test('basic selectors', async (t) => { const alpha = new Updux({ initial: { quux: 3 }, selectors: { @@ -96,7 +96,7 @@ test('basic selectors', { todo: true }, async (t) => { t.equal(store.selectors.getAdd(7), 8); }); -test('mutations', { todo: false }, async (t) => { +test('mutations', async (t) => { const alpha = new Updux({ initial: { quux: 3 }, }); @@ -147,24 +147,23 @@ test('mutations', { todo: false }, async (t) => { }); }); -test( 'middleware', async(t) => { +test('middleware', async (t) => { const fooEffect = sinon.fake.returns(true); const dux = new Updux({ effects: { - foo: () => next => action => { + foo: () => (next) => (action) => { fooEffect(); next(action); - } - } + }, + }, }); const store = dux.createStore(); - t.notOk( fooEffect.called, 'not called yet' ); + t.notOk(fooEffect.called, 'not called yet'); - store.dispatch({type: 'foo'}); + store.dispatch({ type: 'foo' }); - t.ok( fooEffect.called, "now it's been called" ); - -} ); + t.ok(fooEffect.called, "now it's been called"); +}); diff --git a/src/buildSelectors/index.js b/src/buildSelectors/index.js index 9e03942..b2bbc62 100644 --- a/src/buildSelectors/index.js +++ b/src/buildSelectors/index.js @@ -7,6 +7,5 @@ export function buildSelectors(localSelectors, subduxes) { return mapValues(selectors, (func) => (state) => func(state[slice])); }); - console.log(subSelectors); return merge({}, ...subSelectors, localSelectors); } diff --git a/src/selectors.test.js b/src/selectors.test.js index 918328c..1db21ea 100644 --- a/src/selectors.test.js +++ b/src/selectors.test.js @@ -3,7 +3,7 @@ import { test } from 'tap'; import { Updux } from './Updux.js'; import { action } from './actions.js'; -test('basic selectors', async t => { +test('basic selectors', async (t) => { const updux = new Updux({ subduxes: { bogeys: new Updux({ @@ -28,7 +28,7 @@ test('basic selectors', async t => { t.equal(updux.selectors.bogey(state)('foo'), 1); }); -test('available in the middleware', async t => { +test('available in the middleware', async (t) => { const doIt = action('doIt'); const updux = new Updux({ @@ -42,15 +42,18 @@ test('available in the middleware', async t => { }), }, effects: { - doIt: ({ selectors: { bogey }, getState }) => next => action => { - next({ - ...action, - payload: bogey(getState())('enkidu'), - }); - }, + doIt: + ({ selectors: { bogey }, getState }) => + (next) => + (action) => { + next({ + ...action, + payload: bogey(getState())('enkidu'), + }); + }, }, mutations: { - doIt: payload => state => ({ ...state, payload }), + doIt: (payload) => (state) => ({ ...state, payload }), }, }); @@ -60,69 +63,77 @@ test('available in the middleware', async t => { t.match(store.getState(), { payload: 'foo' }); }); -test('selector typescript', async t => { +test('selector typescript', async (t) => { const bar = new Updux({ initial: { baz: 1 }, selectors: { getBaz: (state) => state.baz, - getStringBaz: state => `${state.baz}`, - getMultBaz: state => (mult) => state.baz * mult, + getStringBaz: (state) => `${state.baz}`, + getMultBaz: (state) => (mult) => state.baz * mult, }, }); - t.same(bar.selectors.getBaz(bar.initial), 1); t.same(bar.selectors.getMultBaz({ baz: 3 })(2), 6); - test('subduxes', async t => { + test('subduxes', async (t) => { const foo = new Updux({ subduxes: { bar }, selectors: { - getRoot: () => 'root' - } + getRoot: () => 'root', + }, }); t.same(foo.selectors.getBaz(foo.initial), 1); t.same(foo.selectors.getMultBaz({ bar: { baz: 3 } })(2), 6); - t.ok( foo.selectors.getRoot ); + t.ok(foo.selectors.getRoot); }); - test('no root selector', async t => { + test('no root selector', async (t) => { const foo = new Updux({ subduxes: { quux: new Updux({}), bar: new Updux({ selectors: { - getBaz: () => 'baz' - } - }) - } + getBaz: () => 'baz', + }, + }), + }, }); t.ok(foo.selectors.getBaz); }); }); -test('selector in mw', async () => { - const myDux = new Updux( - { - initial: { stuff: 12 }, - subduxes: { - bar: new Updux({ - initial: 'potato', - selectors: { getBar: () => 'meh' } - }) - }, - selectors: { - // TODO here we should auto-populate the state - getStuff: (state) => state.stuff - } - } - ); - - myDux.addEffect( '*', ({ - selectors, getState - }) => () => () => { +test('selector in mw', async (t) => { + const myDux = new Updux({ + initial: { stuff: 12 }, + subduxes: { + bar: new Updux({ + initial: 'potato', + selectors: { getBar: () => 'meh' }, + }), + }, + selectors: { + getStuff: (state) => state.stuff, + }, }); + + let ranEffect = false; + + myDux.addEffect('*', ({ selectors, getState }) => () => () => { + ['getStuff', 'getBar'].forEach((selector) => + t.type(selectors[selector], 'function') + ); + + t.equal(getState.getStuff(), 12); + t.equal(getState.getBar(), 'meh'); + + ranEffect = true; + }); + + myDux.createStore().dispatch({ type: 'foo' }); + + t.ok(ranEffect); });