Commit c4e31100 by Shavonn Brown Committed by GitHub

deps so can mock in tests (#21827)

parent 7720bfab
import { findNewName, nameExits, InitDataSourceSettingDependencies, testDataSource } from './actions';
import {
findNewName,
nameExits,
InitDataSourceSettingDependencies,
testDataSource,
TestDataSourceDependencies,
} from './actions';
import { getMockPlugin, getMockPlugins } from '../../plugins/__mocks__/pluginMocks';
import { thunkTester } from 'test/core/thunk/thunkTester';
import {
......@@ -11,10 +17,17 @@ import {
import { initDataSourceSettings } from '../state/actions';
import { ThunkResult, ThunkDispatch } from 'app/types';
import { GenericDataSourcePlugin } from '../settings/PluginSettings';
import * as DatasourceSrv from 'app/features/plugins/datasource_srv';
jest.mock('app/features/plugins/datasource_srv');
const getDatasourceSrvMock = (DatasourceSrv.getDatasourceSrv as any) as jest.Mock<DatasourceSrv.DatasourceSrv>;
const getBackendSrvMock = () =>
({
get: jest.fn().mockReturnValue({
testDatasource: jest.fn().mockReturnValue({
status: '',
message: '',
}),
}),
withNoBackendCache: jest.fn().mockImplementationOnce(cb => cb()),
} as any);
describe('Name exists', () => {
const plugins = getMockPlugins(5);
......@@ -131,8 +144,8 @@ describe('initDataSourceSettings', () => {
describe('testDataSource', () => {
describe('when a datasource is tested', () => {
it('then testDataSourceStarting and testDataSourceSucceeded should be dispatched', async () => {
getDatasourceSrvMock.mockImplementation(
() =>
const dependencies: TestDataSourceDependencies = {
getDatasourceSrv: () =>
({
get: jest.fn().mockReturnValue({
testDatasource: jest.fn().mockReturnValue({
......@@ -140,8 +153,9 @@ describe('testDataSource', () => {
message: '',
}),
}),
} as any)
);
} as any),
getBackendSrv: getBackendSrvMock,
};
const state = {
testingStatus: {
status: '',
......@@ -150,22 +164,23 @@ describe('testDataSource', () => {
};
const dispatchedActions = await thunkTester(state)
.givenThunk(testDataSource)
.whenThunkIsDispatched('Azure Monitor');
.whenThunkIsDispatched('Azure Monitor', dependencies);
expect(dispatchedActions).toEqual([testDataSourceStarting(), testDataSourceSucceeded(state.testingStatus)]);
});
it('then testDataSourceFailed should be dispatched', async () => {
getDatasourceSrvMock.mockImplementation(
() =>
const dependencies: TestDataSourceDependencies = {
getDatasourceSrv: () =>
({
get: jest.fn().mockReturnValue({
testDatasource: jest.fn().mockImplementation(() => {
throw new Error('Error testing datasource');
}),
}),
} as any)
);
} as any),
getBackendSrv: getBackendSrvMock,
};
const result = {
message: 'Error testing datasource',
};
......@@ -177,7 +192,7 @@ describe('testDataSource', () => {
};
const dispatchedActions = await thunkTester(state)
.givenThunk(testDataSource)
.whenThunkIsDispatched('Azure Monitor');
.whenThunkIsDispatched('Azure Monitor', dependencies);
expect(dispatchedActions).toEqual([testDataSourceStarting(), testDataSourceFailed(result)]);
});
......
......@@ -21,6 +21,7 @@ import {
} from './reducers';
import { buildCategories } from './buildCategories';
import { getDataSource, getDataSourceMeta } from './selectors';
import { getDataSourceSrv } from '@grafana/runtime';
export interface DataSourceTypesLoadedPayload {
plugins: DataSourcePluginMeta[];
......@@ -34,6 +35,11 @@ export interface InitDataSourceSettingDependencies {
importDataSourcePlugin: typeof importDataSourcePlugin;
}
export interface TestDataSourceDependencies {
getDatasourceSrv: typeof getDataSourceSrv;
getBackendSrv: typeof getBackendSrv;
}
export const initDataSourceSettings = (
pageId: number,
dependencies: InitDataSourceSettingDependencies = {
......@@ -67,9 +73,15 @@ export const initDataSourceSettings = (
};
};
export const testDataSource = (dataSourceName: string): ThunkResult<void> => {
export const testDataSource = (
dataSourceName: string,
dependencies: TestDataSourceDependencies = {
getDatasourceSrv,
getBackendSrv,
}
): ThunkResult<void> => {
return async (dispatch: ThunkDispatch, getState) => {
const dsApi = await getDatasourceSrv().get(dataSourceName);
const dsApi = await dependencies.getDatasourceSrv().get(dataSourceName);
if (!dsApi.testDatasource) {
return;
......@@ -77,7 +89,7 @@ export const testDataSource = (dataSourceName: string): ThunkResult<void> => {
dispatch(testDataSourceStarting());
getBackendSrv().withNoBackendCache(async () => {
dependencies.getBackendSrv().withNoBackendCache(async () => {
try {
const result = await dsApi.testDatasource();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment