Stop query from executing if predicate is empty

If user types a query in the search bar with incomplete predicate then we do not
execute the query if it is one of the pre-defined ones.

Bug: Issue 7083
Change-Id: I5b29caf3a025f8b0282185133e18de1b6f21c859
This commit is contained in:
Dhruv Srivastava
2019-10-12 13:33:09 +02:00
parent bcd6dba57d
commit 098e93f397
2 changed files with 47 additions and 2 deletions

View File

@@ -96,7 +96,7 @@
// All of the ops, with corresponding negations.
const SEARCH_OPERATORS_WITH_NEGATIONS =
SEARCH_OPERATORS.concat(SEARCH_OPERATORS.map(op => `-${op}`));
SEARCH_OPERATORS.concat(SEARCH_OPERATORS.map(op => `-${op}`));
const MAX_AUTOCOMPLETE_RESULTS = 10;
@@ -191,7 +191,16 @@
} else {
target.blur();
}
if (this._inputVal) {
const trimmedInput = this._inputVal && this._inputVal.trim();
if (trimmedInput) {
const predefinedOpOnlyQuery = SEARCH_OPERATORS_WITH_NEGATIONS.some(
op => {
return op.endsWith(':') && op === trimmedInput;
}
);
if (predefinedOpOnlyQuery) {
return;
}
this.dispatchEvent(new CustomEvent('handle-search', {
detail: {inputVal: this._inputVal},
}));

View File

@@ -93,6 +93,42 @@ limitations under the License.
assert.isFalse(searchSpy.called);
});
test('Predefined query op with no predication doesnt trigger nav', () => {
const searchSpy = sandbox.spy();
element.addEventListener('handle-search', searchSpy);
element.value = 'added:';
MockInteractions.pressAndReleaseKeyOn(element.$.searchInput.$.input, 13,
null, 'enter');
assert.isFalse(searchSpy.called);
});
test('predefined predicate query triggers nav', () => {
const searchSpy = sandbox.spy();
element.addEventListener('handle-search', searchSpy);
element.value = 'age:1week';
MockInteractions.pressAndReleaseKeyOn(element.$.searchInput.$.input, 13,
null, 'enter');
assert.isTrue(searchSpy.called);
});
test('undefined predicate query triggers nav', () => {
const searchSpy = sandbox.spy();
element.addEventListener('handle-search', searchSpy);
element.value = 'random:1week';
MockInteractions.pressAndReleaseKeyOn(element.$.searchInput.$.input, 13,
null, 'enter');
assert.isTrue(searchSpy.called);
});
test('empty undefined predicate query triggers nav', () => {
const searchSpy = sandbox.spy();
element.addEventListener('handle-search', searchSpy);
element.value = 'random:';
MockInteractions.pressAndReleaseKeyOn(element.$.searchInput.$.input, 13,
null, 'enter');
assert.isTrue(searchSpy.called);
});
test('keyboard shortcuts', () => {
const focusSpy = sandbox.spy(element.$.searchInput, 'focus');
const selectAllSpy = sandbox.spy(element.$.searchInput, 'selectAll');