add selector tests

This commit is contained in:
Yanick Champoux 2021-10-08 12:42:08 -04:00
parent 2c26f9652b
commit 04159ec7cc
4 changed files with 67 additions and 55 deletions

View File

@ -116,8 +116,11 @@ export class Updux {
return this; return this;
} }
createStore() { addEffect(action, effect) {
this.#effects = [...this.#effects, [action, effect]];
}
createStore() {
const store = reduxCreateStore( const store = reduxCreateStore(
this.reducer, this.reducer,
this.initial, this.initial,

View File

@ -56,7 +56,7 @@ test('addAction', async (t) => {
t.same(Object.keys(dux.actions).sort(), ['bar', 'foo']); 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({ const alpha = new Updux({
initial: { quux: 3 }, initial: { quux: 3 },
selectors: { selectors: {
@ -96,7 +96,7 @@ test('basic selectors', { todo: true }, async (t) => {
t.equal(store.selectors.getAdd(7), 8); t.equal(store.selectors.getAdd(7), 8);
}); });
test('mutations', { todo: false }, async (t) => { test('mutations', async (t) => {
const alpha = new Updux({ const alpha = new Updux({
initial: { quux: 3 }, 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 fooEffect = sinon.fake.returns(true);
const dux = new Updux({ const dux = new Updux({
effects: { effects: {
foo: () => next => action => { foo: () => (next) => (action) => {
fooEffect(); fooEffect();
next(action); next(action);
} },
} },
}); });
const store = dux.createStore(); 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");
});
} );

View File

@ -7,6 +7,5 @@ export function buildSelectors(localSelectors, subduxes) {
return mapValues(selectors, (func) => (state) => func(state[slice])); return mapValues(selectors, (func) => (state) => func(state[slice]));
}); });
console.log(subSelectors);
return merge({}, ...subSelectors, localSelectors); return merge({}, ...subSelectors, localSelectors);
} }

View File

@ -3,7 +3,7 @@ import { test } from 'tap';
import { Updux } from './Updux.js'; import { Updux } from './Updux.js';
import { action } from './actions.js'; import { action } from './actions.js';
test('basic selectors', async t => { test('basic selectors', async (t) => {
const updux = new Updux({ const updux = new Updux({
subduxes: { subduxes: {
bogeys: new Updux({ bogeys: new Updux({
@ -28,7 +28,7 @@ test('basic selectors', async t => {
t.equal(updux.selectors.bogey(state)('foo'), 1); 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 doIt = action('doIt');
const updux = new Updux({ const updux = new Updux({
@ -42,15 +42,18 @@ test('available in the middleware', async t => {
}), }),
}, },
effects: { effects: {
doIt: ({ selectors: { bogey }, getState }) => next => action => { doIt:
next({ ({ selectors: { bogey }, getState }) =>
...action, (next) =>
payload: bogey(getState())('enkidu'), (action) => {
}); next({
}, ...action,
payload: bogey(getState())('enkidu'),
});
},
}, },
mutations: { 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' }); t.match(store.getState(), { payload: 'foo' });
}); });
test('selector typescript', async t => { test('selector typescript', async (t) => {
const bar = new Updux({ const bar = new Updux({
initial: { baz: 1 }, initial: { baz: 1 },
selectors: { selectors: {
getBaz: (state) => state.baz, getBaz: (state) => state.baz,
getStringBaz: state => `${state.baz}`, getStringBaz: (state) => `${state.baz}`,
getMultBaz: state => (mult) => state.baz * mult, getMultBaz: (state) => (mult) => state.baz * mult,
}, },
}); });
t.same(bar.selectors.getBaz(bar.initial), 1); t.same(bar.selectors.getBaz(bar.initial), 1);
t.same(bar.selectors.getMultBaz({ baz: 3 })(2), 6); t.same(bar.selectors.getMultBaz({ baz: 3 })(2), 6);
test('subduxes', async t => { test('subduxes', async (t) => {
const foo = new Updux({ const foo = new Updux({
subduxes: { bar }, subduxes: { bar },
selectors: { selectors: {
getRoot: () => 'root' getRoot: () => 'root',
} },
}); });
t.same(foo.selectors.getBaz(foo.initial), 1); t.same(foo.selectors.getBaz(foo.initial), 1);
t.same(foo.selectors.getMultBaz({ bar: { baz: 3 } })(2), 6); 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({ const foo = new Updux({
subduxes: { subduxes: {
quux: new Updux({}), quux: new Updux({}),
bar: new Updux({ bar: new Updux({
selectors: { selectors: {
getBaz: () => 'baz' getBaz: () => 'baz',
} },
}) }),
} },
}); });
t.ok(foo.selectors.getBaz); t.ok(foo.selectors.getBaz);
}); });
}); });
test('selector in mw', async () => { test('selector in mw', async (t) => {
const myDux = new Updux( const myDux = new Updux({
{ initial: { stuff: 12 },
initial: { stuff: 12 }, subduxes: {
subduxes: { bar: new Updux({
bar: new Updux({ initial: 'potato',
initial: 'potato', selectors: { getBar: () => 'meh' },
selectors: { getBar: () => 'meh' } }),
}) },
}, selectors: {
selectors: { getStuff: (state) => state.stuff,
// TODO here we should auto-populate the state },
getStuff: (state) => state.stuff
}
}
);
myDux.addEffect( '*', ({
selectors, getState
}) => () => () => {
}); });
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);
}); });