updux/src/selectors.test.ts

43 lines
1.0 KiB
TypeScript

import { test, expect } from 'vitest';
import Updux, { createAction } from './index.js';
test('basic selectors', () => {
type State = { x: number };
const foo = new Updux({
initialState: {
x: 1,
},
selectors: {
getX: ({ x }: State) => x,
},
subduxes: {
bar: new Updux({
initialState: { y: 2 },
selectors: {
getY: ({ y }: { y: number }) => y,
getYPlus:
({ y }) =>
(incr: number) =>
(y + incr) as number,
},
}),
},
});
const sample = {
x: 4,
bar: { y: 3 },
};
expect(foo.selectors.getY(sample)).toBe(3);
expect(foo.selectors.getX(sample)).toBe(4);
expect(foo.selectors.getYPlus(sample)(3)).toBe(6);
const store = foo.createStore();
expect(store.getState.getY()).toBe(2);
expect(store.getState.getYPlus(3)).toBe(5);
});