redux-testkit defines

This commit is contained in:
akizimov
2018-05-04 15:43:28 +03:00
parent 05bfe66185
commit 32050c3814
4 changed files with 61 additions and 78 deletions
+36 -60
View File
@@ -2,68 +2,44 @@
// Project: https://github.com/wix/redux-testkit#readme
// Definitions by: Andrey Kizimov <https://github.com/Bookler96>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
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;
};
interface ReducerMainCommands {
expect(action: Redux.AnyAction): InternalReducerCommands;
execute(action: Redux.AnyAction): any;
expect: (action: Redux.Action) => {
toReturnState(expected: any): any,
toStayTheSame(): any;
toChangeInState(expectedChanges: any): any;
};
execute(action: Redux.Action): any;
};
export function Selector(selector: (state: any, action: any) => any): {
expect(state: any, ...args: any[]): any;
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 namespace FlushThunks {
function createMiddleware(): {
flush(): any;
reset(): any;
};
}
interface InternalReducerCommands {
toReturnState(expected: Redux.AnyAction): any;
toStayTheSame(): any;
toChangeInState(expectedChanges: any): any;
}
interface ReducerAction extends ReducerMainCommands {
withState(state: any): ReducerMainCommands;
}
//-----------------------------------------------------------------------
interface SelectorExpect {
toReturn(expected: any): any;
}
interface SelectorAction {
expect(state: any, ...arg: any[]): SelectorExpect;
execute(state: any, ...arg: any[]): any;
}
//-----------------------------------------------------------------------
interface DispatchObject {
isFunction(): boolean,
isPlainObject(): boolean,
getType(): any,
getAction(): any,
getName(): any
}
interface ThunkMainCommands {
execute(...arg: any[]): DispatchObject[];
}
interface ThunkAction extends ThunkMainCommands {
withState(state: any): ThunkMainCommands;
}
//-----------------------------------------------------------------------
interface FlushThunksMiddleware {
flush(): void;
reset(): void;
}
export class FlushThunks {
static createMiddleware(): FlushThunksMiddleware;
}
//-----------------------------------------------------------------------
export function Reducer(action: Redux.Reducer<any>): ReducerAction;
export function Selector(action: Redux.Reducer<any>): SelectorAction;
export function Thunk(action: Redux.Reducer<any>): ThunkAction;
+7
View File
@@ -0,0 +1,7 @@
{
"private": true,
"dependencies": {
"redux": "^3.6.0",
"redux-thunk": "^2.2.0"
}
}
+17 -18
View File
@@ -1,5 +1,5 @@
import {Reducer, Selector, FlushThunks, Thunk} from 'redux-testkit';
import {Action, Dispatch} from 'redux';
import { Reducer, Selector, FlushThunks, Thunk } from 'redux-testkit';
import { Action, Dispatch } from 'redux';
interface SimpleState {
currentState: string;
@@ -8,11 +8,8 @@ interface SimpleState {
const TO_FINISH_STATE = 'TO_FINISH_STATE';
const TO_INITIAL_STATE = 'TO_INITIAL_STATE';
enum NumberType {
ODD = 'ODD',
EVEN = 'EVEN'
}
const ODD_NUMBERS = 'ODD_NUMBERS';
const EVEN_NUMBERS = 'EVEN_NUMBERS';
const simpleState: SimpleState = {
currentState: "initial",
@@ -23,21 +20,23 @@ const simpleAction = (state: SimpleState = simpleState, action: Action): SimpleS
if (action.type === TO_FINISH_STATE) {
return {...state, currentState: 'finish'};
}
else if (action.type === TO_INITIAL_STATE) {
if (action.type === TO_INITIAL_STATE) {
return {...state, currentState: 'initial'};
}
return state;
};
const getNumbers = (state: SimpleState = simpleState, type: NumberType = NumberType.EVEN): number[] => {
const getNumbers = (state: SimpleState = simpleState, type: string): number[] => {
return state.numbers.filter(element => {
const division = element % 2;
if (division === 0 && type === NumberType.EVEN) {
if (division === 0 && type === ODD_NUMBERS) {
return element;
}
else if (division !== 0 && type === NumberType.ODD) {
if (division !== 0 && type === EVEN_NUMBERS) {
return element;
}
});
@@ -52,9 +51,9 @@ const thunkAction2: Action = {
};
const thunk = () => {
return function (dispatch: Dispatch) {
dispatch({ thunkAction1 });
dispatch({ thunkAction2 });
return (dispatch: Dispatch<Action>): void => {
dispatch(thunkAction1);
dispatch(thunkAction2);
};
};
@@ -62,8 +61,8 @@ Reducer(simpleAction).expect({ type: 'WRONG_TYPE' }).toStayTheSame();
Reducer(simpleAction).withState(simpleState).expect({ type: TO_FINISH_STATE }).toChangeInState({ currentState: 'finish' });
Reducer(simpleAction).expect({ type: TO_INITIAL_STATE }).toReturnState(simpleState);
Selector(getNumbers).expect(simpleState, NumberType.EVEN).toReturn([2, 4, 6, 8]);
Selector(getNumbers).expect(simpleState, EVEN_NUMBERS).toReturn([2, 4, 6, 8]);
Selector(getNumbers).execute(simpleState, ODD_NUMBERS);
const odd = Selector(getNumbers).execute(simpleState, NumberType.ODD);
const dispatches = Thunk(thunk).execute();
Thunk(thunk).execute();
Thunk(thunk).withState(simpleState).execute();
+1
View File
@@ -7,6 +7,7 @@
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"