Files
grafana/public/test/core/redux/reducerTester.ts
Hugo Häggmark 4f0fa776be Chore: Migrates reducers and actions to Redux Toolkit (#21287)
* Refactor: Adds Redux Toolkit package

* Refactor: Uses configureStore from Redux Toolkit

* Refactor: Migrates applicationReducer

* Refactor: Migrates appNotificationsReducer

* Refactor: Migrates locationReducer

* Refactor: Migrates navModelReducer

* Refactor: Migrates teamsReducer and teamReducer

* Refactor: Migrates cleanUpAction

* Refactor: Migrates alertRulesReducer

* Refactor: Cleans up recursiveCleanState

* Refactor: Switched to Angular compatible reducers

* Refactor: Migrates folderReducer

* Refactor: Migrates dashboardReducer

* Migrates panelEditorReducer

* Refactor: Migrates dataSourcesReducer

* Refactor: Migrates usersReducer

* Refactor: Migrates organizationReducer

* Refactor: Migrates pluginsReducer

* Refactor: Migrates ldapReducer and ldapUserReducer

* Refactor: Migrates apiKeysReducer

* Refactor: Migrates exploreReducer and itemReducer

* Refactor: Removes actionCreatorFactory and reducerFactory

* Refactor: Moves mocks to test section

* Docs: Removes sections about home grown framework

* Update contribute/style-guides/redux.md

Co-Authored-By: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>

* Refactor: Cleans up some code

* Refactor: Adds state typings

* Refactor: Cleans up typings

* Refactor: Adds comment about ImmerJs autoFreeze

Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>
2020-01-13 08:03:22 +01:00

97 lines
2.7 KiB
TypeScript

import { Reducer } from 'redux';
import { PayloadAction } from '@reduxjs/toolkit';
export interface Given<State> {
givenReducer: (reducer: Reducer<State, PayloadAction<any>>, state: State, disableDeepFreeze?: boolean) => When<State>;
}
export interface When<State> {
whenActionIsDispatched: (action: PayloadAction<any>) => Then<State>;
}
export interface Then<State> {
thenStateShouldEqual: (state: State) => When<State>;
thenStatePredicateShouldEqual: (predicate: (resultingState: State) => boolean) => When<State>;
}
interface ObjectType extends Object {
[key: string]: any;
}
export const deepFreeze = <T>(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<State> extends Given<State>, When<State>, Then<State> {}
export const reducerTester = <State>(): Given<State> => {
let reducerUnderTest: Reducer<State, PayloadAction<any>>;
let resultingState: State;
let initialState: State;
const givenReducer = (
reducer: Reducer<State, PayloadAction<any>>,
state: State,
disableDeepFreeze = false
): When<State> => {
reducerUnderTest = reducer;
initialState = { ...state };
if (!disableDeepFreeze) {
initialState = deepFreeze(initialState);
}
return instance;
};
const whenActionIsDispatched = (action: PayloadAction<any>): Then<State> => {
resultingState = reducerUnderTest(resultingState || initialState, action);
return instance;
};
const thenStateShouldEqual = (state: State): When<State> => {
expect(state).toEqual(resultingState);
return instance;
};
const thenStatePredicateShouldEqual = (predicate: (resultingState: State) => boolean): When<State> => {
expect(predicate(resultingState)).toBe(true);
return instance;
};
const instance: ReducerTester<State> = {
thenStateShouldEqual,
thenStatePredicateShouldEqual,
givenReducer,
whenActionIsDispatched,
};
return instance;
};