grafana/public/app/features/variables/editor/getVariableQueryEditor.test.tsx

104 lines
4.4 KiB
TypeScript
Raw Normal View History

Variables: Adds new Api that allows proper QueryEditors for Query variables (#28217) * Initial * WIP * wip * Refactor: fixing types * Refactor: Fixed more typings * Feature: Moves TestData to new API * Feature: Moves CloudMonitoringDatasource to new API * Feature: Moves PrometheusDatasource to new Variables API * Refactor: Clean up comments * Refactor: changes to QueryEditorProps instead * Refactor: cleans up testdata, prometheus and cloud monitoring variable support * Refactor: adds variableQueryRunner * Refactor: adds props to VariableQueryEditor * Refactor: reverted Loki editor * Refactor: refactor queryrunner into smaller pieces * Refactor: adds upgrade query thunk * Tests: Updates old tests * Docs: fixes build errors for exported api * Tests: adds guard tests * Tests: adds QueryRunner tests * Tests: fixes broken tests * Tests: adds variableQueryObserver tests * Test: adds tests for operator functions * Test: adds VariableQueryRunner tests * Refactor: renames dataSource * Refactor: adds definition for standard variable support * Refactor: adds cancellation to OptionPicker * Refactor: changes according to Dominiks suggestion * Refactor:tt * Refactor: adds tests for factories * Refactor: restructuring a bit * Refactor: renames variableQueryRunner.ts * Refactor: adds quick exit when runRequest returns errors * Refactor: using TextArea from grafana/ui * Refactor: changed from interfaces to classes instead * Tests: fixes broken test * Docs: fixes doc issue count * Docs: fixes doc issue count * Refactor: Adds check for self referencing queries * Tests: fixed unused variable * Refactor: Changes comments
2020-11-18 08:10:32 -06:00
import { VariableSupportType } from '@grafana/data';
import { getVariableQueryEditor, StandardVariableQueryEditor } from './getVariableQueryEditor';
import { LegacyVariableQueryEditor } from './LegacyVariableQueryEditor';
describe('getVariableQueryEditor', () => {
describe('happy cases', () => {
describe('when called with a data source with custom variable support', () => {
it('then it should return correct editor', async () => {
const editor: any = StandardVariableQueryEditor;
const datasource: any = {
variables: { getType: () => VariableSupportType.Custom, query: () => undefined, editor },
};
const result = await getVariableQueryEditor(datasource);
expect(result).toBe(editor);
});
});
describe('when called with a data source with standard variable support', () => {
it('then it should return correct editor', async () => {
const editor: any = StandardVariableQueryEditor;
const datasource: any = {
variables: { getType: () => VariableSupportType.Standard, toDataQuery: () => undefined },
};
const result = await getVariableQueryEditor(datasource);
expect(result).toBe(editor);
});
});
describe('when called with a data source with datasource variable support', () => {
it('then it should return correct editor', async () => {
const editor: any = StandardVariableQueryEditor;
const plugin = { components: { QueryEditor: editor } };
const importDataSourcePluginFunc = jest.fn().mockResolvedValue(plugin);
const datasource: any = { variables: { getType: () => VariableSupportType.Datasource }, meta: {} };
const result = await getVariableQueryEditor(datasource, importDataSourcePluginFunc);
expect(result).toBe(editor);
expect(importDataSourcePluginFunc).toHaveBeenCalledTimes(1);
expect(importDataSourcePluginFunc).toHaveBeenCalledWith({});
});
});
describe('when called with a data source with legacy variable support', () => {
it('then it should return correct editor', async () => {
const editor: any = StandardVariableQueryEditor;
const plugin = { components: { VariableQueryEditor: editor } };
const importDataSourcePluginFunc = jest.fn().mockResolvedValue(plugin);
const datasource: any = { metricFindQuery: () => undefined, meta: {} };
const result = await getVariableQueryEditor(datasource, importDataSourcePluginFunc);
expect(result).toBe(editor);
expect(importDataSourcePluginFunc).toHaveBeenCalledTimes(1);
expect(importDataSourcePluginFunc).toHaveBeenCalledWith({});
});
});
});
describe('negative cases', () => {
describe('when variable support is not recognized', () => {
it('then it should return null', async () => {
const datasource: any = {};
const result = await getVariableQueryEditor(datasource);
expect(result).toBeNull();
});
});
describe('when called with a data source with datasource variable support but missing QueryEditor', () => {
it('then it should return throw', async () => {
const plugin = { components: {} };
const importDataSourcePluginFunc = jest.fn().mockResolvedValue(plugin);
const datasource: any = { variables: { getType: () => VariableSupportType.Datasource }, meta: {} };
await expect(getVariableQueryEditor(datasource, importDataSourcePluginFunc)).rejects.toThrow(
new Error('Missing QueryEditor in plugin definition.')
);
expect(importDataSourcePluginFunc).toHaveBeenCalledTimes(1);
expect(importDataSourcePluginFunc).toHaveBeenCalledWith({});
});
});
describe('when called with a data source with legacy variable support but missing VariableQueryEditor', () => {
it('then it should return LegacyVariableQueryEditor', async () => {
const plugin = { components: {} };
const importDataSourcePluginFunc = jest.fn().mockResolvedValue(plugin);
const datasource: any = { metricFindQuery: () => undefined, meta: {} };
const result = await getVariableQueryEditor(datasource, importDataSourcePluginFunc);
expect(result).toBe(LegacyVariableQueryEditor);
expect(importDataSourcePluginFunc).toHaveBeenCalledTimes(1);
expect(importDataSourcePluginFunc).toHaveBeenCalledWith({});
});
});
});
});