From 93649e1fb5ec33114cb3d5c7d9c9295b4d300b25 Mon Sep 17 00:00:00 2001 From: Giordano Ricci Date: Mon, 14 Dec 2020 14:36:07 +0000 Subject: [PATCH] Elasticsearch: ensure query model has timeField configured in datasource settings (#29807) --- .../ElasticsearchQueryContext.test.tsx | 17 +++++++++++++++++ .../QueryEditor/ElasticsearchQueryContext.tsx | 7 ++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.test.tsx b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.test.tsx index 6240b2b6cb6..5f04a1e7ead 100644 --- a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.test.tsx +++ b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.test.tsx @@ -1,5 +1,6 @@ import React, { FunctionComponent } from 'react'; import { renderHook } from '@testing-library/react-hooks'; +import { render } from '@testing-library/react'; import { ElasticsearchProvider, useDatasource, useQuery } from './ElasticsearchQueryContext'; import { ElasticsearchQuery } from '../../types'; import { ElasticDatasource } from '../../datasource'; @@ -11,6 +12,22 @@ const query: ElasticsearchQuery = { }; describe('ElasticsearchQueryContext', () => { + it('Should call onChange with the default query when the query is empty', () => { + const datasource = { timeField: 'TIMEFIELD' } as ElasticDatasource; + const onChange = jest.fn(); + + render(); + + const changedQuery: ElasticsearchQuery = onChange.mock.calls[0][0]; + expect(changedQuery.query).toBeDefined(); + expect(changedQuery.alias).toBeDefined(); + expect(changedQuery.metrics).toBeDefined(); + expect(changedQuery.bucketAggs).toBeDefined(); + + // Should also set timeField to the configured `timeField` option in datasource configuration + expect(changedQuery.timeField).toBe(datasource.timeField); + }); + describe('useQuery Hook', () => { it('Should throw when used outside of ElasticsearchQueryContext', () => { const { result } = renderHook(() => useQuery()); diff --git a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.tsx b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.tsx index b1322f49417..8fb94b487ff 100644 --- a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.tsx +++ b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.tsx @@ -24,7 +24,12 @@ export const ElasticsearchProvider: FunctionComponent = ({ children, onCh bucketAggs: bucketAggsReducer, }); - const dispatch = useStatelessReducer(newState => onChange({ ...query, ...newState }), query, reducer); + const dispatch = useStatelessReducer( + // timeField is part of the query model, but its value is always set to be the one from datasource settings. + newState => onChange({ ...query, ...newState, timeField: datasource.timeField }), + query, + reducer + ); // This initializes the query by dispatching an init action to each reducer. // useStatelessReducer will then call `onChange` with the newly generated query