From 09b6f76746fa597d9bbbfea4cc32806d17349cf8 Mon Sep 17 00:00:00 2001 From: Law Smith Date: Thu, 29 Nov 2018 16:45:28 -0800 Subject: [PATCH 1/2] Added support for params when starting sagaTester. --- types/redux-saga-tester/index.d.ts | 28 +++---- types/redux-saga-tester/package.json | 7 ++ .../redux-saga-tester-tests.ts | 74 ++++++++++++------- 3 files changed, 65 insertions(+), 44 deletions(-) create mode 100644 types/redux-saga-tester/package.json diff --git a/types/redux-saga-tester/index.d.ts b/types/redux-saga-tester/index.d.ts index fda97e494b..5778edf1bd 100644 --- a/types/redux-saga-tester/index.d.ts +++ b/types/redux-saga-tester/index.d.ts @@ -1,27 +1,19 @@ // Type definitions for redux-saga-tester 1.0 // Project: https://github.com/wix/redux-saga-tester#readme -// Definitions by: Ben Lorantfy +// Definitions by: Ben Lorantfy , Law Smith // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 2.4 + +import {Task} from 'redux-saga'; +import {AnyAction, Middleware, Reducer, ReducersMapObject} from 'redux'; -export interface AnyAction { - type: string; - [key: string]: any; -} -export type Reducer = (state: object, action: AnyAction) => object; -export interface ReducerMap { - [key: string]: Reducer; -} -export type ReduxMiddleware = (options: { dispatch: (action: AnyAction) => void, getState: () => object }) - => (next: (action: AnyAction) => any) - => any; export type SagaFunction = (...args: any[]) => any; -export interface SagaTesterOptions { +export interface SagaTesterOptions { initialState?: StateType; - reducers?: ReducerMap|Reducer; - middlewares?: ReduxMiddleware[]; - combineReducers?: (map: ReducerMap) => Reducer; + reducers?: ReducersMapObject | Reducer; + middlewares?: Middleware[]; + combineReducers?: (map: ReducersMapObject) => Reducer; ignoreReduxActions?: boolean; options?: object; } @@ -32,7 +24,7 @@ export default class SagaTester { /** * Starts execution of the provided saga. */ - start(saga: SagaFunction): void; + start(saga: SagaFunction, ...args: any[]): Task; /** * Dispatches an action to the redux store. diff --git a/types/redux-saga-tester/package.json b/types/redux-saga-tester/package.json new file mode 100644 index 0000000000..8d41143894 --- /dev/null +++ b/types/redux-saga-tester/package.json @@ -0,0 +1,7 @@ +{ + "private": true, + "dependencies": { + "redux": "^3.7.2", + "redux-saga": "^0.15.3" + } +} diff --git a/types/redux-saga-tester/redux-saga-tester-tests.ts b/types/redux-saga-tester/redux-saga-tester-tests.ts index 0dba716533..4cd9c001c9 100644 --- a/types/redux-saga-tester/redux-saga-tester-tests.ts +++ b/types/redux-saga-tester/redux-saga-tester-tests.ts @@ -2,62 +2,79 @@ import SagaTester from 'redux-saga-tester'; // constructor with all options new SagaTester({ - initialState: { - orders: [], - }, - reducers: { - orders: (state, action) => state, - }, - middlewares: [ - ({ dispatch, getState }) => (next) => (action: { type: string }) => { - dispatch({ type: 'BLA' }); - getState(); + initialState: { + orders: [], + }, + reducers: { + orders: (state, action) => state, + }, + middlewares: [ + ({dispatch, getState}) => (next) => (action) => { + dispatch({type: 'BLA'}); + getState(); - return next(action); + return next(action); + }, + ], + ignoreReduxActions: false, + options: { + a: 23, }, - ], - ignoreReduxActions: false, - options: { - a: 23, - }, }); // constructor with function as reducers new SagaTester({ - reducers: (state, action) => state, + reducers: (state = {}, action) => state, +}); + +// constructor with reducer map +new SagaTester({ + reducers: { + a: (state, action) => state, + b: (state, action) => state, + c: (state, action) => state, + } }); // constructor with no options interface MockStateType { - orders: Array<{ name: string }>; + orders: Array<{ name: string }>; } -const sagaTester = new SagaTester({ initialState: { orders: [] }}); + +const sagaTester = new SagaTester({initialState: {orders: []}}); // start function* fakeSaga() { - return 23; + return 23; +} + +function* fakeSagaWithParams(param1: string, param2: number) { + return param1.length > param2 } sagaTester.start(fakeSaga); +sagaTester.start(fakeSagaWithParams, 'foo', 3); // dispatch -sagaTester.dispatch({ type: 'LOAD_ORDERS', orders: [] }); +sagaTester.dispatch({type: 'LOAD_ORDERS', orders: []}); // updateState -sagaTester.updateState({ orders: [] }); +sagaTester.updateState({orders: []}); // getState sagaTester.getState().orders; // waitFor -sagaTester.waitFor('LOAD_ORDERS').then(() => {}); -sagaTester.waitFor('LOAD_ORDERS', true).then(() => {}); +sagaTester.waitFor('LOAD_ORDERS').then(() => { +}); +sagaTester.waitFor('LOAD_ORDERS', true).then(() => { +}); // wasCalled -sagaTester.wasCalled("LOAD_ORDERS"); +sagaTester.wasCalled('LOAD_ORDERS'); // numCalled -sagaTester.numCalled("LOAD_ORDERS") === 1; +sagaTester.numCalled('LOAD_ORDERS') === 1; // getLatestCalledAction sagaTester.getLatestCalledAction().type; @@ -68,3 +85,8 @@ sagaTester.getCalledActions()[0].type; // getCalledActions sagaTester.reset(); sagaTester.reset(true); + +// getState +const state = sagaTester.getState(); +state.orders.length; +state.orders[0].name; From 8d5940c36bdd76ba364fa79271aff55cc8e0fc48 Mon Sep 17 00:00:00 2001 From: Law Smith Date: Thu, 29 Nov 2018 17:50:40 -0800 Subject: [PATCH 2/2] Fixing linter errors. --- types/redux-saga-tester/index.d.ts | 4 ++-- types/redux-saga-tester/redux-saga-tester-tests.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/types/redux-saga-tester/index.d.ts b/types/redux-saga-tester/index.d.ts index 5778edf1bd..d7772626ef 100644 --- a/types/redux-saga-tester/index.d.ts +++ b/types/redux-saga-tester/index.d.ts @@ -4,8 +4,8 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.4 -import {Task} from 'redux-saga'; -import {AnyAction, Middleware, Reducer, ReducersMapObject} from 'redux'; +import { Task } from 'redux-saga'; +import { AnyAction, Middleware, Reducer, ReducersMapObject } from 'redux'; export type SagaFunction = (...args: any[]) => any; diff --git a/types/redux-saga-tester/redux-saga-tester-tests.ts b/types/redux-saga-tester/redux-saga-tester-tests.ts index 4cd9c001c9..48eb490d0a 100644 --- a/types/redux-saga-tester/redux-saga-tester-tests.ts +++ b/types/redux-saga-tester/redux-saga-tester-tests.ts @@ -49,7 +49,7 @@ function* fakeSaga() { } function* fakeSagaWithParams(param1: string, param2: number) { - return param1.length > param2 + return param1.length > param2; } sagaTester.start(fakeSaga);