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