redux-testkit defines

This commit is contained in:
akizimov
2018-05-04 16:54:10 +03:00
parent 32050c3814
commit ecbd9b2da8
2 changed files with 41 additions and 27 deletions

View File

@@ -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;
};
}

View File

@@ -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();