2020-02-04 17:02:28 +00:00
|
|
|
import { action, payload } from 'ts-action';
|
2020-01-28 15:57:28 +00:00
|
|
|
import u from 'updeep';
|
2019-10-23 21:54:37 +00:00
|
|
|
|
2020-02-04 17:02:28 +00:00
|
|
|
import Updux from '.';
|
|
|
|
|
2019-10-23 21:54:37 +00:00
|
|
|
const noopEffect = () => () => () => {};
|
|
|
|
|
2020-01-28 15:57:28 +00:00
|
|
|
test('actions defined in effects and mutations, multi-level', () => {
|
2020-02-04 17:02:28 +00:00
|
|
|
const bar = action('bar',(payload,meta) => ({payload,meta}) );
|
|
|
|
const foo = action('foo',(limit:number) => ({payload:{ limit} }) );
|
|
|
|
|
2020-01-28 15:57:28 +00:00
|
|
|
const {actions} = new Updux({
|
2020-02-04 17:02:28 +00:00
|
|
|
effects: [ [ foo, noopEffect ] ],
|
|
|
|
mutations: [ [ bar, () => () => null ] ],
|
2019-10-23 21:54:37 +00:00
|
|
|
subduxes: {
|
|
|
|
mysub: {
|
2020-01-28 15:57:28 +00:00
|
|
|
effects: {baz: noopEffect},
|
|
|
|
mutations: {quux: () => () => null},
|
2019-10-23 21:54:37 +00:00
|
|
|
actions: {
|
2020-02-04 17:02:28 +00:00
|
|
|
foo
|
2020-01-28 15:57:28 +00:00
|
|
|
},
|
2019-10-23 21:54:37 +00:00
|
|
|
},
|
|
|
|
myothersub: {
|
2020-02-04 17:02:28 +00:00
|
|
|
effects: [ [foo, noopEffect] ],
|
2020-01-28 15:57:28 +00:00
|
|
|
},
|
|
|
|
},
|
2019-10-23 21:54:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const types = Object.keys(actions);
|
|
|
|
types.sort();
|
|
|
|
|
2020-01-28 15:57:28 +00:00
|
|
|
expect(types).toEqual(['bar', 'baz', 'foo', 'quux']);
|
|
|
|
|
|
|
|
expect(actions.bar()).toEqual({type: 'bar'});
|
|
|
|
expect(actions.bar('xxx')).toEqual({type: 'bar', payload: 'xxx'});
|
2020-02-04 17:02:28 +00:00
|
|
|
expect(actions.bar(undefined, 'yyy')).toEqual({type: 'bar', payload: undefined, meta: 'yyy'});
|
2020-01-28 15:57:28 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-01-28 15:57:28 +00:00
|
|
|
test('string', () => {
|
2020-02-04 17:02:28 +00:00
|
|
|
updux.addAction( action('foo', payload() ));
|
2020-01-28 15:57:28 +00:00
|
|
|
expect(updux.actions.foo('yo')).toMatchObject({
|
|
|
|
type: 'foo',
|
|
|
|
payload: 'yo',
|
|
|
|
});
|
|
|
|
});
|
2019-10-23 21:54:37 +00:00
|
|
|
|
2020-01-28 15:57:28 +00:00
|
|
|
test('actionCreator inlined', () => {
|
2020-02-04 17:02:28 +00:00
|
|
|
updux.addAction( 'baz', (x) => ({payload: {x}}));
|
2020-01-28 15:57:28 +00:00
|
|
|
expect(updux.actions.baz(3)).toMatchObject({
|
|
|
|
type: 'baz', payload: { x: 3 }
|
|
|
|
});
|
|
|
|
});
|
2019-10-23 21:54:37 +00:00
|
|
|
});
|