diff --git a/public/app/features/datasources/state/actions.test.ts b/public/app/features/datasources/state/actions.test.ts index 6a007639234..594a1eab6fd 100644 --- a/public/app/features/datasources/state/actions.test.ts +++ b/public/app/features/datasources/state/actions.test.ts @@ -4,6 +4,7 @@ import { InitDataSourceSettingDependencies, testDataSource, TestDataSourceDependencies, + getDataSourceUsingUidOrId, } from './actions'; import { getMockPlugin, getMockPlugins } from '../../plugins/__mocks__/pluginMocks'; import { thunkTester } from 'test/core/thunk/thunkTester'; @@ -17,6 +18,15 @@ import { import { initDataSourceSettings } from '../state/actions'; import { ThunkResult, ThunkDispatch } from 'app/types'; import { GenericDataSourcePlugin } from '../settings/PluginSettings'; +import { getBackendSrv } from 'app/core/services/backend_srv'; +import { BackendSrvRequest, FetchResponse } from '@grafana/runtime'; +import { of } from 'rxjs'; + +jest.mock('app/core/services/backend_srv'); +jest.mock('@grafana/runtime', () => ({ + ...((jest.requireActual('@grafana/runtime') as unknown) as object), + getBackendSrv: jest.fn(), +})); const getBackendSrvMock = () => ({ @@ -54,6 +64,79 @@ const failDataSourceTest = async (error: object) => { return dispatchedActions; }; +describe('getDataSourceUsingUidOrId', () => { + const uidResponse = { + ok: true, + data: { + id: 111, + uid: 'abcdefg', + }, + }; + + const idResponse = { + ok: true, + data: { + id: 222, + uid: 'xyz', + }, + }; + + it('should return UID response data', async () => { + (getBackendSrv as jest.Mock).mockReturnValueOnce({ + fetch: (options: BackendSrvRequest) => { + return of(uidResponse as FetchResponse); + }, + }); + + expect(await getDataSourceUsingUidOrId('abcdefg')).toBe(uidResponse.data); + }); + + it('should return ID response data', async () => { + const uidResponse = { + ok: false, + }; + + (getBackendSrv as jest.Mock) + .mockReturnValueOnce({ + fetch: (options: BackendSrvRequest) => { + return of(uidResponse as FetchResponse); + }, + }) + .mockReturnValueOnce({ + fetch: (options: BackendSrvRequest) => { + return of(idResponse as FetchResponse); + }, + }); + + expect(await getDataSourceUsingUidOrId(222)).toBe(idResponse.data); + }); + + it('should return empty response data', async () => { + // @ts-ignore + delete window.location; + window.location = {} as any; + + const uidResponse = { + ok: false, + }; + + (getBackendSrv as jest.Mock) + .mockReturnValueOnce({ + fetch: (options: BackendSrvRequest) => { + return of(uidResponse as FetchResponse); + }, + }) + .mockReturnValueOnce({ + fetch: (options: BackendSrvRequest) => { + return of(idResponse as FetchResponse); + }, + }); + + expect(await getDataSourceUsingUidOrId('222')).toStrictEqual({}); + expect(window.location.href).toBe('/datasources/edit/xyz'); + }); +}); + describe('Name exists', () => { const plugins = getMockPlugins(5); diff --git a/public/app/features/datasources/state/actions.ts b/public/app/features/datasources/state/actions.ts index d8d8d296ad2..9b773f042f9 100644 --- a/public/app/features/datasources/state/actions.ts +++ b/public/app/features/datasources/state/actions.ts @@ -138,7 +138,7 @@ export function loadDataSource(uid: string): ThunkResult { /** * Get data source by uid or id, if old id detected handles redirect */ -async function getDataSourceUsingUidOrId(uid: string): Promise { +export async function getDataSourceUsingUidOrId(uid: string | number): Promise { // Try first with uid api try { const byUid = await lastValueFrom( @@ -157,7 +157,7 @@ async function getDataSourceUsingUidOrId(uid: string): Promise({ @@ -167,6 +167,12 @@ async function getDataSourceUsingUidOrId(uid: string): Promise