PromQueryEditor.test.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { screen, render } from '@testing-library/react';
  2. import React from 'react';
  3. import { dateTime, CoreApp } from '@grafana/data';
  4. import { PrometheusDatasource } from '../datasource';
  5. import { PromQuery } from '../types';
  6. import { PromQueryEditor, testIds } from './PromQueryEditor';
  7. jest.mock('app/features/dashboard/services/TimeSrv', () => {
  8. return {
  9. getTimeSrv: () => ({
  10. timeRange: () => ({
  11. from: dateTime(),
  12. to: dateTime(),
  13. }),
  14. }),
  15. };
  16. });
  17. jest.mock('./monaco-query-field/MonacoQueryFieldWrapper', () => {
  18. const fakeQueryField = () => <div>prometheus query field</div>;
  19. return {
  20. MonacoQueryFieldWrapper: fakeQueryField,
  21. };
  22. });
  23. const setup = (propOverrides?: object) => {
  24. const datasourceMock: unknown = {
  25. createQuery: jest.fn((q) => q),
  26. getPrometheusTime: jest.fn((date, roundup) => 123),
  27. languageProvider: {
  28. start: () => Promise.resolve([]),
  29. syntax: () => {},
  30. getLabelKeys: () => [],
  31. metrics: [],
  32. },
  33. getInitHints: () => [],
  34. };
  35. const datasource: PrometheusDatasource = datasourceMock as PrometheusDatasource;
  36. const onRunQuery = jest.fn();
  37. const onChange = jest.fn();
  38. const query: PromQuery = { expr: '', refId: 'A' };
  39. const props: any = {
  40. datasource,
  41. onChange,
  42. onRunQuery,
  43. query,
  44. };
  45. Object.assign(props, propOverrides);
  46. return render(<PromQueryEditor {...props} />);
  47. };
  48. describe('Render PromQueryEditor with basic options', () => {
  49. it('should render editor', () => {
  50. setup();
  51. expect(screen.getByTestId(testIds.editor)).toBeInTheDocument();
  52. });
  53. it('should render exemplar editor for dashboard', () => {
  54. setup({ app: CoreApp.Dashboard });
  55. expect(screen.getByTestId(testIds.editor)).toBeInTheDocument();
  56. expect(screen.getByTestId(testIds.exemplar)).toBeInTheDocument();
  57. });
  58. it('should not render exemplar editor for unified alerting', () => {
  59. setup({ app: CoreApp.UnifiedAlerting });
  60. expect(screen.getByTestId(testIds.editor)).toBeInTheDocument();
  61. expect(screen.queryByTestId(testIds.exemplar)).not.toBeInTheDocument();
  62. });
  63. });