QueryHeader.test.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { act, render, screen, waitFor } from '@testing-library/react';
  2. import React from 'react';
  3. import { setupMockedDataSource } from '../__mocks__/CloudWatchDataSource';
  4. import { CloudWatchLogsQuery, CloudWatchMetricsQuery, MetricEditorMode, MetricQueryType } from '../types';
  5. import QueryHeader from './QueryHeader';
  6. const ds = setupMockedDataSource({
  7. variables: [],
  8. });
  9. ds.datasource.getRegions = jest.fn().mockResolvedValue([]);
  10. describe('QueryHeader', () => {
  11. it('should display metric options for metrics', async () => {
  12. const query: CloudWatchMetricsQuery = {
  13. queryMode: 'Metrics',
  14. id: '',
  15. region: 'us-east-2',
  16. namespace: '',
  17. period: '',
  18. alias: '',
  19. metricName: '',
  20. dimensions: {},
  21. matchExact: true,
  22. statistic: '',
  23. expression: '',
  24. refId: '',
  25. };
  26. const onChange = jest.fn();
  27. const onRunQuery = jest.fn();
  28. query.metricEditorMode = MetricEditorMode.Code;
  29. query.metricQueryType = MetricQueryType.Query;
  30. render(
  31. <QueryHeader
  32. sqlCodeEditorIsDirty={true}
  33. datasource={ds.datasource}
  34. query={query}
  35. onChange={onChange}
  36. onRunQuery={onRunQuery}
  37. />
  38. );
  39. const builderElement = screen.getByLabelText('Builder');
  40. expect(builderElement).toBeInTheDocument();
  41. await act(async () => {
  42. await builderElement.click();
  43. });
  44. const modalTitleElem = screen.getByText('Are you sure?');
  45. expect(modalTitleElem).toBeInTheDocument();
  46. expect(onChange).not.toHaveBeenCalled();
  47. });
  48. it('should not display metric options for logs', async () => {
  49. const onChange = jest.fn();
  50. const onRunQuery = jest.fn();
  51. const query: CloudWatchLogsQuery = {
  52. queryType: 'Metrics',
  53. id: '',
  54. region: 'us-east-2',
  55. expression: '',
  56. refId: '',
  57. queryMode: 'Logs',
  58. };
  59. render(
  60. <QueryHeader
  61. sqlCodeEditorIsDirty={true}
  62. datasource={ds.datasource}
  63. query={query}
  64. onChange={onChange}
  65. onRunQuery={onRunQuery}
  66. />
  67. );
  68. await waitFor(() => {
  69. expect(screen.queryByLabelText('Builder')).toBeNull();
  70. expect(screen.queryByLabelText('Code')).toBeNull();
  71. });
  72. });
  73. });