updux/dist/selectors.test.js

48 lines
1.4 KiB
JavaScript

import { test, expect } from 'vitest';
import Updux from './index.js';
describe('basic selectors', () => {
const config = {
initialState: {
x: 1,
},
selectors: {
getX: ({ x }) => x,
},
subduxes: {
bar: new Updux({
initialState: { y: 2 },
selectors: {
getY: ({ y }) => y,
getYPlus: ({ y }) => (incr) => (y + incr),
},
}),
// cause the type to fail
baz: new Updux({
selectors: {
getFromBaz: () => 'potato',
},
}),
},
};
const foo = new Updux(config);
const sample = {
x: 4,
bar: { y: 3 },
};
test('updux selectors', () => {
expect(foo.selectors.getX(sample)).toBe(4);
expect(foo.selectors.getY(sample)).toBe(3);
expect(foo.selectors.getYPlus(sample)(3)).toBe(6);
});
test('store selectors', () => {
const store = foo.createStore();
expect(store.getState.getY()).toBe(2);
expect(store.getState.getYPlus(3)).toBe(5);
});
test('addSelector', () => {
const dux = new Updux({ initialState: 13 }).addSelector('plusHundred', (state) => state + 100);
expectTypeOf(dux.selectors.plusHundred).toMatchTypeOf();
expect(dux.selectors.plusHundred(7)).toBe(107);
});
});