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:
Hugo Häggmark
2019-10-04 04:37:16 -07:00
committed by GitHub
parent 81dd57524d
commit 989f98efda
8 changed files with 250 additions and 48 deletions

View File

@ -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;
};