Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #4632 #5093

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/js/select2/data/select.js
Expand Up @@ -244,7 +244,7 @@ define([
};

SelectAdapter.prototype._normalizeItem = function (item) {
if (!$.isPlainObject(item)) {
if(item !== Object(item)){
item = {
id: item,
text: item
Expand Down
14 changes: 12 additions & 2 deletions tests/data/array-tests.js
Expand Up @@ -4,6 +4,15 @@ var ArrayData = require('select2/data/array');
var $ = require('jquery');
var Options = require('select2/options');

var UserDefinedType = function( id, text ){
var self=this;

self.id=id;
self.text=text;

return self;
};

var arrayOptions = new Options({
data: [
{
Expand All @@ -17,7 +26,8 @@ var arrayOptions = new Options({
{
id: '2',
text: '2'
}
},
new UserDefinedType(1, 'aaaaaa')
]
});

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

assert.equal(
$select.find('option').length,
3,
4,
'An <option> element should be created for each object'
);
});
Expand Down
65 changes: 65 additions & 0 deletions tests/data/select-tests.js
Expand Up @@ -487,3 +487,68 @@ test('select option construction accepts id="" (empty string) value',
'Built option value should be an empty string.'
);
});

test('user-defined types are normalized properly', function (assert) {
var $select = $('#qunit-fixture .user-defined'),

UserDefinedType = function( id, text ){
var self=this;

self.id=id;
self.text=text;

return self;
};

var testData = [
'Test',
{
id: 4,
text: 'item'
},
new UserDefinedType(1, 'aaaaaa')
];

var data = new SelectData($select, selectOptions);

var normalizedItem = data._normalizeItem(testData[0]);
var normalizedItem2 = data._normalizeItem(testData[1]);
var normalizedItem3 = data._normalizeItem(testData[2]);

assert.equal(
testData[0],
normalizedItem.id,
'id property should be equal to text after normalize'
);

assert.equal(
testData[0],
normalizedItem.text,
'text property should be equal after normalize'
);

assert.equal(
testData[1].id,
normalizedItem2.id,
'id property should be equal after normalize'
);

assert.equal(
testData[1].text,
normalizedItem2.text,
'text property should be equal after normalize'
);

assert.equal(
testData[2].id,
normalizedItem3.id,
'id property should be equal after normalize'
);

assert.equal(
testData[2].text,
normalizedItem3.text,
'text property should be equal after normalize'
);

});
2 changes: 2 additions & 0 deletions tests/unit.html
Expand Up @@ -46,6 +46,8 @@
<option value="two">Two</option>
<option value="one">Uno</option>
</select>

<select class="user-defined"></select>
</div>

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