grafana/public/app/features/variables/editor/getVariableQueryEditor.test.tsx
Josh Hunt 3c6e0e8ef8
Chore: ESlint import order (#44959)
* Add and configure eslint-plugin-import

* Fix the lint:ts npm command

* Autofix + prettier all the files

* Manually fix remaining files

* Move jquery code in jest-setup to external file to safely reorder imports

* Resolve issue caused by circular dependencies within Prometheus

* Update .betterer.results

* Fix missing // @ts-ignore

* ignore iconBundle.ts

* Fix missing // @ts-ignore
2022-04-22 14:33:13 +01:00

105 lines
4.4 KiB
TypeScript

import { VariableSupportType } from '@grafana/data';
import { LegacyVariableQueryEditor } from './LegacyVariableQueryEditor';
import { getVariableQueryEditor, StandardVariableQueryEditor } from './getVariableQueryEditor';
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({});
});
});
});
});