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

Don't request suggestions for blank queries #74

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
4 changes: 4 additions & 0 deletions src/js/typeahead_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ var TypeaheadView = (function() {
var that = this,
query = this.inputView.getQuery();

if (utils.isBlankString(query)) {
return;
}

utils.each(this.datasets, function(i, dataset) {
dataset.getSuggestions(query, function(suggestions) {
that._renderSuggestions(query, dataset, suggestions);
Expand Down
2 changes: 2 additions & 0 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var utils = {
return (/msie [\w.]+/i).test(navigator.userAgent);
},

isBlankString: function(str) { return !str || /^\s*$/.test(str); },

isString: function(obj) { return typeof obj === 'string'; },

isNumber: function(obj) { return typeof obj === 'number'; },
Expand Down
26 changes: 22 additions & 4 deletions test/typeahead_view_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,28 @@ describe('TypeaheadView', function() {
expect(this.dropdownView.clearSuggestions).toHaveBeenCalled();
});

it('should call dropdownView.renderSuggestions for each dataset',
function() {
this.inputView.trigger('queryChange');
expect(this.dropdownView.renderSuggestions.callCount).toBe(3);
describe('if query is a blank string', function() {
beforeEach(function() {
this.inputView.getQuery.andReturn(' ');
this.inputView.trigger('queryChange');
});

it('should not call dropdownView.renderSuggestions for each dataset',
function() {
expect(this.dropdownView.renderSuggestions.callCount).toBe(0);
});
});

describe('if query is not a blank string', function() {
beforeEach(function() {
this.inputView.getQuery.andReturn('not blank');
this.inputView.trigger('queryChange');
});

it('should call dropdownView.renderSuggestions for each dataset',
function() {
expect(this.dropdownView.renderSuggestions.callCount).toBe(3);
});
});

describe('if language direction has changed', function() {
Expand Down