CloudWatch: Fix test on metric stat editor statistic field (#67064)

pull/67080/head
Shirley 2 years ago committed by GitHub
parent b992bdb62d
commit d120c05608
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      public/app/plugins/datasource/cloudwatch/__mocks__/CloudWatchDataSource.ts
  2. 9
      public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.test.tsx
  3. 2
      public/app/plugins/datasource/cloudwatch/components/MetricStatEditor/MetricStatEditor.tsx

@ -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,
};

@ -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(<MetricStatEditor {...props} onChange={onChange} />);
@ -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(<MetricStatEditor {...props} onChange={onChange} />);

@ -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;
}

Loading…
Cancel
Save