Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty search queries (ignore it) #954

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/features/popup-menu/PopupMenuComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default function PopupMenuComponent(props) {
return originalEntries;
}

if (!value) {
if (!value.trim()) {
return originalEntries.filter(({ rank = 0 }) => rank >= 0);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/features/search-pad/SearchPad.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ SearchPad.prototype._search = function(pattern) {
this._clearResults();

// do not search on empty query
if (!pattern || pattern === '') {
if (!pattern.trim()) {
return;
}

Expand Down
6 changes: 6 additions & 0 deletions lib/features/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export default function search(items, pattern, options) {
keys
} = options;

pattern = pattern.trim().toLowerCase();

if (!pattern) {
throw new Error('<pattern> must not be empty');
}

const words = pattern.trim().toLowerCase().split(/\s+/);

return items.flatMap((item) => {
Expand Down
19 changes: 19 additions & 0 deletions test/spec/features/popup-menu/PopupMenuSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,25 @@ describe('features/popup-menu', function() {
}));


it('should handle whitespace only search', inject(async function(popupMenu) {

// given
popupMenu.registerProvider('test-menu', testMenuProvider);
popupMenu.open({}, 'test-menu', { x: 100, y: 100 }, { search: true });

// when
await triggerSearch(' ');

// then
await waitFor(() => {
const shownEntries = queryPopupAll('.entry');

// just ignores it
expect(shownEntries).to.have.length(5);
});
}));


describe('ranking', function() {

it('should hide rank < 0 items', inject(async function(popupMenu) {
Expand Down
13 changes: 13 additions & 0 deletions test/spec/features/search-pad/SearchPadSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,19 @@ describe('features/search-pad', function() {
}));


it('should ignore whitespace only', inject(function(canvas) {

// given
var find = sinon.spy(searchProvider, 'find');

// when
typeText(input_node, ' ');

// then
expect(find).callCount(0);
}));


it('should search per key stroke', inject(function(canvas) {

// given
Expand Down
16 changes: 16 additions & 0 deletions test/spec/features/search/searchSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,22 @@ describe('features/search', function() {
expect(results).to.have.length(1);
}));


it('should error on whitespace pattern', inject(function(search) {

// given
const fn = () => {
search([], ' ', {
keys: [
'title'
]
});
};

// then
expect(fn).to.throw(/<pattern> must not be empty/);
}));

});


Expand Down