Add subscription test
This commit is contained in:
parent
04159ec7cc
commit
5247aa2255
@ -1,46 +1,50 @@
|
||||
import tap from 'tap';
|
||||
import Updux from '.';
|
||||
import { action, payload } from 'ts-action';
|
||||
import u from '@yanick/updeep';
|
||||
|
||||
tap.test( 'subscriptions', async() => {
|
||||
|
||||
const inc = action('inc');
|
||||
const set_double = action('set_double', payload<number>());
|
||||
const set_copy = action('set_copy');
|
||||
|
||||
const dux = new Updux({
|
||||
initial: {
|
||||
x: 0,
|
||||
double: 0,
|
||||
copy: 0,
|
||||
},
|
||||
actions: {
|
||||
inc,
|
||||
},
|
||||
mutations: [
|
||||
[inc, payload => u({ x: x => x + 1 })],
|
||||
[set_double, double => u({ double })],
|
||||
],
|
||||
mutations: {
|
||||
inc: payload => u({ x: x => x + 1 }),
|
||||
set_copy: copy => u({ copy }),
|
||||
},
|
||||
});
|
||||
|
||||
dux.addSubscription(store => (state, unsubscribe) => {
|
||||
if (state.x > 2) return unsubscribe();
|
||||
|
||||
store.dispatch(set_double(state.x * 2));
|
||||
store.dispatch(set_copy(state.x));
|
||||
});
|
||||
|
||||
const store = dux.createStore();
|
||||
|
||||
store.dispatch(inc());
|
||||
|
||||
tap.same(store.getState(), { x: 1, double: 2 });
|
||||
tap.same(store.getState(), { x: 1, copy: 1 });
|
||||
|
||||
store.dispatch(inc());
|
||||
store.dispatch(inc());
|
||||
|
||||
tap.same(store.getState(), { x: 3, double: 4 }, 'we unsubscribed');
|
||||
tap.same(store.getState(), { x: 3, copy: 2 }, 'we unsubscribed');
|
||||
|
||||
|
||||
} );
|
||||
|
||||
tap.test('subduxes subscriptions', async t => {
|
||||
const inc_top = action('inc_top');
|
||||
const inc_bar = action('inc_bar');
|
||||
const transform_bar = action('transform_bar', payload());
|
||||
const transform_bar = action('transform_bar');
|
||||
|
||||
const bar = new Updux({
|
||||
initial: 'a',
|
||||
@ -79,7 +83,7 @@ tap.test('subduxes subscriptions', async t => {
|
||||
],
|
||||
subscriptions: [
|
||||
store => {
|
||||
let previous: any;
|
||||
let previous;
|
||||
return ({ count }) => {
|
||||
if (count !== previous) {
|
||||
previous = count;
|
Loading…
Reference in New Issue
Block a user