From d120c05608bd2ae18d07501f03cbe3d1370aa0bc Mon Sep 17 00:00:00 2001
From: Shirley <4163034+fridgepoet@users.noreply.github.com>
Date: Sat, 22 Apr 2023 11:28:34 +0200
Subject: [PATCH] CloudWatch: Fix test on metric stat editor statistic field
(#67064)
---
.../cloudwatch/__mocks__/CloudWatchDataSource.ts | 10 +++++++++-
.../MetricStatEditor/MetricStatEditor.test.tsx | 9 ++++-----
.../components/MetricStatEditor/MetricStatEditor.tsx | 2 +-
3 files changed, 14 insertions(+), 7 deletions(-)
diff --git a/public/app/plugins/datasource/cloudwatch/__mocks__/CloudWatchDataSource.ts b/public/app/plugins/datasource/cloudwatch/__mocks__/CloudWatchDataSource.ts
index 72221bc968a..f58543dbdce 100644
--- a/public/app/plugins/datasource/cloudwatch/__mocks__/CloudWatchDataSource.ts
+++ b/public/app/plugins/datasource/cloudwatch/__mocks__/CloudWatchDataSource.ts
@@ -1,6 +1,7 @@
import { of } from 'rxjs';
import {
+ CustomVariableModel,
DataSourceInstanceSettings,
DataSourcePluginMeta,
PluginMetaInfo,
@@ -11,7 +12,6 @@ import { getBackendSrv, setBackendSrv } from '@grafana/runtime';
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { TemplateSrv } from 'app/features/templating/template_srv';
import { initialCustomVariableModelState } from 'app/features/variables/custom/reducer';
-import { CustomVariableModel } from 'app/features/variables/types';
import { CloudWatchDatasource } from '../datasource';
import { CloudWatchJsonData } from '../types';
@@ -256,3 +256,11 @@ export const accountIdVariable: CustomVariableModel = {
options: [{ value: 'templatedRegion', text: 'templatedRegion', selected: true }],
multi: false,
};
+
+export const statisticVariable: CustomVariableModel = {
+ ...initialCustomVariableModelState,
+ id: 'statistic',
+ name: 'statistic',
+ current: { value: 'some stat', text: 'some stat', selected: true },
+ multi: false,
+};
diff --git a/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.test.tsx b/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.test.tsx
index 9393625583b..bc1126972f3 100644
--- a/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.test.tsx
+++ b/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.test.tsx
@@ -6,13 +6,13 @@ import selectEvent from 'react-select-event';
import { config } from '@grafana/runtime';
import { MetricStatEditor } from '..';
-import { setupMockedDataSource } from '../../__mocks__/CloudWatchDataSource';
+import { setupMockedDataSource, statisticVariable } from '../../__mocks__/CloudWatchDataSource';
import { validMetricSearchBuilderQuery } from '../../__mocks__/queries';
import { MetricStat } from '../../types';
const originalFeatureToggleValue = config.featureToggles.cloudWatchCrossAccountQuerying;
const ds = setupMockedDataSource({
- variables: [],
+ variables: [statisticVariable],
});
ds.datasource.resources.getNamespaces = jest.fn().mockResolvedValue([]);
@@ -40,9 +40,8 @@ describe('MetricStatEditor', () => {
config.featureToggles.cloudWatchCrossAccountQuerying = originalFeatureToggleValue;
});
describe('statistics field', () => {
- test.each([['Average', 'p23.23', 'p34', '$statistic']])('should accept valid values', async (statistic) => {
+ test.each(['Average', 'p23.23', 'p34', '$statistic'])('should accept valid values', async (statistic) => {
const onChange = jest.fn();
- props.datasource.getVariables = jest.fn().mockReturnValue(['$statistic']);
render();
@@ -54,7 +53,7 @@ describe('MetricStatEditor', () => {
expect(onChange).toHaveBeenCalledWith({ ...props.metricStat, statistic });
});
- test.each([['CustomStat', 'p23,23', '$statistic']])('should not accept invalid values', async (statistic) => {
+ test.each(['CustomStat', 'p23,23', '$someUnknownValue'])('should not accept invalid values', async (statistic) => {
const onChange = jest.fn();
render();
diff --git a/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.tsx b/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.tsx
index 6604fa3a1b0..abfe5b6f40c 100644
--- a/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.tsx
+++ b/public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.tsx
@@ -117,7 +117,7 @@ export function MetricStatEditor({
!statistic ||
(!standardStatistics.includes(statistic) &&
!/^p\d{2}(?:\.\d{1,2})?$/.test(statistic) &&
- !statistic.startsWith('$'))
+ !datasource.templateSrv.containsTemplate(statistic))
) {
return;
}