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