Skip to content

Commit

Permalink
Merge pull request #74 from jharding/65-dont-request-suggestions-for-…
Browse files Browse the repository at this point in the history
…blank-queries

Don't request suggestions for blank queries
  • Loading branch information
jharding committed Mar 3, 2013
2 parents c5ac72c + b4a8ad9 commit 8fc7229
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
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

0 comments on commit 8fc7229

Please sign in to comment.