2019-11-05 18:06:17 +00:00
|
|
|
import Updux from './updux';
|
|
|
|
|
|
|
|
const foo = new Updux<number>({
|
|
|
|
initial: 0,
|
|
|
|
mutations: {
|
|
|
|
doIt: () => (state: number) => {
|
|
|
|
return state + 1;
|
|
|
|
},
|
2019-11-06 00:24:03 +00:00
|
|
|
doTheThing: () => (state: number) => {
|
|
|
|
return state + 3;
|
|
|
|
},
|
2019-11-05 18:06:17 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const bar = new Updux<{foo: number}>({
|
|
|
|
subduxes: {foo},
|
|
|
|
});
|
|
|
|
|
2019-11-06 00:24:03 +00:00
|
|
|
bar.addMutation(
|
|
|
|
foo.actions.doTheThing,
|
|
|
|
(_, action) => state => {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
baz: bar.subduxUpreducer(action)(state),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
2019-11-05 18:06:17 +00:00
|
|
|
bar.addMutation(
|
|
|
|
foo.actions.doIt,
|
|
|
|
() => (state: any) => ({...state, bar: 'yay'}),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
|
|
|
test('initial', () => {
|
|
|
|
expect(bar.initial).toEqual({foo: 0});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('foo alone', () => {
|
|
|
|
expect(foo.reducer(undefined, foo.actions.doIt())).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('sink mutations', () => {
|
|
|
|
expect(bar.reducer(undefined, bar.actions.doIt())).toEqual({
|
|
|
|
foo: 0,
|
|
|
|
bar: 'yay',
|
|
|
|
});
|
|
|
|
});
|
2019-11-06 00:24:03 +00:00
|
|
|
|
|
|
|
test('sink mutation and subduxUpreducer', () => {
|
|
|
|
expect(bar.reducer(undefined, bar.actions.doTheThing())).toEqual({
|
|
|
|
foo: 0,
|
|
|
|
baz: {foo: 3},
|
|
|
|
});
|
|
|
|
});
|