⚝
One Hat Cyber Team
⚝
Your IP:
216.73.217.4
Server IP:
41.128.143.86
Server:
Linux host.raqmix.cloud 6.8.0-1025-azure #30~22.04.1-Ubuntu SMP Wed Mar 12 15:28:20 UTC 2025 x86_64
Server Software:
Apache
PHP Version:
8.3.23
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
usr
/
share
/
grafana
/
public
/
test
/
core
/
redux
/
View File Name :
reducerTester.ts
import { AnyAction } from '@reduxjs/toolkit'; import { cloneDeep } from 'lodash'; import { Action } from 'redux'; import { StoreState } from 'app/types'; type GrafanaReducer
= (state: S, action: A) => S; export interface Given
{ givenReducer: ( reducer: GrafanaReducer
, state: State, showDebugOutput?: boolean, disableDeepFreeze?: boolean ) => When
; } export interface When
{ whenActionIsDispatched: (action: AnyAction) => Then
; } export interface Then
{ thenStateShouldEqual: (state: State) => When
; thenStatePredicateShouldEqual: (predicate: (resultingState: State) => boolean) => When
; whenActionIsDispatched: (action: AnyAction) => Then
; } const isNotException = (object: unknown, propertyName: string) => typeof object === 'function' ? propertyName !== 'caller' && propertyName !== 'callee' && propertyName !== 'arguments' : true; export const deepFreeze =
(obj: T): T => { if (typeof obj === 'object') { for (const key in obj) { const prop = obj[key]; if ( prop && Object.hasOwn(obj, key) && isNotException(obj, key) && (typeof prop === 'object' || typeof prop === 'function') && !Object.isFrozen(prop) ) { deepFreeze(prop); } } } return Object.freeze(obj); }; interface ReducerTester
extends Given
, When
, Then
{} export const reducerTester =
(): Given
=> { let reducerUnderTest: GrafanaReducer
; let resultingState: State; let initialState: State; let showDebugOutput = false; const givenReducer = ( reducer: GrafanaReducer
, state: State, debug = false, disableDeepFreeze = false ): When
=> { reducerUnderTest = reducer; initialState = cloneDeep(state); if (!disableDeepFreeze && (typeof state === 'object' || typeof state === 'function')) { deepFreeze(initialState); } showDebugOutput = debug; return instance; }; const whenActionIsDispatched = (action: AnyAction): Then
=> { resultingState = reducerUnderTest(resultingState || initialState, action); return instance; }; const thenStateShouldEqual = (state: State): When
=> { if (showDebugOutput) { console.log(JSON.stringify(resultingState, null, 2)); } expect(resultingState).toEqual(state); return instance; }; const thenStatePredicateShouldEqual = (predicate: (resultingState: State) => boolean): When
=> { if (showDebugOutput) { console.log(JSON.stringify(resultingState, null, 2)); } expect(predicate(resultingState)).toBe(true); return instance; }; const instance: ReducerTester
= { thenStateShouldEqual, thenStatePredicateShouldEqual, givenReducer, whenActionIsDispatched, }; return instance; };