tests are passing
This commit is contained in:
parent
f744616cb2
commit
931377b584
@ -10,6 +10,7 @@ import {
|
|||||||
UpduxMiddlewareAPI,
|
UpduxMiddlewareAPI,
|
||||||
EffectEntry
|
EffectEntry
|
||||||
} from "../types";
|
} from "../types";
|
||||||
|
import Updux from "..";
|
||||||
|
|
||||||
const MiddlewareFor = (
|
const MiddlewareFor = (
|
||||||
type: any,
|
type: any,
|
||||||
@ -23,12 +24,13 @@ const MiddlewareFor = (
|
|||||||
|
|
||||||
type Next = (action: Action) => any;
|
type Next = (action: Action) => any;
|
||||||
|
|
||||||
function sliceMw(slice: string, mw: Middleware): Middleware {
|
function sliceMw(slice: string, mw: Middleware, updux: Updux): Middleware {
|
||||||
return api => {
|
return api => {
|
||||||
const getSliceState =
|
const getSliceState =
|
||||||
slice.length > 0 ? () => fp.get(slice, api.getState()) : api.getState;
|
slice.length > 0 ? () => fp.get(slice, api.getState()) : api.getState;
|
||||||
const getRootState = (api as any).getRootState || api.getState;
|
const getRootState = (api as any).getRootState || api.getState;
|
||||||
return mw({ ...api, getState: getSliceState, getRootState } as any);
|
return mw({ ...api, getState: getSliceState, getRootState,
|
||||||
|
selectors: updux.selectors } as any);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,11 +39,11 @@ function buildMiddleware<S = any>(
|
|||||||
actions: Dictionary<ActionCreator> = {}
|
actions: Dictionary<ActionCreator> = {}
|
||||||
): UpduxMiddleware<S> {
|
): UpduxMiddleware<S> {
|
||||||
let mws = middlewareEntries
|
let mws = middlewareEntries
|
||||||
.map(([slice, actionType, mw, isGen]: any) =>
|
.map(([updux, slice, actionType, mw, isGen]: any) =>
|
||||||
isGen ? [slice, actionType, mw()] : [slice, actionType, mw]
|
isGen ? [updux, slice, actionType, mw()] : [updux, slice, actionType, mw]
|
||||||
)
|
)
|
||||||
.map(([slice, actionType, mw]) =>
|
.map(([updux, slice, actionType, mw]) =>
|
||||||
MiddlewareFor(actionType, sliceMw(slice, mw))
|
MiddlewareFor(actionType, sliceMw(slice, mw, updux))
|
||||||
);
|
);
|
||||||
|
|
||||||
return (api: UpduxMiddlewareAPI<S>) => {
|
return (api: UpduxMiddlewareAPI<S>) => {
|
||||||
|
@ -17,11 +17,39 @@ test('basic selectors', () => {
|
|||||||
const state = {
|
const state = {
|
||||||
bogeys: {
|
bogeys: {
|
||||||
foo: 1,
|
foo: 1,
|
||||||
bar: 2
|
bar: 2,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
expect( updux.selectors.bogeys(state) ).toEqual( { foo:1, bar :2 } );
|
expect(updux.selectors.bogeys(state)).toEqual({ foo: 1, bar: 2 });
|
||||||
expect( (updux.selectors.bogey(state) as any)('foo')).toEqual(1);
|
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();
|
||||||
|
|
||||||
|
expect(store.getState()).toMatchObject({ payload: 'foo' });
|
||||||
});
|
});
|
||||||
|
@ -200,9 +200,10 @@ export interface UpduxMiddlewareAPI<S> {
|
|||||||
dispatch: UpduxDispatch;
|
dispatch: UpduxDispatch;
|
||||||
getState(): any;
|
getState(): any;
|
||||||
getRootState(): S;
|
getRootState(): S;
|
||||||
|
selectors: Dictionary<Selector>;
|
||||||
}
|
}
|
||||||
export type UpduxMiddleware<S = any> = (
|
export type UpduxMiddleware<S = any> = (
|
||||||
api: UpduxMiddlewareAPI<S>
|
api: UpduxMiddlewareAPI<S>
|
||||||
) => (next: UpduxDispatch) => (action: Action) => any;
|
) => (next: UpduxDispatch) => (action: Action) => any;
|
||||||
|
|
||||||
export type Selector<S = any> = (state:S) => unknown;
|
export type Selector<S = any> = (state:S) => any;
|
||||||
|
@ -200,22 +200,21 @@ export class Updux<S = any> {
|
|||||||
get _middlewareEntries() {
|
get _middlewareEntries() {
|
||||||
const groupByOrder = (mws: any) =>
|
const groupByOrder = (mws: any) =>
|
||||||
fp.groupBy(
|
fp.groupBy(
|
||||||
([_, actionType]: any) =>
|
([a,b, actionType]: any) =>
|
||||||
["^", "$"].includes(actionType) ? actionType : "middle",
|
["^", "$"].includes(actionType) ? actionType : "middle",
|
||||||
mws
|
mws
|
||||||
);
|
);
|
||||||
|
|
||||||
let subs = fp.flow([
|
let subs = fp.flow([
|
||||||
fp.mapValues("_middlewareEntries"),
|
|
||||||
fp.toPairs,
|
fp.toPairs,
|
||||||
fp.map(([slice, entries]) =>
|
fp.map(([slice, updux]) =>
|
||||||
entries.map(([ps, ...args]: any) => [[slice, ...ps], ...args])
|
updux._middlewareEntries.map(([u, ps, ...args]: any) => [u,[slice, ...ps], ...args])
|
||||||
),
|
),
|
||||||
fp.flatten,
|
fp.flatten,
|
||||||
groupByOrder
|
groupByOrder
|
||||||
])(this.subduxes);
|
])(this.subduxes);
|
||||||
|
|
||||||
let local = groupByOrder(this.localEffects.map(x => [[], ...x]));
|
let local = groupByOrder(this.localEffects.map(x => [this,[], ...x]));
|
||||||
|
|
||||||
return fp.flatten(
|
return fp.flatten(
|
||||||
[
|
[
|
||||||
|
Loading…
Reference in New Issue
Block a user