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;
}
createStore() {
addEffect(action, effect) {
this.#effects = [...this.#effects, [action, effect]];
}
createStore() {
const store = reduxCreateStore(
this.reducer,
this.initial,

View File

@ -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 },
});
@ -152,11 +152,11 @@ test( 'middleware', async(t) => {
const dux = new Updux({
effects: {
foo: () => next => action => {
foo: () => (next) => (action) => {
fooEffect();
next(action);
}
}
},
},
});
const store = dux.createStore();
@ -166,5 +166,4 @@ test( 'middleware', async(t) => {
store.dispatch({ type: 'foo' });
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]));
});
console.log(subSelectors);
return merge({}, ...subSelectors, localSelectors);
}

View File

@ -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,7 +42,10 @@ test('available in the middleware', async t => {
}),
},
effects: {
doIt: ({ selectors: { bogey }, getState }) => next => action => {
doIt:
({ selectors: { bogey }, getState }) =>
(next) =>
(action) => {
next({
...action,
payload: bogey(getState())('enkidu'),
@ -50,7 +53,7 @@ test('available in the middleware', async t => {
},
},
mutations: {
doIt: payload => state => ({ ...state, payload }),
doIt: (payload) => (state) => ({ ...state, payload }),
},
});
@ -60,26 +63,25 @@ 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);
@ -88,41 +90,50 @@ test('selector typescript', async t => {
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(
{
test('selector in mw', async (t) => {
const myDux = new Updux({
initial: { stuff: 12 },
subduxes: {
bar: new Updux({
initial: 'potato',
selectors: { getBar: () => 'meh' }
})
selectors: { getBar: () => 'meh' },
}),
},
selectors: {
// TODO here we should auto-populate the state
getStuff: (state) => state.stuff
}
}
getStuff: (state) => state.stuff,
},
});
let ranEffect = false;
myDux.addEffect('*', ({ selectors, getState }) => () => () => {
['getStuff', 'getBar'].forEach((selector) =>
t.type(selectors[selector], 'function')
);
myDux.addEffect( '*', ({
selectors, getState
}) => () => () => {
t.equal(getState.getStuff(), 12);
t.equal(getState.getBar(), 'meh');
ranEffect = true;
});
myDux.createStore().dispatch({ type: 'foo' });
t.ok(ranEffect);
});