add selector tests
This commit is contained in:
parent
2c26f9652b
commit
04159ec7cc
@ -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,
|
||||||
|
@ -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");
|
||||||
|
});
|
||||||
} );
|
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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,7 +42,10 @@ test('available in the middleware', async t => {
|
|||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
effects: {
|
effects: {
|
||||||
doIt: ({ selectors: { bogey }, getState }) => next => action => {
|
doIt:
|
||||||
|
({ selectors: { bogey }, getState }) =>
|
||||||
|
(next) =>
|
||||||
|
(action) => {
|
||||||
next({
|
next({
|
||||||
...action,
|
...action,
|
||||||
payload: bogey(getState())('enkidu'),
|
payload: bogey(getState())('enkidu'),
|
||||||
@ -50,7 +53,7 @@ test('available in the middleware', async t => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
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: {
|
||||||
// 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( '*', ({
|
t.equal(getState.getStuff(), 12);
|
||||||
selectors, getState
|
t.equal(getState.getBar(), 'meh');
|
||||||
}) => () => () => {
|
|
||||||
|
ranEffect = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
myDux.createStore().dispatch({ type: 'foo' });
|
||||||
|
|
||||||
|
t.ok(ranEffect);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user