PromQueryEditorByApp.test.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { render, RenderResult } from '@testing-library/react';
  2. import { noop } from 'lodash';
  3. import React from 'react';
  4. import { CoreApp } from '@grafana/data';
  5. import { PrometheusDatasource } from '../datasource';
  6. import { testIds as regularTestIds } from './PromQueryEditor';
  7. import { PromQueryEditorByApp } from './PromQueryEditorByApp';
  8. import { testIds as alertingTestIds } from './PromQueryEditorForAlerting';
  9. // the monaco-based editor uses lazy-loading and that does not work
  10. // well with this test, and we do not need the monaco-related
  11. // functionality in this test anyway, so we mock it out.
  12. jest.mock('./monaco-query-field/MonacoQueryFieldLazy', () => {
  13. const fakeQueryField = (props: any) => {
  14. return <input onBlur={props.onBlur} data-testid={'dummy-code-input'} type={'text'} />;
  15. };
  16. return {
  17. MonacoQueryFieldLazy: fakeQueryField,
  18. };
  19. });
  20. jest.mock('@grafana/runtime', () => {
  21. const runtime = jest.requireActual('@grafana/runtime');
  22. return {
  23. __esModule: true,
  24. ...runtime,
  25. config: {
  26. ...runtime.config,
  27. featureToggles: {
  28. ...runtime.config.featureToggles,
  29. promQueryBuilder: true,
  30. },
  31. },
  32. };
  33. });
  34. function setup(app: CoreApp): RenderResult & { onRunQuery: jest.Mock } {
  35. const dataSource = {
  36. createQuery: jest.fn((q) => q),
  37. getInitHints: () => [],
  38. getPrometheusTime: jest.fn((date, roundup) => 123),
  39. getQueryHints: jest.fn(() => []),
  40. languageProvider: {
  41. start: () => Promise.resolve([]),
  42. syntax: () => {},
  43. getLabelKeys: () => [],
  44. metrics: [],
  45. },
  46. } as unknown as PrometheusDatasource;
  47. const onRunQuery = jest.fn();
  48. const renderOutput = render(
  49. <PromQueryEditorByApp
  50. app={app}
  51. onChange={noop}
  52. onRunQuery={onRunQuery}
  53. datasource={dataSource}
  54. query={{ refId: 'A', expr: '' }}
  55. />
  56. );
  57. return {
  58. ...renderOutput,
  59. onRunQuery,
  60. };
  61. }
  62. describe('PromQueryEditorByApp', () => {
  63. it('should render simplified query editor for cloud alerting', () => {
  64. const { getByTestId, queryByTestId } = setup(CoreApp.CloudAlerting);
  65. expect(getByTestId(alertingTestIds.editor)).toBeInTheDocument();
  66. expect(queryByTestId(regularTestIds.editor)).toBeNull();
  67. });
  68. it('should render editor selector for unkown apps', () => {
  69. const { getByTestId, queryByTestId } = setup(CoreApp.Unknown);
  70. expect(getByTestId('QueryEditorModeToggle')).toBeInTheDocument();
  71. expect(queryByTestId(alertingTestIds.editor)).toBeNull();
  72. });
  73. it('should render editor selector for explore', () => {
  74. const { getByTestId, queryByTestId } = setup(CoreApp.Explore);
  75. expect(getByTestId('QueryEditorModeToggle')).toBeInTheDocument();
  76. expect(queryByTestId(alertingTestIds.editor)).toBeNull();
  77. });
  78. it('should render editor selector for dashboard', () => {
  79. const { getByTestId, queryByTestId } = setup(CoreApp.Dashboard);
  80. expect(getByTestId('QueryEditorModeToggle')).toBeInTheDocument();
  81. expect(queryByTestId(alertingTestIds.editor)).toBeNull();
  82. });
  83. });