mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 20:22:21 +08:00

* Feature: Adds connectWithCleanup HOC * Refactor: Small typings * Refactor: Makes UseEffect run on Mount and UnMount only * Refactor: Adds tests and rootReducer
98 lines
2.7 KiB
TypeScript
98 lines
2.7 KiB
TypeScript
import { Reducer } from 'redux';
|
|
|
|
import { ActionOf } from 'app/core/redux/actionCreatorFactory';
|
|
|
|
export interface Given<State> {
|
|
givenReducer: (reducer: Reducer<State, ActionOf<any>>, state: State, disableDeepFreeze?: boolean) => When<State>;
|
|
}
|
|
|
|
export interface When<State> {
|
|
whenActionIsDispatched: (action: ActionOf<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, ActionOf<any>>;
|
|
let resultingState: State;
|
|
let initialState: State;
|
|
|
|
const givenReducer = (
|
|
reducer: Reducer<State, ActionOf<any>>,
|
|
state: State,
|
|
disableDeepFreeze = false
|
|
): When<State> => {
|
|
reducerUnderTest = reducer;
|
|
initialState = { ...state };
|
|
if (!disableDeepFreeze) {
|
|
initialState = deepFreeze(initialState);
|
|
}
|
|
|
|
return instance;
|
|
};
|
|
|
|
const whenActionIsDispatched = (action: ActionOf<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;
|
|
};
|