diff --git a/src/js/typeahead_view.js b/src/js/typeahead_view.js index d3498388..a77eddf0 100644 --- a/src/js/typeahead_view.js +++ b/src/js/typeahead_view.js @@ -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); diff --git a/src/js/utils.js b/src/js/utils.js index 15d937f3..62f3b9c5 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -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'; }, diff --git a/test/typeahead_view_spec.js b/test/typeahead_view_spec.js index b0933097..516d00dc 100644 --- a/test/typeahead_view_spec.js +++ b/test/typeahead_view_spec.js @@ -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() {