diff --git a/types/redux-testkit/index.d.ts b/types/redux-testkit/index.d.ts index 0e9dc4ff74..daf8c2af43 100644 --- a/types/redux-testkit/index.d.ts +++ b/types/redux-testkit/index.d.ts @@ -2,68 +2,44 @@ // Project: https://github.com/wix/redux-testkit#readme // Definitions by: Andrey Kizimov // 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): { + 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, 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): ReducerAction; -export function Selector(action: Redux.Reducer): SelectorAction; -export function Thunk(action: Redux.Reducer): ThunkAction; - diff --git a/types/redux-testkit/package.json b/types/redux-testkit/package.json new file mode 100644 index 0000000000..f682c6113f --- /dev/null +++ b/types/redux-testkit/package.json @@ -0,0 +1,7 @@ +{ + "private": true, + "dependencies": { + "redux": "^3.6.0", + "redux-thunk": "^2.2.0" + } +} diff --git a/types/redux-testkit/redux-testkit-tests.ts b/types/redux-testkit/redux-testkit-tests.ts index 3e0fa7b1be..efbab291dc 100644 --- a/types/redux-testkit/redux-testkit-tests.ts +++ b/types/redux-testkit/redux-testkit-tests.ts @@ -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): 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(); diff --git a/types/redux-testkit/tsconfig.json b/types/redux-testkit/tsconfig.json index be52e915a3..82c91d335a 100644 --- a/types/redux-testkit/tsconfig.json +++ b/types/redux-testkit/tsconfig.json @@ -7,6 +7,7 @@ "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, + "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ "../"