grafana/public/app/plugins/datasource/elasticsearch/hooks/useFields.test.tsx
Giordano Ricci cdb4785496
Elasticsearch: Allow omitting field when metric supports inline script (#32839)
* Elasticsearch: Allow omitting field when metric supports inline script

* Add tests for MetricEditor to show a None option

* Add tests for useFields hook

* Alerting: allow elasticsearch metrics without field
2021-04-14 15:18:06 +01:00

74 lines
2.7 KiB
TypeScript

import React, { ReactNode } from 'react';
import { ElasticDatasource } from '../datasource';
import { from } from 'rxjs';
import { ElasticsearchProvider } from '../components/QueryEditor/ElasticsearchQueryContext';
import { getDefaultTimeRange } from '@grafana/data';
import { ElasticsearchQuery } from '../types';
import { defaultBucketAgg, defaultMetricAgg } from '../query_def';
import { renderHook } from '@testing-library/react-hooks';
import { useFields } from './useFields';
import { MetricAggregationType } from '../components/QueryEditor/MetricAggregationsEditor/aggregations';
import { BucketAggregationType } from '../components/QueryEditor/BucketAggregationsEditor/aggregations';
describe('useFields hook', () => {
// TODO: If we move the field type to the configuration objects as described in the hook's source
// we can stop testing for getField to be called with the correct parameters.
it("returns a function that calls datasource's getFields with the correct parameters", async () => {
const timeRange = getDefaultTimeRange();
const query: ElasticsearchQuery = {
refId: 'A',
query: '',
metrics: [defaultMetricAgg()],
bucketAggs: [defaultBucketAgg()],
};
const getFields: ElasticDatasource['getFields'] = jest.fn(() => from([[]]));
const wrapper = ({ children }: { children: ReactNode }) => (
<ElasticsearchProvider
datasource={{ getFields } as ElasticDatasource}
query={query}
range={timeRange}
onChange={() => {}}
onRunQuery={() => {}}
>
{children}
</ElasticsearchProvider>
);
//
// METRIC AGGREGATIONS
//
// Cardinality works on every kind of data
const { result, rerender } = renderHook(
(aggregationType: BucketAggregationType | MetricAggregationType) => useFields(aggregationType),
{ wrapper, initialProps: 'cardinality' }
);
result.current();
expect(getFields).toHaveBeenLastCalledWith(undefined, timeRange);
// All other metric aggregations only work on numbers
rerender('avg');
result.current();
expect(getFields).toHaveBeenLastCalledWith('number', timeRange);
//
// BUCKET AGGREGATIONS
//
// Date Histrogram only works on dates
rerender('date_histogram');
result.current();
expect(getFields).toHaveBeenLastCalledWith('date', timeRange);
// Geohash Grid only works on geo_point data
rerender('geohash_grid');
result.current();
expect(getFields).toHaveBeenLastCalledWith('geo_point', timeRange);
// All other bucket aggregation work on any kind of data
rerender('terms');
result.current();
expect(getFields).toHaveBeenLastCalledWith(undefined, timeRange);
});
});