subscribe test

typescript
Yanick Champoux 2021-10-09 12:13:50 -04:00
parent 5eeb4d4ab7
commit c1c1edf588
2 changed files with 66 additions and 1 deletions

View File

@ -67,7 +67,17 @@ export class Updux {
constructor(config) {
this.#initial = config.initial ?? {};
this.#subduxes = config.subduxes ?? {};
this.#actions = config.actions ?? {};
if( config.actions ) {
for (const [ type, actionArg ] of Object.entries(config.actions)) {
if( typeof actionArg === 'function' && actionArg.type ) {
this.#actions[type] = actionArg;
}
else {
this.#actions[type] = action(type,actionArg)
}
}
}
this.#selectors = config.selectors ?? {};
this.#mutations = config.mutations ?? {};

View File

@ -1,4 +1,5 @@
import tap from 'tap';
import sinon from 'sinon';
import u from '@yanick/updeep';
import { Updux } from './Updux.js';
@ -105,3 +106,57 @@ tap.test('subduxes subscriptions', async (t) => {
bar: 'look at look at aaaa',
});
});
tap.test( "subscription within subduxes", {only: true},async(t) => {
let innerState = sinon.fake.returns(null);
let outerState = sinon.fake.returns(null);
const inner = new Updux({
initial: 1,
actions: { inc: null },
mutations: {
inc: () => state => state + 1,
},
subscriptions: [
store => (state, previous, unsub) => {
if(!previous) return;
store.subscribe( innerState );
unsub();
}
],
})
const dux = new Updux({
subduxes: { inner },
subscriptions: [
store => (state, previous, unsub) => {
console.log(state,previous);
if(!previous) return;
store.subscribe( outerState );
unsub();
}
],
});
const store = dux.createStore();
store.dispatch({ type: 'noop' });
store.dispatch({ type: 'noop' });
t.notOk( innerState.called );
t.notOk( outerState.called );
store.dispatch.inc();
// still not called, but waiting, now
t.notOk( innerState.called );
t.notOk( outerState.called );
store.dispatch.inc();
console.log(outerState.firstCall.args);
// console.log(outerState.firstCall)
} );