grafana/public/app/features/variables/datasource/actions.test.ts

183 lines
6.8 KiB
TypeScript
Raw Normal View History

import { reduxTester } from '../../../../test/core/redux/reduxTester';
import { TemplatingState } from '../state/reducers';
import { getRootReducer } from '../state/helpers';
import { toVariableIdentifier, toVariablePayload } from '../state/types';
import { variableAdapters } from '../adapters';
import { createDataSourceVariableAdapter } from './adapter';
import {
DataSourceVariableActionDependencies,
initDataSourceVariableEditor,
updateDataSourceVariableOptions,
} from './actions';
import { DataSourceInstanceSettings, DataSourceJsonData, DataSourcePluginMeta } from '@grafana/data';
import { getMockPlugin } from '../../plugins/__mocks__/pluginMocks';
import { createDataSourceOptions } from './reducer';
import { addVariable, setCurrentVariableValue } from '../state/sharedReducer';
import { changeVariableEditorExtended } from '../editor/reducer';
import { datasourceBuilder } from '../shared/testing/builders';
interface Args {
sources?: DataSourceInstanceSettings[];
query?: string;
regex?: string;
}
function getTestContext({ sources = [], query, regex }: Args = {}) {
const getListMock = jest.fn().mockReturnValue(sources);
const getDatasourceSrvMock = jest.fn().mockReturnValue({ getList: getListMock });
const dependencies: DataSourceVariableActionDependencies = { getDatasourceSrv: getDatasourceSrvMock };
const datasource = datasourceBuilder()
.withId('0')
.withQuery(query)
.withRegEx(regex)
.build();
return { getListMock, getDatasourceSrvMock, dependencies, datasource };
}
describe('data source actions', () => {
variableAdapters.setInit(() => [createDataSourceVariableAdapter()]);
describe('when updateDataSourceVariableOptions is dispatched', () => {
describe('and there is no regex', () => {
it('then the correct actions are dispatched', async () => {
const meta = getMockPlugin({ name: 'mock-data-name', id: 'mock-data-id' });
const sources: DataSourceInstanceSettings[] = [
getDataSourceInstanceSetting('first-name', meta),
getDataSourceInstanceSetting('second-name', meta),
];
const { datasource, dependencies, getListMock, getDatasourceSrvMock } = getTestContext({
sources,
query: 'mock-data-id',
});
Variables: migrates ad hoc variable type to react/redux. (#22784) * Refactor: moves all the newVariables part to features/variables directory * Feature: adds datasource type * Tests: adds reducer tests * Tests: covers data source actions with tests * Chore: reduces strict null errors * boilerplate that will be replaced by real code. * added old editor template. * added initial version of ad hoc editor. * added working (apart from add) version of the editor. * Added placeholder for picker. * Have a working UI. Need to connect it so we refresh the variables on changes. * variable should be updated now. * removed console.log * made the url work. * cleaned up the adapter. * added possiblity to create filter directly from table. * moved infotext from general reducer to extended value of adhoc. * fixed strict null errors. * fixed strict null errors. * fixed issue where remove was displayed before being added. * fixed issue with fragment key. * changed so template_src is using the redux variables. * minor refactorings. * moved adhoc picker to adhoc variable. * adding tests for reducer and fixed bug. * added tests or urlparser. * added tests for ad hoc actions. * added more tests. * added more tests. * fixed strict null error. * fixed copy n pase error. * added utilit for getting new variable index. * removed console.log * added location to reducerTester type and created a module type for it. * changed so we only have one builder pattern. * fixed tests to use static expected values. * fixed strict errors. * fixed more strict errors. Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>
2020-03-23 03:00:36 -05:00
const tester = await reduxTester<{ templating: TemplatingState }>()
.givenRootReducer(getRootReducer())
.whenActionIsDispatched(
addVariable(toVariablePayload(datasource, { global: false, index: 0, model: datasource }))
)
.whenAsyncActionIsDispatched(
updateDataSourceVariableOptions(toVariableIdentifier(datasource), dependencies),
true
);
await tester.thenDispatchedActionsShouldEqual(
createDataSourceOptions(
toVariablePayload(
{ type: 'datasource', id: '0' },
{
sources: [
{ name: 'first-name', value: 'first-name', meta },
{ name: 'second-name', value: 'second-name', meta },
],
regex: (undefined as unknown) as RegExp,
}
)
),
setCurrentVariableValue(
toVariablePayload(
{ type: 'datasource', id: '0' },
{ option: { text: 'first-name', value: 'first-name', selected: false } }
)
)
);
expect(getListMock).toHaveBeenCalledTimes(1);
expect(getListMock).toHaveBeenCalledWith({ metrics: true, variables: false });
expect(getDatasourceSrvMock).toHaveBeenCalledTimes(1);
});
});
describe('and there is a regex', () => {
it('then the correct actions are dispatched', async () => {
const meta = getMockPlugin({ name: 'mock-data-name', id: 'mock-data-id' });
const sources: DataSourceInstanceSettings[] = [
getDataSourceInstanceSetting('first-name', meta),
getDataSourceInstanceSetting('second-name', meta),
];
const { datasource, dependencies, getListMock, getDatasourceSrvMock } = getTestContext({
sources,
query: 'mock-data-id',
regex: '/.*(second-name).*/',
});
const tester = await reduxTester<{ templating: TemplatingState }>()
.givenRootReducer(getRootReducer())
.whenActionIsDispatched(
addVariable(toVariablePayload(datasource, { global: false, index: 0, model: datasource }))
)
.whenAsyncActionIsDispatched(
updateDataSourceVariableOptions(toVariableIdentifier(datasource), dependencies),
true
);
await tester.thenDispatchedActionsShouldEqual(
createDataSourceOptions(
toVariablePayload(
{ type: 'datasource', id: '0' },
{
sources: [
{ name: 'first-name', value: 'first-name', meta },
{ name: 'second-name', value: 'second-name', meta },
],
regex: /.*(second-name).*/,
}
)
),
setCurrentVariableValue(
toVariablePayload(
{ type: 'datasource', id: '0' },
{ option: { text: 'second-name', value: 'second-name', selected: false } }
)
)
);
expect(getListMock).toHaveBeenCalledTimes(1);
expect(getListMock).toHaveBeenCalledWith({ metrics: true, variables: false });
expect(getDatasourceSrvMock).toHaveBeenCalledTimes(1);
});
});
});
describe('when initDataSourceVariableEditor is dispatched', () => {
it('then the correct actions are dispatched', async () => {
const meta = getMockPlugin({ name: 'mock-data-name', id: 'mock-data-id' });
const sources: DataSourceInstanceSettings[] = [
getDataSourceInstanceSetting('first-name', meta),
getDataSourceInstanceSetting('second-name', meta),
];
const { dependencies, getListMock, getDatasourceSrvMock } = getTestContext({ sources });
await reduxTester<{ templating: TemplatingState }>()
.givenRootReducer(getRootReducer())
.whenActionIsDispatched(initDataSourceVariableEditor(dependencies))
.thenDispatchedActionsShouldEqual(
changeVariableEditorExtended({
propName: 'dataSourceTypes',
propValue: [
{ text: '', value: '' },
{ text: 'mock-data-name', value: 'mock-data-id' },
],
})
);
expect(getListMock).toHaveBeenCalledTimes(1);
expect(getListMock).toHaveBeenCalledWith({ metrics: true, variables: true });
expect(getDatasourceSrvMock).toHaveBeenCalledTimes(1);
});
});
});
function getDataSourceInstanceSetting(name: string, meta: DataSourcePluginMeta): DataSourceInstanceSettings {
return {
id: 1,
uid: '',
type: '',
name,
meta,
jsonData: ({} as unknown) as DataSourceJsonData,
};
}