Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit df713e3

Browse files
committed
Fixed unit testing
1 parent d311644 commit df713e3

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/select.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@
6565
};
6666
}
6767

68+
/**
69+
* Add closest() to jqLite.
70+
*/
71+
if (angular.element.prototype.closest === undefined) {
72+
angular.element.prototype.closest = function( selector) {
73+
var elem = this[0];
74+
var matchesSelector = elem.matches || elem.webkitMatchesSelector || elem.mozMatchesSelector || elem.msMatchesSelector;
75+
76+
while (elem) {
77+
if (matchesSelector.bind(elem)(selector)) {
78+
return elem;
79+
} else {
80+
elem = elem.parentElement;
81+
}
82+
}
83+
return false;
84+
};
85+
}
86+
6887
angular.module('ui.select', [])
6988

7089
.constant('uiSelectConfig', {
@@ -1079,24 +1098,6 @@
10791098
$select.selected = ngModel.$viewValue;
10801099
};
10811100

1082-
/**
1083-
* Checks if the current target is a ui-select-match element
1084-
*
1085-
* @param target
1086-
* @returns {boolean}
1087-
*/
1088-
function isClosedUiSelect(target) {
1089-
var node = target.parentNode;
1090-
while (node !== null) {
1091-
if (angular.element(node).hasClass('ui-select-container') &&
1092-
angular.element(node).hasClass('open')) {
1093-
return true;
1094-
}
1095-
node = node.parentNode;
1096-
}
1097-
return false;
1098-
}
1099-
11001101
function onDocumentClick(e) {
11011102
var contains = false;
11021103

@@ -1109,7 +1110,7 @@
11091110
}
11101111

11111112
if (!contains && !$select.clickTriggeredSelect) {
1112-
$select.close(isClosedUiSelect(e.target)); // Skip focusser if the target is another select
1113+
$select.close(angular.element(e.target).closest('.ui-select-container.open').length > 0); // Skip focusser if the target is another select
11131114
scope.$digest();
11141115
}
11151116
$select.clickTriggeredSelect = false;

test/select.spec.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ describe('ui-select tests', function() {
318318
it('should close an opened select when another one is opened', function() {
319319
var el1 = createUiSelect();
320320
var el2 = createUiSelect();
321+
el1.appendTo(document.body);
322+
el2.appendTo(document.body);
321323

322324
expect(isDropdownOpened(el1)).toEqual(false);
323325
expect(isDropdownOpened(el2)).toEqual(false);
@@ -327,7 +329,10 @@ describe('ui-select tests', function() {
327329
clickMatch(el2);
328330
expect(isDropdownOpened(el1)).toEqual(false);
329331
expect(isDropdownOpened(el2)).toEqual(true);
330-
});
332+
333+
el1.remove();
334+
el2.remove();
335+
});
331336

332337
describe('disabled options', function() {
333338
function createUiSelect(attrs) {

0 commit comments

Comments
 (0)