updux/src/actions.test.js

87 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-08-25 18:19:45 +00:00
import { test, expect } from 'vitest';
import { action } from './actions.js';
2022-08-25 20:36:37 +00:00
import { Updux } from './Updux.js';
2022-08-25 18:19:45 +00:00
test('basic action', () => {
2022-08-25 23:43:07 +00:00
const foo = action('foo', (thing) => ({ thing }));
2022-08-25 18:19:45 +00:00
expect(foo('bar')).toEqual({
type: 'foo',
payload: {
thing: 'bar',
},
});
});
2022-08-25 20:36:37 +00:00
2022-08-26 00:06:52 +00:00
test('Updux config accepts actions', () => {
2022-08-25 20:36:37 +00:00
const foo = new Updux({
actions: {
2022-08-26 00:06:52 +00:00
one: action('one', (x) => ({ x })),
two: action('two', (x) => x),
},
2022-08-25 20:36:37 +00:00
});
expect(Object.keys(foo.actions)).toHaveLength(2);
2022-08-26 00:06:52 +00:00
expect(foo.actions.one).toBeTypeOf('function');
expect(foo.actions.one('potato')).toEqual({
2022-08-25 20:36:37 +00:00
type: 'one',
payload: {
2022-08-26 00:06:52 +00:00
x: 'potato',
},
2022-08-25 20:36:37 +00:00
});
2022-08-26 00:06:52 +00:00
});
2022-08-25 20:36:37 +00:00
2022-08-26 00:06:52 +00:00
test('subduxes actions', () => {
const foo = new Updux({
actions: {
foo: null,
},
subduxes: {
beta: {
actions: {
bar: null,
},
},
},
});
2022-08-25 20:36:37 +00:00
2022-08-26 00:06:52 +00:00
expect(foo.actions).toHaveProperty('foo');
expect(foo.actions).toHaveProperty('bar');
});
2022-08-26 18:41:22 +00:00
test('throw if double action', () => {
2022-08-26 19:30:03 +00:00
expect(
() =>
new Updux({
2022-08-26 18:41:22 +00:00
actions: {
foo: action('foo'),
},
2022-08-26 19:30:03 +00:00
subduxes: {
beta: {
actions: {
foo: action('foo'),
},
},
},
}),
).toThrow(/action 'foo' already defined/);
2022-08-26 18:41:22 +00:00
});
2022-08-26 19:29:24 +00:00
test('action definition shortcut', () => {
const foo = new Updux({
actions: {
foo: null,
2022-08-26 19:30:03 +00:00
bar: (x) => ({ x }),
},
});
2022-08-26 19:29:24 +00:00
2022-08-26 19:30:03 +00:00
expect(foo.actions.foo('hello')).toEqual({ type: 'foo', payload: 'hello' });
expect(foo.actions.bar('hello')).toEqual({
type: 'bar',
payload: { x: 'hello' },
});
2022-08-26 19:29:24 +00:00
});