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