2014-09-22 01:04:41 +04:00
|
|
|
module('Data adapters - Array');
|
|
|
|
|
|
|
|
var ArrayData = require('select2/data/array');
|
|
|
|
var $ = require('jquery');
|
|
|
|
var Options = require('select2/options');
|
|
|
|
|
|
|
|
var options = new Options({
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
id: 'default',
|
|
|
|
text: 'Default'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '1',
|
|
|
|
text: 'One'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '2',
|
|
|
|
text: '2'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
test('current gets default for single', function (assert) {
|
|
|
|
var $select = $('#qunit-fixture .single');
|
|
|
|
|
|
|
|
var data = new ArrayData($select, options);
|
|
|
|
|
|
|
|
data.current(function (val) {
|
2014-10-20 01:15:32 +04:00
|
|
|
assert.equal(
|
|
|
|
val.length,
|
|
|
|
0,
|
2014-09-22 01:04:41 +04:00
|
|
|
'There should be no default selection.'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('current gets default for multiple', function (assert) {
|
|
|
|
var $select = $('#qunit-fixture .multiple');
|
|
|
|
|
|
|
|
var data = new ArrayData($select, options);
|
|
|
|
|
|
|
|
data.current(function (val) {
|
2014-10-20 01:15:32 +04:00
|
|
|
assert.equal(
|
|
|
|
val.length,
|
|
|
|
0,
|
2014-09-22 01:04:41 +04:00
|
|
|
'There should be no default selection.'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('current works with existing selections', function (assert) {
|
|
|
|
var $select = $('#qunit-fixture .multiple');
|
|
|
|
|
|
|
|
var data = new ArrayData($select, options);
|
|
|
|
|
|
|
|
$select.val(['3']);
|
|
|
|
|
|
|
|
data.current(function (val) {
|
2014-10-20 01:15:32 +04:00
|
|
|
assert.equal(
|
|
|
|
val.length,
|
|
|
|
1,
|
2014-10-20 05:03:48 +04:00
|
|
|
'There should only be one existing selection.'
|
2014-10-20 01:15:32 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
var option = val[0];
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
option.id,
|
|
|
|
'3',
|
2014-10-20 05:03:48 +04:00
|
|
|
'The id should be equal to the value of the option tag.'
|
2014-10-20 01:15:32 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
option.text,
|
|
|
|
'Three',
|
2014-10-20 05:03:48 +04:00
|
|
|
'The text should be equal to the text of the option tag.'
|
2014-09-22 01:04:41 +04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('current works with selected data', function (assert) {
|
|
|
|
var $select = $('#qunit-fixture .single');
|
|
|
|
|
|
|
|
var data = new ArrayData($select, options);
|
|
|
|
|
|
|
|
data.select({
|
|
|
|
id: '2',
|
|
|
|
text: '2'
|
|
|
|
});
|
|
|
|
|
|
|
|
data.current(function (val) {
|
2014-10-20 05:03:48 +04:00
|
|
|
assert.equal(
|
|
|
|
val.length,
|
|
|
|
1,
|
|
|
|
'There should only be one option selected.'
|
|
|
|
);
|
|
|
|
|
|
|
|
var option = val[0];
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
option.id,
|
|
|
|
'2',
|
|
|
|
'The id should match the original id from the array.'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
option.text,
|
|
|
|
'2',
|
|
|
|
'The text should match the original text from the array.'
|
2014-09-22 01:04:41 +04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('select works for single', function (assert) {
|
|
|
|
var $select = $('#qunit-fixture .single');
|
|
|
|
|
|
|
|
var data = new ArrayData($select, options);
|
|
|
|
|
|
|
|
assert.equal($select.val(), null);
|
|
|
|
|
|
|
|
data.select({
|
|
|
|
id: '1',
|
|
|
|
text: 'One'
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal($select.val(), '1');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('multiple sets the value', function (assert) {
|
|
|
|
var $select = $('#qunit-fixture .multiple');
|
|
|
|
|
|
|
|
var data = new ArrayData($select, options);
|
|
|
|
|
|
|
|
assert.equal($select.val(), null);
|
|
|
|
|
|
|
|
data.select({
|
|
|
|
id: 'default',
|
|
|
|
text: 'Default'
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual($select.val(), ['default']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('multiple adds to the old value', function (assert) {
|
|
|
|
var $select = $('#qunit-fixture .multiple');
|
|
|
|
|
|
|
|
var data = new ArrayData($select, options);
|
|
|
|
|
|
|
|
$select.val(['3']);
|
|
|
|
|
|
|
|
assert.deepEqual($select.val(), ['3']);
|
|
|
|
|
|
|
|
data.select({
|
|
|
|
id: 'default',
|
|
|
|
text: 'Default'
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual($select.val(), ['3', 'default']);
|
|
|
|
});
|