getVariableQueryEditor.test.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { VariableSupportType } from '@grafana/data';
  2. import { LegacyVariableQueryEditor } from './LegacyVariableQueryEditor';
  3. import { getVariableQueryEditor, StandardVariableQueryEditor } from './getVariableQueryEditor';
  4. describe('getVariableQueryEditor', () => {
  5. describe('happy cases', () => {
  6. describe('when called with a data source with custom variable support', () => {
  7. it('then it should return correct editor', async () => {
  8. const editor: any = StandardVariableQueryEditor;
  9. const datasource: any = {
  10. variables: { getType: () => VariableSupportType.Custom, query: () => undefined, editor },
  11. };
  12. const result = await getVariableQueryEditor(datasource);
  13. expect(result).toBe(editor);
  14. });
  15. });
  16. describe('when called with a data source with standard variable support', () => {
  17. it('then it should return correct editor', async () => {
  18. const editor: any = StandardVariableQueryEditor;
  19. const datasource: any = {
  20. variables: { getType: () => VariableSupportType.Standard, toDataQuery: () => undefined },
  21. };
  22. const result = await getVariableQueryEditor(datasource);
  23. expect(result).toBe(editor);
  24. });
  25. });
  26. describe('when called with a data source with datasource variable support', () => {
  27. it('then it should return correct editor', async () => {
  28. const editor: any = StandardVariableQueryEditor;
  29. const plugin = { components: { QueryEditor: editor } };
  30. const importDataSourcePluginFunc = jest.fn().mockResolvedValue(plugin);
  31. const datasource: any = { variables: { getType: () => VariableSupportType.Datasource }, meta: {} };
  32. const result = await getVariableQueryEditor(datasource, importDataSourcePluginFunc);
  33. expect(result).toBe(editor);
  34. expect(importDataSourcePluginFunc).toHaveBeenCalledTimes(1);
  35. expect(importDataSourcePluginFunc).toHaveBeenCalledWith({});
  36. });
  37. });
  38. describe('when called with a data source with legacy variable support', () => {
  39. it('then it should return correct editor', async () => {
  40. const editor: any = StandardVariableQueryEditor;
  41. const plugin = { components: { VariableQueryEditor: editor } };
  42. const importDataSourcePluginFunc = jest.fn().mockResolvedValue(plugin);
  43. const datasource: any = { metricFindQuery: () => undefined, meta: {} };
  44. const result = await getVariableQueryEditor(datasource, importDataSourcePluginFunc);
  45. expect(result).toBe(editor);
  46. expect(importDataSourcePluginFunc).toHaveBeenCalledTimes(1);
  47. expect(importDataSourcePluginFunc).toHaveBeenCalledWith({});
  48. });
  49. });
  50. });
  51. describe('negative cases', () => {
  52. describe('when variable support is not recognized', () => {
  53. it('then it should return null', async () => {
  54. const datasource: any = {};
  55. const result = await getVariableQueryEditor(datasource);
  56. expect(result).toBeNull();
  57. });
  58. });
  59. describe('when called with a data source with datasource variable support but missing QueryEditor', () => {
  60. it('then it should return throw', async () => {
  61. const plugin = { components: {} };
  62. const importDataSourcePluginFunc = jest.fn().mockResolvedValue(plugin);
  63. const datasource: any = { variables: { getType: () => VariableSupportType.Datasource }, meta: {} };
  64. await expect(getVariableQueryEditor(datasource, importDataSourcePluginFunc)).rejects.toThrow(
  65. new Error('Missing QueryEditor in plugin definition.')
  66. );
  67. expect(importDataSourcePluginFunc).toHaveBeenCalledTimes(1);
  68. expect(importDataSourcePluginFunc).toHaveBeenCalledWith({});
  69. });
  70. });
  71. describe('when called with a data source with legacy variable support but missing VariableQueryEditor', () => {
  72. it('then it should return LegacyVariableQueryEditor', async () => {
  73. const plugin = { components: {} };
  74. const importDataSourcePluginFunc = jest.fn().mockResolvedValue(plugin);
  75. const datasource: any = { metricFindQuery: () => undefined, meta: {} };
  76. const result = await getVariableQueryEditor(datasource, importDataSourcePluginFunc);
  77. expect(result).toBe(LegacyVariableQueryEditor);
  78. expect(importDataSourcePluginFunc).toHaveBeenCalledTimes(1);
  79. expect(importDataSourcePluginFunc).toHaveBeenCalledWith({});
  80. });
  81. });
  82. });
  83. });