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

Commit 9990e61

Browse files
epelcThomasBurleson
authored andcommitted
fix(autocomplete): tests wait for promises to complete now
Fixes #2462. Closes #2710.
1 parent 7430e68 commit 9990e61

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ describe('<md-autocomplete>', function() {
5858
ctrl.keydown({ keyCode: $mdConstant.KEY_CODE.DOWN_ARROW, preventDefault: angular.noop });
5959
ctrl.keydown({ keyCode: $mdConstant.KEY_CODE.ENTER, preventDefault: angular.noop });
6060
scope.$apply();
61+
$timeout.flush();
62+
6163
expect(scope.searchText).toBe('foo');
6264
expect(scope.selectedItem).toBe(scope.match(scope.searchText)[0]);
6365
}));
@@ -96,6 +98,8 @@ describe('<md-autocomplete>', function() {
9698
ctrl.keydown({ keyCode: $mdConstant.KEY_CODE.DOWN_ARROW, preventDefault: angular.noop });
9799
ctrl.keydown({ keyCode: $mdConstant.KEY_CODE.ENTER, preventDefault: angular.noop });
98100
scope.$apply();
101+
$timeout.flush();
102+
99103
expect(scope.searchText).toBe('foo');
100104
expect(scope.selectedItem).toBe(scope.match(scope.searchText)[0]);
101105
}));
@@ -153,6 +157,7 @@ describe('<md-autocomplete>', function() {
153157

154158
ctrl.select(0);
155159
element.scope().$apply();
160+
$timeout.flush();
156161

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

197202
ctrl.select(0);
198203
element.scope().$apply();
204+
$timeout.flush();
199205

200206
expect(scope.itemChanged).toHaveBeenCalled();
201207
expect(scope.itemChanged.calls.mostRecent().args[0].display).toBe('foo');

src/components/autocomplete/js/autocompleteController.js

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,19 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
239239
$scope.searchText = val;
240240
});
241241
}
242-
if (angular.isFunction($scope.itemChange) && selectedItem !== previousSelectedItem)
243-
$scope.itemChange(getItemScope(selectedItem));
242+
243+
if (selectedItem !== previousSelectedItem) announceItemChange(selectedItem);
244+
}
245+
246+
/**
247+
* Use the user-defined expression to announce changes each time a new item is selected
248+
*/
249+
function announceItemChange( current ) {
250+
angular.isFunction($scope.itemChange) && $scope.itemChange( getItemAsNameVal(current) );
251+
}
252+
253+
function announceTextChange( value ) {
254+
angular.isFunction($scope.textChange) && $scope.textChange(value);
244255
}
245256

246257
/**
@@ -283,25 +294,27 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
283294
*/
284295
function handleSearchText (searchText, previousSearchText) {
285296
ctrl.index = getDefaultIndex();
286-
//-- do nothing on init
297+
// do nothing on init
287298
if (searchText === previousSearchText) return;
288299

289300
getDisplayValue($scope.selectedItem).then(function(val) {
290-
//-- clear selected item if search text no longer matches it
291-
if (searchText !== val) $scope.selectedItem = null;
292-
else return;
293-
294-
//-- trigger change event if available
295-
if (angular.isFunction($scope.textChange) && searchText !== previousSearchText)
296-
$scope.textChange(getItemScope($scope.selectedItem));
297-
//-- cancel results if search text is not long enough
298-
if (!isMinLengthMet()) {
299-
ctrl.loading = false;
300-
ctrl.matches = [];
301-
ctrl.hidden = shouldHide();
302-
updateMessages();
303-
} else {
304-
handleQuery();
301+
// clear selected item if search text no longer matches it
302+
if (searchText !== val)
303+
{
304+
$scope.selectedItem = null;
305+
306+
// trigger change event if available
307+
if ( searchText !== previousSearchText ) announceTextChange(searchText);
308+
309+
// cancel results if search text is not long enough
310+
if (!isMinLengthMet()) {
311+
ctrl.loading = false;
312+
ctrl.matches = [];
313+
ctrl.hidden = shouldHide();
314+
updateMessages();
315+
} else {
316+
handleQuery();
317+
}
305318
}
306319
});
307320

@@ -378,21 +391,34 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
378391
* @returns {*}
379392
*/
380393
function getDisplayValue (item) {
381-
return (item && $scope.itemText) ? $q.when($scope.itemText(getItemScope(item))) : $q.when(item);
394+
return $q.when( getItemText(item) || item );
395+
396+
/**
397+
* Getter function to invoke user-defined expression (in the directive)
398+
* to convert your object to a single string.
399+
*/
400+
function getItemText(item) {
401+
return (item && $scope.itemText) ? $scope.itemText(getItemAsNameVal(item)) : null;
402+
}
382403
}
383404

384405
/**
385406
* Returns the locals object for compiling item templates.
386407
* @param item
387408
* @returns {{}}
388409
*/
389-
function getItemScope (item) {
390-
if (!item) return;
410+
function getItemAsNameVal (item) {
411+
if (!item) return undefined;
412+
391413
var locals = {};
392414
if (ctrl.itemName) locals[ctrl.itemName] = item;
415+
393416
return locals;
394417
}
395418

419+
420+
421+
396422
/**
397423
* Returns the default index based on whether or not autoselect is enabled.
398424
* @returns {number}
@@ -414,7 +440,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
414440
* @returns {*}
415441
*/
416442
function getCurrentDisplayValue () {
417-
return getDisplayValue(ctrl.matches[ctrl.index]);
443+
return getDisplayValue( ctrl.matches[ctrl.index] );
418444
}
419445

420446
/**

src/components/chips/chips.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ describe('<md-chips>', function() {
159159
element.scope().$apply(function() {
160160
autocompleteCtrl.select(0);
161161
});
162-
162+
$timeout.flush();
163+
163164
expect(scope.items.length).toBe(4);
164165
expect(scope.items[3]).toBe('Kiwi');
165166
}));

0 commit comments

Comments
 (0)