1
import * as React from 'react';
2
import { mount, shallow } from 'enzyme';
3
import Panel, { GraphDisplayMode, PanelOptions, PanelType } from './Panel';
4
import GraphControls from './GraphControls';
5
import { NavLink, TabPane } from 'reactstrap';
6
import TimeInput from './TimeInput';
7
import DataTable from './DataTable';
8
import { GraphTabContent } from './GraphTabContent';
12
expr: 'prometheus_engine',
13
type: PanelType.Table,
15
endTime: 1572100217898,
17
displayMode: GraphDisplayMode.Lines,
20
onOptionsChanged: (): void => {
26
'prometheus_engine_queries',
27
'prometheus_engine_queries_concurrent_max',
28
'prometheus_engine_query_duration_seconds',
30
removePanel: (): void => {
33
onExecuteQuery: (): void => {
37
enableAutocomplete: true,
38
enableHighlighting: true,
43
describe('Panel', () => {
44
const panel = shallow(<Panel {...defaultProps} />);
46
it('renders NavLinks', () => {
47
const results: PanelOptions[] = [];
48
const onOptionsChanged = (opts: PanelOptions): void => {
51
const panel = shallow(<Panel {...defaultProps} onOptionsChanged={onOptionsChanged} />);
52
const links = panel.find(NavLink);
54
{ panelType: 'Table', active: true },
55
{ panelType: 'Graph', active: false },
56
].forEach((tc: { panelType: string; active: boolean }, i: number) => {
57
const link = links.at(i);
58
const className = tc.active ? 'active' : '';
59
expect(link.prop('className')).toEqual(className);
60
link.simulate('click');
62
expect(results).toHaveLength(0);
64
expect(results).toHaveLength(1);
65
expect(results[0].type).toEqual(tc.panelType.toLowerCase());
71
it('renders a TabPane with a TimeInput and a DataTable when in table mode', () => {
72
const tab = panel.find(TabPane).filterWhere((tab) => tab.prop('tabId') === 'table');
73
const timeInput = tab.find(TimeInput);
74
expect(timeInput.prop('time')).toEqual(defaultProps.options.endTime);
75
expect(timeInput.prop('range')).toEqual(defaultProps.options.range);
76
expect(timeInput.prop('placeholder')).toEqual('Evaluation time');
77
expect(tab.find(DataTable)).toHaveLength(1);
80
it('renders a TabPane with a Graph and GraphControls when in graph mode', () => {
82
expr: 'prometheus_engine',
83
type: PanelType.Graph,
85
endTime: 1572100217898,
87
displayMode: GraphDisplayMode.Lines,
90
const graphPanel = mount(<Panel {...defaultProps} options={options} />);
91
const controls = graphPanel.find(GraphControls);
92
graphPanel.setState({ data: { resultType: 'matrix', result: [] } });
93
const graph = graphPanel.find(GraphTabContent);
94
expect(controls.prop('endTime')).toEqual(options.endTime);
95
expect(controls.prop('range')).toEqual(options.range);
96
expect(controls.prop('resolution')).toEqual(options.resolution);
97
expect(controls.prop('displayMode')).toEqual(options.displayMode);
98
expect(graph.prop('displayMode')).toEqual(options.displayMode);
101
describe('when switching between modes', () => {
103
{ from: PanelType.Table, to: PanelType.Graph },
104
{ from: PanelType.Graph, to: PanelType.Table },
105
].forEach(({ from, to }: { from: PanelType; to: PanelType }) => {
106
it(`${from} -> ${to} nulls out data`, () => {
109
options: { ...defaultProps.options, type: from },
111
const panel = shallow(<Panel {...props} />);
112
const instance: any = panel.instance();
113
panel.setState({ data: 'somedata' });
114
expect(panel.state('data')).toEqual('somedata');
115
instance.handleChangeType(to);
116
expect(panel.state('data')).toBeNull();
121
describe('when clicking on current mode', () => {
122
[PanelType.Table, PanelType.Graph].forEach((mode: PanelType) => {
123
it(`${mode} keeps data`, () => {
126
options: { ...defaultProps.options, type: mode },
128
const panel = shallow(<Panel {...props} />);
129
const instance: any = panel.instance();
130
panel.setState({ data: 'somedata' });
131
expect(panel.state('data')).toEqual('somedata');
132
instance.handleChangeType(mode);
133
expect(panel.state('data')).toEqual('somedata');
138
describe('when changing query then time', () => {
139
it('executes the new query', () => {
140
const initialExpr = 'time()';
141
const newExpr = 'time() - time()';
142
const panel = shallow(<Panel {...defaultProps} options={{ ...defaultProps.options, expr: initialExpr }} />);
143
const instance: any = panel.instance();
144
instance.executeQuery();
145
const executeQuerySpy = jest.spyOn(instance, 'executeQuery');
146
//change query without executing
147
panel.setProps({ options: { ...defaultProps.options, expr: newExpr } });
148
expect(executeQuerySpy).toHaveBeenCalledTimes(0);
149
const debounceExecuteQuerySpy = jest.spyOn(instance, 'debounceExecuteQuery');
150
//execute query implicitly with time change
151
panel.setProps({ options: { ...defaultProps.options, expr: newExpr, endTime: 1575744840 } });
152
expect(debounceExecuteQuerySpy).toHaveBeenCalledTimes(1);