diff --git a/public/app/features/explore/QueryField.test.tsx b/public/app/features/explore/QueryField.test.tsx index 9b3e915a93b..e58127345f0 100644 --- a/public/app/features/explore/QueryField.test.tsx +++ b/public/app/features/explore/QueryField.test.tsx @@ -4,16 +4,28 @@ import { shallow } from 'enzyme'; import { QueryField } from './QueryField'; describe('', () => { - it('renders with null initial value', () => { + it('should render with null initial value', () => { const wrapper = shallow(); expect(wrapper.find('div').exists()).toBeTruthy(); }); - it('renders with empty initial value', () => { + + it('should render with empty initial value', () => { const wrapper = shallow(); expect(wrapper.find('div').exists()).toBeTruthy(); }); - it('renders with initial value', () => { + + it('should render with initial value', () => { const wrapper = shallow(); expect(wrapper.find('div').exists()).toBeTruthy(); }); + + it('should execute query when enter is pressed and there are no suggestions visible', () => { + const wrapper = shallow(); + const instance = wrapper.instance() as QueryField; + instance.executeOnChangeAndRunQueries = jest.fn(); + const handleEnterAndTabKeySpy = jest.spyOn(instance, 'handleEnterAndTabKey'); + instance.onKeyDown({ key: 'Enter', preventDefault: () => {} } as KeyboardEvent, {}); + expect(handleEnterAndTabKeySpy).toBeCalled(); + expect(instance.executeOnChangeAndRunQueries).toBeCalled(); + }); }); diff --git a/public/app/features/explore/QueryField.tsx b/public/app/features/explore/QueryField.tsx index 9474b22ebf4..0bbc97ba16c 100644 --- a/public/app/features/explore/QueryField.tsx +++ b/public/app/features/explore/QueryField.tsx @@ -307,29 +307,23 @@ export class QueryField extends React.PureComponent { const { typeaheadIndex, suggestions } = this.state; - if (this.menuEl) { - // Dont blur input - event.preventDefault(); - if (!suggestions || suggestions.length === 0) { - return undefined; - } + event.preventDefault(); - const suggestion = getSuggestionByIndex(suggestions, typeaheadIndex); - const nextChange = this.applyTypeahead(change, suggestion); - - const insertTextOperation = nextChange.operations.find((operation: any) => operation.type === 'insert_text'); - if (insertTextOperation) { - return undefined; - } - - return true; - } else if (!event.shiftKey) { - // Run queries if Shift is not pressed, otherwise pass through + if (event.shiftKey) { + // pass through if shift is pressed + return undefined; + } else if (!this.menuEl) { this.executeOnChangeAndRunQueries(); - return true; + } else if (!suggestions || suggestions.length === 0) { + return undefined; } - return undefined; + + const suggestion = getSuggestionByIndex(suggestions, typeaheadIndex); + const nextChange = this.applyTypeahead(change, suggestion); + + const insertTextOperation = nextChange.operations.find((operation: any) => operation.type === 'insert_text'); + return insertTextOperation ? true : undefined; }; onKeyDown = (event: KeyboardEvent, change: Change) => {