mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-31 03:02:04 +08:00
redux-testkit defines
This commit is contained in:
44
types/redux-testkit/index.d.ts
vendored
44
types/redux-testkit/index.d.ts
vendored
@@ -7,39 +7,37 @@
|
||||
import * as Redux from 'redux';
|
||||
import { ThunkAction } from 'redux-thunk';
|
||||
|
||||
export function Reducer(action: Redux.Reducer<any>): {
|
||||
withState(state: any): {
|
||||
expect: (action: Redux.Action) => {
|
||||
toReturnState(expected: any): any,
|
||||
toStayTheSame(): any;
|
||||
toChangeInState(expectedChanges: any): any;
|
||||
},
|
||||
execute(action: Redux.Action): any;
|
||||
};
|
||||
|
||||
export interface ReducerTestkit {
|
||||
expect: (action: Redux.Action) => {
|
||||
toReturnState(expected: any): any,
|
||||
toStayTheSame(): any;
|
||||
toChangeInState(expectedChanges: any): any;
|
||||
toReturnState(expected: any): void,
|
||||
toStayTheSame(): void;
|
||||
toChangeInState(expectedChanges: any): void;
|
||||
};
|
||||
execute(action: Redux.Action): any;
|
||||
execute(action: Redux.Action): any; // state
|
||||
}
|
||||
|
||||
export interface ThunkTestkit {
|
||||
execute(...args: any[]): any;
|
||||
}
|
||||
|
||||
export function Reducer(action: Redux.Reducer<any>): ReducerTestkit & {
|
||||
withState(state: any): ReducerTestkit;
|
||||
};
|
||||
|
||||
export function Selector(selector: (state: any, action: any) => any): {
|
||||
expect(state: any, ...args: any[]): any;
|
||||
expect(state: any, ...args: any[]): {
|
||||
toReturn(expected: any): void;
|
||||
};
|
||||
execute(state: any, ...args: any[]): any;
|
||||
};
|
||||
|
||||
export function Thunk(thunkFunc: ThunkAction<any, any, any>, extraArg?: any): {
|
||||
execute(...args: any[]): any;
|
||||
withState(state: any): {
|
||||
execute(...args: any[]): any;
|
||||
}
|
||||
export function Thunk(thunkFunc: ThunkAction<any, any, any>, extraArg?: any): ThunkTestkit & {
|
||||
withState(state: any): ThunkTestkit;
|
||||
};
|
||||
|
||||
export namespace FlushThunks {
|
||||
function createMiddleware(): {
|
||||
flush(): any;
|
||||
reset(): any;
|
||||
function createMiddleware(): Redux.Middleware & {
|
||||
flush(): void;
|
||||
reset(): void;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Reducer, Selector, FlushThunks, Thunk } from 'redux-testkit';
|
||||
import { Action, Dispatch } from 'redux';
|
||||
import { Action, Dispatch, createStore, applyMiddleware } from 'redux';
|
||||
import thunk from 'redux-thunk';
|
||||
|
||||
interface SimpleState {
|
||||
currentState: string;
|
||||
@@ -42,6 +43,14 @@ const getNumbers = (state: SimpleState = simpleState, type: string): number[] =>
|
||||
});
|
||||
};
|
||||
|
||||
const reducer = (state = { count: 0 }, action: Action): any => {
|
||||
if (action.type === 'count') {
|
||||
state.count++;
|
||||
}
|
||||
|
||||
return state;
|
||||
};
|
||||
|
||||
const thunkAction1: Action = {
|
||||
type: TO_FINISH_STATE
|
||||
};
|
||||
@@ -50,7 +59,7 @@ const thunkAction2: Action = {
|
||||
type: TO_INITIAL_STATE
|
||||
};
|
||||
|
||||
const thunk = () => {
|
||||
const thunkAction = () => {
|
||||
return (dispatch: Dispatch<Action>): void => {
|
||||
dispatch(thunkAction1);
|
||||
dispatch(thunkAction2);
|
||||
@@ -64,5 +73,12 @@ Reducer(simpleAction).expect({ type: TO_INITIAL_STATE }).toReturnState(simpleSta
|
||||
Selector(getNumbers).expect(simpleState, EVEN_NUMBERS).toReturn([2, 4, 6, 8]);
|
||||
Selector(getNumbers).execute(simpleState, ODD_NUMBERS);
|
||||
|
||||
Thunk(thunk).execute();
|
||||
Thunk(thunk).withState(simpleState).execute();
|
||||
Thunk(thunkAction).execute();
|
||||
Thunk(thunkAction).withState(simpleState).execute();
|
||||
|
||||
const flushThunks = FlushThunks.createMiddleware();
|
||||
|
||||
const store = createStore(reducer, applyMiddleware(flushThunks, thunk));
|
||||
|
||||
flushThunks.flush();
|
||||
flushThunks.reset();
|
||||
|
||||
Reference in New Issue
Block a user