Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(autocomplete): handle undefined searchText
Browse files Browse the repository at this point in the history
If the autocomplete's bound searchText was set to undefined,
it would throw an error.

Fix by ensuring that the searchText will always be an empty
string when performing the `handleQuery()` method.

Fixes #5162. Fixes #5393. References #5344.
  • Loading branch information
topherfangio committed Oct 30, 2015
1 parent c99541f commit 9260a54
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/components/autocomplete/autocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,38 @@ describe('<md-autocomplete>', function() {
}));
});

describe('when required', function() {
it('properly handles md-min-length="0" and undefined searchText', function() {
var scope = createScope();
var template = '\
<md-autocomplete\
md-selected-item="selectedItem"\
md-search-text="searchText"\
md-items="item in match(searchText)"\
md-item-text="item.display"\
md-min-length="0" \
required\
placeholder="placeholder">\
<span md-highlight-text="searchText">{{item.display}}</span>\
</md-autocomplete>';

var error;

try {
var element = compile(template, scope);

scope.searchText = undefined;
scope.$digest();
} catch (e) {
error = e;
}

expect(error).toBe(undefined);

element.remove();
});
});

describe('md-highlight-text', function() {
it('should update when content is modified', inject(function() {
var template = '<div md-highlight-text="query">{{message}}</div>';
Expand Down
2 changes: 1 addition & 1 deletion src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
* results first, then forwards the process to `fetchResults` if necessary.
*/
function handleQuery () {
var searchText = $scope.searchText,
var searchText = $scope.searchText || '',
term = searchText.toLowerCase();
//-- if results are cached, pull in cached results
if (!$scope.noCache && cache[ term ]) {
Expand Down

0 comments on commit 9260a54

Please sign in to comment.