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