updux/src/actions.test.ts

66 lines
1.5 KiB
TypeScript
Raw Normal View History

import Updux, {actionCreator} from '.';
import u from 'updeep';
2019-10-23 21:54:37 +00:00
const noopEffect = () => () => () => {};
test('actions defined in effects and mutations, multi-level', () => {
const {actions} = new Updux({
2019-10-23 21:54:37 +00:00
effects: {
foo: noopEffect,
2019-10-23 21:54:37 +00:00
},
mutations: {bar: () => () => null},
2019-10-23 21:54:37 +00:00
subduxes: {
mysub: {
effects: {baz: noopEffect},
mutations: {quux: () => () => null},
2019-10-23 21:54:37 +00:00
actions: {
foo: (limit: number) => ({limit}),
},
2019-10-23 21:54:37 +00:00
},
myothersub: {
effects: {
foo: noopEffect,
},
},
},
2019-10-23 21:54:37 +00:00
});
const types = Object.keys(actions);
types.sort();
expect(types).toEqual(['bar', 'baz', 'foo', 'quux']);
expect(actions.bar()).toEqual({type: 'bar'});
expect(actions.bar('xxx')).toEqual({type: 'bar', payload: 'xxx'});
expect(actions.bar(undefined, 'yyy')).toEqual({type: 'bar', meta: 'yyy'});
expect(actions.foo(12)).toEqual({type: 'foo', payload: {limit: 12}});
});
describe('different calls to addAction', () => {
const updux = new Updux();
2019-10-23 21:54:37 +00:00
test('string', () => {
updux.addAction('foo');
expect(updux.actions.foo('yo')).toMatchObject({
type: 'foo',
payload: 'yo',
});
});
2019-10-23 21:54:37 +00:00
test('actionCreator', () => {
const bar = actionCreator('bar', null);
updux.addAction(bar);
expect(updux.actions.bar()).toMatchObject({
type: 'bar',
});
});
test('actionCreator inlined', () => {
updux.addAction('baz', (x) => ({x}));
expect(updux.actions.baz(3)).toMatchObject({
type: 'baz', payload: { x: 3 }
});
});
2019-10-23 21:54:37 +00:00
});