mirror of
https://github.com/grafana/grafana.git
synced 2025-08-03 06:22:13 +08:00
Feature: Adds connectWithCleanup HOC (#19392)
* Feature: Adds connectWithCleanup HOC * Refactor: Small typings * Refactor: Makes UseEffect run on Mount and UnMount only * Refactor: Adds tests and rootReducer
This commit is contained in:
@ -3,7 +3,7 @@ import { Reducer } from 'redux';
|
||||
import { ActionOf } from 'app/core/redux/actionCreatorFactory';
|
||||
|
||||
export interface Given<State> {
|
||||
givenReducer: (reducer: Reducer<State, ActionOf<any>>, state: State) => When<State>;
|
||||
givenReducer: (reducer: Reducer<State, ActionOf<any>>, state: State, disableDeepFreeze?: boolean) => When<State>;
|
||||
}
|
||||
|
||||
export interface When<State> {
|
||||
@ -12,6 +12,7 @@ export interface When<State> {
|
||||
|
||||
export interface Then<State> {
|
||||
thenStateShouldEqual: (state: State) => When<State>;
|
||||
thenStatePredicateShouldEqual: (predicate: (resultingState: State) => boolean) => When<State>;
|
||||
}
|
||||
|
||||
interface ObjectType extends Object {
|
||||
@ -53,10 +54,16 @@ export const reducerTester = <State>(): Given<State> => {
|
||||
let resultingState: State;
|
||||
let initialState: State;
|
||||
|
||||
const givenReducer = (reducer: Reducer<State, ActionOf<any>>, state: State): When<State> => {
|
||||
const givenReducer = (
|
||||
reducer: Reducer<State, ActionOf<any>>,
|
||||
state: State,
|
||||
disableDeepFreeze = false
|
||||
): When<State> => {
|
||||
reducerUnderTest = reducer;
|
||||
initialState = { ...state };
|
||||
initialState = deepFreeze(initialState);
|
||||
if (!disableDeepFreeze) {
|
||||
initialState = deepFreeze(initialState);
|
||||
}
|
||||
|
||||
return instance;
|
||||
};
|
||||
@ -73,7 +80,18 @@ export const reducerTester = <State>(): Given<State> => {
|
||||
return instance;
|
||||
};
|
||||
|
||||
const instance: ReducerTester<State> = { thenStateShouldEqual, givenReducer, whenActionIsDispatched };
|
||||
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;
|
||||
};
|
||||
|
Reference in New Issue
Block a user