2023-09-06 19:09:45 +00:00
|
|
|
import { createAction } from '@reduxjs/toolkit';
|
2023-03-09 15:41:15 +00:00
|
|
|
import { test, expect } from 'vitest';
|
2023-09-06 19:09:45 +00:00
|
|
|
import Updux from './Updux.js';
|
2023-03-09 15:41:15 +00:00
|
|
|
|
|
|
|
test('set a mutation', () => {
|
|
|
|
const dux = new Updux({
|
2023-03-23 22:15:32 +00:00
|
|
|
initialState: 'potato',
|
2023-03-09 15:41:15 +00:00
|
|
|
actions: {
|
2023-09-06 19:09:45 +00:00
|
|
|
foo: (x: string) => ({ x }),
|
2024-08-08 13:28:44 +00:00
|
|
|
bar: null,
|
2023-03-09 15:41:15 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
let didIt = false;
|
|
|
|
|
|
|
|
dux.addMutation(dux.actions.foo, (payload, action) => () => {
|
2023-09-06 19:09:45 +00:00
|
|
|
expectTypeOf(payload).toMatchTypeOf<{ x: string }>();
|
|
|
|
|
2023-03-09 15:41:15 +00:00
|
|
|
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({
|
2023-03-23 22:15:32 +00:00
|
|
|
initialState: '',
|
2023-03-09 15:41:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
dux.addMutation(
|
|
|
|
() => true,
|
|
|
|
(payload, action) => () => 'got it',
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(dux.reducer(undefined, { type: 'foo' })).toEqual('got it');
|
|
|
|
});
|
2023-03-09 15:59:00 +00:00
|
|
|
|
|
|
|
test('default mutation', () => {
|
|
|
|
const dux = new Updux({
|
2023-03-23 22:15:32 +00:00
|
|
|
initialState: '',
|
2023-03-09 15:59:00 +00:00
|
|
|
actions: {
|
2024-08-08 13:28:44 +00:00
|
|
|
foo: null,
|
2023-03-09 15:59:00 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
dux.addMutation(dux.actions.foo, () => () => 'got it');
|
|
|
|
|
2024-08-08 13:28:44 +00:00
|
|
|
dux.setDefaultMutation((_payload, action) => () => action.type);
|
2023-03-09 15:59:00 +00:00
|
|
|
|
|
|
|
expect(dux.reducer(undefined, { type: 'foo' })).toEqual('got it');
|
|
|
|
expect(dux.reducer(undefined, { type: 'bar' })).toEqual('bar');
|
|
|
|
});
|
2023-03-09 19:48:11 +00:00
|
|
|
|
|
|
|
test('mutation of a subdux', () => {
|
|
|
|
const baz = createAction('baz');
|
2023-03-09 20:13:13 +00:00
|
|
|
const noop = createAction('noop');
|
|
|
|
const stopit = createAction('stopit');
|
2023-03-09 19:48:11 +00:00
|
|
|
|
|
|
|
const bar = new Updux({
|
2023-03-23 22:15:32 +00:00
|
|
|
initialState: 0,
|
2023-03-09 19:48:11 +00:00
|
|
|
actions: {
|
|
|
|
baz,
|
2023-03-09 20:13:13 +00:00
|
|
|
stopit,
|
2023-03-09 19:48:11 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
bar.addMutation(baz, () => () => 1);
|
2023-03-09 20:13:13 +00:00
|
|
|
bar.addMutation(stopit, () => () => 2);
|
2023-03-09 19:48:11 +00:00
|
|
|
|
|
|
|
const foo = new Updux({
|
|
|
|
subduxes: { bar },
|
|
|
|
});
|
2023-03-09 20:13:13 +00:00
|
|
|
foo.addMutation(stopit, () => (state) => state, true);
|
2023-03-09 19:48:11 +00:00
|
|
|
|
2023-03-09 20:13:13 +00:00
|
|
|
expect(foo.reducer(undefined, noop())).toHaveProperty('bar', 0);
|
2023-03-09 19:48:11 +00:00
|
|
|
expect(foo.reducer(undefined, baz())).toHaveProperty('bar', 1);
|
2023-03-09 20:13:13 +00:00
|
|
|
expect(foo.reducer(undefined, stopit())).toHaveProperty('bar', 0);
|
2023-03-09 19:48:11 +00:00
|
|
|
});
|
2023-09-06 19:09:45 +00:00
|
|
|
|
|
|
|
test('actionType as string', () => {
|
|
|
|
const dux = new Updux({
|
|
|
|
actions: {
|
|
|
|
doIt: (id: number) => id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
dux.addMutation('doIt', (payload) => (state) => {
|
|
|
|
expectTypeOf(payload).toMatchTypeOf<number>();
|
|
|
|
return state;
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
// @ts-ignore
|
|
|
|
dux.addMutation('unknown', () => (x) => x);
|
|
|
|
}).toThrow();
|
|
|
|
});
|