add selector tests
This commit is contained in:
parent
2c26f9652b
commit
04159ec7cc
@ -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,
|
||||
|
@ -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");
|
||||
});
|
||||
|
@ -7,6 +7,5 @@ export function buildSelectors(localSelectors, subduxes) {
|
||||
return mapValues(selectors, (func) => (state) => func(state[slice]));
|
||||
});
|
||||
|
||||
console.log(subSelectors);
|
||||
return merge({}, ...subSelectors, localSelectors);
|
||||
}
|
||||
|
@ -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);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user