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

Commit

Permalink
fix(autocomplete): tests wait for promises to complete now
Browse files Browse the repository at this point in the history
Fixes #2462. Closes #2710.
  • Loading branch information
epelc authored and Splaktar committed Jul 4, 2015
1 parent d6c2f6c commit 539740d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
6 changes: 6 additions & 0 deletions src/components/autocomplete/autocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ describe('<md-autocomplete>', function() {
ctrl.keydown({ keyCode: $mdConstant.KEY_CODE.DOWN_ARROW, preventDefault: angular.noop });
ctrl.keydown({ keyCode: $mdConstant.KEY_CODE.ENTER, preventDefault: angular.noop });
scope.$apply();
$timeout.flush();

expect(scope.searchText).toBe('foo');
expect(scope.selectedItem).toBe(scope.match(scope.searchText)[0]);
}));
Expand Down Expand Up @@ -96,6 +98,8 @@ describe('<md-autocomplete>', function() {
ctrl.keydown({ keyCode: $mdConstant.KEY_CODE.DOWN_ARROW, preventDefault: angular.noop });
ctrl.keydown({ keyCode: $mdConstant.KEY_CODE.ENTER, preventDefault: angular.noop });
scope.$apply();
$timeout.flush();

expect(scope.searchText).toBe('foo');
expect(scope.selectedItem).toBe(scope.match(scope.searchText)[0]);
}));
Expand Down Expand Up @@ -153,6 +157,7 @@ describe('<md-autocomplete>', function() {

ctrl.select(0);
element.scope().$apply();
$timeout.flush();

expect(scope.searchText).toBe('foo');
expect(scope.selectedItem).not.toBeNull();
Expand Down Expand Up @@ -196,6 +201,7 @@ describe('<md-autocomplete>', function() {

ctrl.select(0);
element.scope().$apply();
$timeout.flush();

expect(scope.itemChanged).toHaveBeenCalled();
expect(scope.itemChanged.calls.mostRecent().args[0].display).toBe('foo');
Expand Down
70 changes: 48 additions & 22 deletions src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,19 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
$scope.searchText = val;
});
}
if (angular.isFunction($scope.itemChange) && selectedItem !== previousSelectedItem)
$scope.itemChange(getItemScope(selectedItem));

if (selectedItem !== previousSelectedItem) announceItemChange(selectedItem);
}

/**
* Use the user-defined expression to announce changes each time a new item is selected
*/
function announceItemChange( current ) {
angular.isFunction($scope.itemChange) && $scope.itemChange( getItemAsNameVal(current) );
}

function announceTextChange( value ) {
angular.isFunction($scope.textChange) && $scope.textChange(value);
}

/**
Expand Down Expand Up @@ -283,25 +294,27 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
*/
function handleSearchText (searchText, previousSearchText) {
ctrl.index = getDefaultIndex();
//-- do nothing on init
// do nothing on init
if (searchText === previousSearchText) return;

getDisplayValue($scope.selectedItem).then(function(val) {
//-- clear selected item if search text no longer matches it
if (searchText !== val) $scope.selectedItem = null;
else return;

//-- trigger change event if available
if (angular.isFunction($scope.textChange) && searchText !== previousSearchText)
$scope.textChange(getItemScope($scope.selectedItem));
//-- cancel results if search text is not long enough
if (!isMinLengthMet()) {
ctrl.loading = false;
ctrl.matches = [];
ctrl.hidden = shouldHide();
updateMessages();
} else {
handleQuery();
// clear selected item if search text no longer matches it
if (searchText !== val)
{
$scope.selectedItem = null;

// trigger change event if available
if ( searchText !== previousSearchText ) announceTextChange(searchText);

// cancel results if search text is not long enough
if (!isMinLengthMet()) {
ctrl.loading = false;
ctrl.matches = [];
ctrl.hidden = shouldHide();
updateMessages();
} else {
handleQuery();
}
}
});

Expand Down Expand Up @@ -378,21 +391,34 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
* @returns {*}
*/
function getDisplayValue (item) {
return (item && $scope.itemText) ? $q.when($scope.itemText(getItemScope(item))) : $q.when(item);
return $q.when( getItemText(item) || item );

/**
* Getter function to invoke user-defined expression (in the directive)
* to convert your object to a single string.
*/
function getItemText(item) {
return (item && $scope.itemText) ? $scope.itemText(getItemAsNameVal(item)) : null;
}
}

/**
* Returns the locals object for compiling item templates.
* @param item
* @returns {{}}
*/
function getItemScope (item) {
if (!item) return;
function getItemAsNameVal (item) {
if (!item) return undefined;

var locals = {};
if (ctrl.itemName) locals[ctrl.itemName] = item;

return locals;
}




/**
* Returns the default index based on whether or not autoselect is enabled.
* @returns {number}
Expand All @@ -414,7 +440,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
* @returns {*}
*/
function getCurrentDisplayValue () {
return getDisplayValue(ctrl.matches[ctrl.index]);
return getDisplayValue( ctrl.matches[ctrl.index] );
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/components/chips/chips.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ describe('<md-chips>', function() {
element.scope().$apply(function() {
autocompleteCtrl.select(0);
});

$timeout.flush();

expect(scope.items.length).toBe(4);
expect(scope.items[3]).toBe('Kiwi');
}));
Expand Down

0 comments on commit 539740d

Please sign in to comment.