56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import { test, expect } from 'vitest';
|
|
|
|
import Updux from './index.js';
|
|
|
|
test('set a mutation', () => {
|
|
const dux = new Updux({
|
|
initial: 'potato',
|
|
actions: {
|
|
foo: (x) => ({ x }),
|
|
bar: 0,
|
|
},
|
|
});
|
|
|
|
let didIt = false;
|
|
|
|
dux.addMutation(dux.actions.foo, (payload, action) => () => {
|
|
didIt = true;
|
|
expect(payload).toEqual({ x: 'hello ' });
|
|
expect(action).toEqual(dux.actions.foo('hello '));
|
|
return payload.x + action.type;
|
|
});
|
|
|
|
const result = dux.reducer(undefined, dux.actions.foo('hello '));
|
|
expect(didIt).toBeTruthy();
|
|
expect(result).toEqual('hello foo');
|
|
});
|
|
|
|
test('catch-all mutation', () => {
|
|
const dux = new Updux({
|
|
initial: '',
|
|
});
|
|
|
|
dux.addMutation(
|
|
() => true,
|
|
(payload, action) => () => 'got it',
|
|
);
|
|
|
|
expect(dux.reducer(undefined, { type: 'foo' })).toEqual('got it');
|
|
});
|
|
|
|
test('default mutation', () => {
|
|
const dux = new Updux({
|
|
initial: '',
|
|
actions: {
|
|
foo: 0,
|
|
},
|
|
});
|
|
|
|
dux.addMutation(dux.actions.foo, () => () => 'got it');
|
|
|
|
dux.addDefaultMutation((_payload, action) => () => action.type);
|
|
|
|
expect(dux.reducer(undefined, { type: 'foo' })).toEqual('got it');
|
|
expect(dux.reducer(undefined, { type: 'bar' })).toEqual('bar');
|
|
});
|