LokiQueryField.test.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { render } from '@testing-library/react';
  2. import React, { ComponentProps } from 'react';
  3. import { dateTime } from '@grafana/data';
  4. import { LokiQueryField } from './LokiQueryField';
  5. type Props = ComponentProps<typeof LokiQueryField>;
  6. const defaultProps: Props = {
  7. datasource: {
  8. languageProvider: {
  9. start: () => Promise.resolve(['label1']),
  10. fetchLabels: Promise.resolve(['label1']),
  11. getSyntax: () => {},
  12. getLabelKeys: () => ['label1'],
  13. getLabelValues: () => ['value1'],
  14. } as any,
  15. getInitHints: () => [],
  16. } as any,
  17. range: {
  18. from: dateTime([2021, 1, 11, 12, 0, 0]),
  19. to: dateTime([2021, 1, 11, 18, 0, 0]),
  20. raw: {
  21. from: 'now-1h',
  22. to: 'now',
  23. },
  24. },
  25. query: { expr: '', refId: '' },
  26. onRunQuery: () => {},
  27. onChange: () => {},
  28. history: [],
  29. };
  30. describe('LokiQueryField', () => {
  31. it('refreshes metrics when time range changes over 1 minute', async () => {
  32. const fetchLabelsMock = jest.fn();
  33. const props = defaultProps;
  34. props.datasource.languageProvider.fetchLabels = fetchLabelsMock;
  35. const { rerender } = render(<LokiQueryField {...props} />);
  36. expect(fetchLabelsMock).not.toHaveBeenCalled();
  37. // 2 minutes difference over the initial time
  38. const newRange = {
  39. from: dateTime([2021, 1, 11, 12, 2, 0]),
  40. to: dateTime([2021, 1, 11, 18, 2, 0]),
  41. raw: {
  42. from: 'now-1h',
  43. to: 'now',
  44. },
  45. };
  46. rerender(<LokiQueryField {...props} range={newRange} />);
  47. expect(fetchLabelsMock).toHaveBeenCalledTimes(1);
  48. });
  49. it('does not refreshes metrics when time range change by less than 1 minute', async () => {
  50. const fetchLabelsMock = jest.fn();
  51. const props = defaultProps;
  52. props.datasource.languageProvider.fetchLabels = fetchLabelsMock;
  53. const { rerender } = render(<LokiQueryField {...props} />);
  54. expect(fetchLabelsMock).not.toHaveBeenCalled();
  55. // 20 seconds difference over the initial time
  56. const newRange = {
  57. from: dateTime([2021, 1, 11, 12, 0, 20]),
  58. to: dateTime([2021, 1, 11, 18, 0, 20]),
  59. raw: {
  60. from: 'now-1h',
  61. to: 'now',
  62. },
  63. };
  64. rerender(<LokiQueryField {...props} range={newRange} />);
  65. expect(fetchLabelsMock).not.toHaveBeenCalled();
  66. });
  67. });