updux/src/selectors.test.ts

56 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-01-03 15:37:21 +00:00
import Updux from '.';
test('basic selectors', () => {
const updux = new Updux({
subduxes: {
bogeys: {
selectors: {
bogey: (bogeys: any) => (id: string) => bogeys[id],
},
},
},
selectors: {
bogeys: ({ bogeys }: any) => bogeys,
},
2020-01-03 22:27:11 +00:00
});
2020-01-03 15:37:21 +00:00
const state = {
bogeys: {
foo: 1,
2020-01-04 00:40:20 +00:00
bar: 2,
},
2020-01-03 15:37:21 +00:00
};
2020-01-04 00:40:20 +00:00
expect(updux.selectors.bogeys(state)).toEqual({ foo: 1, bar: 2 });
expect((updux.selectors.bogey(state) as any)('foo')).toEqual(1);
});
test('available in the middleware', () => {
const updux = new Updux({
subduxes: {
bogeys: {
initial: { enkidu: 'foo' },
selectors: {
bogey: (bogeys: any) => (id: string) => bogeys[id],
},
},
},
effects: {
doIt: ({ selectors: { bogey }, getState }) => next => action => {
next({
...action,
payload: bogey(getState())('enkidu'),
});
},
},
mutations: {
doIt: payload => state => ({ ...state, payload }),
},
});
const store = updux.createStore();
store.dispatch.doIt();
2020-01-03 15:37:21 +00:00
2020-01-04 00:40:20 +00:00
expect(store.getState()).toMatchObject({ payload: 'foo' });
2020-01-03 15:37:21 +00:00
});