CompletionItemProvider.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { CompletionItemPriority } from '@grafana/experimental';
  2. import { Monaco, monacoTypes } from '@grafana/ui';
  3. import { afterLabelValue, insideLabelValue } from '../__mocks__/dynamic-label-test-data';
  4. import MonacoMock from '../__mocks__/monarch/Monaco';
  5. import TextModel from '../__mocks__/monarch/TextModel';
  6. import { DynamicLabelsCompletionItemProvider } from './CompletionItemProvider';
  7. import cloudWatchDynamicLabelsLanguageDefinition from './definition';
  8. import { DYNAMIC_LABEL_PATTERNS } from './language';
  9. const getSuggestions = async (value: string, position: monacoTypes.IPosition) => {
  10. const setup = new DynamicLabelsCompletionItemProvider();
  11. const monaco = MonacoMock as Monaco;
  12. const provider = setup.getCompletionProvider(monaco, cloudWatchDynamicLabelsLanguageDefinition);
  13. const { suggestions } = await provider.provideCompletionItems(
  14. TextModel(value) as monacoTypes.editor.ITextModel,
  15. position
  16. );
  17. return suggestions;
  18. };
  19. describe('Dynamic labels: CompletionItemProvider', () => {
  20. describe('getSuggestions', () => {
  21. it('returns all dynamic labels in case current token is a whitespace', async () => {
  22. const { query, position } = afterLabelValue;
  23. const suggestions = await getSuggestions(query, position);
  24. expect(suggestions.length).toEqual(DYNAMIC_LABEL_PATTERNS.length + 1); // + 1 for the dimension suggestions
  25. });
  26. it('should return suggestion for dimension label that has high prio', async () => {
  27. const { query, position } = afterLabelValue;
  28. const suggestions = await getSuggestions(query, position);
  29. expect(suggestions.length).toBeTruthy();
  30. const highPrioSuggestsions = suggestions.filter((s) => s.sortText === CompletionItemPriority.High);
  31. expect(highPrioSuggestsions.length).toBe(1);
  32. expect(highPrioSuggestsions[0].label).toBe("${PROP('Dim.')}");
  33. });
  34. it('doesnt return suggestions if cursor is inside a dynamic label', async () => {
  35. const { query, position } = insideLabelValue;
  36. const suggestions = await getSuggestions(query, position);
  37. expect(suggestions.length).toBe(0);
  38. });
  39. });
  40. });