2021-10-07 16:04:15 +00:00
|
|
|
import { test } from 'tap';
|
|
|
|
|
|
|
|
import { Updux } from './Updux.js';
|
|
|
|
import { action } from './actions.js';
|
|
|
|
|
|
|
|
test('basic', async (t) => {
|
|
|
|
const doIt = action('doIt');
|
|
|
|
const thisToo = action('thisToo');
|
|
|
|
|
|
|
|
const dux = new Updux({
|
|
|
|
initial: '',
|
|
|
|
actions: { doIt, thisToo },
|
|
|
|
mutations: {
|
|
|
|
doIt: () => () => 'bingo',
|
|
|
|
thisToo: () => () => 'straight type',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
t.equal(dux.reducer(undefined, dux.actions.doIt()), 'bingo');
|
|
|
|
|
|
|
|
t.equal(dux.reducer(undefined, dux.actions.thisToo()), 'straight type');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('override', async (t) => {
|
|
|
|
const foo = action('foo');
|
|
|
|
|
|
|
|
const dux = new Updux({
|
|
|
|
initial: { alpha: [] },
|
|
|
|
mutations: {
|
|
|
|
'*': (payload, action) => (state) => ({
|
|
|
|
...state,
|
|
|
|
alpha: [...state.alpha, action.type],
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
subduxes: {
|
|
|
|
subbie: new Updux({
|
|
|
|
initial: 0,
|
|
|
|
actions: {
|
|
|
|
foo,
|
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
foo: () => (state) => state + 1,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
let state = [foo(), { type: 'bar' }].reduce(
|
|
|
|
(state, action) => dux.upreducer(action)(state),
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
|
|
|
|
t.match(state, {
|
|
|
|
alpha: ['foo', 'bar'],
|
|
|
|
subbie: 1,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('order of processing', async (t) => {
|
|
|
|
const foo = action('foo');
|
|
|
|
|
|
|
|
const dux = new Updux({
|
|
|
|
initial: {
|
|
|
|
x: [],
|
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
foo:
|
|
|
|
() =>
|
|
|
|
({ x }) => ({ x: [...x, 'main'] }),
|
|
|
|
},
|
|
|
|
subduxes: {
|
|
|
|
x: new Updux({
|
|
|
|
actions: { foo },
|
|
|
|
mutations: {
|
|
|
|
foo: () => (state) => [...state, 'subdux'],
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
t.same(dux.reducer(undefined, foo()), { x: ['subdux', 'main'] });
|
|
|
|
});
|
|
|
|
|
2021-10-12 22:13:59 +00:00
|
|
|
test('setMutation', async (t) => {
|
2021-10-07 16:04:15 +00:00
|
|
|
const foo = action('foo');
|
|
|
|
|
|
|
|
const dux = new Updux({
|
|
|
|
initial: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
t.equal(dux.reducer(undefined, foo()), '', 'noop');
|
|
|
|
|
2021-10-12 22:13:59 +00:00
|
|
|
dux.setMutation('foo', () => () => 'foo');
|
2021-10-07 16:04:15 +00:00
|
|
|
|
|
|
|
t.equal(dux.reducer(undefined, foo()), 'foo', 'foo was added');
|
|
|
|
|
|
|
|
await t.test('name as function', async (t) => {
|
|
|
|
const bar = action('bar');
|
2021-10-12 22:13:59 +00:00
|
|
|
dux.setMutation(bar, () => () => 'bar');
|
2021-10-07 16:04:15 +00:00
|
|
|
|
|
|
|
t.equal(dux.reducer(undefined, bar()), 'bar', 'bar was added');
|
|
|
|
});
|
|
|
|
});
|