updux/src/sink.test.ts

58 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-06-02 20:00:48 +00:00
import Updux, { dux } from '.';
import tap from 'tap';
import { action } from 'ts-action';
const foo = dux({
initial: 0,
actions: {
doIt: action('doIt'),
doTheThing: action('doTheThing'),
2019-11-05 18:06:17 +00:00
},
2020-06-02 20:00:48 +00:00
mutations: {
doIt: () => (state: number) => {
return state + 1;
},
doTheThing: () => (state: number) => {
return state + 3;
},
2019-11-06 00:24:03 +00:00
},
2019-11-05 18:06:17 +00:00
});
2020-06-02 20:00:48 +00:00
const bar: any = new Updux<{ foo: number }>({
subduxes: { foo },
2019-11-05 18:06:17 +00:00
});
2019-11-06 00:24:03 +00:00
bar.addMutation(
2020-06-02 20:00:48 +00:00
foo.actions.doTheThing,
(_, action) => state => {
return {
...state,
baz: foo.upreducer(action)(state.foo),
};
},
true
2019-11-06 00:24:03 +00:00
);
2019-11-05 18:06:17 +00:00
bar.addMutation(
2020-06-02 20:00:48 +00:00
foo.actions.doIt,
() => (state: any) => ({ ...state, bar: 'yay' }),
true
2019-11-05 18:06:17 +00:00
);
2020-06-02 20:00:48 +00:00
tap.same(bar.initial, { foo: 0 });
2019-11-05 18:06:17 +00:00
2020-06-02 20:00:48 +00:00
tap.test('foo alone', t => {
t.is(foo.reducer(undefined, foo.actions.doIt()), 1);
t.end();
2019-11-05 18:06:17 +00:00
});
2020-06-02 20:00:48 +00:00
tap.test('sink mutations', t => {
t.same(
bar.reducer(undefined, bar.actions.doIt()), {
foo: 0,
bar: 'yay',
});
2019-11-06 00:24:03 +00:00
2020-06-02 20:00:48 +00:00
t.end();
2019-11-06 00:24:03 +00:00
});