forked from angular-ui/ui-select
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselect.spec.js
217 lines (166 loc) · 6.74 KB
/
select.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
'use strict';
describe('ui-select tests', function() {
var scope, $rootScope, $compile;
beforeEach(module('ngSanitize', 'ui.select'));
beforeEach(inject(function(_$rootScope_, _$compile_) {
$rootScope = _$rootScope_;
scope = $rootScope.$new();
$compile = _$compile_;
scope.people = [
{ name: 'Adam', email: '[email protected]', age: 10 },
{ name: 'Amalie', email: '[email protected]', age: 12 },
{ name: 'Wladimir', email: '[email protected]', age: 30 },
{ name: 'Samantha', email: '[email protected]', age: 31 },
{ name: 'Estefanía', email: 'estefaní[email protected]', age: 16 },
{ name: 'Natasha', email: '[email protected]', age: 54 },
{ name: 'Nicole', email: '[email protected]', age: 43 },
{ name: 'Adrian', email: '[email protected]', age: 21 }
];
scope.names = [
'Adam',
'Amalie',
'Wladimir',
'Samantha',
'Estefanía',
'Natasha',
'Nicole',
'Adrian'
];
}));
// Utility functions
function compileTemplate(template) {
var el = $compile(angular.element(template))(scope);
scope.$digest();
return el;
}
function generateAttrsHtml(attrs) {
var attrsHtml = '';
if (attrs !== undefined) {
if (attrs.disabled !== undefined) { attrsHtml += ' ng-disabled="' + attrs.disabled + '"'; }
if (attrs.required !== undefined) { attrsHtml += ' ng-required="' + attrs.required + '"'; }
if (attrs.allowNewValues !== undefined) { attrsHtml += ' allow-new-values="' + attrs.allowNewValues + '"'; }
}
return attrsHtml;
}
function createUiSelectCollection(attrs) {
return compileTemplate(
'<ui-select ng-model="selection"' + generateAttrsHtml(attrs) + '> \
<match placeholder="Pick one...">{{$select.selected}}</match> \
<choices repeat="name in names | filter: $select.search"> \
<div ng-bind-html="name | highlight: $select.search"></div> \
</choices> \
</ui-select>'
);
}
function createUiSelect(attrs) {
return compileTemplate(
'<ui-select ng-model="selection"' + generateAttrsHtml(attrs) + '> \
<match placeholder="Pick one...">{{$select.selected.name}}</match> \
<choices repeat="person in people | filter: $select.search"> \
<div ng-bind-html="person.name | highlight: $select.search"></div> \
<div ng-bind-html="person.email | highlight: $select.search"></div> \
</choices> \
</ui-select>'
);
}
function getMatchLabel(el) {
return $(el).find('.ui-select-match > span[ng-transclude]:not(.ng-hide)').text();
}
function clickItem(el, text) {
$(el).find('.ui-select-choices-row > div:contains("' + text + '")').click();
scope.$digest();
}
function clickMatch(el) {
$(el).find('.ui-select-match').click();
scope.$digest();
}
it('should compile child directives', function() {
var el = createUiSelect();
var searchEl = $(el).find('.ui-select-search');
expect(searchEl.length).toEqual(1);
var matchEl = $(el).find('.ui-select-match');
expect(matchEl.length).toEqual(1);
var choicesContentEl = $(el).find('.ui-select-choices-content');
expect(choicesContentEl.length).toEqual(1);
var choicesContainerEl = $(el).find('.ui-select-choices');
expect(choicesContainerEl.length).toEqual(1);
var choicesElems = $(el).find('.ui-select-choices-row');
expect(choicesElems.length).toEqual(8);
});
it('should correctly render initial state', function() {
scope.selection = scope.people[0];
var el = createUiSelect();
expect(getMatchLabel(el)).toEqual('Adam');
});
it('should display the choices when activated', function() {
var el = createUiSelect();
// Does not work with jQuery 2.*, have to use jQuery 1.11.*
// This will be fixed in AngularJS 1.3
// See issue with unit-testing directive using karma https://github.com/angular/angular.js/issues/4640#issuecomment-35002427
expect(el.scope().$select.open).toEqual(false);
clickMatch(el);
expect(el.scope().$select.open).toEqual(true);
expect($(el).find('.ui-select-choices').parent().hasClass('select2-display-none')).toEqual(false);
});
it('should select an item', function() {
var el = createUiSelect();
clickItem(el, 'Samantha');
expect(getMatchLabel(el)).toEqual('Samantha');
});
it('should select an item (controller)', function() {
var el = createUiSelect();
el.scope().$select.select(scope.people[1]);
scope.$digest();
expect(getMatchLabel(el)).toEqual('Amalie');
});
it('should not select a non existing item', function() {
var el = createUiSelect();
clickItem(el, "I don't exist");
expect(getMatchLabel(el)).toEqual('');
});
it('should close the choices when an item is selected', function() {
var el = createUiSelect();
$(el).find('.ui-select-match').click();
scope.$digest();
expect(el.scope().$select.open).toEqual(true);
clickItem(el, 'Samantha');
expect(el.scope().$select.open).toEqual(false);
expect($(el).find('.ui-select-choices').parent().hasClass('select2-display-none')).toEqual(true);
});
it('should be disabled if the attribute says so', function() {
var el1 = createUiSelect({disabled: true});
expect(el1.scope().$select.disabled).toEqual(true);
clickMatch(el1);
expect(el1.scope().$select.open).toEqual(false);
var el2 = createUiSelect({disabled: false});
expect(el2.scope().$select.disabled).toEqual(false);
clickMatch(el2);
expect(el2.scope().$select.open).toEqual(true);
var el3 = createUiSelect();
expect(el3.scope().$select.disabled).toEqual(false);
clickMatch(el3);
expect(el3.scope().$select.open).toEqual(true);
});
it('should allow new values if the attribute says so', function() {
var el = createUiSelectCollection({allowNewValues: true});
clickMatch(el);
$(el).scope().$select.select("I don't exist");
expect($(el).scope().$select.selected).toEqual("I don't exist");
});
// 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
it('should not display the placeholder when item evaluates to false', function() {
scope.items = [ 'false' ];
var el = compileTemplate(
'<ui-select ng-model="selection"> \
<match>{{$select.selected}}</match> \
<choices repeat="item in items | filter: $select.search"> \
<div ng-bind-html="item | highlight: $select.search"></div> \
</choices> \
</ui-select>'
);
expect(el.scope().$select.selected).toEqual(undefined);
clickItem(el, 'false');
expect(el.scope().$select.selected).toEqual('false');
expect(getMatchLabel(el)).toEqual('false');
});
});