add the test

This commit is contained in:
Yanick Champoux 2021-10-08 11:46:19 -04:00
parent e4eff8a113
commit 2c26f9652b

View File

@ -1,13 +1,14 @@
import { test } from 'tap'; import { test } from 'tap';
import Updux, { dux, coduxes, DuxState } from '.';
import { expectType } from 'tsd'; import { Updux } from './Updux.js';
import { action } from './actions.js';
test('basic selectors', async t => { test('basic selectors', async t => {
const updux = dux({ const updux = new Updux({
subduxes: { subduxes: {
bogeys: dux({ bogeys: new Updux({
selectors: { selectors: {
bogey: (bogeys: any) => (id: string) => bogeys[id], bogey: (bogeys) => (id) => bogeys[id],
}, },
}), }),
}, },
@ -28,12 +29,15 @@ test('basic selectors', async t => {
}); });
test('available in the middleware', async t => { test('available in the middleware', async t => {
const updux = dux<any, any>({ const doIt = action('doIt');
const updux = new Updux({
actions: { doIt },
subduxes: { subduxes: {
bogeys: dux({ bogeys: new Updux({
initial: { enkidu: 'foo' }, initial: { enkidu: 'foo' },
selectors: { selectors: {
bogey: (bogeys: any) => (id: string) => bogeys[id], bogey: (bogeys) => (id) => bogeys[id],
}, },
}), }),
}, },
@ -57,36 +61,27 @@ test('available in the middleware', async t => {
}); });
test('selector typescript', async t => { test('selector typescript', async t => {
const bar = dux({ const bar = new Updux({
initial: { baz: 1 } as { baz: number }, initial: { baz: 1 },
selectors: { selectors: {
getBaz: (state: { baz: number }) => state.baz, getBaz: (state) => state.baz,
getStringBaz: state => `${state.baz}`, getStringBaz: state => `${state.baz}`,
getMultBaz: state => (mult: number) => state.baz * mult, getMultBaz: state => (mult) => state.baz * mult,
}, },
}); });
expectType<{
getBaz: Function;
getStringBaz: Function;
}>(bar.selectors);
t.same(bar.selectors.getBaz(bar.initial), 1); t.same(bar.selectors.getBaz(bar.initial), 1);
t.same(bar.selectors.getMultBaz({ baz: 3 })(2), 6); t.same(bar.selectors.getMultBaz({ baz: 3 })(2), 6);
test('subduxes', async t => { test('subduxes', async t => {
const foo = dux({ const foo = new Updux({
subduxes: { bar }, subduxes: { bar },
...coduxes( dux({}) ),
selectors: { selectors: {
getRoot: () => 'root' getRoot: () => 'root'
} }
}); });
expectType<{
({ bar: { baz: number } }): number;
}>(foo.selectors.getBaz);
t.same(foo.selectors.getBaz(foo.initial), 1); t.same(foo.selectors.getBaz(foo.initial), 1);
t.same(foo.selectors.getMultBaz({ bar: { baz: 3 } })(2), 6); t.same(foo.selectors.getMultBaz({ bar: { baz: 3 } })(2), 6);
@ -94,10 +89,10 @@ test('selector typescript', async t => {
}); });
test('no root selector', async t => { test('no root selector', async t => {
const foo = dux({ const foo = new Updux({
subduxes: { subduxes: {
quux: dux({}), quux: new Updux({}),
bar: dux({ bar: new Updux({
selectors: { selectors: {
getBaz: () => 'baz' getBaz: () => 'baz'
} }
@ -114,14 +109,14 @@ test('selector in mw', async () => {
{ {
initial: { stuff: 12 }, initial: { stuff: 12 },
subduxes: { subduxes: {
bar: dux({ bar: new Updux({
initial: 'potato', initial: 'potato',
selectors: { getBar: () => 'meh' } selectors: { getBar: () => 'meh' }
}) })
}, },
selectors: { selectors: {
// TODO here we should auto-populate the state // TODO here we should auto-populate the state
getStuff: (state: {stuff: number}) => state.stuff getStuff: (state) => state.stuff
} }
} }
); );
@ -129,7 +124,5 @@ test('selector in mw', async () => {
myDux.addEffect( '*', ({ myDux.addEffect( '*', ({
selectors, getState selectors, getState
}) => () => () => { }) => () => () => {
expectType<DuxState<typeof myDux>>( getState() );
expectType<(...args:any[]) => number>(selectors.getStuff);
}); });
}); });