From ecbd9b2da8a90b2791c90bae81e3588ee9e1b3de Mon Sep 17 00:00:00 2001 From: akizimov Date: Fri, 4 May 2018 16:54:10 +0300 Subject: [PATCH] redux-testkit defines --- types/redux-testkit/index.d.ts | 44 +++++++++++----------- types/redux-testkit/redux-testkit-tests.ts | 24 ++++++++++-- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/types/redux-testkit/index.d.ts b/types/redux-testkit/index.d.ts index daf8c2af43..47aa006cd2 100644 --- a/types/redux-testkit/index.d.ts +++ b/types/redux-testkit/index.d.ts @@ -7,39 +7,37 @@ 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; - }; - +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): 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, extraArg?: any): { - execute(...args: any[]): any; - withState(state: any): { - execute(...args: any[]): any; - } +export function Thunk(thunkFunc: ThunkAction, extraArg?: any): ThunkTestkit & { + withState(state: any): ThunkTestkit; }; export namespace FlushThunks { - function createMiddleware(): { - flush(): any; - reset(): any; + function createMiddleware(): Redux.Middleware & { + flush(): void; + reset(): void; }; } diff --git a/types/redux-testkit/redux-testkit-tests.ts b/types/redux-testkit/redux-testkit-tests.ts index efbab291dc..2fdbf14856 100644 --- a/types/redux-testkit/redux-testkit-tests.ts +++ b/types/redux-testkit/redux-testkit-tests.ts @@ -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): 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();