select2Spec.js
16.7 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// a helper directive for injecting formatters and parsers
angular.module('ui.select2').directive('injectTransformers', [ function () {
return {
restrict: 'A',
require: 'ngModel',
priority: -1,
link: function (scope, element, attr, ngModel) {
var local = scope.$eval(attr.injectTransformers);
if (!angular.isObject(local) || !angular.isFunction(local.fromModel) || !angular.isFunction(local.fromElement)) {
throw "The injectTransformers directive must be bound to an object with two functions (`fromModel` and `fromElement`)";
}
ngModel.$parsers.push(local.fromElement);
ngModel.$formatters.push(local.fromModel);
}
};
}]);
/*global describe, beforeEach, module, inject, it, spyOn, expect, $ */
describe('uiSelect2', function () {
'use strict';
var scope, $compile, options, $timeout;
beforeEach(module('ui.select2'));
beforeEach(inject(function (_$rootScope_, _$compile_, _$window_, _$timeout_) {
scope = _$rootScope_.$new();
$compile = _$compile_;
$timeout = _$timeout_;
scope.options = {
query: function (query) {
var data = {
results: [{ id: 1, text: 'first' }]
};
query.callback(data);
}
};
scope.transformers = {
fromModel: function (modelValue) {
if (!modelValue) {
return modelValue;
}
if (angular.isArray(modelValue)) {
return modelValue.map(function (val) {
val.text += " - I've been formatted";
return val;
});
}
if (angular.isObject(modelValue)) {
modelValue.text += " - I've been formatted";
return modelValue;
}
return modelValue + " - I've been formatted";
},
fromElement: function (elementValue) {
var suffix = " - I've been formatted";
if (!elementValue) {
return elementValue;
}
if (angular.isArray(elementValue)) {
return elementValue.map(function (val) {
val.text += val.text.slice(0, val.text.indexOf(" - I've been formatted"));
return val;
});
}
if (angular.isObject(elementValue)) {
elementValue.text = elementValue.text.slice(0, elementValue.text.indexOf(suffix));
return elementValue;
}
if (elementValue) {
return elementValue.slice(0, elementValue.indexOf(suffix));
}
return undefined;
}
};
}));
/**
* Compile a template synchronously
* @param {String} template The string to compile
* @return {Object} A reference to the compiled template
*/
function compile(template) {
var element = $compile(template)(scope);
scope.$apply();
$timeout.flush();
return element;
}
describe('with a <select> element', function () {
describe('compiling this directive', function () {
it('should throw an error if we have no model defined', function () {
expect(function(){
compile('<select type="text" ui-select2></select>');
}).toThrow();
});
it('should create proper DOM structure', function () {
var element = compile('<select ui-select2 ng-model="foo"></select>');
expect(element.siblings().is('div.select2-container')).toBe(true);
});
it('should not modify the model if there is no initial value', function(){
//TODO
});
});
describe('when model is changed programmatically', function(){
describe('for single select', function(){
it('should set select2 to the value', function(){
scope.foo = 'First';
var element = compile('<select ui-select2 ng-model="foo"><option>First</option><option>Second</option></select>');
expect(element.select2('val')).toBe('First');
scope.$apply('foo = "Second"');
expect(element.select2('val')).toBe('Second');
});
it('should handle falsey values', function(){
scope.foo = 'First';
var element = compile('<select ui-select2="{allowClear:true}" ng-model="foo"><option>First</option><option>Second</option></select>');
expect(element.select2('val')).toBe('First');
scope.$apply('foo = false');
expect(element.select2('val')).toBe(null);
scope.$apply('foo = "Second"');
scope.$apply('foo = null');
expect(element.select2('val')).toBe(null);
scope.$apply('foo = "Second"');
scope.$apply('foo = undefined');
expect(element.select2('val')).toBe(null);
});
});
describe('for multiple select', function(){
it('should set select2 to multiple value', function(){
scope.foo = ['First'];
var element = compile('<select ui-select2="{allowClear:true}" multiple ng-model="foo"><option>First</option><option>Second</option><option>Third</option></select>');
expect(element.select2('val')).toEqual(['First']);
scope.$apply('foo = ["Second"]');
expect(element.select2('val')).toEqual(['Second']);
scope.$apply('foo = ["Second","Third"]');
expect(element.select2('val')).toEqual(['Second','Third']);
});
it('should handle falsey values', function(){
scope.foo = ['First'];
var element = compile('<select ui-select2="{allowClear:true}" multiple ng-model="foo"><option>First</option><option>Second</option><option>Third</option></select>');
expect(element.val()).toEqual(['First']);
scope.$apply('foo = ["Second"]');
scope.$apply('foo = false');
expect(element.select2('val')).toEqual([]);
scope.$apply('foo = ["Second"]');
scope.$apply('foo = null');
expect(element.select2('val')).toEqual([]);
scope.$apply('foo = ["Second"]');
scope.$apply('foo = undefined');
expect(element.select2('val')).toEqual([]);
});
});
});
it('should observe the disabled attribute', function () {
var element = compile('<select ui-select2 ng-model="foo" ng-disabled="disabled"></select>');
expect(element.siblings().hasClass('select2-container-disabled')).toBe(false);
scope.$apply('disabled = true');
expect(element.siblings().hasClass('select2-container-disabled')).toBe(true);
scope.$apply('disabled = false');
expect(element.siblings().hasClass('select2-container-disabled')).toBe(false);
});
it('should observe the multiple attribute', function () {
var element = $compile('<select ui-select2 ng-model="foo" ng-multiple="multiple"></select>')(scope);
expect(element.siblings().hasClass('select2-container-multi')).toBe(false);
scope.$apply('multiple = true');
expect(element.siblings().hasClass('select2-container-multi')).toBe(true);
scope.$apply('multiple = false');
expect(element.siblings().hasClass('select2-container-multi')).toBe(false);
});
it('should observe an option with ng-repeat for changes', function(){
scope.items = ['first', 'second', 'third'];
scope.foo = 'fourth';
var element = compile('<select ui-select2 ng-model="foo"><option ng-repeat="item in items">{{item}}</option></select>');
expect(element.select2('val')).toBe(null);
scope.$apply('foo="fourth";items=["fourth"]');
$timeout.flush();
expect(element.select2('val')).toBe('fourth');
});
});
describe('with an <input> element', function () {
describe('compiling this directive', function () {
it('should throw an error if we have no model defined', function () {
expect(function() {
compile('<input ui-select2/>');
}).toThrow();
});
it('should create proper DOM structure', function () {
var element = compile('<input ui-select2="options" ng-model="foo"/>');
expect(element.siblings().is('div.select2-container')).toBe(true);
});
it('should not modify the model if there is no initial value', function(){
//TODO
});
});
describe('when model is changed programmatically', function(){
describe('for single-select', function(){
it('should call select2(data, ...) for objects', function(){
var element = compile('<input ng-model="foo" ui-select2="options">');
spyOn($.fn, 'select2');
scope.$apply('foo={ id: 1, text: "first" }');
expect(element.select2).toHaveBeenCalledWith('data', { id: 1, text: "first" });
});
it('should call select2(val, ...) for strings', function(){
var element = compile('<input ng-model="foo" ui-select2="options">');
spyOn($.fn, 'select2');
scope.$apply('foo="first"');
expect(element.select2).toHaveBeenCalledWith('val', 'first');
});
});
describe('for multi-select', function(){
it('should call select2(data, ...) for arrays', function(){
var element = compile('<input ng-model="foo" multiple ui-select2="options">');
spyOn($.fn, 'select2');
scope.$apply('foo=[{ id: 1, text: "first" },{ id: 2, text: "second" }]');
expect(element.select2).toHaveBeenCalledWith('data', [{ id: 1, text: "first" },{ id: 2, text: "second" }]);
});
it('should call select2(data, []) for falsey values', function(){
var element = compile('<input ng-model="foo" multiple ui-select2="options">');
spyOn($.fn, 'select2');
scope.$apply('foo=[]');
expect(element.select2).toHaveBeenCalledWith('data', []);
});
xit('should call select2(val, ...) for strings', function(){
var element = compile('<input ng-model="foo" multiple ui-select2="options">');
spyOn($.fn, 'select2');
scope.$apply('foo="first,second"');
expect(element.select2).toHaveBeenCalledWith('val', 'first,second');
});
});
});
describe('consumers of ngModel should correctly use $viewValue', function() {
it('should use any formatters if present (select - single select)', function(){
scope.foo = 'First';
var element = compile('<select ui-select2 ng-model="foo" inject-transformers="transformers"><option>First - I\'ve been formatted</option><option>Second - I\'ve been formatted</option></select>');
expect(element.select2('val')).toBe('First - I\'ve been formatted');
scope.$apply('foo = "Second"');
expect(element.select2('val')).toBe('Second - I\'ve been formatted');
});
// isMultiple && falsey
it('should use any formatters if present (input multi select - falsey value)', function() {
// need special function to hit this case
// old code checked modelValue... can't just pass undefined to model value because view value will be the same
scope.transformers.fromModel = function(modelValue) {
if (modelValue === "magic") {
return undefined;
}
return modelValue;
};
var element = compile('<input ng-model="foo" multiple ui-select2="options" inject-transformers="transformers">');
spyOn($.fn, 'select2');
scope.$apply('foo="magic"');
expect(element.select2).toHaveBeenCalledWith('data', []);
});
// isMultiple && isArray
it('should use any formatters if present (input multi select)', function() {
var element = compile('<input ng-model="foo" multiple ui-select2="options" inject-transformers="transformers">');
spyOn($.fn, 'select2');
scope.$apply('foo=[{ id: 1, text: "first" },{ id: 2, text: "second" }]');
expect(element.select2).toHaveBeenCalledWith('data', [{ id: 1, text: "first - I've been formatted" },{ id: 2, text: "second - I've been formatted" }]);
});
// isMultiple...
xit('should use any formatters if present (input multi select - non array)', function() {
var element = compile('<input ng-model="foo" multiple ui-select2="options" inject-transformers="transformers">');
spyOn($.fn, 'select2');
scope.$apply('foo={ id: 1, text: "first" }');
expect(element.select2).toHaveBeenCalledWith('val', { id: 1, text: "first - I've been formatted" });
});
// !isMultiple
it('should use any formatters if present (input - single select - object)', function() {
var element = compile('<input ng-model="foo" ui-select2="options" inject-transformers="transformers">');
spyOn($.fn, 'select2');
scope.$apply('foo={ id: 1, text: "first" }');
expect(element.select2).toHaveBeenCalledWith('data', { id: 1, text: "first - I've been formatted" });
});
it('should use any formatters if present (input - single select - non object)', function() {
var element = compile('<input ng-model="foo" ui-select2="options" inject-transformers="transformers">');
spyOn($.fn, 'select2');
scope.$apply('foo="first"');
expect(element.select2).toHaveBeenCalledWith('val', "first - I've been formatted");
});
it('should not set the default value using scope.$eval', function() {
// testing directive instantiation - change order of test
spyOn($.fn, 'select2');
spyOn($.fn, 'val');
scope.$apply('foo=[{ id: 1, text: "first" },{ id: 2, text: "second" }]');
var element = compile('<input ng-model="foo" multiple ui-select2="options" inject-transformers="transformers">');
expect(element.val).not.toHaveBeenCalledWith([{ id: 1, text: "first" },{ id: 2, text: "second" }]);
});
it('should expect a default value to be set with a call to the render method', function() {
// this should monitor the events after init, when the timeout callback executes
var opts = angular.copy(scope.options);
opts.multiple = true;
scope.$apply('foo=[{ id: 1, text: "first" },{ id: 2, text: "second" }]');
spyOn($.fn, 'select2');
var element = compile('<input ng-model="foo" multiple ui-select2="options" inject-transformers="transformers">');
// select 2 init
expect(element.select2).toHaveBeenCalledWith(opts);
// callback setting
expect(element.select2).toHaveBeenCalledWith('data', [{ id: 1, text: "first - I've been formatted" },{ id: 2, text: "second - I've been formatted" }]);
// retieve data
expect(element.select2).toHaveBeenCalledWith('data');
});
});
it('should set the model when the user selects an item', function(){
var element = compile('<input ng-model="foo" multiple ui-select2="options">');
// TODO: programmactically select an option
// expect(scope.foo).toBe(/* selected val */) ;
});
it('updated the view when model changes with complex object', function(){
scope.foo = [{'id': '0', 'text': '0'}];
scope.options['multiple'] = true;
var element = compile('<input ng-model="foo" ui-select2="options">');
scope.$digest();
scope.foo.push({'id': '1', 'text': '1'});
scope.$digest();
expect(element.select2('data')).toEqual(
[{'id': '0', 'text': '0'}, {'id': '1', 'text': '1'}]);
});
describe('simple_tags', function() {
beforeEach(function() {
scope.options['multiple'] = true;
scope.options['simple_tags'] = true;
scope.options['tags'] = [];
});
it('Initialize the select2 view based on list of strings.', function() {
scope.foo = ['tag1', 'tag2'];
var element = compile('<input ng-model="foo" ui-select2="options">');
scope.$digest();
expect(element.select2('data')).toEqual([
{'id': 'tag1', 'text': 'tag1'},
{'id': 'tag2', 'text': 'tag2'}
]);
});
it(
'When list is empty select2 view model is also initialized as empty',
function() {
scope.foo = [];
var element = compile('<input ng-model="foo" ui-select2="options">');
scope.$digest();
expect(element.select2('data')).toEqual([]);
});
it(
'Updating the model with a string will update the select2 view model.',
function() {
scope.foo = [];
var element = compile('<input ng-model="foo" ui-select2="options">');
scope.$digest();
scope.foo.push('tag1');
scope.$digest();
expect(element.select2('data')).toEqual([
{'id': 'tag1', 'text': 'tag1'}
]);
});
it(
'Updating the select2 model will update AngularJS model with a string.',
function() {
scope.foo = [];
var element = compile('<input ng-model="foo" ui-select2="options">');
scope.$digest();
element.select2('data', [
{'id':'tag1', 'text': 'tag1'},
{'id':'tag2', 'text': 'tag2'}
]);
element.trigger('change');
expect(scope.foo).toEqual(['tag1', 'tag2']);
});
});
});
});