From d3a70de962325b264d9c53086df1f59a7cb85a03 Mon Sep 17 00:00:00 2001 From: Virginia Cepeda Date: Fri, 9 Sep 2022 08:34:38 -0300 Subject: [PATCH] Alerting: Use original query time range when duplicating a query (#54805) * Use original query time range when duplicating it If there's no time range, then assign a default one * Add tests * Import default relative time range from common lib --- .../rule-editor/QueryEditor.test.tsx | 49 +++++++++++++++++++ .../components/rule-editor/QueryEditor.tsx | 7 ++- 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 public/app/features/alerting/unified/components/rule-editor/QueryEditor.test.tsx diff --git a/public/app/features/alerting/unified/components/rule-editor/QueryEditor.test.tsx b/public/app/features/alerting/unified/components/rule-editor/QueryEditor.test.tsx new file mode 100644 index 00000000000..fb7016485bc --- /dev/null +++ b/public/app/features/alerting/unified/components/rule-editor/QueryEditor.test.tsx @@ -0,0 +1,49 @@ +import { getDefaultRelativeTimeRange } from '@grafana/data'; + +import { QueryEditor } from './QueryEditor'; + +const onChangeMock = jest.fn(); +describe('Query Editor', () => { + it('should maintain the original query time range when duplicating it', () => { + const query = { + refId: 'A', + queryType: '', + datasourceUid: '', + model: { refId: 'A', hide: false }, + relativeTimeRange: { from: 100, to: 0 }, + }; + const queryEditor = new QueryEditor({ + onChange: onChangeMock, + value: [query], + }); + + queryEditor.onDuplicateQuery(query); + + expect(onChangeMock).toHaveBeenCalledWith([ + query, + { ...query, ...{ refId: 'B', model: { refId: 'B', hide: false } } }, + ]); + }); + + it('should use the default query time range if none is set when duplicating a query', () => { + const query = { + refId: 'A', + queryType: '', + datasourceUid: '', + model: { refId: 'A', hide: false }, + }; + const queryEditor = new QueryEditor({ + onChange: onChangeMock, + value: [query], + }); + + queryEditor.onDuplicateQuery(query); + + const defaultRange = getDefaultRelativeTimeRange(); + + expect(onChangeMock).toHaveBeenCalledWith([ + query, + { ...query, ...{ refId: 'B', relativeTimeRange: defaultRange, model: { refId: 'B', hide: false } } }, + ]); + }); +}); diff --git a/public/app/features/alerting/unified/components/rule-editor/QueryEditor.tsx b/public/app/features/alerting/unified/components/rule-editor/QueryEditor.tsx index 71a38b2ac18..243309d58b9 100644 --- a/public/app/features/alerting/unified/components/rule-editor/QueryEditor.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/QueryEditor.tsx @@ -180,7 +180,10 @@ export class QueryEditor extends PureComponent { } } -const addQuery = (queries: AlertQuery[], queryToAdd: Pick): AlertQuery[] => { +const addQuery = ( + queries: AlertQuery[], + queryToAdd: Pick +): AlertQuery[] => { const refId = getNextRefIdChar(queries); const query: AlertQuery = { @@ -192,7 +195,7 @@ const addQuery = (queries: AlertQuery[], queryToAdd: Pick