/usr/share/grafana/public/app/features/variables/state
import { DataSourceRef } from '@grafana/data'; import { adHocBuilder, queryBuilder } from '../shared/testing/builders'; import { toVariablePayload } from '../utils'; import { migrateVariablesDatasourceNameToRef } from './actions'; import { getPreloadedState } from './helpers'; import { toKeyedAction } from './keyedVariablesReducer'; import { changeVariableProp } from './sharedReducer'; function getTestContext(ds: DataSourceRef, dsInstance?: { uid: string; type: string }) { jest.clearAllMocks(); const key = 'key'; const query = queryBuilder().withId('query').withRootStateKey(key).withName('query').withDatasource(ds).build(); const adhoc = adHocBuilder().withId('adhoc').withRootStateKey(key).withName('adhoc').withDatasource(ds).build(); const templatingState = { variables: { query, adhoc } }; const state = getPreloadedState(key, templatingState); const dispatch = jest.fn(); const getState = jest.fn().mockReturnValue(state); const getInstanceSettingsMock = jest.fn().mockReturnValue(dsInstance); const getDatasourceSrvFunc = jest.fn().mockReturnValue({ get: jest.fn().mockResolvedValue({}), getList: jest.fn().mockReturnValue([]), getInstanceSettings: getInstanceSettingsMock, }); return { key, query, adhoc, dispatch, getState, getDatasourceSrvFunc }; } describe('migrateVariablesDatasourceNameToRef', () => { describe('when called and variables have legacy data source props', () => { describe('and data source exists', () => { it('then correct actions are dispatched', async () => { const legacyDs = '${ds}' as unknown as DataSourceRef; const { query, adhoc, dispatch, getState, getDatasourceSrvFunc, key } = getTestContext(legacyDs, { uid: 'a random uid', type: 'prometheus', }); migrateVariablesDatasourceNameToRef(key, getDatasourceSrvFunc)(dispatch, getState, undefined); expect(dispatch).toHaveBeenCalledTimes(2); expect(dispatch.mock.calls[0][0]).toEqual( toKeyedAction( key, changeVariableProp( toVariablePayload(query, { propName: 'datasource', propValue: { uid: 'a random uid', type: 'prometheus' }, }) ) ) ); expect(dispatch.mock.calls[1][0]).toEqual( toKeyedAction( key, changeVariableProp( toVariablePayload(adhoc, { propName: 'datasource', propValue: { uid: 'a random uid', type: 'prometheus' }, }) ) ) ); }); }); describe('and data source does not exist', () => { it('then correct actions are dispatched', async () => { const legacyDs = '${ds}' as unknown as DataSourceRef; const { query, adhoc, dispatch, getState, getDatasourceSrvFunc, key } = getTestContext(legacyDs, undefined); migrateVariablesDatasourceNameToRef(key, getDatasourceSrvFunc)(dispatch, getState, undefined); expect(dispatch).toHaveBeenCalledTimes(2); expect(dispatch.mock.calls[0][0]).toEqual( toKeyedAction( key, changeVariableProp(toVariablePayload(query, { propName: 'datasource', propValue: { uid: '${ds}' } })) ) ); expect(dispatch.mock.calls[1][0]).toEqual( toKeyedAction( key, changeVariableProp(toVariablePayload(adhoc, { propName: 'datasource', propValue: { uid: '${ds}' } })) ) ); }); }); }); describe('when called and variables have dataSourceRef', () => { it('then no actions are dispatched', async () => { const legacyDs = { uid: '${ds}', type: 'prometheus' }; const { dispatch, getState, getDatasourceSrvFunc, key } = getTestContext(legacyDs, undefined); migrateVariablesDatasourceNameToRef(key, getDatasourceSrvFunc)(dispatch, getState, undefined); expect(dispatch).toHaveBeenCalledTimes(0); }); }); });
.
Edit
..
Edit
__tests__
Edit
actions.test.ts
Edit
actions.ts
Edit
helpers.ts
Edit
initVariableTransaction.test.ts
Edit
keyedVariablesReducer.test.ts
Edit
keyedVariablesReducer.ts
Edit
migrateVariablesDatasourceNameToRef.test.ts
Edit
onTimeRangeUpdated.test.ts
Edit
processVariable.test.ts
Edit
reducers.test.ts
Edit
reducers.ts
Edit
selectors.ts
Edit
setOptionFromUrl.test.ts
Edit
sharedReducer.test.ts
Edit
sharedReducer.ts
Edit
templateVarsChangedInUrl.test.ts
Edit
transactionReducer.test.ts
Edit
transactionReducer.ts
Edit
types.ts
Edit
upgradeLegacyQueries.test.ts
Edit
variablesReducer.ts
Edit