Chore: Allow reducerTester to work with every data type & payload-less actions (#29241)

This commit is contained in:
Giordano Ricci 2020-11-20 13:11:52 +00:00 committed by GitHub
parent 6838af5ce1
commit f5916260bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,10 @@
import { Reducer } from 'redux';
import { PayloadAction } from '@reduxjs/toolkit';
import { PayloadAction, Action } from '@reduxjs/toolkit';
import { cloneDeep } from 'lodash';
export interface Given<State> {
givenReducer: (
reducer: Reducer<State, PayloadAction<any>>,
reducer: Reducer<State, PayloadAction<any> | Action<any>>,
state: State,
showDebugOutput?: boolean,
disableDeepFreeze?: boolean
@ -11,7 +12,7 @@ export interface Given<State> {
}
export interface When<State> {
whenActionIsDispatched: (action: PayloadAction<any>) => Then<State>;
whenActionIsDispatched: (action: PayloadAction<any> | Action<any>) => Then<State>;
}
export interface Then<State> {
@ -66,9 +67,9 @@ export const reducerTester = <State>(): Given<State> => {
disableDeepFreeze = false
): When<State> => {
reducerUnderTest = reducer;
initialState = { ...state };
if (!disableDeepFreeze) {
initialState = deepFreeze(initialState);
initialState = cloneDeep(state);
if (!disableDeepFreeze && (typeof state === 'object' || typeof state === 'function')) {
deepFreeze(initialState);
}
showDebugOutput = debug;