AnnotationQueryEditor.test.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { render, screen, waitFor } from '@testing-library/react';
  2. import React from 'react';
  3. import '@testing-library/jest-dom';
  4. import { QueryEditorProps } from '@grafana/data';
  5. import { setupMockedDataSource } from '../__mocks__/CloudWatchDataSource';
  6. import { CloudWatchDatasource } from '../datasource';
  7. import { CloudWatchAnnotationQuery, CloudWatchJsonData, CloudWatchMetricsQuery, CloudWatchQuery } from '../types';
  8. import { AnnotationQueryEditor } from './AnnotationQueryEditor';
  9. const ds = setupMockedDataSource({
  10. variables: [],
  11. });
  12. const q: CloudWatchQuery = {
  13. queryMode: 'Annotations',
  14. region: 'us-east-2',
  15. namespace: '',
  16. period: '',
  17. metricName: '',
  18. dimensions: {},
  19. matchExact: true,
  20. statistic: '',
  21. refId: '',
  22. prefixMatching: false,
  23. actionPrefix: '',
  24. alarmNamePrefix: '',
  25. };
  26. ds.datasource.getRegions = jest.fn().mockResolvedValue([]);
  27. ds.datasource.getNamespaces = jest.fn().mockResolvedValue([]);
  28. ds.datasource.getMetrics = jest.fn().mockResolvedValue([]);
  29. ds.datasource.getDimensionKeys = jest.fn().mockResolvedValue([]);
  30. ds.datasource.getVariables = jest.fn().mockReturnValue([]);
  31. const props: QueryEditorProps<CloudWatchDatasource, CloudWatchQuery, CloudWatchJsonData> = {
  32. datasource: ds.datasource,
  33. query: q,
  34. onChange: jest.fn(),
  35. onRunQuery: jest.fn(),
  36. };
  37. describe('AnnotationQueryEditor', () => {
  38. it('should not display match exact switch', async () => {
  39. render(<AnnotationQueryEditor {...props} />);
  40. await waitFor(() => {
  41. expect(screen.queryByText('Match exact')).toBeNull();
  42. });
  43. });
  44. it('should return an error component in case CloudWatchQuery is not CloudWatchAnnotationQuery', async () => {
  45. ds.datasource.getDimensionValues = jest.fn().mockResolvedValue([[{ label: 'dimVal1', value: 'dimVal1' }]]);
  46. render(
  47. <AnnotationQueryEditor {...props} query={{ ...props.query, queryMode: 'Metrics' } as CloudWatchMetricsQuery} />
  48. );
  49. await waitFor(() => expect(screen.getByText('Invalid annotation query')).toBeInTheDocument());
  50. });
  51. it('should not display wildcard option in dimension value dropdown', async () => {
  52. ds.datasource.getDimensionValues = jest.fn().mockResolvedValue([[{ label: 'dimVal1', value: 'dimVal1' }]]);
  53. (props.query as CloudWatchAnnotationQuery).dimensions = { instanceId: 'instance-123' };
  54. render(<AnnotationQueryEditor {...props} />);
  55. const valueElement = screen.getByText('instance-123');
  56. expect(valueElement).toBeInTheDocument();
  57. expect(screen.queryByText('*')).toBeNull();
  58. valueElement.click();
  59. await waitFor(() => {
  60. expect(screen.queryByText('*')).toBeNull();
  61. });
  62. });
  63. });