grafana/public/app/plugins/datasource/cloud-monitoring/annotationSupport.test.ts
Kevin Yu 0a95d493e3
Cloud Monitoring: Use new annotation API (#49026)
* remove angular code

* format annotation on backend

* format time with time type instead of string

* update annotation query tests

* update get alignment data function

* update annotation query editor

* add annotation query editor test

* update struct

* add tests

* remove extracted function

* remove non-null assertion

* remove stray commented out console.log

* fix jest haste map warning

* add alignment period

* add AnnotationMetricQuery type
2022-05-19 13:52:52 -07:00

115 lines
3.5 KiB
TypeScript

import { AnnotationQuery } from '@grafana/data';
import { createMockDatasource } from './__mocks__/cloudMonitoringDatasource';
import { CloudMonitoringAnnotationSupport } from './annotationSupport';
import {
AlignmentTypes,
CloudMonitoringQuery,
EditorMode,
LegacyCloudMonitoringAnnotationQuery,
MetricKind,
QueryType,
} from './types';
const query: CloudMonitoringQuery = {
refId: 'query',
queryType: QueryType.METRICS,
type: 'annotationQuery',
intervalMs: 0,
metricQuery: {
editorMode: EditorMode.Visual,
projectName: 'project-name',
metricType: '',
filters: [],
metricKind: MetricKind.GAUGE,
valueType: '',
title: '',
text: '',
query: '',
crossSeriesReducer: 'REDUCE_NONE',
perSeriesAligner: AlignmentTypes.ALIGN_NONE,
},
};
const legacyQuery: LegacyCloudMonitoringAnnotationQuery = {
projectName: 'project-name',
metricType: 'metric-type',
filters: ['filter1', 'filter2'],
metricKind: MetricKind.CUMULATIVE,
valueType: 'value-type',
refId: 'annotationQuery',
title: 'title',
text: 'text',
};
const annotationQuery: AnnotationQuery<CloudMonitoringQuery> = {
name: 'Anno',
enable: false,
iconColor: '',
target: query,
};
const legacyAnnotationQuery: AnnotationQuery<LegacyCloudMonitoringAnnotationQuery> = {
name: 'Anno',
enable: false,
iconColor: '',
target: legacyQuery,
};
const ds = createMockDatasource();
const annotationSupport = CloudMonitoringAnnotationSupport(ds);
describe('CloudMonitoringAnnotationSupport', () => {
describe('prepareAnnotation', () => {
it('returns query if it is already a Cloud Monitoring annotation query', () => {
expect(annotationSupport.prepareAnnotation?.(annotationQuery)).toBe(annotationQuery);
});
it('returns an updated query if it is a legacy Cloud Monitoring annotation query', () => {
const expectedQuery = {
datasource: undefined,
enable: false,
iconColor: '',
name: 'Anno',
target: {
intervalMs: 0,
metricQuery: {
crossSeriesReducer: 'REDUCE_NONE',
editorMode: 'visual',
filters: ['filter1', 'filter2'],
metricKind: 'CUMULATIVE',
metricType: 'metric-type',
perSeriesAligner: 'ALIGN_NONE',
projectName: 'project-name',
query: '',
text: 'text',
title: 'title',
},
queryType: 'metrics',
refId: 'annotationQuery',
type: 'annotationQuery',
},
};
expect(annotationSupport.prepareAnnotation?.(legacyAnnotationQuery)).toEqual(expectedQuery);
});
});
describe('prepareQuery', () => {
it('should ensure queryType is set to "metrics"', () => {
const queryWithoutMetricsQueryType = { ...annotationQuery, queryType: 'blah' };
expect(annotationSupport.prepareQuery?.(queryWithoutMetricsQueryType)).toEqual(
expect.objectContaining({ queryType: 'metrics' })
);
});
it('should ensure type is set "annotationQuery"', () => {
const queryWithoutAnnotationQueryType = { ...annotationQuery, type: 'blah' };
expect(annotationSupport.prepareQuery?.(queryWithoutAnnotationQueryType)).toEqual(
expect.objectContaining({ type: 'annotationQuery' })
);
});
it('should return undefined if there is no query', () => {
const queryWithUndefinedTarget = { ...annotationQuery, target: undefined };
expect(annotationSupport.prepareQuery?.(queryWithUndefinedTarget)).toBeUndefined();
});
});
});