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