reducerTester.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { AnyAction } from '@reduxjs/toolkit';
  2. import { cloneDeep } from 'lodash';
  3. import { Action } from 'redux';
  4. type GrafanaReducer<S = any, A extends Action = AnyAction> = (state: S, action: A) => S;
  5. export interface Given<State> {
  6. givenReducer: (
  7. reducer: GrafanaReducer<State, AnyAction>,
  8. state: State,
  9. showDebugOutput?: boolean,
  10. disableDeepFreeze?: boolean
  11. ) => When<State>;
  12. }
  13. export interface When<State> {
  14. whenActionIsDispatched: (action: AnyAction) => Then<State>;
  15. }
  16. export interface Then<State> {
  17. thenStateShouldEqual: (state: State) => When<State>;
  18. thenStatePredicateShouldEqual: (predicate: (resultingState: State) => boolean) => When<State>;
  19. whenActionIsDispatched: (action: AnyAction) => Then<State>;
  20. }
  21. interface ObjectType extends Object {
  22. [key: string]: any;
  23. }
  24. export const deepFreeze = <T>(obj: T): T => {
  25. Object.freeze(obj);
  26. const isNotException = (object: any, propertyName: any) =>
  27. typeof object === 'function'
  28. ? propertyName !== 'caller' && propertyName !== 'callee' && propertyName !== 'arguments'
  29. : true;
  30. const hasOwnProp = Object.prototype.hasOwnProperty;
  31. if (obj && obj instanceof Object) {
  32. const object: ObjectType = obj;
  33. Object.getOwnPropertyNames(object).forEach((propertyName) => {
  34. const objectProperty: any = object[propertyName];
  35. if (
  36. hasOwnProp.call(object, propertyName) &&
  37. isNotException(object, propertyName) &&
  38. objectProperty &&
  39. (typeof objectProperty === 'object' || typeof objectProperty === 'function') &&
  40. Object.isFrozen(objectProperty) === false
  41. ) {
  42. deepFreeze(objectProperty);
  43. }
  44. });
  45. }
  46. return obj;
  47. };
  48. interface ReducerTester<State> extends Given<State>, When<State>, Then<State> {}
  49. export const reducerTester = <State>(): Given<State> => {
  50. let reducerUnderTest: GrafanaReducer<State, AnyAction>;
  51. let resultingState: State;
  52. let initialState: State;
  53. let showDebugOutput = false;
  54. const givenReducer = (
  55. reducer: GrafanaReducer<State, AnyAction>,
  56. state: State,
  57. debug = false,
  58. disableDeepFreeze = false
  59. ): When<State> => {
  60. reducerUnderTest = reducer;
  61. initialState = cloneDeep(state);
  62. if (!disableDeepFreeze && (typeof state === 'object' || typeof state === 'function')) {
  63. deepFreeze(initialState);
  64. }
  65. showDebugOutput = debug;
  66. return instance;
  67. };
  68. const whenActionIsDispatched = (action: AnyAction): Then<State> => {
  69. resultingState = reducerUnderTest(resultingState || initialState, action);
  70. return instance;
  71. };
  72. const thenStateShouldEqual = (state: State): When<State> => {
  73. if (showDebugOutput) {
  74. console.log(JSON.stringify(resultingState, null, 2));
  75. }
  76. expect(resultingState).toEqual(state);
  77. return instance;
  78. };
  79. const thenStatePredicateShouldEqual = (predicate: (resultingState: State) => boolean): When<State> => {
  80. if (showDebugOutput) {
  81. console.log(JSON.stringify(resultingState, null, 2));
  82. }
  83. expect(predicate(resultingState)).toBe(true);
  84. return instance;
  85. };
  86. const instance: ReducerTester<State> = {
  87. thenStateShouldEqual,
  88. thenStatePredicateShouldEqual,
  89. givenReducer,
  90. whenActionIsDispatched,
  91. };
  92. return instance;
  93. };