updux/src/subscriptions.test.js

108 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-06-19 23:29:12 +00:00
import tap from 'tap';
2021-05-02 18:35:37 +00:00
import u from '@yanick/updeep';
2020-06-19 23:29:12 +00:00
2021-10-08 23:56:55 +00:00
import { Updux } from './Updux.js';
import { action } from './actions.js';
2020-06-19 23:29:12 +00:00
2021-10-08 23:56:55 +00:00
tap.test('subscriptions', async () => {
const inc = action('inc');
const set_copy = action('set_copy');
2020-06-19 23:29:12 +00:00
2021-10-08 23:56:55 +00:00
const dux = new Updux({
initial: {
x: 0,
copy: 0,
},
actions: {
inc,
set_copy,
},
mutations: {
inc: (payload) => u({ x: (x) => x + 1 }),
set_copy: (copy) => u({ copy }),
},
});
2020-06-19 23:29:12 +00:00
2021-10-08 23:56:55 +00:00
dux.addSubscription((store) => (state, previous, unsubscribe) => {
if (state.x > 2) return unsubscribe();
2020-06-19 23:29:12 +00:00
2021-10-08 23:56:55 +00:00
store.dispatch(set_copy(state.x));
});
2020-06-19 23:29:12 +00:00
2021-10-08 23:56:55 +00:00
const store = dux.createStore();
2020-06-19 23:29:12 +00:00
2021-10-08 23:56:55 +00:00
store.dispatch(inc());
2020-06-19 23:29:12 +00:00
2021-10-08 23:56:55 +00:00
tap.same(store.getState(), { x: 1, copy: 1 });
2021-10-08 19:33:45 +00:00
2021-10-08 23:56:55 +00:00
store.dispatch(inc());
store.dispatch(inc());
2021-10-08 19:33:45 +00:00
2021-10-08 23:56:55 +00:00
tap.same(store.getState(), { x: 3, copy: 2 }, 'we unsubscribed');
});
2020-06-19 23:29:12 +00:00
2021-10-08 23:56:55 +00:00
tap.test('subduxes subscriptions', async (t) => {
2020-06-19 23:29:12 +00:00
const inc_top = action('inc_top');
const inc_bar = action('inc_bar');
2021-10-08 19:33:45 +00:00
const transform_bar = action('transform_bar');
2020-06-19 23:29:12 +00:00
const bar = new Updux({
initial: 'a',
2021-10-08 23:56:55 +00:00
actions: { inc_bar, transform_bar },
mutations: {
inc_bar: () => (state) => state + 'a',
transform_bar: (outcome) => () => outcome,
},
2020-06-19 23:29:12 +00:00
subscriptions: [
2021-10-08 23:56:55 +00:00
(store) => (state, previous, unsubscribe) => {
2020-06-19 23:29:12 +00:00
if (state.length <= 2) return;
unsubscribe();
store.dispatch(transform_bar('look at ' + state));
},
],
});
const dux = new Updux({
initial: {
count: 0,
},
2021-10-08 23:56:55 +00:00
subduxes: { bar },
actions: {
inc_top,
},
mutations: {
inc_top: () => u({ count: (count) => count + 1 }),
},
effects: {
'*': () => (next) => (action) => {
next(action);
},
2020-06-19 23:29:12 +00:00
},
subscriptions: [
2021-10-08 23:56:55 +00:00
(store) => {
return ({ count }, { count: previous } = {}) => {
2020-06-19 23:29:12 +00:00
if (count !== previous) {
previous = count;
2021-10-08 23:56:55 +00:00
store.dispatch.inc_bar();
2020-06-19 23:29:12 +00:00
}
};
},
],
});
const store = dux.createStore();
store.dispatch(inc_top());
store.dispatch(inc_top());
t.same(store.getState(), {
count: 2,
bar: 'look at look at aaa',
});
store.dispatch(inc_top());
t.same(store.getState(), {
count: 3,
bar: 'look at look at aaaa',
});
});