PanelQueryEditor.test.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { act, render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import { QueryEditorProps } from '@grafana/data';
  4. import { setupMockedDataSource } from '../__mocks__/CloudWatchDataSource';
  5. import { CloudWatchDatasource } from '../datasource';
  6. import { CloudWatchQuery, CloudWatchJsonData, MetricEditorMode, MetricQueryType } from '../types';
  7. import { PanelQueryEditor } from './PanelQueryEditor';
  8. // the following three fields are added to legacy queries in the dashboard migrator
  9. const migratedFields = {
  10. statistic: 'Average',
  11. metricEditorMode: MetricEditorMode.Builder,
  12. metricQueryType: MetricQueryType.Query,
  13. };
  14. const props: QueryEditorProps<CloudWatchDatasource, CloudWatchQuery, CloudWatchJsonData> = {
  15. datasource: setupMockedDataSource().datasource,
  16. onRunQuery: jest.fn(),
  17. onChange: jest.fn(),
  18. query: {} as CloudWatchQuery,
  19. };
  20. describe('PanelQueryEditor should render right editor', () => {
  21. describe('when using grafana 6.3.0 metric query', () => {
  22. it('should render the metrics query editor', async () => {
  23. const query = {
  24. ...migratedFields,
  25. dimensions: {
  26. InstanceId: 'i-123',
  27. },
  28. expression: '',
  29. highResolution: false,
  30. id: '',
  31. metricName: 'CPUUtilization',
  32. namespace: 'AWS/EC2',
  33. period: '',
  34. refId: 'A',
  35. region: 'default',
  36. returnData: false,
  37. };
  38. await act(async () => {
  39. render(<PanelQueryEditor {...props} query={query} />);
  40. });
  41. expect(screen.getByText('Metric name')).toBeInTheDocument();
  42. });
  43. });
  44. describe('when using grafana 7.0.0 style metrics query', () => {
  45. it('should render the metrics query editor', async () => {
  46. const query = {
  47. ...migratedFields,
  48. alias: '',
  49. apiMode: 'Logs',
  50. dimensions: {
  51. InstanceId: 'i-123',
  52. },
  53. expression: '',
  54. id: '',
  55. logGroupNames: [],
  56. matchExact: true,
  57. metricName: 'CPUUtilization',
  58. namespace: 'AWS/EC2',
  59. period: '',
  60. queryMode: 'Logs',
  61. refId: 'A',
  62. region: 'ap-northeast-2',
  63. statistics: 'Average',
  64. } as any;
  65. await act(async () => {
  66. render(<PanelQueryEditor {...props} query={query} />);
  67. });
  68. expect(screen.getByText('Choose Log Groups')).toBeInTheDocument();
  69. });
  70. });
  71. describe('when using grafana 7.0.0 style logs query', () => {
  72. it('should render the metrics query editor', async () => {
  73. const query = {
  74. ...migratedFields,
  75. alias: '',
  76. apiMode: 'Logs',
  77. dimensions: {
  78. InstanceId: 'i-123',
  79. },
  80. expression: '',
  81. id: '',
  82. logGroupNames: [],
  83. matchExact: true,
  84. metricName: 'CPUUtilization',
  85. namespace: 'AWS/EC2',
  86. period: '',
  87. queryMode: 'Logs',
  88. refId: 'A',
  89. region: 'ap-northeast-2',
  90. statistic: 'Average',
  91. } as any;
  92. await act(async () => {
  93. render(<PanelQueryEditor {...props} query={query} />);
  94. });
  95. expect(screen.getByText('Log Groups')).toBeInTheDocument();
  96. });
  97. });
  98. describe('when using grafana query from curated ec2 dashboard', () => {
  99. it('should render the metrics query editor', async () => {
  100. const query = {
  101. ...migratedFields,
  102. alias: 'Inbound',
  103. dimensions: {
  104. InstanceId: '*',
  105. },
  106. expression:
  107. "SUM(REMOVE_EMPTY(SEARCH('{AWS/EC2,InstanceId} MetricName=\"NetworkIn\"', 'Sum', $period)))/$period",
  108. id: '',
  109. matchExact: true,
  110. metricName: 'NetworkOut',
  111. namespace: 'AWS/EC2',
  112. period: '$period',
  113. refId: 'B',
  114. region: '$region',
  115. statistic: 'Average',
  116. } as any;
  117. await act(async () => {
  118. render(<PanelQueryEditor {...props} query={query} />);
  119. });
  120. expect(screen.getByText('Metric name')).toBeInTheDocument();
  121. });
  122. });
  123. });