add the test
This commit is contained in:
parent
e4eff8a113
commit
2c26f9652b
@ -1,13 +1,14 @@
|
||||
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 => {
|
||||
const updux = dux({
|
||||
const updux = new Updux({
|
||||
subduxes: {
|
||||
bogeys: dux({
|
||||
bogeys: new Updux({
|
||||
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 => {
|
||||
const updux = dux<any, any>({
|
||||
const doIt = action('doIt');
|
||||
|
||||
const updux = new Updux({
|
||||
actions: { doIt },
|
||||
subduxes: {
|
||||
bogeys: dux({
|
||||
bogeys: new Updux({
|
||||
initial: { enkidu: 'foo' },
|
||||
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 => {
|
||||
const bar = dux({
|
||||
initial: { baz: 1 } as { baz: number },
|
||||
const bar = new Updux({
|
||||
initial: { baz: 1 },
|
||||
selectors: {
|
||||
getBaz: (state: { baz: number }) => state.baz,
|
||||
getBaz: (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.getMultBaz({ baz: 3 })(2), 6);
|
||||
|
||||
test('subduxes', async t => {
|
||||
const foo = dux({
|
||||
const foo = new Updux({
|
||||
subduxes: { bar },
|
||||
...coduxes( dux({}) ),
|
||||
selectors: {
|
||||
getRoot: () => 'root'
|
||||
}
|
||||
});
|
||||
|
||||
expectType<{
|
||||
({ bar: { baz: number } }): number;
|
||||
}>(foo.selectors.getBaz);
|
||||
|
||||
t.same(foo.selectors.getBaz(foo.initial), 1);
|
||||
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 => {
|
||||
const foo = dux({
|
||||
const foo = new Updux({
|
||||
subduxes: {
|
||||
quux: dux({}),
|
||||
bar: dux({
|
||||
quux: new Updux({}),
|
||||
bar: new Updux({
|
||||
selectors: {
|
||||
getBaz: () => 'baz'
|
||||
}
|
||||
@ -114,14 +109,14 @@ test('selector in mw', async () => {
|
||||
{
|
||||
initial: { stuff: 12 },
|
||||
subduxes: {
|
||||
bar: dux({
|
||||
bar: new Updux({
|
||||
initial: 'potato',
|
||||
selectors: { getBar: () => 'meh' }
|
||||
})
|
||||
},
|
||||
selectors: {
|
||||
// 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( '*', ({
|
||||
selectors, getState
|
||||
}) => () => () => {
|
||||
expectType<DuxState<typeof myDux>>( getState() );
|
||||
expectType<(...args:any[]) => number>(selectors.getStuff);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user