CompletionItemProvider.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { getTemplateSrv } from '@grafana/runtime';
  2. import { Monaco, monacoTypes } from '@grafana/ui';
  3. import * as MetricMathTestData from '../../__mocks__/metric-math-test-data';
  4. import MonacoMock from '../../__mocks__/monarch/Monaco';
  5. import TextModel from '../../__mocks__/monarch/TextModel';
  6. import { CloudWatchDatasource } from '../../datasource';
  7. import cloudWatchMetricMathLanguageDefinition from '../definition';
  8. import {
  9. METRIC_MATH_FNS,
  10. METRIC_MATH_KEYWORDS,
  11. METRIC_MATH_OPERATORS,
  12. METRIC_MATH_PERIODS,
  13. METRIC_MATH_STATISTIC_KEYWORD_STRINGS,
  14. } from '../language';
  15. import { MetricMathCompletionItemProvider } from './CompletionItemProvider';
  16. const getSuggestions = async (value: string, position: monacoTypes.IPosition) => {
  17. const setup = new MetricMathCompletionItemProvider(
  18. {
  19. getVariables: () => [],
  20. getActualRegion: () => 'us-east-2',
  21. } as any as CloudWatchDatasource,
  22. getTemplateSrv()
  23. );
  24. const monaco = MonacoMock as Monaco;
  25. const provider = setup.getCompletionProvider(monaco, cloudWatchMetricMathLanguageDefinition);
  26. const { suggestions } = await provider.provideCompletionItems(
  27. TextModel(value) as monacoTypes.editor.ITextModel,
  28. position
  29. );
  30. return suggestions;
  31. };
  32. describe('MetricMath: CompletionItemProvider', () => {
  33. describe('getSuggestions', () => {
  34. it('returns a suggestion for every metric math function when the input field is empty', async () => {
  35. const { query, position } = MetricMathTestData.singleLineEmptyQuery;
  36. const suggestions = await getSuggestions(query, position);
  37. expect(suggestions.length).toEqual(METRIC_MATH_FNS.length);
  38. });
  39. it('returns a suggestion for every metric math operator when at the end of a function', async () => {
  40. const { query, position } = MetricMathTestData.afterFunctionQuery;
  41. const suggestions = await getSuggestions(query, position);
  42. expect(suggestions.length).toEqual(METRIC_MATH_OPERATORS.length);
  43. });
  44. it('returns a suggestion for every metric math function and keyword if at the start of the second argument of a function', async () => {
  45. const { query, position } = MetricMathTestData.secondArgQuery;
  46. const suggestions = await getSuggestions(query, position);
  47. expect(suggestions.length).toEqual(METRIC_MATH_FNS.length + METRIC_MATH_KEYWORDS.length);
  48. });
  49. it('does not have any particular suggestions if within a string', async () => {
  50. const { query, position } = MetricMathTestData.withinStringQuery;
  51. const suggestions = await getSuggestions(query, position);
  52. expect(suggestions.length).toEqual(0);
  53. });
  54. it('returns a suggestion for every statistic if the second arg of a search function', async () => {
  55. const { query, position } = MetricMathTestData.secondArgAfterSearchQuery;
  56. const suggestions = await getSuggestions(query, position);
  57. expect(suggestions.length).toEqual(METRIC_MATH_STATISTIC_KEYWORD_STRINGS.length);
  58. });
  59. it('returns a suggestion for every period if the third arg of a search function', async () => {
  60. const { query, position } = MetricMathTestData.thirdArgAfterSearchQuery;
  61. const suggestions = await getSuggestions(query, position);
  62. expect(suggestions.length).toEqual(METRIC_MATH_PERIODS.length);
  63. });
  64. });
  65. });