forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPromQueryEditorByApp.test.tsx
82 lines (67 loc) · 2.64 KB
/
PromQueryEditorByApp.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Core Grafana history https://github.com/grafana/grafana/blob/v11.0.0-preview/public/app/plugins/datasource/prometheus/components/PromQueryEditorByApp.test.tsx
import { render, screen } from '@testing-library/react';
import { noop } from 'lodash';
import { CoreApp } from '@grafana/data';
import { PrometheusDatasource } from '../datasource';
import { PromQueryEditorByApp } from './PromQueryEditorByApp';
import { alertingTestIds } from './PromQueryEditorForAlerting';
import { Props } from './monaco-query-field/MonacoQueryFieldProps';
// the monaco-based editor uses lazy-loading and that does not work
// well with this test, and we do not need the monaco-related
// functionality in this test anyway, so we mock it out.
jest.mock('./monaco-query-field/MonacoQueryFieldLazy', () => {
const fakeQueryField = (props: Props) => {
return <input onBlur={(e) => props.onBlur(e.currentTarget.value)} data-testid={'dummy-code-input'} type={'text'} />;
};
return {
MonacoQueryFieldLazy: fakeQueryField,
};
});
function setup(app: CoreApp): { onRunQuery: jest.Mock } {
const dataSource = {
getInitHints: () => [],
getPrometheusTime: jest.fn((date, roundup) => 123),
getQueryHints: jest.fn(() => []),
getDebounceTimeInMilliseconds: jest.fn(() => 300),
languageProvider: {
start: () => Promise.resolve([]),
syntax: () => {},
getLabelKeys: () => [],
metrics: [],
},
} as unknown as PrometheusDatasource;
const onRunQuery = jest.fn();
render(
<PromQueryEditorByApp
app={app}
onChange={noop}
onRunQuery={onRunQuery}
datasource={dataSource}
query={{ refId: 'A', expr: '' }}
/>
);
return {
onRunQuery,
};
}
describe('PromQueryEditorByApp', () => {
it('should render simplified query editor for cloud alerting', async () => {
setup(CoreApp.CloudAlerting);
expect(await screen.findByTestId(alertingTestIds.editor)).toBeInTheDocument();
});
it('should render editor selector for unkown apps', () => {
setup(CoreApp.Unknown);
expect(screen.getByTestId('QueryEditorModeToggle')).toBeInTheDocument();
expect(screen.queryByTestId(alertingTestIds.editor)).toBeNull();
});
it('should render editor selector for explore', () => {
setup(CoreApp.Explore);
expect(screen.getByTestId('QueryEditorModeToggle')).toBeInTheDocument();
expect(screen.queryByTestId(alertingTestIds.editor)).toBeNull();
});
it('should render editor selector for dashboard', () => {
setup(CoreApp.Dashboard);
expect(screen.getByTestId('QueryEditorModeToggle')).toBeInTheDocument();
expect(screen.queryByTestId(alertingTestIds.editor)).toBeNull();
});
});