useFields.test.tsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { renderHook } from '@testing-library/react-hooks';
  2. import React, { PropsWithChildren } from 'react';
  3. import { from } from 'rxjs';
  4. import { getDefaultTimeRange } from '@grafana/data';
  5. import { BucketAggregationType } from '../components/QueryEditor/BucketAggregationsEditor/aggregations';
  6. import { ElasticsearchProvider } from '../components/QueryEditor/ElasticsearchQueryContext';
  7. import { MetricAggregationType } from '../components/QueryEditor/MetricAggregationsEditor/aggregations';
  8. import { ElasticDatasource } from '../datasource';
  9. import { defaultBucketAgg, defaultMetricAgg } from '../query_def';
  10. import { ElasticsearchQuery } from '../types';
  11. import { useFields } from './useFields';
  12. describe('useFields hook', () => {
  13. // TODO: If we move the field type to the configuration objects as described in the hook's source
  14. // we can stop testing for getField to be called with the correct parameters.
  15. it("returns a function that calls datasource's getFields with the correct parameters", async () => {
  16. const timeRange = getDefaultTimeRange();
  17. const query: ElasticsearchQuery = {
  18. refId: 'A',
  19. query: '',
  20. metrics: [defaultMetricAgg()],
  21. bucketAggs: [defaultBucketAgg()],
  22. };
  23. const getFields: ElasticDatasource['getFields'] = jest.fn(() => from([[]]));
  24. const wrapper = ({ children }: PropsWithChildren<{}>) => (
  25. <ElasticsearchProvider
  26. datasource={{ getFields } as ElasticDatasource}
  27. query={query}
  28. range={timeRange}
  29. onChange={() => {}}
  30. onRunQuery={() => {}}
  31. >
  32. {children}
  33. </ElasticsearchProvider>
  34. );
  35. //
  36. // METRIC AGGREGATIONS
  37. //
  38. // Cardinality works on every kind of data
  39. const { result, rerender } = renderHook(
  40. (aggregationType: BucketAggregationType | MetricAggregationType) => useFields(aggregationType),
  41. { wrapper, initialProps: 'cardinality' }
  42. );
  43. result.current();
  44. expect(getFields).toHaveBeenLastCalledWith([], timeRange);
  45. // All other metric aggregations only work on numbers
  46. rerender('avg');
  47. result.current();
  48. expect(getFields).toHaveBeenLastCalledWith(['number'], timeRange);
  49. //
  50. // BUCKET AGGREGATIONS
  51. //
  52. // Date Histrogram only works on dates
  53. rerender('date_histogram');
  54. result.current();
  55. expect(getFields).toHaveBeenLastCalledWith(['date'], timeRange);
  56. // Histrogram only works on numbers
  57. rerender('histogram');
  58. result.current();
  59. expect(getFields).toHaveBeenLastCalledWith(['number'], timeRange);
  60. // Geohash Grid only works on geo_point data
  61. rerender('geohash_grid');
  62. result.current();
  63. expect(getFields).toHaveBeenLastCalledWith(['geo_point'], timeRange);
  64. // All other bucket aggregation work on any kind of data
  65. rerender('terms');
  66. result.current();
  67. expect(getFields).toHaveBeenLastCalledWith([], timeRange);
  68. // top_metrics work on only on numeric data in 7.7
  69. rerender('top_metrics');
  70. result.current();
  71. expect(getFields).toHaveBeenLastCalledWith(['number'], timeRange);
  72. });
  73. });