The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/test/core/thunk/thunkTester.ts

43 lines
1.1 KiB

import { PayloadAction } from '@reduxjs/toolkit';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
const mockStore = configureMockStore([thunk]);
export interface ThunkGiven {
givenThunk: (thunkFunction: any) => ThunkWhen;
}
export interface ThunkWhen {
whenThunkIsDispatched: (...args: any) => Promise<Array<PayloadAction<any>>>;
}
export const thunkTester = (initialState: unknown, debug?: boolean): ThunkGiven => {
const store = mockStore(initialState);
let thunkUnderTest: any = null;
let dispatchedActions: Array<PayloadAction<any>> = [];
const givenThunk = (thunkFunction: any): ThunkWhen => {
thunkUnderTest = thunkFunction;
return instance;
};
const whenThunkIsDispatched = async (...args: any): Promise<Array<PayloadAction<any>>> => {
await store.dispatch(thunkUnderTest(...args));
dispatchedActions = store.getActions();
if (debug) {
console.log('resultingActions:', JSON.stringify(dispatchedActions, null, 2));
}
return dispatchedActions;
};
const instance = {
givenThunk,
whenThunkIsDispatched,
};
return instance;
};