2022-08-25 23:43:07 +00:00
|
|
|
import { test, expect } from 'vitest';
|
|
|
|
import schema from 'json-schema-shorthand';
|
|
|
|
import u from 'updeep';
|
|
|
|
|
|
|
|
import { action } from './actions.js';
|
|
|
|
|
2022-08-28 16:47:24 +00:00
|
|
|
import { Updux, dux } from './Updux.js';
|
2022-08-25 23:43:07 +00:00
|
|
|
|
|
|
|
test('set a mutation', () => {
|
|
|
|
const dux = new Updux({
|
|
|
|
initial: {
|
2022-08-26 00:06:52 +00:00
|
|
|
x: 'potato',
|
2022-08-25 23:43:07 +00:00
|
|
|
},
|
|
|
|
actions: {
|
2022-08-26 00:06:52 +00:00
|
|
|
foo: action('foo', (x) => ({ x })),
|
2022-08-25 23:43:07 +00:00
|
|
|
bar: action('bar'),
|
2022-08-26 00:06:52 +00:00
|
|
|
},
|
2022-08-25 23:43:07 +00:00
|
|
|
});
|
|
|
|
|
2022-08-28 16:47:24 +00:00
|
|
|
dux.setMutation(dux.actions.foo, (payload, action) => {
|
|
|
|
expect(payload).toEqual({ x: 'hello ' });
|
|
|
|
expect(action).toEqual(dux.actions.foo('hello '));
|
|
|
|
return u({
|
2022-08-26 00:06:52 +00:00
|
|
|
x: payload.x + action.type,
|
2022-08-28 16:47:24 +00:00
|
|
|
});
|
|
|
|
});
|
2022-08-25 23:43:07 +00:00
|
|
|
|
2022-08-26 00:06:52 +00:00
|
|
|
const result = dux.reducer(undefined, dux.actions.foo('hello '));
|
|
|
|
expect(result).toEqual({
|
|
|
|
x: 'hello foo',
|
|
|
|
});
|
2022-08-25 23:43:07 +00:00
|
|
|
});
|
2022-08-28 16:47:24 +00:00
|
|
|
|
|
|
|
test('mutation of a subdux', async () => {
|
|
|
|
const bar = dux({
|
|
|
|
actions: {
|
|
|
|
baz: null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
bar.setMutation('baz', () => (state) => ({ ...state, x: 1 }));
|
|
|
|
|
|
|
|
const foo = dux({
|
|
|
|
subduxes: { bar },
|
|
|
|
});
|
|
|
|
|
|
|
|
const store = foo.createStore();
|
|
|
|
store.dispatch.baz();
|
|
|
|
expect(store.getState()).toMatchObject({ bar: { x: 1 } });
|
|
|
|
});
|
|
|
|
|
|
|
|
test('strings and generators', async () => {
|
|
|
|
const actionA = action('a');
|
|
|
|
|
|
|
|
const foo = dux({
|
|
|
|
actions: {
|
|
|
|
b: null,
|
|
|
|
a: actionA,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// as a string and defined
|
|
|
|
expect(() => foo.setMutation('a', () => {})).not.toThrow();
|
|
|
|
|
|
|
|
// as a generator and defined
|
|
|
|
expect(() => foo.setMutation(actionA, () => {})).not.toThrow();
|
|
|
|
|
|
|
|
// as a string, not defined
|
|
|
|
expect(() => foo.setMutation('c', () => {})).toThrow();
|
|
|
|
|
|
|
|
foo.setMutation(action('d'), () => {});
|
|
|
|
|
|
|
|
expect(foo.actions.d).toBeTypeOf('function');
|
|
|
|
});
|