annotationSupport.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { AnnotationQuery } from '@grafana/data';
  2. import { createMockDatasource } from './__mocks__/cloudMonitoringDatasource';
  3. import { CloudMonitoringAnnotationSupport } from './annotationSupport';
  4. import {
  5. AlignmentTypes,
  6. CloudMonitoringQuery,
  7. EditorMode,
  8. LegacyCloudMonitoringAnnotationQuery,
  9. MetricKind,
  10. QueryType,
  11. } from './types';
  12. const query: CloudMonitoringQuery = {
  13. refId: 'query',
  14. queryType: QueryType.METRICS,
  15. type: 'annotationQuery',
  16. intervalMs: 0,
  17. metricQuery: {
  18. editorMode: EditorMode.Visual,
  19. projectName: 'project-name',
  20. metricType: '',
  21. filters: [],
  22. metricKind: MetricKind.GAUGE,
  23. valueType: '',
  24. title: '',
  25. text: '',
  26. query: '',
  27. crossSeriesReducer: 'REDUCE_NONE',
  28. perSeriesAligner: AlignmentTypes.ALIGN_NONE,
  29. },
  30. };
  31. const legacyQuery: LegacyCloudMonitoringAnnotationQuery = {
  32. projectName: 'project-name',
  33. metricType: 'metric-type',
  34. filters: ['filter1', 'filter2'],
  35. metricKind: MetricKind.CUMULATIVE,
  36. valueType: 'value-type',
  37. refId: 'annotationQuery',
  38. title: 'title',
  39. text: 'text',
  40. };
  41. const annotationQuery: AnnotationQuery<CloudMonitoringQuery> = {
  42. name: 'Anno',
  43. enable: false,
  44. iconColor: '',
  45. target: query,
  46. };
  47. const legacyAnnotationQuery: AnnotationQuery<LegacyCloudMonitoringAnnotationQuery> = {
  48. name: 'Anno',
  49. enable: false,
  50. iconColor: '',
  51. target: legacyQuery,
  52. };
  53. const ds = createMockDatasource();
  54. const annotationSupport = CloudMonitoringAnnotationSupport(ds);
  55. describe('CloudMonitoringAnnotationSupport', () => {
  56. describe('prepareAnnotation', () => {
  57. it('returns query if it is already a Cloud Monitoring annotation query', () => {
  58. expect(annotationSupport.prepareAnnotation?.(annotationQuery)).toBe(annotationQuery);
  59. });
  60. it('returns an updated query if it is a legacy Cloud Monitoring annotation query', () => {
  61. const expectedQuery = {
  62. datasource: undefined,
  63. enable: false,
  64. iconColor: '',
  65. name: 'Anno',
  66. target: {
  67. intervalMs: 0,
  68. metricQuery: {
  69. crossSeriesReducer: 'REDUCE_NONE',
  70. editorMode: 'visual',
  71. filters: ['filter1', 'filter2'],
  72. metricKind: 'CUMULATIVE',
  73. metricType: 'metric-type',
  74. perSeriesAligner: 'ALIGN_NONE',
  75. projectName: 'project-name',
  76. query: '',
  77. text: 'text',
  78. title: 'title',
  79. },
  80. queryType: 'metrics',
  81. refId: 'annotationQuery',
  82. type: 'annotationQuery',
  83. },
  84. };
  85. expect(annotationSupport.prepareAnnotation?.(legacyAnnotationQuery)).toEqual(expectedQuery);
  86. });
  87. });
  88. describe('prepareQuery', () => {
  89. it('should ensure queryType is set to "metrics"', () => {
  90. const queryWithoutMetricsQueryType = { ...annotationQuery, queryType: 'blah' };
  91. expect(annotationSupport.prepareQuery?.(queryWithoutMetricsQueryType)).toEqual(
  92. expect.objectContaining({ queryType: 'metrics' })
  93. );
  94. });
  95. it('should ensure type is set "annotationQuery"', () => {
  96. const queryWithoutAnnotationQueryType = { ...annotationQuery, type: 'blah' };
  97. expect(annotationSupport.prepareQuery?.(queryWithoutAnnotationQueryType)).toEqual(
  98. expect.objectContaining({ type: 'annotationQuery' })
  99. );
  100. });
  101. it('should return undefined if there is no query', () => {
  102. const queryWithUndefinedTarget = { ...annotationQuery, target: undefined };
  103. expect(annotationSupport.prepareQuery?.(queryWithUndefinedTarget)).toBeUndefined();
  104. });
  105. });
  106. });