2021-02-16 06:51:34 +01:00
|
|
|
import { DataSourceInstanceSettings } from '@grafana/data';
|
|
|
|
|
|
2020-03-16 06:32:04 +01:00
|
|
|
import { reduxTester } from '../../../../test/core/redux/reduxTester';
|
2021-02-18 06:21:35 +01:00
|
|
|
import { getRootReducer, RootReducerType } from '../state/helpers';
|
2020-03-16 06:32:04 +01:00
|
|
|
import { toVariableIdentifier, toVariablePayload } from '../state/types';
|
|
|
|
|
import { variableAdapters } from '../adapters';
|
|
|
|
|
import { createDataSourceVariableAdapter } from './adapter';
|
|
|
|
|
import {
|
|
|
|
|
DataSourceVariableActionDependencies,
|
|
|
|
|
initDataSourceVariableEditor,
|
|
|
|
|
updateDataSourceVariableOptions,
|
|
|
|
|
} from './actions';
|
|
|
|
|
import { getMockPlugin } from '../../plugins/__mocks__/pluginMocks';
|
|
|
|
|
import { createDataSourceOptions } from './reducer';
|
2020-12-09 11:18:58 +01:00
|
|
|
import { addVariable, setCurrentVariableValue } from '../state/sharedReducer';
|
2020-03-16 06:32:04 +01:00
|
|
|
import { changeVariableEditorExtended } from '../editor/reducer';
|
2020-03-23 13:45:08 +01:00
|
|
|
import { datasourceBuilder } from '../shared/testing/builders';
|
2021-02-16 06:51:34 +01:00
|
|
|
import { getDataSourceInstanceSetting } from '../shared/testing/helpers';
|
2020-03-16 06:32:04 +01:00
|
|
|
|
2020-12-09 11:18:58 +01:00
|
|
|
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 };
|
2021-01-20 07:59:48 +01:00
|
|
|
const datasource = datasourceBuilder().withId('0').withQuery(query).withRegEx(regex).build();
|
2020-12-09 11:18:58 +01:00
|
|
|
|
|
|
|
|
return { getListMock, getDatasourceSrvMock, dependencies, datasource };
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 06:32:04 +01:00
|
|
|
describe('data source actions', () => {
|
2020-03-23 15:33:17 +01:00
|
|
|
variableAdapters.setInit(() => [createDataSourceVariableAdapter()]);
|
2020-03-16 06:32:04 +01:00
|
|
|
|
|
|
|
|
describe('when updateDataSourceVariableOptions is dispatched', () => {
|
|
|
|
|
describe('and there is no regex', () => {
|
|
|
|
|
it('then the correct actions are dispatched', async () => {
|
2020-12-09 11:18:58 +01:00
|
|
|
const meta = getMockPlugin({ name: 'mock-data-name', id: 'mock-data-id' });
|
|
|
|
|
const sources: DataSourceInstanceSettings[] = [
|
|
|
|
|
getDataSourceInstanceSetting('first-name', meta),
|
|
|
|
|
getDataSourceInstanceSetting('second-name', meta),
|
2020-03-16 06:32:04 +01:00
|
|
|
];
|
2020-12-09 11:18:58 +01:00
|
|
|
const { datasource, dependencies, getListMock, getDatasourceSrvMock } = getTestContext({
|
|
|
|
|
sources,
|
|
|
|
|
query: 'mock-data-id',
|
|
|
|
|
});
|
2020-03-23 09:00:36 +01:00
|
|
|
|
2021-02-18 06:21:35 +01:00
|
|
|
const tester = await reduxTester<RootReducerType>()
|
2020-04-03 09:38:14 +02:00
|
|
|
.givenRootReducer(getRootReducer())
|
|
|
|
|
.whenActionIsDispatched(
|
|
|
|
|
addVariable(toVariablePayload(datasource, { global: false, index: 0, model: datasource }))
|
|
|
|
|
)
|
2020-03-16 06:32:04 +01:00
|
|
|
.whenAsyncActionIsDispatched(
|
|
|
|
|
updateDataSourceVariableOptions(toVariableIdentifier(datasource), dependencies),
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
|
2020-03-16 13:45:51 +01:00
|
|
|
await tester.thenDispatchedActionsShouldEqual(
|
2020-03-16 06:32:04 +01:00
|
|
|
createDataSourceOptions(
|
2020-12-09 11:18:58 +01:00
|
|
|
toVariablePayload(
|
|
|
|
|
{ type: 'datasource', id: '0' },
|
|
|
|
|
{
|
2021-02-16 06:51:34 +01:00
|
|
|
sources,
|
2022-02-02 12:02:32 +00:00
|
|
|
regex: undefined as unknown as RegExp,
|
2020-12-09 11:18:58 +01:00
|
|
|
}
|
|
|
|
|
)
|
2020-03-16 06:32:04 +01:00
|
|
|
),
|
|
|
|
|
setCurrentVariableValue(
|
|
|
|
|
toVariablePayload(
|
2020-03-23 13:45:08 +01:00
|
|
|
{ type: 'datasource', id: '0' },
|
2020-03-16 06:32:04 +01:00
|
|
|
{ option: { text: 'first-name', value: 'first-name', selected: false } }
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2020-12-09 11:18:58 +01:00
|
|
|
expect(getListMock).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(getListMock).toHaveBeenCalledWith({ metrics: true, variables: false });
|
2020-03-16 06:32:04 +01:00
|
|
|
expect(getDatasourceSrvMock).toHaveBeenCalledTimes(1);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('and there is a regex', () => {
|
|
|
|
|
it('then the correct actions are dispatched', async () => {
|
2020-12-09 11:18:58 +01:00
|
|
|
const meta = getMockPlugin({ name: 'mock-data-name', id: 'mock-data-id' });
|
|
|
|
|
const sources: DataSourceInstanceSettings[] = [
|
|
|
|
|
getDataSourceInstanceSetting('first-name', meta),
|
|
|
|
|
getDataSourceInstanceSetting('second-name', meta),
|
2020-03-16 06:32:04 +01:00
|
|
|
];
|
|
|
|
|
|
2020-12-09 11:18:58 +01:00
|
|
|
const { datasource, dependencies, getListMock, getDatasourceSrvMock } = getTestContext({
|
|
|
|
|
sources,
|
|
|
|
|
query: 'mock-data-id',
|
|
|
|
|
regex: '/.*(second-name).*/',
|
|
|
|
|
});
|
|
|
|
|
|
2021-02-18 06:21:35 +01:00
|
|
|
const tester = await reduxTester<RootReducerType>()
|
2020-04-03 09:38:14 +02:00
|
|
|
.givenRootReducer(getRootReducer())
|
|
|
|
|
.whenActionIsDispatched(
|
|
|
|
|
addVariable(toVariablePayload(datasource, { global: false, index: 0, model: datasource }))
|
|
|
|
|
)
|
2020-03-16 06:32:04 +01:00
|
|
|
.whenAsyncActionIsDispatched(
|
|
|
|
|
updateDataSourceVariableOptions(toVariableIdentifier(datasource), dependencies),
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
|
2020-03-16 13:45:51 +01:00
|
|
|
await tester.thenDispatchedActionsShouldEqual(
|
2020-03-16 06:32:04 +01:00
|
|
|
createDataSourceOptions(
|
2020-12-09 11:18:58 +01:00
|
|
|
toVariablePayload(
|
|
|
|
|
{ type: 'datasource', id: '0' },
|
|
|
|
|
{
|
2021-02-16 06:51:34 +01:00
|
|
|
sources,
|
2020-12-09 11:18:58 +01:00
|
|
|
regex: /.*(second-name).*/,
|
|
|
|
|
}
|
|
|
|
|
)
|
2020-03-16 06:32:04 +01:00
|
|
|
),
|
|
|
|
|
setCurrentVariableValue(
|
|
|
|
|
toVariablePayload(
|
2020-03-23 13:45:08 +01:00
|
|
|
{ type: 'datasource', id: '0' },
|
2020-03-16 06:32:04 +01:00
|
|
|
{ option: { text: 'second-name', value: 'second-name', selected: false } }
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2020-12-09 11:18:58 +01:00
|
|
|
expect(getListMock).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(getListMock).toHaveBeenCalledWith({ metrics: true, variables: false });
|
2020-03-16 06:32:04 +01:00
|
|
|
expect(getDatasourceSrvMock).toHaveBeenCalledTimes(1);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('when initDataSourceVariableEditor is dispatched', () => {
|
|
|
|
|
it('then the correct actions are dispatched', async () => {
|
2020-12-09 11:18:58 +01:00
|
|
|
const meta = getMockPlugin({ name: 'mock-data-name', id: 'mock-data-id' });
|
|
|
|
|
const sources: DataSourceInstanceSettings[] = [
|
|
|
|
|
getDataSourceInstanceSetting('first-name', meta),
|
|
|
|
|
getDataSourceInstanceSetting('second-name', meta),
|
2020-03-16 06:32:04 +01:00
|
|
|
];
|
|
|
|
|
|
2020-12-09 11:18:58 +01:00
|
|
|
const { dependencies, getListMock, getDatasourceSrvMock } = getTestContext({ sources });
|
2020-03-16 06:32:04 +01:00
|
|
|
|
2021-02-18 06:21:35 +01:00
|
|
|
await reduxTester<RootReducerType>()
|
2020-04-03 09:38:14 +02:00
|
|
|
.givenRootReducer(getRootReducer())
|
2020-12-09 11:18:58 +01:00
|
|
|
.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 });
|
2020-03-16 06:32:04 +01:00
|
|
|
expect(getDatasourceSrvMock).toHaveBeenCalledTimes(1);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|