Skip to content

Commit b11d6e2

Browse files
committed
Fix for issue select2#4632
1 parent b559310 commit b11d6e2

4 files changed

Lines changed: 80 additions & 3 deletions

File tree

src/js/select2/data/select.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ define([
244244
};
245245

246246
SelectAdapter.prototype._normalizeItem = function (item) {
247-
if (!$.isPlainObject(item)) {
247+
if(item !== Object(item)){
248248
item = {
249249
id: item,
250250
text: item

tests/data/array-tests.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ var ArrayData = require('select2/data/array');
44
var $ = require('jquery');
55
var Options = require('select2/options');
66

7+
var UserDefinedType = function( id, text ){
8+
var self=this;
9+
10+
self.id=id;
11+
self.text=text;
12+
13+
return self;
14+
};
15+
716
var arrayOptions = new Options({
817
data: [
918
{
@@ -17,7 +26,8 @@ var arrayOptions = new Options({
1726
{
1827
id: '2',
1928
text: '2'
20-
}
29+
},
30+
new UserDefinedType(1, 'aaaaaa')
2131
]
2232
});
2333

@@ -216,7 +226,7 @@ test('option tags are automatically generated', function (assert) {
216226

217227
assert.equal(
218228
$select.find('option').length,
219-
3,
229+
4,
220230
'An <option> element should be created for each object'
221231
);
222232
});

tests/data/select-tests.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,3 +487,68 @@ test('select option construction accepts id="" (empty string) value',
487487
'Built option value should be an empty string.'
488488
);
489489
});
490+
491+
test('user-defined types are normalized properly', function (assert) {
492+
var $select = $('#qunit-fixture .user-defined'),
493+
494+
UserDefinedType = function( id, text ){
495+
var self=this;
496+
497+
self.id=id;
498+
self.text=text;
499+
500+
return self;
501+
};
502+
503+
var testData = [
504+
'Test',
505+
{
506+
id: 4,
507+
text: 'item'
508+
},
509+
new UserDefinedType(1, 'aaaaaa')
510+
];
511+
512+
var data = new SelectData($select, selectOptions);
513+
514+
var normalizedItem = data._normalizeItem(testData[0]);
515+
var normalizedItem2 = data._normalizeItem(testData[1]);
516+
var normalizedItem3 = data._normalizeItem(testData[2]);
517+
518+
assert.equal(
519+
testData[0],
520+
normalizedItem.id,
521+
'id property should be equal to text after normalize'
522+
);
523+
524+
assert.equal(
525+
testData[0],
526+
normalizedItem.text,
527+
'text property should be equal after normalize'
528+
);
529+
530+
assert.equal(
531+
testData[1].id,
532+
normalizedItem2.id,
533+
'id property should be equal after normalize'
534+
);
535+
536+
assert.equal(
537+
testData[1].text,
538+
normalizedItem2.text,
539+
'text property should be equal after normalize'
540+
);
541+
542+
assert.equal(
543+
testData[2].id,
544+
normalizedItem3.id,
545+
'id property should be equal after normalize'
546+
);
547+
548+
assert.equal(
549+
testData[2].text,
550+
normalizedItem3.text,
551+
'text property should be equal after normalize'
552+
);
553+
554+
});

tests/unit.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
<option value="two">Two</option>
4747
<option value="one">Uno</option>
4848
</select>
49+
50+
<select class="user-defined"></select>
4951
</div>
5052

5153
<script src="vendor/qunit-1.23.1.js" type="text/javascript"></script>

0 commit comments

Comments
 (0)