useLokiSyntax.test.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { renderHook, act } from 'react-hooks-testing-library';
  2. import { DataSourceStatus } from '@grafana/ui/src/types/datasource';
  3. import { AbsoluteTimeRange } from '@grafana/data';
  4. import LanguageProvider from 'app/plugins/datasource/loki/language_provider';
  5. import { useLokiSyntax } from './useLokiSyntax';
  6. import { CascaderOption } from 'app/plugins/datasource/loki/components/LokiQueryFieldForm';
  7. import { makeMockLokiDatasource } from '../mocks';
  8. describe('useLokiSyntax hook', () => {
  9. const datasource = makeMockLokiDatasource({});
  10. const languageProvider = new LanguageProvider(datasource);
  11. const logLabelOptionsMock = ['Holy mock!'];
  12. const logLabelOptionsMock2 = ['Mock the hell?!'];
  13. const logLabelOptionsMock3 = ['Oh my mock!'];
  14. const rangeMock: AbsoluteTimeRange = {
  15. from: 1560153109000,
  16. to: 1560163909000,
  17. };
  18. languageProvider.refreshLogLabels = () => {
  19. languageProvider.logLabelOptions = logLabelOptionsMock;
  20. return Promise.resolve();
  21. };
  22. languageProvider.fetchLogLabels = () => {
  23. languageProvider.logLabelOptions = logLabelOptionsMock2;
  24. return Promise.resolve([]);
  25. };
  26. const activeOptionMock: CascaderOption = {
  27. label: '',
  28. value: '',
  29. };
  30. it('should provide Loki syntax when used', async () => {
  31. const { result, waitForNextUpdate } = renderHook(() =>
  32. useLokiSyntax(languageProvider, DataSourceStatus.Connected, rangeMock)
  33. );
  34. expect(result.current.syntax).toEqual(null);
  35. await waitForNextUpdate();
  36. expect(result.current.syntax).toEqual(languageProvider.getSyntax());
  37. });
  38. it('should fetch labels on first call', async () => {
  39. const { result, waitForNextUpdate } = renderHook(() =>
  40. useLokiSyntax(languageProvider, DataSourceStatus.Connected, rangeMock)
  41. );
  42. expect(result.current.isSyntaxReady).toBeFalsy();
  43. expect(result.current.logLabelOptions).toEqual([]);
  44. await waitForNextUpdate();
  45. expect(result.current.isSyntaxReady).toBeTruthy();
  46. expect(result.current.logLabelOptions).toEqual(logLabelOptionsMock2);
  47. });
  48. it('should try to fetch missing options when active option changes', async () => {
  49. const { result, waitForNextUpdate } = renderHook(() =>
  50. useLokiSyntax(languageProvider, DataSourceStatus.Connected, rangeMock)
  51. );
  52. await waitForNextUpdate();
  53. expect(result.current.logLabelOptions).toEqual(logLabelOptionsMock2);
  54. languageProvider.fetchLabelValues = (key: string) => {
  55. languageProvider.logLabelOptions = logLabelOptionsMock3;
  56. return Promise.resolve();
  57. };
  58. act(() => result.current.setActiveOption([activeOptionMock]));
  59. await waitForNextUpdate();
  60. expect(result.current.logLabelOptions).toEqual(logLabelOptionsMock3);
  61. });
  62. });