-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
ArgQueryEditor.test.tsx
84 lines (74 loc) · 3.5 KB
/
ArgQueryEditor.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
import { render, screen } from '@testing-library/react';
import React from 'react';
import createMockDatasource from '../../__mocks__/datasource';
import createMockQuery from '../../__mocks__/query';
import ArgQueryEditor from './ArgQueryEditor';
jest.mock('@grafana/runtime', () => ({
...(jest.requireActual('@grafana/runtime') as unknown as object),
getTemplateSrv: () => ({
replace: (val: string) => {
return val;
},
}),
}));
const variableOptionGroup = {
label: 'Template variables',
options: [],
};
const defaultProps = {
query: createMockQuery(),
datasource: createMockDatasource(),
variableOptionGroup: variableOptionGroup,
onChange: jest.fn(),
setError: jest.fn(),
};
describe('ArgQueryEditor', () => {
it('should render', async () => {
render(<ArgQueryEditor {...defaultProps} />);
expect(await screen.findByTestId('azure-monitor-arg-query-editor-with-experimental-ui')).toBeInTheDocument();
});
it('should select a subscription from the fetched array', async () => {
const datasource = createMockDatasource({
getSubscriptions: jest.fn().mockResolvedValue([{ value: 'foo' }]),
});
const onChange = jest.fn();
render(<ArgQueryEditor {...defaultProps} datasource={datasource} onChange={onChange} />);
expect(await screen.findByTestId('azure-monitor-arg-query-editor-with-experimental-ui')).toBeInTheDocument();
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ subscriptions: ['foo'] }));
});
it('should select a subscription from existing query', async () => {
const onChange = jest.fn();
const query = createMockQuery({
subscriptions: ['bar'],
});
render(<ArgQueryEditor {...defaultProps} onChange={onChange} query={query} />);
expect(await screen.findByTestId('azure-monitor-arg-query-editor-with-experimental-ui')).toBeInTheDocument();
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ subscriptions: ['bar'] }));
});
it('should change the subscription if the selected one is not part of the fetched array', async () => {
const onChange = jest.fn();
const datasource = createMockDatasource({
getSubscriptions: jest.fn().mockResolvedValue([{ value: 'foo' }]),
});
const query = createMockQuery({
subscriptions: ['bar'],
});
render(<ArgQueryEditor {...defaultProps} datasource={datasource} onChange={onChange} query={query} />);
expect(await screen.findByTestId('azure-monitor-arg-query-editor-with-experimental-ui')).toBeInTheDocument();
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ subscriptions: ['foo'] }));
expect(onChange).not.toHaveBeenCalledWith(expect.objectContaining({ subscriptions: ['bar'] }));
});
it('should keep a subset of subscriptions if the new list does not contain all the query subscriptions', async () => {
const onChange = jest.fn();
const datasource = createMockDatasource({
getSubscriptions: jest.fn().mockResolvedValue([{ value: 'foo' }, { value: 'bar' }]),
});
const query = createMockQuery({
subscriptions: ['foo', 'bar', 'foobar'],
});
render(<ArgQueryEditor {...defaultProps} datasource={datasource} onChange={onChange} query={query} />);
expect(await screen.findByTestId('azure-monitor-arg-query-editor-with-experimental-ui')).toBeInTheDocument();
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ subscriptions: ['foo', 'bar'] }));
expect(onChange).not.toHaveBeenCalledWith(expect.objectContaining({ subscriptions: ['foo', 'bar', 'foobar'] }));
});
});