mirror of
https://github.com/grafana/grafana.git
synced 2025-02-15 01:53:33 -06:00
* Refactor: Adds variables in state to TemplateSrv * Refactor: Introduces some typings and structures * Refactor: Introduces picker to adapter * Refactor: Removes useState and introduces contains instead * Refactor: Introduces Variable Editor * Refactor: Adds uuid and simplifies state * Refactor: Consolidates to VariableRenderer * Refactor: Adds name change capability * Refactor: Adds variableMiddleware * Refactor: Adds ability to change Angular type to type in State * Fix: Fixes so we check for duplicate names * Refactor: Adds ability to change State type to type in Angular * Refactor: Updates name in dashboard templating list * Refactor: Adds label change to VariableEditor * Refactor: Adds hide change to VariableEditor * Refactor: Adds update ability * Refactor: Adds tooltip * Refactor: Adds SelectionOptionsEditor * Refactor: Adds query editor and validation * Refactor: Adds regex and sort to editor * Refactor: Adds Selection options * Refactor: Adds Varible Values Previewer * Refactor: Changes from array in state to Record * Refactor: Removes getVariableAtIndex from templateSrv * Tests: Fixs broken tests * Chore: Fixes duplicate merge import * Refactor: Removes strict null errors * Refactor: Adds duplicate variable * Refactor: Adds remove variable * Refactor: Adds change order of variables * Refactor: Adds add new variable * Chore: Fixes Prettier formatting * Refactor: Adds VariablePicker * Fixed so sub menu is displayed when we only have redux template variables. * removed unused variable. * tags will be visibile in the new react picker. * added some nice colors to the tags. * Added thunk for selecting a tag. * Refactor: Cleans up templating state when dashboard unloads * Refactor: Adds save capabilities for variables in state * added possibility to select tag. * Added so you can deselect a tag. * Fixed issue with coloring on top. * minor refactoring to make the code more slim. * Refactor: Fixes dispatch return and copy of variable * selecting options when tag i selected * small refactoring. * fixed so we use options. * Refactor: Adds getValueForUrl capabilities * first implementation of keyboard navigation on picker. * removed comment. * fixed so you can toggle all options. * Refactor: Simplified state handling using Redux Toolkit and flat reducer structure * Refactor: Adds sharedTemplatingReducer and queryVariableReducer * Tests: Fixs broken tests * Chore: Removes some strict null errors * Tests: Fix broken tests * Refactor: Splitted QueryVariablePicker into smaller components * Refactor: Moves linktext and selected tags to component instead * Fix: Fixes the ability to have multiple dropdowns opened at same time * Fix: Fixes onKeyDown from prev refactor * Refactor: Adds searchfilter searching * Tests: Fixes after running e2e tests * Refactor: Adds an attempt to solve dependencies at startup * Refactor: Adds feature toggle * Refactor: Resets all angular files to master * Refactor: Move stuff to query folder * Refactor: Initial commit for SubMenu component * Refactor: Updated DashboardModel with new list * Refactor: Adds feature toggle to dashboard model and friends * Refactor: Adds picker to SubMenu * Refactor: Fixes styling on SubMenu * Refactor: Fixes processvariables * Refactor: Initial EditorList skeleton * Refactor: Refactors out VariableEditorList and VariableEditorContainer * Refactor: Adds New variable functionality * Refactor: Adds registred types * Refactor: Adds edit existing variable functionality * Refactor: Changes params to thunks * Refactor: Small fix for cleaning up state when clicking update/add * Refactor: Better typings for outer containers * Refactor: Adds change order functionality * Refactor: Removed notify angular args * Change so the url is in sync with the redux template variables. * Adding support for saving proper values and checking changes. * Refactor: Adds duplicate variable functionality * Feature: Adds remove variable functionality * Refactor: Small refactor so e2e tests work as before * Refactor: Returns null if no visible variables * Refactor: Adds annotations to SubMenu * Refactor: Fixes toggling of annotations in SubMenu * added dashboard links to new submehu. * Refactor: Small refactor breaking up into smaller components * Fix: Fixes infinite recursive loop when changing varible name * Templating: Do not mutate location query state * Refactor: Fixes minor timing issue when adding new variable * Refactor: removes initialization in variable_srv constructor * Refactor: Suggestion on how to handle templating.list in DashboardExporter * Refactor: Adds getVariables typings and changes ChangeTracker and ShareSnapshotCtrl * Refactor: Adds getVariable on DashboardModel and changes DashboardMigrator * Fix: Fixes repeated panels * wip: starting to add custom variable type. * Refactor: Merging two different toVariablePayload functions * Tests: Fixes broken tests * Fix: Reduces strict null errors * Tests: Initial commit and fixes strange dependency order * Tests: Covers sharedTemplatingReducer with tests * Refactor: Rename state/index.ts => state/reducers.ts as every where else * Refactor: Renames and moves adapters.ts * Tests: Adds tests for templatingReducer * Tests: Adds intitial tests for queryVariableReducer * starting to ad custom variable. * Tests: Adds more queryVariableReducer tests * Added support for custom variable. Next up applying some DRY principles and refactoring. * fixed compile issue. * added todo. * Tests: Fixes broken test * Tests: Covers queryVariableReducer with tests and fixed a couple of bugs * Fix: Fixes broken test * Fix: Reduces strict null errors * change so custom won't be depending on anything elese. * fixed descriptions. * removed unused dependency. * Fixed issue when adding a new variable and editor is being unmount twice. * fixed issue with select option loop. * changed so we update query on typing in editor and removed it from component state. * Moved runQuery up one level in the component tree. * renamed action and moved it to custom actions. * moved applyStateChanges to shared code. * removed todo comment. * first stab on moving picker to more general. * Refactor: Changes so we always show variables type * removed duplicate code regarding picker. * Did some renamings. * Feature: Adds text box variable type * moved tests from query reducer to picker reducer. * Removed picker from VariableState. * removed reference to picker. * Some more refactorings of the picker reducer + actions. * Chore: Refactors away editor state to its own state slice (#22515) * Refactor: Inital move, tests not working * Tests: Adds editorReducer tests * Refactor: Cleaning up * Refactor: Moves logic to thunk instead * Refactor: Initial commit * Refactor: Combines reducers to one state * Refactor: Adds combine reducers * moved navigation logic flow to a thunk instead of in the component. * fixed issue with rendering picker link. * Refactor: Removes variable prop from templating.variables * refactored and removed some more code. * Feature: adds Constant variable type * fixed so tags can be selected. * Fix: fixes default hide for constant and enum order * fixed so tags works again. * Fix: fixes so we use Angular editor when newVariables is not defined * Fix: fixes wrong hide default for Constant variable * Fix: fixes bug when using duplicate button * Fix: changes action id * Tests: prepares for newVariables * Chore: reduces strict null errors * Refactor: removes uuidInEditorReducer for simplification * Chore: changes after PR comments * Chore: uses getConfig instead of config * Tests: fixes so e2e tests check for feature toggle and fixed initLock bug * Refactor: changes so sharedReducer uses createSlice instead * Refactor: changes textBoxVariableReducer to use creactSlice instead * Refactor: changes queryBoxVariableReducer to use creactSlice instead * Refactor: changes customVariableReducer to use creactSlice instead * Refactor: changes constantVariableReducer to use creactSlice instead * Refactor: moves types to specific types.ts files instead * changed so we use queryValue stored on the variable to populate options input when opening picker. * Feature: adds the ability to test templating thunks using real store and middleware * Chore: cleans up unused import * excluded queryValue from the getSaveModel * Refactor: adds whenAsyncActionIsDispatched to reduxTester * Tests: adds initial tests for processVariables * Added reducer tests for constant variable. * added tests for custom reducer. * added tets for texbox reducer. * Tests: adds more tests for ProcessVariable * Refactor: fixes processVariable flow so we do notrun updateOptions twice * Tests: finishes tests for processVariables and removed skip test * added actions tests for custom and constant. * Tests: adds tests for setOptionFromUrl * Tests: adds a naive variable mock builder * Tests: adds tests for validateVariableSelectionState * added tests for query variable actions. * added last test for query actions. * added more tests. * some more tests. * fixed typing errors. * Fixed issues with variable tags. Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com> Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
573 lines
25 KiB
TypeScript
573 lines
25 KiB
TypeScript
import { variableAdapters } from '../adapters';
|
|
import { createQueryVariableAdapter } from './adapter';
|
|
import { reduxTester } from '../../../../test/core/redux/reduxTester';
|
|
import { getTemplatingRootReducer } from '../state/helpers';
|
|
import { QueryVariableModel, VariableHide, VariableSort, VariableRefresh } from '../variable';
|
|
import { toVariablePayload, ALL_VARIABLE_VALUE, ALL_VARIABLE_TEXT } from '../state/types';
|
|
import { setCurrentVariableValue, changeVariableProp } from '../state/sharedReducer';
|
|
import { initDashboardTemplating } from '../state/actions';
|
|
import { TemplatingState } from '../state/reducers';
|
|
import {
|
|
updateQueryVariableOptions,
|
|
initQueryVariableEditor,
|
|
changeQueryVariableDataSource,
|
|
changeQueryVariableQuery,
|
|
} from './actions';
|
|
import { updateVariableOptions, updateVariableTags } from './reducer';
|
|
import {
|
|
setIdInEditor,
|
|
removeVariableEditorError,
|
|
addVariableEditorError,
|
|
changeVariableEditorExtended,
|
|
} from '../editor/reducer';
|
|
import DefaultVariableQueryEditor from '../DefaultVariableQueryEditor';
|
|
import { expect } from 'test/lib/common';
|
|
|
|
const mocks: Record<string, any> = {
|
|
datasource: {
|
|
metricFindQuery: jest.fn().mockResolvedValue([]),
|
|
},
|
|
datasourceSrv: {
|
|
getMetricSources: jest.fn().mockReturnValue([]),
|
|
},
|
|
pluginLoader: {
|
|
importDataSourcePlugin: jest.fn().mockResolvedValue({ components: {} }),
|
|
},
|
|
};
|
|
|
|
jest.mock('../../plugins/datasource_srv', () => ({
|
|
getDatasourceSrv: jest.fn(() => ({
|
|
get: jest.fn((name: string) => mocks[name]),
|
|
getMetricSources: () => mocks.datasourceSrv.getMetricSources(),
|
|
})),
|
|
}));
|
|
|
|
jest.mock('../../plugins/plugin_loader', () => ({
|
|
importDataSourcePlugin: () => mocks.pluginLoader.importDataSourcePlugin(),
|
|
}));
|
|
|
|
describe('query actions', () => {
|
|
variableAdapters.set('query', createQueryVariableAdapter());
|
|
|
|
describe('when updateQueryVariableOptions is dispatched for variable with tags and includeAll', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const variable = createVariable({ includeAll: true });
|
|
const optionsMetrics = [createMetric('A'), createMetric('B')];
|
|
const tagsMetrics = [createMetric('tagA'), createMetric('tagB')];
|
|
|
|
mockDatasourceMetrics(variable, optionsMetrics, tagsMetrics);
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenAsyncActionIsDispatched(updateQueryVariableOptions(toVariablePayload(variable)), true);
|
|
|
|
const option = createOption(ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE);
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [updateOptions, updateTags, setCurrentAction] = actions;
|
|
const expectedNumberOfActions = 3;
|
|
|
|
expect(updateOptions).toEqual(updateVariableOptions(toVariablePayload(variable, optionsMetrics)));
|
|
expect(updateTags).toEqual(updateVariableTags(toVariablePayload(variable, tagsMetrics)));
|
|
expect(setCurrentAction).toEqual(setCurrentVariableValue(toVariablePayload(variable, { option })));
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when updateQueryVariableOptions is dispatched for variable with tags', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const variable = createVariable({ includeAll: false });
|
|
const optionsMetrics = [createMetric('A'), createMetric('B')];
|
|
const tagsMetrics = [createMetric('tagA'), createMetric('tagB')];
|
|
|
|
mockDatasourceMetrics(variable, optionsMetrics, tagsMetrics);
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenAsyncActionIsDispatched(updateQueryVariableOptions(toVariablePayload(variable)), true);
|
|
|
|
const option = createOption('A');
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [updateOptions, updateTags, setCurrentAction] = actions;
|
|
const expectedNumberOfActions = 3;
|
|
|
|
expect(updateOptions).toEqual(updateVariableOptions(toVariablePayload(variable, optionsMetrics)));
|
|
expect(updateTags).toEqual(updateVariableTags(toVariablePayload(variable, tagsMetrics)));
|
|
expect(setCurrentAction).toEqual(setCurrentVariableValue(toVariablePayload(variable, { option })));
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when updateQueryVariableOptions is dispatched for variable without both tags and includeAll', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const variable = createVariable({ includeAll: false, useTags: false });
|
|
const optionsMetrics = [createMetric('A'), createMetric('B')];
|
|
|
|
mockDatasourceMetrics(variable, optionsMetrics, []);
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenAsyncActionIsDispatched(updateQueryVariableOptions(toVariablePayload(variable)), true);
|
|
|
|
const option = createOption('A');
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [updateOptions, setCurrentAction] = actions;
|
|
const expectedNumberOfActions = 2;
|
|
|
|
expect(updateOptions).toEqual(updateVariableOptions(toVariablePayload(variable, optionsMetrics)));
|
|
expect(setCurrentAction).toEqual(setCurrentVariableValue(toVariablePayload(variable, { option })));
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when updateQueryVariableOptions is dispatched for variable with includeAll but without tags', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const variable = createVariable({ includeAll: true, useTags: false });
|
|
const optionsMetrics = [createMetric('A'), createMetric('B')];
|
|
|
|
mockDatasourceMetrics(variable, optionsMetrics, []);
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenAsyncActionIsDispatched(updateQueryVariableOptions(toVariablePayload(variable)), true);
|
|
|
|
const option = createOption(ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE);
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [updateOptions, setCurrentAction] = actions;
|
|
const expectedNumberOfActions = 2;
|
|
|
|
expect(updateOptions).toEqual(updateVariableOptions(toVariablePayload(variable, optionsMetrics)));
|
|
expect(setCurrentAction).toEqual(setCurrentVariableValue(toVariablePayload(variable, { option })));
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when updateQueryVariableOptions is dispatched for variable open in editor', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const variable = createVariable({ includeAll: true, useTags: false });
|
|
const optionsMetrics = [createMetric('A'), createMetric('B')];
|
|
|
|
mockDatasourceMetrics(variable, optionsMetrics, []);
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenActionIsDispatched(setIdInEditor({ id: variable.uuid }))
|
|
.whenAsyncActionIsDispatched(updateQueryVariableOptions(toVariablePayload(variable)), true);
|
|
|
|
const option = createOption(ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE);
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [clearErrors, updateOptions, setCurrentAction] = actions;
|
|
const expectedNumberOfActions = 3;
|
|
|
|
expect(clearErrors).toEqual(removeVariableEditorError({ errorProp: 'update' }));
|
|
expect(updateOptions).toEqual(updateVariableOptions(toVariablePayload(variable, optionsMetrics)));
|
|
expect(setCurrentAction).toEqual(setCurrentVariableValue(toVariablePayload(variable, { option })));
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when updateQueryVariableOptions is dispatched and fails for variable open in editor', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const variable = createVariable({ includeAll: true, useTags: false });
|
|
const error = { message: 'failed to fetch metrics' };
|
|
|
|
mocks[variable.datasource].metricFindQuery = jest.fn(() => Promise.reject(error));
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenActionIsDispatched(setIdInEditor({ id: variable.uuid }))
|
|
.whenAsyncActionIsDispatched(updateQueryVariableOptions(toVariablePayload(variable)), true);
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [clearErrors, errorOccurred] = actions;
|
|
const expectedNumberOfActions = 2;
|
|
|
|
expect(errorOccurred).toEqual(addVariableEditorError({ errorProp: 'update', errorText: error.message }));
|
|
expect(clearErrors).toEqual(removeVariableEditorError({ errorProp: 'update' }));
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when initQueryVariableEditor is dispatched', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const variable = createVariable({ includeAll: true, useTags: false });
|
|
const defaultMetricSource = { name: '', value: '', meta: {}, sort: '' };
|
|
const testMetricSource = { name: 'test', value: 'test', meta: {}, sort: '' };
|
|
const editor = {};
|
|
|
|
mocks.datasourceSrv.getMetricSources = jest.fn().mockReturnValue([testMetricSource]);
|
|
mocks.pluginLoader.importDataSourcePlugin = jest.fn().mockResolvedValue({
|
|
components: { VariableQueryEditor: editor },
|
|
});
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenAsyncActionIsDispatched(initQueryVariableEditor(toVariablePayload(variable)), true);
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [updateDatasources, setDatasource, setEditor] = actions;
|
|
const expectedNumberOfActions = 3;
|
|
|
|
expect(updateDatasources).toEqual(
|
|
changeVariableEditorExtended({ propName: 'dataSources', propValue: [defaultMetricSource, testMetricSource] })
|
|
);
|
|
expect(setDatasource).toEqual(
|
|
changeVariableEditorExtended({ propName: 'dataSource', propValue: mocks['datasource'] })
|
|
);
|
|
expect(setEditor).toEqual(changeVariableEditorExtended({ propName: 'VariableQueryEditor', propValue: editor }));
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when initQueryVariableEditor is dispatched and metricsource without value is available', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const variable = createVariable({ includeAll: true, useTags: false });
|
|
const defaultMetricSource = { name: '', value: '', meta: {}, sort: '' };
|
|
const testMetricSource = { name: 'test', value: null as string, meta: {}, sort: '' };
|
|
const editor = {};
|
|
|
|
mocks.datasourceSrv.getMetricSources = jest.fn().mockReturnValue([testMetricSource]);
|
|
mocks.pluginLoader.importDataSourcePlugin = jest.fn().mockResolvedValue({
|
|
components: { VariableQueryEditor: editor },
|
|
});
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenAsyncActionIsDispatched(initQueryVariableEditor(toVariablePayload(variable)), true);
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [updateDatasources, setDatasource, setEditor] = actions;
|
|
const expectedNumberOfActions = 3;
|
|
|
|
expect(updateDatasources).toEqual(
|
|
changeVariableEditorExtended({ propName: 'dataSources', propValue: [defaultMetricSource] })
|
|
);
|
|
expect(setDatasource).toEqual(
|
|
changeVariableEditorExtended({ propName: 'dataSource', propValue: mocks['datasource'] })
|
|
);
|
|
expect(setEditor).toEqual(changeVariableEditorExtended({ propName: 'VariableQueryEditor', propValue: editor }));
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when initQueryVariableEditor is dispatched and no metric sources was found', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const variable = createVariable({ includeAll: true, useTags: false });
|
|
const defaultDatasource = { name: '', value: '', meta: {}, sort: '' };
|
|
const editor = {};
|
|
|
|
mocks.datasourceSrv.getMetricSources = jest.fn().mockReturnValue([]);
|
|
mocks.pluginLoader.importDataSourcePlugin = jest.fn().mockResolvedValue({
|
|
components: { VariableQueryEditor: editor },
|
|
});
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenAsyncActionIsDispatched(initQueryVariableEditor(toVariablePayload(variable)), true);
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [updateDatasources, setDatasource, setEditor] = actions;
|
|
const expectedNumberOfActions = 3;
|
|
|
|
expect(updateDatasources).toEqual(
|
|
changeVariableEditorExtended({ propName: 'dataSources', propValue: [defaultDatasource] })
|
|
);
|
|
expect(setDatasource).toEqual(
|
|
changeVariableEditorExtended({ propName: 'dataSource', propValue: mocks['datasource'] })
|
|
);
|
|
expect(setEditor).toEqual(changeVariableEditorExtended({ propName: 'VariableQueryEditor', propValue: editor }));
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when initQueryVariableEditor is dispatched and variable dont have datasource', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const variable = createVariable({ datasource: undefined });
|
|
const ds = { name: '', value: '', meta: {}, sort: '' };
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenAsyncActionIsDispatched(initQueryVariableEditor(toVariablePayload(variable)), true);
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [updateDatasources] = actions;
|
|
const expectedNumberOfActions = 1;
|
|
|
|
expect(updateDatasources).toEqual(changeVariableEditorExtended({ propName: 'dataSources', propValue: [ds] }));
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when changeQueryVariableDataSource is dispatched', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const variable = createVariable({ datasource: 'other' });
|
|
const editor = {};
|
|
|
|
mocks.pluginLoader.importDataSourcePlugin = jest.fn().mockResolvedValue({
|
|
components: { VariableQueryEditor: editor },
|
|
});
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenAsyncActionIsDispatched(changeQueryVariableDataSource(toVariablePayload(variable), 'datasource'), true);
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [updateDatasource, updateEditor] = actions;
|
|
const expectedNumberOfActions = 2;
|
|
|
|
expect(updateDatasource).toEqual(
|
|
changeVariableEditorExtended({ propName: 'dataSource', propValue: mocks.datasource })
|
|
);
|
|
expect(updateEditor).toEqual(
|
|
changeVariableEditorExtended({ propName: 'VariableQueryEditor', propValue: editor })
|
|
);
|
|
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when changeQueryVariableDataSource is dispatched and editor is not configured', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const variable = createVariable({ datasource: 'other' });
|
|
const editor = DefaultVariableQueryEditor;
|
|
|
|
mocks.pluginLoader.importDataSourcePlugin = jest.fn().mockResolvedValue({
|
|
components: {},
|
|
});
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenAsyncActionIsDispatched(changeQueryVariableDataSource(toVariablePayload(variable), 'datasource'), true);
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [updateDatasource, updateEditor] = actions;
|
|
const expectedNumberOfActions = 2;
|
|
|
|
expect(updateDatasource).toEqual(
|
|
changeVariableEditorExtended({ propName: 'dataSource', propValue: mocks.datasource })
|
|
);
|
|
expect(updateEditor).toEqual(
|
|
changeVariableEditorExtended({ propName: 'VariableQueryEditor', propValue: editor })
|
|
);
|
|
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when changeQueryVariableQuery is dispatched', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const optionsMetrics = [createMetric('A'), createMetric('B')];
|
|
const tagsMetrics = [createMetric('tagA'), createMetric('tagB')];
|
|
const variable = createVariable({ datasource: 'datasource', useTags: true, includeAll: true });
|
|
|
|
const query = '$datasource';
|
|
const definition = 'depends on datasource variable';
|
|
|
|
mockDatasourceMetrics({ ...variable, query }, optionsMetrics, tagsMetrics);
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenAsyncActionIsDispatched(changeQueryVariableQuery(toVariablePayload(variable), query, definition), true);
|
|
|
|
const option = createOption(ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE);
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [clearError, changeQuery, changeDefinition, updateOptions, updateTags, setOption] = actions;
|
|
const expectedNumberOfActions = 6;
|
|
|
|
expect(clearError).toEqual(removeVariableEditorError({ errorProp: 'query' }));
|
|
expect(changeQuery).toEqual(
|
|
changeVariableProp(toVariablePayload(variable, { propName: 'query', propValue: query }))
|
|
);
|
|
expect(changeDefinition).toEqual(
|
|
changeVariableProp(toVariablePayload(variable, { propName: 'definition', propValue: definition }))
|
|
);
|
|
expect(updateOptions).toEqual(updateVariableOptions(toVariablePayload(variable, optionsMetrics)));
|
|
expect(updateTags).toEqual(updateVariableTags(toVariablePayload(variable, tagsMetrics)));
|
|
expect(setOption).toEqual(setCurrentVariableValue(toVariablePayload(variable, { option })));
|
|
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when changeQueryVariableQuery is dispatched for variable without tags', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const optionsMetrics = [createMetric('A'), createMetric('B')];
|
|
const variable = createVariable({ datasource: 'datasource', useTags: false, includeAll: true });
|
|
|
|
const query = '$datasource';
|
|
const definition = 'depends on datasource variable';
|
|
|
|
mockDatasourceMetrics({ ...variable, query }, optionsMetrics, []);
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenAsyncActionIsDispatched(changeQueryVariableQuery(toVariablePayload(variable), query, definition), true);
|
|
|
|
const option = createOption(ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE);
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [clearError, changeQuery, changeDefinition, updateOptions, setOption] = actions;
|
|
const expectedNumberOfActions = 5;
|
|
|
|
expect(clearError).toEqual(removeVariableEditorError({ errorProp: 'query' }));
|
|
expect(changeQuery).toEqual(
|
|
changeVariableProp(toVariablePayload(variable, { propName: 'query', propValue: query }))
|
|
);
|
|
expect(changeDefinition).toEqual(
|
|
changeVariableProp(toVariablePayload(variable, { propName: 'definition', propValue: definition }))
|
|
);
|
|
expect(updateOptions).toEqual(updateVariableOptions(toVariablePayload(variable, optionsMetrics)));
|
|
expect(setOption).toEqual(setCurrentVariableValue(toVariablePayload(variable, { option })));
|
|
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when changeQueryVariableQuery is dispatched for variable without tags and all', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const optionsMetrics = [createMetric('A'), createMetric('B')];
|
|
const variable = createVariable({ datasource: 'datasource', useTags: false, includeAll: false });
|
|
const query = '$datasource';
|
|
const definition = 'depends on datasource variable';
|
|
|
|
mockDatasourceMetrics({ ...variable, query }, optionsMetrics, []);
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenAsyncActionIsDispatched(changeQueryVariableQuery(toVariablePayload(variable), query, definition), true);
|
|
|
|
const option = createOption('A');
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [clearError, changeQuery, changeDefinition, updateOptions, setOption] = actions;
|
|
const expectedNumberOfActions = 5;
|
|
|
|
expect(clearError).toEqual(removeVariableEditorError({ errorProp: 'query' }));
|
|
expect(changeQuery).toEqual(
|
|
changeVariableProp(toVariablePayload(variable, { propName: 'query', propValue: query }))
|
|
);
|
|
expect(changeDefinition).toEqual(
|
|
changeVariableProp(toVariablePayload(variable, { propName: 'definition', propValue: definition }))
|
|
);
|
|
expect(updateOptions).toEqual(updateVariableOptions(toVariablePayload(variable, optionsMetrics)));
|
|
expect(setOption).toEqual(setCurrentVariableValue(toVariablePayload(variable, { option })));
|
|
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when changeQueryVariableQuery is dispatched with invalid query', () => {
|
|
it('then correct actions are dispatched', async () => {
|
|
const variable = createVariable({ datasource: 'datasource', useTags: false, includeAll: false });
|
|
const query = `$${variable.name}`;
|
|
const definition = 'depends on datasource variable';
|
|
|
|
const tester = await reduxTester<{ templating: TemplatingState }>()
|
|
.givenRootReducer(getTemplatingRootReducer())
|
|
.whenActionIsDispatched(initDashboardTemplating([variable]))
|
|
.whenAsyncActionIsDispatched(changeQueryVariableQuery(toVariablePayload(variable), query, definition), true);
|
|
|
|
const errorText = 'Query cannot contain a reference to itself. Variable: $' + variable.name;
|
|
|
|
tester.thenDispatchedActionPredicateShouldEqual(actions => {
|
|
const [editorError] = actions;
|
|
const expectedNumberOfActions = 1;
|
|
|
|
expect(editorError).toEqual(addVariableEditorError({ errorProp: 'query', errorText }));
|
|
return actions.length === expectedNumberOfActions;
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
function mockDatasourceMetrics(variable: QueryVariableModel, optionsMetrics: any[], tagsMetrics: any[]) {
|
|
const metrics: Record<string, any[]> = {
|
|
[variable.query]: optionsMetrics,
|
|
[variable.tagsQuery]: tagsMetrics,
|
|
};
|
|
|
|
const { metricFindQuery } = mocks[variable.datasource];
|
|
|
|
metricFindQuery.mockReset();
|
|
metricFindQuery.mockImplementation((query: string) => Promise.resolve(metrics[query] ?? []));
|
|
}
|
|
|
|
function createVariable(extend?: Partial<QueryVariableModel>): QueryVariableModel {
|
|
return {
|
|
type: 'query',
|
|
uuid: '0',
|
|
global: false,
|
|
current: createOption(''),
|
|
options: [],
|
|
query: 'options-query',
|
|
name: 'Constant',
|
|
label: '',
|
|
hide: VariableHide.dontHide,
|
|
skipUrlSync: false,
|
|
index: 0,
|
|
datasource: 'datasource',
|
|
definition: '',
|
|
sort: VariableSort.alphabeticalAsc,
|
|
tags: [],
|
|
tagsQuery: 'tags-query',
|
|
tagValuesQuery: '',
|
|
useTags: true,
|
|
refresh: VariableRefresh.onDashboardLoad,
|
|
regex: '',
|
|
multi: true,
|
|
includeAll: true,
|
|
...(extend ?? {}),
|
|
};
|
|
}
|
|
|
|
function createOption(text: string, value?: string) {
|
|
const metric = createMetric(text);
|
|
return {
|
|
...metric,
|
|
value: value ?? metric.value,
|
|
selected: false,
|
|
};
|
|
}
|
|
|
|
function createMetric(value: string) {
|
|
return {
|
|
value: value,
|
|
text: value,
|
|
};
|
|
}
|