updux/src/actions.test.js

82 lines
1.8 KiB
JavaScript

import { test, expect } from 'vitest';
import { action } from './actions.js';
import { Updux } from './Updux.js';
test('basic action', () => {
const foo = action('foo', (thing) => ({ thing }));
expect(foo('bar')).toEqual({
type: 'foo',
payload: {
thing: 'bar',
},
});
});
test('Updux config accepts actions', () => {
const foo = new Updux({
actions: {
one: action('one', (x) => ({ x })),
two: action('two', (x) => x),
},
});
expect(Object.keys(foo.actions)).toHaveLength(2);
expect(foo.actions.one).toBeTypeOf('function');
expect(foo.actions.one('potato')).toEqual({
type: 'one',
payload: {
x: 'potato',
},
});
});
test('subduxes actions', () => {
const foo = new Updux({
actions: {
foo: null,
},
subduxes: {
beta: {
actions: {
bar: null,
},
},
},
});
expect(foo.actions).toHaveProperty('foo');
expect(foo.actions).toHaveProperty('bar');
});
test('throw if double action', () => {
expect( () => new Updux({
actions: {
foo: action('foo'),
},
subduxes: {
beta: {
actions: {
foo: action('foo'),
},
},
},
}) ).toThrow(/action 'foo' already defined/);
});
test('action definition shortcut', () => {
const foo = new Updux({
actions: {
foo: null,
bar: x => ({x}),
},}
);
expect( foo.actions.foo("hello") ).toEqual({ type: 'foo', payload: 'hello' });
expect( foo.actions.bar("hello") ).toEqual({ type: 'bar', payload: {x:'hello'} });
});