-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMetricEditor.test.tsx
94 lines (77 loc) · 2.69 KB
/
MetricEditor.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
83
84
85
86
87
88
89
90
91
92
93
94
import { act, fireEvent, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React, { PropsWithChildren } from 'react';
import { from } from 'rxjs';
import { CoreApp, getDefaultTimeRange } from '@grafana/data';
import { ElasticDatasource } from '@/datasource';
import { defaultBucketAgg } from '@/queryDef';
import { ElasticsearchQuery, Count, UniqueCount } from '@/types';
import { ElasticsearchProvider } from '../ElasticsearchQueryContext';
import { MetricEditor } from './MetricEditor';
describe('Metric Editor', () => {
it('Should not display a "None" option for "field" if the metric does not support inline script', async () => {
const avg: UniqueCount = {
id: '1',
type: 'cardinality',
};
const query: ElasticsearchQuery = {
refId: 'A',
query: '',
metrics: [avg],
bucketAggs: [defaultBucketAgg('2')],
};
const getFields: ElasticDatasource['getFields'] = jest.fn(() => from([[]]));
const wrapper = ({ children }: PropsWithChildren<{}>) => (
<ElasticsearchProvider
datasource={{ getFields: getFields } as ElasticDatasource}
query={query}
app={CoreApp.Unknown}
range={getDefaultTimeRange()}
onChange={() => {}}
onRunQuery={() => {}}
>
{children}
</ElasticsearchProvider>
);
render(<MetricEditor value={avg} />, { wrapper });
act(() => {
fireEvent.click(screen.getByText('Select Field'));
});
expect(await screen.findByText('No options found')).toBeInTheDocument();
expect(screen.queryByText('None')).not.toBeInTheDocument();
});
it('Should not list special metrics', async () => {
const count: Count = {
id: '1',
type: 'count',
};
const query: ElasticsearchQuery = {
refId: 'A',
query: '',
metrics: [count],
bucketAggs: [],
};
const wrapper = ({ children }: PropsWithChildren<{}>) => (
<ElasticsearchProvider
datasource={{} as ElasticDatasource}
query={query}
app={CoreApp.Explore}
range={getDefaultTimeRange()}
onChange={() => {}}
onRunQuery={() => {}}
>
{children}
</ElasticsearchProvider>
);
render(<MetricEditor value={count} />, { wrapper });
act(() => {
userEvent.click(screen.getByText('Count'));
});
// we check if the list-of-options is visible by
// checking for an item to exist
expect(await screen.findByText('Percentiles')).toBeInTheDocument();
// now we make sure the should-not-be-shown items are not shown
expect(screen.queryByText('Logs')).toBeNull();
expect(screen.queryByText('Raw Data')).toBeNull();
});
});