import { AnyAction } from '@reduxjs/toolkit'; import { cloneDeep } from 'lodash'; import { Action } from 'redux'; type GrafanaReducer = (state: S, action: A) => S; export interface Given { givenReducer: ( reducer: GrafanaReducer, state: State, showDebugOutput?: boolean, disableDeepFreeze?: boolean ) => When; } export interface When { whenActionIsDispatched: (action: AnyAction) => Then; } export interface Then { thenStateShouldEqual: (state: State) => When; thenStatePredicateShouldEqual: (predicate: (resultingState: State) => boolean) => When; whenActionIsDispatched: (action: AnyAction) => Then; } interface ObjectType extends Object { [key: string]: any; } export const deepFreeze = (obj: T): T => { Object.freeze(obj); const isNotException = (object: any, propertyName: any) => typeof object === 'function' ? propertyName !== 'caller' && propertyName !== 'callee' && propertyName !== 'arguments' : true; const hasOwnProp = Object.prototype.hasOwnProperty; if (obj && obj instanceof Object) { const object: ObjectType = obj; Object.getOwnPropertyNames(object).forEach((propertyName) => { const objectProperty: any = object[propertyName]; if ( hasOwnProp.call(object, propertyName) && isNotException(object, propertyName) && objectProperty && (typeof objectProperty === 'object' || typeof objectProperty === 'function') && Object.isFrozen(objectProperty) === false ) { deepFreeze(objectProperty); } }); } return obj; }; interface ReducerTester extends Given, When, Then {} export const reducerTester = (): Given => { let reducerUnderTest: GrafanaReducer; let resultingState: State; let initialState: State; let showDebugOutput = false; const givenReducer = ( reducer: GrafanaReducer, state: State, debug = false, disableDeepFreeze = false ): When => { reducerUnderTest = reducer; initialState = cloneDeep(state); if (!disableDeepFreeze && (typeof state === 'object' || typeof state === 'function')) { deepFreeze(initialState); } showDebugOutput = debug; return instance; }; const whenActionIsDispatched = (action: AnyAction): Then => { resultingState = reducerUnderTest(resultingState || initialState, action); return instance; }; const thenStateShouldEqual = (state: State): When => { if (showDebugOutput) { console.log(JSON.stringify(resultingState, null, 2)); } expect(resultingState).toEqual(state); return instance; }; const thenStatePredicateShouldEqual = (predicate: (resultingState: State) => boolean): When => { if (showDebugOutput) { console.log(JSON.stringify(resultingState, null, 2)); } expect(predicate(resultingState)).toBe(true); return instance; }; const instance: ReducerTester = { thenStateShouldEqual, thenStatePredicateShouldEqual, givenReducer, whenActionIsDispatched, }; return instance; };