updux/dist/actions.test.js

75 lines
2.4 KiB
JavaScript

import { buildActions, createAction, withPayload } from './actions.js';
import Updux from './index.js';
test('basic action', () => {
const foo = createAction('foo', withPayload((thing) => ({ thing })));
expect(foo('bar')).toEqual({
type: 'foo',
payload: {
thing: 'bar',
},
});
expectTypeOf(foo).parameters.toMatchTypeOf();
expectTypeOf(foo).returns.toMatchTypeOf();
});
test('withPayload, just the type', () => {
const foo = createAction('foo', withPayload());
expect(foo('bar')).toEqual({
type: 'foo',
payload: 'bar',
});
expectTypeOf(foo).parameters.toMatchTypeOf();
expectTypeOf(foo).returns.toMatchTypeOf();
});
test('buildActions', () => {
const actions = buildActions({
one: createAction('one'),
two: (x) => x,
withoutValue: null,
}, { a: { actions: buildActions({ three: () => 3 }) } });
expect(actions.one()).toEqual({
type: 'one',
});
expectTypeOf(actions.one()).toMatchTypeOf();
expect(actions.two('potato')).toEqual({ type: 'two', payload: 'potato' });
expectTypeOf(actions.two('potato')).toMatchTypeOf();
expect(actions.three()).toEqual({ type: 'three', payload: 3 });
expectTypeOf(actions.three()).toMatchTypeOf();
expect(actions.withoutValue()).toEqual({ type: 'withoutValue' });
expectTypeOf(actions.withoutValue()).toMatchTypeOf();
});
describe('Updux interactions', () => {
var _a;
const dux = new Updux({
initialState: { a: 3 },
actions: {
add: (x) => x,
withNull: null,
},
subduxes: {
subdux1: new Updux({
actions: {
fromSubdux: (x) => x,
},
}),
subdux2: {
actions: {
ohmy: () => { },
},
},
},
});
test('actions getter', () => {
expect(dux.actions.add).toBeTruthy();
});
expectTypeOf(dux.actions.withNull).toMatchTypeOf();
expect(dux.actions.withNull()).toMatchObject({
type: 'withNull',
});
expectTypeOf(dux.actions).toMatchTypeOf();
expectTypeOf((_a = dux.actions) === null || _a === void 0 ? void 0 : _a.add).not.toEqualTypeOf();
expect(dux.actions.fromSubdux('payload')).toEqual({
type: 'fromSubdux',
payload: 'payload',
});
});