grafana/public/app/features/variables/datasource/actions.test.ts
Hugo Häggmark 3c1f27b0e6
Chore: reduce strict errors for variables (#31241)
* Chore: reduces a lot of variable errors

* Chore: reduces variable Editor errors

* Chore: reduces variable Picker errors

* Chore: reduce error count

* Chore: reduces errors for ChangeEvent instead of FormEvent

* Chore: reduces errors with CombinedState

* Chore: reduces ComponentType errors

* Chore: reduce errors in reducers

* Chore: reduces misc errors

* Chore: reduce AdhocPicker errors

* Chore: reduce error limit

* Update public/app/features/variables/adhoc/picker/AdHocFilterValue.tsx

Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com>

* Chore: updates after PR comments

* Chore: small refactor

Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com>
2021-02-18 06:21:35 +01:00

163 lines
6.2 KiB
TypeScript

import { DataSourceInstanceSettings } from '@grafana/data';
import { reduxTester } from '../../../../test/core/redux/reduxTester';
import { getRootReducer, RootReducerType } from '../state/helpers';
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';
import { addVariable, setCurrentVariableValue } from '../state/sharedReducer';
import { changeVariableEditorExtended } from '../editor/reducer';
import { datasourceBuilder } from '../shared/testing/builders';
import { getDataSourceInstanceSetting } from '../shared/testing/helpers';
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',
});
const tester = await reduxTester<RootReducerType>()
.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(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<RootReducerType>()
.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(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<RootReducerType>()
.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);
});
});
});