grafana/public/app/features/variables/datasource/actions.test.ts
Torkel Ödegaard 3d6380a0aa
QueryGroup & DataSourceSrv & DataSourcePicker changes simplify usage, error handling and reduce duplication, support for uid (#29542)
* Starting moving more stuff into data source picker

* WIP progress

* Progress on datasource picker rethink

* Things are working now some details to figure out

* Removed commented part

* Complex work on getting data source lists

* Fixed variable support showing correct data sources

* Tried fixing dashboard import but failed

* Fixes

* Fixed import dashboard

* Fixed unit test

* Fixed explore test

* Fixed test

* Fix

* fixed more tests

* fixed more tests

* fixed showing which option is default in picker

* Changed query variable to use data source picker, updated tests and e2e

* Fixed more tests

* Updated snapshots, had wrong typescript version
2020-12-04 14:24:55 +01:00

174 lines
6.8 KiB
TypeScript

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 { DataSourcePluginMeta, DataSourceSelectItem } from '@grafana/data';
import { getMockPlugin } from '../../plugins/__mocks__/pluginMocks';
import { createDataSourceOptions } from './reducer';
import { setCurrentVariableValue, addVariable } from '../state/sharedReducer';
import { changeVariableEditorExtended } from '../editor/reducer';
import { datasourceBuilder } from '../shared/testing/builders';
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 sources: DataSourceSelectItem[] = [
{
name: 'first-name',
value: 'first-value',
meta: getMockPlugin({ name: 'mock-data-name', id: 'mock-data-id' }),
},
{
name: 'second-name',
value: 'second-value',
meta: getMockPlugin({ name: 'mock-data-name', id: 'mock-data-id' }),
},
];
const getMetricSourcesMock = jest.fn().mockResolvedValue(sources);
const getDatasourceSrvMock = jest.fn().mockReturnValue({ getMetricSources: getMetricSourcesMock });
const dependencies: DataSourceVariableActionDependencies = { getDatasourceSrv: getDatasourceSrvMock };
const datasource = datasourceBuilder()
.withId('0')
.withQuery('mock-data-id')
.build();
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, regex: (undefined as unknown) as RegExp })
),
setCurrentVariableValue(
toVariablePayload(
{ type: 'datasource', id: '0' },
{ option: { text: 'first-name', value: 'first-name', selected: false } }
)
)
);
expect(getMetricSourcesMock).toHaveBeenCalledTimes(1);
expect(getMetricSourcesMock).toHaveBeenCalledWith({ skipVariables: true });
expect(getDatasourceSrvMock).toHaveBeenCalledTimes(1);
});
});
describe('and there is a regex', () => {
it('then the correct actions are dispatched', async () => {
const sources: DataSourceSelectItem[] = [
{
name: 'first-name',
value: 'first-value',
meta: getMockPlugin({ name: 'mock-data-name', id: 'mock-data-id' }),
},
{
name: 'second-name',
value: 'second-value',
meta: getMockPlugin({ name: 'mock-data-name', id: 'mock-data-id' }),
},
];
const getMetricSourcesMock = jest.fn().mockResolvedValue(sources);
const getDatasourceSrvMock = jest.fn().mockReturnValue({ getMetricSources: getMetricSourcesMock });
const dependencies: DataSourceVariableActionDependencies = { getDatasourceSrv: getDatasourceSrvMock };
const datasource = datasourceBuilder()
.withId('0')
.withQuery('mock-data-id')
.withRegEx('/.*(second-name).*/')
.build();
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, regex: /.*(second-name).*/ })
),
setCurrentVariableValue(
toVariablePayload(
{ type: 'datasource', id: '0' },
{ option: { text: 'second-name', value: 'second-name', selected: false } }
)
)
);
expect(getMetricSourcesMock).toHaveBeenCalledTimes(1);
expect(getMetricSourcesMock).toHaveBeenCalledWith({ skipVariables: true });
expect(getDatasourceSrvMock).toHaveBeenCalledTimes(1);
});
});
});
describe('when initDataSourceVariableEditor is dispatched', () => {
it('then the correct actions are dispatched', async () => {
const sources: DataSourceSelectItem[] = [
{
name: 'first-name',
value: 'first-value',
meta: getMockPlugin({ name: 'mock-data-name', id: 'mock-data-id' }),
},
{
name: 'second-name',
value: 'second-value',
meta: getMockPlugin({ name: 'mock-data-name', id: 'mock-data-id' }),
},
{
name: 'mixed-name',
value: 'mixed-value',
meta: getMockPlugin(({
name: 'mixed-data-name',
id: 'mixed-data-id',
mixed: true,
} as unknown) as DataSourcePluginMeta),
},
];
const getMetricSourcesMock = jest.fn().mockResolvedValue(sources);
const getDatasourceSrvMock = jest.fn().mockReturnValue({ getMetricSources: getMetricSourcesMock });
const dependencies: DataSourceVariableActionDependencies = { getDatasourceSrv: getDatasourceSrvMock };
const tester = await reduxTester<{ templating: TemplatingState }>()
.givenRootReducer(getRootReducer())
.whenAsyncActionIsDispatched(initDataSourceVariableEditor(dependencies));
await tester.thenDispatchedActionsShouldEqual(
changeVariableEditorExtended({
propName: 'dataSourceTypes',
propValue: [
{ text: '', value: '' },
{ text: 'mock-data-name', value: 'mock-data-id' },
],
})
);
expect(getMetricSourcesMock).toHaveBeenCalledTimes(1);
expect(getMetricSourcesMock).toHaveBeenCalledWith();
expect(getDatasourceSrvMock).toHaveBeenCalledTimes(1);
});
});
});