104 lines
2.4 KiB
JavaScript
104 lines
2.4 KiB
JavaScript
|
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'] });
|
||
|
});
|
||
|
|
||
|
test('addMutation', async (t) => {
|
||
|
const foo = action('foo');
|
||
|
|
||
|
const dux = new Updux({
|
||
|
initial: '',
|
||
|
});
|
||
|
|
||
|
t.equal(dux.reducer(undefined, foo()), '', 'noop');
|
||
|
|
||
|
dux.addMutation('foo', () => () => 'foo');
|
||
|
|
||
|
t.equal(dux.reducer(undefined, foo()), 'foo', 'foo was added');
|
||
|
|
||
|
await t.test('name as function', async (t) => {
|
||
|
const bar = action('bar');
|
||
|
dux.addMutation(bar, () => () => 'bar');
|
||
|
|
||
|
t.equal(dux.reducer(undefined, bar()), 'bar', 'bar was added');
|
||
|
});
|
||
|
});
|