1
import * as React from 'react';
2
import { shallow } from 'enzyme';
3
import GraphControls from './GraphControls';
4
import { Button, ButtonGroup, Form, InputGroup, InputGroupAddon, Input } from 'reactstrap';
5
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
6
import { faPlus, faMinus, faChartArea, faChartLine } from '@fortawesome/free-solid-svg-icons';
7
import TimeInput from './TimeInput';
8
import { GraphDisplayMode } from './Panel';
10
const defaultGraphControlProps = {
11
range: 60 * 60 * 24 * 1000,
12
endTime: 1572100217898,
15
displayMode: GraphDisplayMode.Lines,
19
onChangeRange: (): void => {
22
onChangeEndTime: (): void => {
25
onChangeResolution: (): void => {
28
onChangeStacking: (): void => {
31
onChangeShowExemplars: (): void => {
34
onChangeDisplayMode: (): void => {
39
describe('GraphControls', () => {
40
it('renders a form', () => {
41
const controls = shallow(<GraphControls {...defaultGraphControlProps} />);
42
const form = controls.find(Form);
43
expect(form).toHaveLength(1);
44
expect(form.prop('className')).toEqual('graph-controls');
45
expect(form.prop('inline')).toBe(true);
48
it('renders an Input Group for range', () => {
49
const controls = shallow(<GraphControls {...defaultGraphControlProps} />);
50
const form = controls.find(InputGroup);
51
expect(form).toHaveLength(1);
52
expect(form.prop('className')).toEqual('range-input');
53
expect(form.prop('size')).toBe('sm');
56
it('renders a decrease/increase range buttons', () => {
60
title: 'Decrease range',
65
title: 'Increase range',
68
].forEach((testCase) => {
69
const controls = shallow(<GraphControls {...defaultGraphControlProps} />);
70
const addon = controls.find(InputGroupAddon).filterWhere((addon) => addon.prop('addonType') === testCase.position);
71
const button = addon.find(Button);
72
const icon = button.find(FontAwesomeIcon);
73
expect(button.prop('title')).toEqual(testCase.title);
74
expect(icon).toHaveLength(1);
75
expect(icon.prop('icon')).toEqual(testCase.icon);
76
expect(icon.prop('fixedWidth')).toBe(true);
80
it('renders an Input for range', () => {
81
const controls = shallow(<GraphControls {...defaultGraphControlProps} />);
82
const form = controls.find(InputGroup);
83
const input = form.find(Input);
84
expect(input).toHaveLength(1);
85
expect(input.prop('defaultValue')).toEqual('1d');
86
expect(input.prop('innerRef')).toEqual({ current: null });
89
it('renders a TimeInput with props', () => {
90
const controls = shallow(<GraphControls {...defaultGraphControlProps} />);
91
const timeInput = controls.find(TimeInput);
92
expect(timeInput).toHaveLength(1);
93
expect(timeInput.prop('time')).toEqual(1572100217898);
94
expect(timeInput.prop('range')).toEqual(86400000);
95
expect(timeInput.prop('placeholder')).toEqual('End time');
98
it('renders a TimeInput with a callback', () => {
99
const results: (number | null)[] = [];
100
const onChange = (endTime: number | null): void => {
101
results.push(endTime);
103
const controls = shallow(<GraphControls {...defaultGraphControlProps} onChangeEndTime={onChange} />);
104
const timeInput = controls.find(TimeInput);
105
const onChangeTime = timeInput.prop('onChangeTime');
108
expect(results).toHaveLength(1);
109
expect(results[0]).toEqual(5);
112
fail('Expected onChangeTime to be defined but it was not');
116
it('renders a resolution Input with props', () => {
117
const controls = shallow(<GraphControls {...defaultGraphControlProps} />);
118
const input = controls.find(Input).filterWhere((input) => input.prop('className') === 'resolution-input');
119
expect(input.prop('placeholder')).toEqual('Res. (s)');
120
expect(input.prop('defaultValue')).toEqual('10');
121
expect(input.prop('innerRef')).toEqual({ current: null });
122
expect(input.prop('bsSize')).toEqual('sm');
125
it('renders button groups', () => {
127
{ className: 'stacked-input', size: 'sm' },
128
{ className: 'show-exemplars', size: 'sm' },
129
].forEach((testCase, i) => {
130
const controls = shallow(<GraphControls {...defaultGraphControlProps} />);
131
const groups = controls.find(ButtonGroup);
132
expect(groups.get(i).props['className']).toEqual(testCase.className);
133
expect(groups.get(i).props['size']).toEqual(testCase.size);
137
it('renders buttons inside the button group', () => {
140
title: 'Show unstacked line graph',
145
title: 'Show stacked graph',
149
].forEach((testCase) => {
150
const controls = shallow(<GraphControls {...defaultGraphControlProps} />);
151
const group = controls.find(ButtonGroup);
152
const btn = group.find(Button).filterWhere((btn) => btn.prop('title') === testCase.title);
153
expect(btn.prop('active')).toEqual(testCase.active);
154
const icon = btn.find(FontAwesomeIcon);
155
expect(icon.prop('icon')).toEqual(testCase.icon);
159
it('renders buttons with callbacks', () => {
162
title: 'Show unstacked line graph',
166
title: 'Show stacked graph',
169
].forEach((testCase) => {
170
const results: boolean[] = [];
171
const onChange = (mode: GraphDisplayMode): void => {
172
results.push(mode === GraphDisplayMode.Stacked);
174
const controls = shallow(<GraphControls {...defaultGraphControlProps} onChangeDisplayMode={onChange} />);
175
const group = controls.find(ButtonGroup);
176
const btn = group.find(Button).filterWhere((btn) => btn.prop('title') === testCase.title);
177
const onClick = btn.prop('onClick');
179
btn.simulate('click');
180
expect(results).toHaveLength(1);
181
expect(results[0]).toBe(!testCase.active);
184
fail('Expected onClick to be defined but it was not');