1
import * as React from 'react';
2
import { mount, ReactWrapper } from 'enzyme';
3
import ExpressionInput from './ExpressionInput';
4
import { Button, InputGroup, InputGroupAddon } from 'reactstrap';
5
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
6
import { faSearch, faSpinner } from '@fortawesome/free-solid-svg-icons';
8
describe('ExpressionInput', () => {
9
const expressionInputProps = {
13
executeQuery: (): void => {
16
onExpressionChange: (): void => {
20
enableAutocomplete: true,
21
enableHighlighting: true,
25
let expressionInput: ReactWrapper;
27
expressionInput = mount(<ExpressionInput {...expressionInputProps} />);
30
it('renders an InputGroup', () => {
31
const inputGroup = expressionInput.find(InputGroup);
32
expect(inputGroup.prop('className')).toEqual('expression-input');
35
it('renders a search icon when it is not loading', () => {
36
const addon = expressionInput.find(InputGroupAddon).filterWhere((addon) => addon.prop('addonType') === 'prepend');
37
const icon = addon.find(FontAwesomeIcon);
38
expect(icon.prop('icon')).toEqual(faSearch);
41
it('renders a loading icon when it is loading', () => {
42
const expressionInput = mount(<ExpressionInput {...expressionInputProps} loading={true} />);
43
const addon = expressionInput.find(InputGroupAddon).filterWhere((addon) => addon.prop('addonType') === 'prepend');
44
const icon = addon.find(FontAwesomeIcon);
45
expect(icon.prop('icon')).toEqual(faSpinner);
46
expect(icon.prop('spin')).toBe(true);
49
it('renders a CodeMirror expression input', () => {
50
const input = expressionInput.find('div.cm-expression-input');
51
expect(input.text()).toContain('node_cpu');
54
it('renders an execute button', () => {
55
const addon = expressionInput.find(InputGroupAddon).filterWhere((addon) => addon.prop('addonType') === 'append');
56
const button = addon.find(Button).find('.execute-btn').first();
57
expect(button.prop('color')).toEqual('primary');
58
expect(button.text()).toEqual('Execute');
61
it('executes the query when clicking the execute button', () => {
62
const spyExecuteQuery = jest.fn();
63
const props = { ...expressionInputProps, executeQuery: spyExecuteQuery };
64
const wrapper = mount(<ExpressionInput {...props} />);
65
const btn = wrapper.find(Button).filterWhere((btn) => btn.hasClass('execute-btn'));
66
btn.simulate('click');
67
expect(spyExecuteQuery).toHaveBeenCalledTimes(1);