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

Commit e7ef5ee

Browse files
committed
More unit tests
1 parent 780835f commit e7ef5ee

File tree

6 files changed

+47
-27
lines changed

6 files changed

+47
-27
lines changed

src/bootstrap/choices.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<ul class="ui-select-choices ui-select-choices-content dropdown-menu"
22
role="menu" aria-labelledby="dLabel">
3-
<li class="ui-select-choices-row" ng-class="{active: $select.activeIdx == $index}">
3+
<li class="ui-select-choices-row" ng-class="{active: $select.activeIndex === $index}">
44
<a href="javascript:void(0)" ng-transclude></a>
55
</li>
66
</ul>

src/select.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ angular.module('ui.select', [])
1919

2020
.constant('uiSelectConfig', {
2121
theme: 'select2',
22-
placeholder: 'Select Item'
22+
placeholder: ''
2323
})
2424

2525
.directive('uiSelect',
@@ -43,12 +43,14 @@ angular.module('ui.select', [])
4343

4444
ctrl.open = false;
4545

46+
ctrl.input = $element.find('input'); // TODO could break if input is at other template
47+
4648
// When the user clicks on ui-select, displays the dropdown list
4749
ctrl.activate = function() {
4850
if (ctrl.disabled === false) {
4951
ctrl.open = true;
5052
// Give it time to appear before focus
51-
setTimeout(function() {
53+
$timeout(function() {
5254
ctrl.input[0].focus();
5355
});
5456
}
@@ -65,8 +67,6 @@ angular.module('ui.select', [])
6567
ctrl.open = false;
6668
ctrl.search = '';
6769
};
68-
69-
ctrl.input = $element.find('input'); // TODO could break if input is at other template
7070
}],
7171

7272
link: function(scope, element, attrs, controllers, transcludeFn) {
@@ -145,7 +145,7 @@ angular.module('ui.select', [])
145145

146146
tElement.querySelectorAll('.ui-select-choices-row')
147147
.attr('ng-repeat', tAttrs.repeat)
148-
.attr('ng-mouseenter', '$select.activeIdx = $index')
148+
.attr('ng-mouseenter', '$select.activeIndex = $index')
149149
.attr('ng-click', '$select.select(item)');
150150

151151
return function(scope, element, attrs, $select) {
@@ -155,7 +155,7 @@ angular.module('ui.select', [])
155155
};
156156

157157
scope.$watch('$select.search', function() {
158-
scope.$select.activeIdx = 0;
158+
scope.$select.activeIndex = 0;
159159
});
160160

161161
// Bind keyboard events related to choices
@@ -167,19 +167,19 @@ angular.module('ui.select', [])
167167
var rows = element.querySelectorAll('.ui-select-choices-row');
168168

169169
if (evt.which === 40) { // down(40)
170-
if (scope.$select.activeIdx < rows.length) {
171-
scope.$select.activeIdx = (scope.$select.activeIdx + 1) % rows.length || rows.length - 1;
170+
if (scope.$select.activeIndex < rows.length) {
171+
scope.$select.activeIndex = (scope.$select.activeIndex + 1) % rows.length || rows.length - 1;
172172
scope.$digest();
173173
}
174174

175175
} else if (evt.which === 38) { // up(38)
176-
if (scope.$select.activeIdx > 0) {
177-
scope.$select.activeIdx--;
176+
if (scope.$select.activeIndex > 0) {
177+
scope.$select.activeIndex--;
178178
scope.$digest();
179179
}
180180

181181
} else if (evt.which === 13 || evt.which === 9) { // enter(13) and tab(9)
182-
angular.element(rows[scope.$select.activeIdx]).triggerHandler('click');
182+
angular.element(rows[scope.$select.activeIndex]).triggerHandler('click');
183183

184184
} else if (evt.which === 27) { // esc(27)
185185
evt.stopPropagation();

src/select2/choices.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<ul class="ui-select-choices ui-select-choices-content select2-results">
2-
<li class="ui-select-choices-row" ng-class="{'select2-highlighted': $select.activeIdx == $index}">
2+
<li class="ui-select-choices-row" ng-class="{'select2-highlighted': $select.activeIndex === $index}">
33
<div class="select2-result-label" ng-transclude></div>
44
</li>
55
</ul>

src/select2/match.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
do not work: [class^="select2-choice"]
55
-->
66
<a class="select2-choice ui-select-match"
7-
ng-class="{'select2-default': $select.selected == undefined}"
7+
ng-class="{'select2-default': $select.selected === undefined}"
88
ng-click="$select.activate()">
99
<span ng-hide="$select.selected !== undefined" class="select2-chosen">{{$select.placeholder}}</span>
1010
<span ng-show="$select.selected !== undefined" class="select2-chosen" ng-transclude></span>

src/selectize/choices.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div ng-show="$select.open" class="ui-select-choices selectize-dropdown single">
22
<div class="ui-select-choices-content selectize-dropdown-content">
33
<div class="ui-select-choices-row"
4-
ng-class="{'active': $select.activeIdx == $index}">
4+
ng-class="{'active': $select.activeIndex === $index}">
55
<div class="option" data-selectable ng-transclude></div>
66
</div>
77
</div>

test/select.spec.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe('ui-select tests', function() {
88
$rootScope = _$rootScope_;
99
scope = $rootScope.$new();
1010
$compile = _$compile_;
11+
1112
scope.matches = [
1213
{ name: 'Wladimir Coka', email: '[email protected]' },
1314
{ name: 'Samantha Smith', email: '[email protected]' },
@@ -39,14 +40,14 @@ describe('ui-select tests', function() {
3940
<match placeholder="Pick one...">{{$select.selected.name}}</match> \
4041
<choices repeat="item in matches | filter: $select.search"> \
4142
<div ng-bind-html="trustAsHtml((item.name | highlight: $select.search))"></div> \
42-
<div>{{item.email}}</div> \
43+
<div ng-bind-html="trustAsHtml((item.email | highlight: $select.search))"></div> \
4344
</choices> \
4445
</ui-select>'
4546
);
4647
}
4748

4849
function getMatchLabel(el) {
49-
return $(el).find('.ui-select-match > span[ng-transclude]').text();
50+
return $(el).find('.ui-select-match > span[ng-transclude]:not(.ng-hide)').text();
5051
}
5152

5253
function clickItem(el, text) {
@@ -98,11 +99,7 @@ describe('ui-select tests', function() {
9899
clickMatch(el);
99100

100101
expect(el.scope().$select.open).toEqual(true);
101-
102-
// FIXME This should work and does not inside Karma
103-
var visible = $(el).find('.ui-select-choices').is(':visible');
104-
//expect(visible).toEqual(true);
105-
expect(visible).toEqual(false); // FIXME Always false in Karma
102+
expect($(el).find('.ui-select-choices').parent().hasClass('select2-display-none')).toEqual(false);
106103
});
107104

108105
it('should select an item', function() {
@@ -115,9 +112,8 @@ describe('ui-select tests', function() {
115112

116113
it('should select an item (controller)', function() {
117114
var el = createUiSelect();
118-
var controller = el.controller('uiSelect');
119115

120-
controller.select(scope.matches[1]);
116+
el.scope().$select.select(scope.matches[1]);
121117
scope.$digest();
122118

123119
expect(getMatchLabel(el)).toEqual('Samantha Smith');
@@ -141,20 +137,44 @@ describe('ui-select tests', function() {
141137

142138
clickItem(el, 'Samantha Smith');
143139

144-
var visible = $(el).find('.ui-select-choices').is(':visible');
145-
expect(visible).toEqual(false); // FIXME Always false in Karma
146-
147140
expect(el.scope().$select.open).toEqual(false);
141+
expect($(el).find('.ui-select-choices').parent().hasClass('select2-display-none')).toEqual(true);
148142
});
149143

150144
it('should be disabled if the attribute says so', function() {
151145
var el1 = createUiSelect({disabled: true});
152146
expect(el1.scope().$select.disabled).toEqual(true);
147+
clickMatch(el1);
148+
expect(el1.scope().$select.open).toEqual(false);
153149

154150
var el2 = createUiSelect({disabled: false});
155151
expect(el2.scope().$select.disabled).toEqual(false);
152+
clickMatch(el2);
153+
expect(el2.scope().$select.open).toEqual(true);
156154

157155
var el3 = createUiSelect();
158156
expect(el3.scope().$select.disabled).toEqual(false);
157+
clickMatch(el3);
158+
expect(el3.scope().$select.open).toEqual(true);
159+
});
160+
161+
// See when an item that evaluates to false (such as "false" or "no") is selected, the placeholder is shown https://github.com/angular-ui/ui-select/pull/32
162+
it('should not display the placeholder when item evaluates to false', function() {
163+
scope.matches = [ 'false' ];
164+
165+
var el = compileTemplate(
166+
'<ui-select ng-model="selection"> \
167+
<match>{{$select.selected}}</match> \
168+
<choices repeat="item in matches | filter: $select.search"> \
169+
<div ng-bind-html="trustAsHtml((item | highlight: $select.search))"></div> \
170+
</choices> \
171+
</ui-select>'
172+
);
173+
expect(el.scope().$select.selected).toEqual(undefined);
174+
175+
clickItem(el, 'false');
176+
177+
expect(el.scope().$select.selected).toEqual('false');
178+
expect(getMatchLabel(el)).toEqual('false');
159179
});
160180
});

0 commit comments

Comments
 (0)