1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-06-26 03:57:36 +02:00

add static files

This commit is contained in:
Pierre-antoine Comby
2020-02-25 22:15:46 +01:00
parent 2de5a8030e
commit 70c09da9f8
336 changed files with 82322 additions and 0 deletions

View File

@ -0,0 +1,332 @@
module('Data adapters - Array');
var ArrayData = require('select2/data/array');
var $ = require('jquery');
var Options = require('select2/options');
var Utils = require('select2/utils');
var UserDefinedType = function (id, text) {
var self = this;
self.id = id;
self.text = text;
return self;
};
var arrayOptions = new Options({
data: [
{
id: 'default',
text: 'Default'
},
{
id: '1',
text: 'One'
},
{
id: '2',
text: '2'
},
new UserDefinedType(1, 'aaaaaa')
]
});
var extraOptions = new Options ({
data: [
{
id: 'default',
text: 'Default',
extra: true
},
{
id: 'One',
text: 'One',
extra: true
}
]
});
var nestedOptions = new Options({
data: [
{
text: 'Default',
children: [
{
text: 'Next',
children: [
{
id: 'a',
text: 'Option'
}
]
}
]
}
]
});
test('current gets default for single', function (assert) {
var $select = $('#qunit-fixture .single-empty');
var data = new ArrayData($select, arrayOptions);
data.current(function (val) {
assert.equal(
val.length,
1,
'There should always be a selected item for array data.'
);
var item = val[0];
assert.equal(
item.id,
'default',
'The first item should be selected'
);
});
});
test('current gets default for multiple', function (assert) {
var $select = $('#qunit-fixture .multiple');
var data = new ArrayData($select, arrayOptions);
data.current(function (val) {
assert.equal(
val.length,
0,
'There should be no default selection.'
);
});
});
test('current works with existing selections', function (assert) {
var $select = $('#qunit-fixture .multiple');
var data = new ArrayData($select, arrayOptions);
$select.val(['One']);
data.current(function (val) {
assert.equal(
val.length,
1,
'There should only be one existing selection.'
);
var option = val[0];
assert.equal(
option.id,
'One',
'The id should be equal to the value of the option tag.'
);
assert.equal(
option.text,
'One',
'The text should be equal to the text of the option tag.'
);
});
});
test('current works with selected data', function (assert) {
var $select = $('#qunit-fixture .single-empty');
var data = new ArrayData($select, arrayOptions);
data.select({
id: '2',
text: '2'
});
data.current(function (val) {
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.'
);
});
});
test('select works for single', function (assert) {
var $select = $('#qunit-fixture .single-empty');
var data = new ArrayData($select, arrayOptions);
assert.equal(
$select.val(),
'default',
'There should already be a selection'
);
data.select({
id: '1',
text: 'One'
});
assert.equal(
$select.val(),
'1',
'The selected value should be the same as the selected id'
);
});
test('multiple sets the value', function (assert) {
var $select = $('#qunit-fixture .multiple');
var data = new ArrayData($select, arrayOptions);
assert.ok(
$select.val() == null || $select.val().length == 0,
'nothing should be selected'
);
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, arrayOptions);
$select.val(['One']);
assert.deepEqual($select.val(), ['One']);
data.select({
id: 'default',
text: 'Default'
});
assert.deepEqual($select.val(), ['One', 'default']);
});
test('option tags are automatically generated', function (assert) {
var $select = $('#qunit-fixture .single-empty');
var data = new ArrayData($select, arrayOptions);
assert.equal(
$select.find('option').length,
4,
'An <option> element should be created for each object'
);
});
test('option tags can receive new data', function(assert) {
var $select = $('#qunit-fixture .single');
var data = new ArrayData($select, extraOptions);
assert.equal(
$select.find('option').length,
2,
'Only one more <option> element should be created'
);
data.select({
id: 'default'
});
assert.ok(
Utils.GetData($select.find(':selected')[0], 'data').extra,
'<option> default should have new data'
);
data.select({
id: 'One'
});
assert.ok(
Utils.GetData($select.find(':selected')[0], 'data').extra,
'<option> One should have new data'
);
});
test('optgroup tags can also be generated', function (assert) {
var $select = $('#qunit-fixture .single-empty');
var data = new ArrayData($select, nestedOptions);
assert.equal(
$select.find('option').length,
1,
'An <option> element should be created for the one selectable object'
);
assert.equal(
$select.find('optgroup').length,
2,
'An <optgroup> element should be created for the two with children'
);
});
test('optgroup tags have the right properties', function (assert) {
var $select = $('#qunit-fixture .single-empty');
var data = new ArrayData($select, nestedOptions);
var $group = $select.children('optgroup');
assert.equal(
$group.prop('label'),
'Default',
'An `<optgroup>` label should match the text property'
);
assert.equal(
$group.children().length,
1,
'The <optgroup> should have one child under it'
);
});
test('existing selections are respected on initialization', function (assert) {
var $select = $(
'<select>' +
'<option>First</option>' +
'<option selected>Second</option>' +
'</select>'
);
var options = new Options({
data: [
{
id: 'Second',
text: 'Second'
},
{
id: 'Third',
text: 'Third'
}
]
});
assert.equal($select.val(), 'Second');
var data = new ArrayData($select, options);
assert.equal($select.val(), 'Second');
});

View File

@ -0,0 +1,29 @@
module('Data adapters - Base');
var BaseData = require('select2/data/base');
var $ = require('jquery');
var Options = require('select2/options');
var options = new Options({});
test('current is required', function (assert) {
var data = new BaseData($('#qunit-fixture select'), options);
assert.throws(
function () {
data.current(function () {});
},
'current has no default implementation'
);
});
test('query is required', function (assert) {
var data = new BaseData($('#qunit-fixture select'), options);
assert.throws(
function () {
data.query({}, function () {});
},
'query has no default implementation'
);
});

View File

@ -0,0 +1,158 @@
module('Data adapters - <input> compatibility');
var $ = require('jquery');
var Options = require('select2/options');
var Utils = require('select2/utils');
var ArrayData = require('select2/data/array');
var InputData = require('select2/compat/inputData');
var InputAdapter = Utils.Decorate(ArrayData, InputData);
test('test that options can be selected', function (assert) {
var options = new Options({
data: [
{
id: 'test',
text: 'Test'
}
]
});
var $element = $('<input />');
var adapter = new InputAdapter($element, options);
adapter.select({
id: 'test'
});
assert.equal(
$element.val(),
'test',
'The id of the item should be the value'
);
});
test('unselect the single selected option clears the value', function (assert) {
var options = new Options({
data: [
{
id: 'test',
text: 'Test',
selected: true
}
]
});
var $element = $('<input />');
var adapter = new InputAdapter($element, options);
adapter.unselect({
id: 'test'
});
assert.equal(
$element.val(),
'',
'The id should no longer be in the value'
);
});
test('options can be unselected individually', function (assert) {
var options = new Options({
data: [
{
id: 'test',
text: 'Test'
},
{
id: 'test2',
text: 'Test2'
},
{
id: 'test3',
text: 'Test3'
}
]
});
var $element = $('<input />');
$element.val('test,test2,test3');
var adapter = new InputAdapter($element, options);
adapter.unselect({
id: 'test2'
});
assert.equal(
$element.val(),
'test,test3',
'The value should contain all the still selected options'
);
});
test('default values can be set', function (assert) {
assert.expect(4);
var options = new Options({
data: [
{
id: 'test',
text: 'Test'
}
]
});
var $element = $('<input value="test" />');
var adapter = new InputAdapter($element, options);
adapter.current(function (data) {
assert.equal(
data.length,
1,
'There should only be a single selected option'
);
var item = data[0];
assert.equal(item.id, 'test');
assert.equal(item.text, 'Test');
});
assert.equal(
$element.val(),
'test',
'The value should not have been altered'
);
});
test('no default value', function (assert) {
assert.expect(2);
var options = new Options({
data: [
{
id: 'test',
text: 'Test'
}
]
});
var $element = $('<input />');
var adapter = new InputAdapter($element, options);
adapter.current(function (data) {
assert.equal(
data.length,
0,
'There should be no selected options'
);
});
assert.equal(
$element.val(),
'',
'The value should not have been altered'
);
});

View File

@ -0,0 +1,138 @@
module('Data adapters - Maximum input length');
var MaximumInputLength = require('select2/data/maximumInputLength');
var $ = require('jquery');
var Options = require('select2/options');
var Utils = require('select2/utils');
function MaximumInputStub () {
this.called = false;
}
MaximumInputStub.prototype.query = function (params, callback) {
this.called = true;
};
var MaximumInputData = Utils.Decorate(MaximumInputStub, MaximumInputLength);
test('0 never displays the notice', function (assert) {
var zeroOptions = new Options({
maximumInputLength: 0
});
var data = new MaximumInputData(null, zeroOptions);
data.trigger = function () {
assert.ok(false, 'No events should be triggered');
};
data.query({
term: ''
});
assert.ok(data.called);
data = new MaximumInputData(null, zeroOptions);
data.query({
term: 'test'
});
assert.ok(data.called);
});
test('< 0 never displays the notice', function (assert) {
var negativeOptions = new Options({
maximumInputLength: -1
});
var data = new MaximumInputData(null, negativeOptions);
data.trigger = function () {
assert.ok(false, 'No events should be triggered');
};
data.query({
term: ''
});
assert.ok(data.called);
data = new MaximumInputData(null, negativeOptions);
data.query({
term: 'test'
});
assert.ok(data.called);
});
test('triggers when input is too long', function (assert) {
var options = new Options({
maximumInputLength: 1
});
var data = new MaximumInputData(null, options);
data.trigger = function () {
assert.ok(true, 'The event should be triggered.');
};
data.query({
term: 'no'
});
assert.ok(!data.called, 'The adapter should not be called');
});
test('does not trigger when equal', function (assert) {
var options = new Options({
maximumInputLength: 10
});
var data = new MaximumInputData(null, options);
data.trigger = function () {
assert.ok(false, 'The event should not be triggered.');
};
data.query({
term: '1234567890'
});
assert.ok(data.called);
});
test('does not trigger when less', function (assert) {
var options = new Options({
maximumInputLength: 10
});
var data = new MaximumInputData(null, options);
data.trigger = function () {
assert.ok(false, 'The event should not be triggered.');
};
data.query({
term: '123'
});
assert.ok(data.called);
});
test('works with null term', function (assert) {
var options = new Options({
maximumInputLength: 1
});
var data = new MaximumInputData(null, options);
data.trigger = function () {
assert.ok(false, 'The event should not be triggered');
};
data.query({});
assert.ok(data.called);
});

View File

@ -0,0 +1,202 @@
module('Data adapters - Maximum selection length');
var MaximumSelectionLength = require('select2/data/maximumSelectionLength');
var $ = require('jquery');
var Options = require('select2/options');
var Utils = require('select2/utils');
function MaximumSelectionStub () {
this.called = false;
this.currentData = [];
}
MaximumSelectionStub.prototype.current = function (callback) {
callback(this.currentData);
};
MaximumSelectionStub.prototype.val = function (val) {
this.currentData.push(val);
};
MaximumSelectionStub.prototype.query = function (params, callback) {
this.called = true;
};
var MaximumSelectionData = Utils.Decorate(
MaximumSelectionStub,
MaximumSelectionLength
);
test('0 never displays the notice', function (assert) {
var zeroOptions = new Options({
maximumSelectionLength: 0
});
var data = new MaximumSelectionData(null, zeroOptions);
data.trigger = function () {
assert.ok(false, 'No events should be triggered');
};
data.query({
term: ''
});
assert.ok(data.called);
data = new MaximumSelectionData(null, zeroOptions);
data.trigger = function () {
assert.ok(false, 'No events should be triggered');
};
data.val('1');
data.query({
term: ''
});
assert.ok(data.called);
data = new MaximumSelectionData(null, zeroOptions);
data.trigger = function () {
assert.ok(false, 'No events should be triggered');
};
data.val('1');
data.val('2');
data.query({
term: ''
});
assert.ok(data.called);
});
test('< 0 never displays the notice', function (assert) {
var negativeOptions = new Options({
maximumSelectionLength: -1
});
var data = new MaximumSelectionData(null, negativeOptions);
data.trigger = function () {
assert.ok(false, 'No events should be triggered');
};
data.query({
term: ''
});
assert.ok(data.called);
data = new MaximumSelectionData(null, negativeOptions);
data.trigger = function () {
assert.ok(false, 'No events should be triggered');
};
data.val('1');
data.query({
term: ''
});
assert.ok(data.called);
data = new MaximumSelectionData(null, negativeOptions);
data.trigger = function () {
assert.ok(false, 'No events should be triggered');
};
data.val('1');
data.val('2');
data.query({
term: ''
});
assert.ok(data.called);
});
test('triggers when >= 1 selection' , function (assert) {
var maxOfOneOptions = new Options({
maximumSelectionLength: 1
});
var data = new MaximumSelectionData(null, maxOfOneOptions);
data.trigger = function () {
assert.ok(false, 'No events should be triggered');
};
data.query({
term: ''
});
assert.ok(data.called);
data = new MaximumSelectionData(null, maxOfOneOptions);
data.trigger = function () {
assert.ok(true, 'The event should be triggered.');
};
data.val('1');
data.query({
term: ''
});
assert.ok(!data.called);
});
test('triggers when >= 2 selections' , function (assert) {
var maxOfTwoOptions = new Options({
maximumSelectionLength: 2
});
var data = new MaximumSelectionData(null, maxOfTwoOptions);
data.trigger = function () {
assert.ok(false, 'No events should be triggered');
};
data.query({
term: ''
});
assert.ok(data.called);
data = new MaximumSelectionData(null, maxOfTwoOptions);
data.trigger = function () {
assert.ok(false, 'No events should be triggered');
};
data.val('1');
data.query({
term: ''
});
assert.ok(data.called);
data = new MaximumSelectionData(null, maxOfTwoOptions);
data.trigger = function () {
assert.ok(true, 'The event should be triggered.');
};
data.val('1');
data.val('2');
data.query({
term: ''
});
assert.ok(!data.called);
});

View File

@ -0,0 +1,138 @@
module('Data adapters - Minimum input length');
var MinimumInputLength = require('select2/data/minimumInputLength');
var $ = require('jquery');
var Options = require('select2/options');
var Utils = require('select2/utils');
function StubData () {
this.called = false;
}
StubData.prototype.query = function (params, callback) {
this.called = true;
};
var MinimumData = Utils.Decorate(StubData, MinimumInputLength);
test('0 never displays the notice', function (assert) {
var zeroOptions = new Options({
minimumInputLength: 0
});
var data = new MinimumData(null, zeroOptions);
data.trigger = function () {
assert.ok(false, 'No events should be triggered');
};
data.query({
term: ''
});
assert.ok(data.called);
data = new MinimumData(null, zeroOptions);
data.query({
term: 'test'
});
assert.ok(data.called);
});
test('< 0 never displays the notice', function (assert) {
var negativeOptions = new Options({
minimumInputLength: -1
});
var data = new MinimumData(null, negativeOptions);
data.trigger = function () {
assert.ok(false, 'No events should be triggered');
};
data.query({
term: ''
});
assert.ok(data.called);
data = new MinimumData(null, negativeOptions);
data.query({
term: 'test'
});
assert.ok(data.called);
});
test('triggers when input is not long enough', function (assert) {
var options = new Options({
minimumInputLength: 10
});
var data = new MinimumData(null, options);
data.trigger = function () {
assert.ok(true, 'The event should be triggered.');
};
data.query({
term: 'no'
});
assert.ok(!data.called);
});
test('does not trigger when equal', function (assert) {
var options = new Options({
minimumInputLength: 10
});
var data = new MinimumData(null, options);
data.trigger = function () {
assert.ok(false, 'The event should not be triggered.');
};
data.query({
term: '1234567890'
});
assert.ok(data.called);
});
test('does not trigger when greater', function (assert) {
var options = new Options({
minimumInputLength: 10
});
var data = new MinimumData(null, options);
data.trigger = function () {
assert.ok(false, 'The event should not be triggered.');
};
data.query({
term: '12345678901'
});
assert.ok(data.called);
});
test('works with null term', function (assert) {
var options = new Options({
minimumInputLength: 1
});
var data = new MinimumData(null, options);
data.trigger = function () {
assert.ok(true, 'The event should be triggered');
};
data.query({});
assert.ok(!data.called);
});

View File

@ -0,0 +1,557 @@
module('Data adapters - Select - current');
var SelectData = require('select2/data/select');
var $ = require('jquery');
var Options = require('select2/options');
var selectOptions = new Options({});
test('current gets default for single', function (assert) {
var $select = $('#qunit-fixture .single');
var data = new SelectData($select, selectOptions);
data.current(function (data) {
assert.equal(
data.length,
1,
'There should only be one selected option'
);
var option = data[0];
assert.equal(
option.id,
'One',
'The value of the option tag should be the id'
);
assert.equal(
option.text,
'One',
'The text within the option tag should be the text'
);
});
});
test('current gets default for multiple', function (assert) {
var $select = $('#qunit-fixture .multiple');
var data = new SelectData($select, selectOptions);
data.current(function (data) {
assert.equal(
data.length,
0,
'Multiple selects have no default selection.'
);
});
});
test('current gets options with explicit value', function (assert) {
var $select = $('#qunit-fixture .single');
var $option = $('<option value="1">One</option>');
$select.append($option);
var data = new SelectData($select, selectOptions);
$select.val('1');
data.current(function (data) {
assert.equal(
data.length,
1,
'There should be one selected option'
);
var option = data[0];
assert.equal(
option.id,
'1',
'The option value should be the selected id'
);
assert.equal(
option.text,
'One',
'The text should match the text for the option tag'
);
});
});
test('current gets options with implicit value', function (assert) {
var $select = $('#qunit-fixture .single');
var data = new SelectData($select, selectOptions);
$select.val('One');
data.current(function (val) {
assert.equal(
val.length,
1,
'There should only be one selected value'
);
var option = val[0];
assert.equal(
option.id,
'One',
'The id should be the same as the option text'
);
assert.equal(
option.text,
'One',
'The text should be the same as the option text'
);
});
});
test('select works for single', function (assert) {
var $select = $('#qunit-fixture .single-with-placeholder');
var data = new SelectData($select, selectOptions);
assert.equal($select.val(), 'placeholder');
data.select({
id: 'One',
text: 'One'
});
assert.equal($select.val(), 'One');
});
test('multiple sets the value', function (assert) {
var $select = $('#qunit-fixture .multiple');
var data = new SelectData($select, selectOptions);
assert.ok(
$select.val() == null || $select.val().length == 0,
'nothing should be selected'
);
data.select({
id: 'Two',
text: 'Two'
});
assert.deepEqual($select.val(), ['Two']);
});
test('multiple adds to the old value', function (assert) {
var $select = $('#qunit-fixture .multiple');
var data = new SelectData($select, selectOptions);
$select.val(['Two']);
assert.deepEqual($select.val(), ['Two']);
data.select({
id: 'One',
text: 'One'
});
assert.deepEqual($select.val(), ['One', 'Two']);
});
test('duplicates - single - same id on select triggers change',
function (assert) {
var $select = $('#qunit-fixture .duplicates');
var data = new SelectData($select, data);
var second = $('#qunit-fixture .duplicates option')[2];
var changeTriggered = false;
assert.equal($select.val(), 'one');
$select.on('change', function () {
changeTriggered = true;
});
data.select({
id: 'one',
text: 'Uno',
element: second
});
assert.equal(
$select.val(),
'one',
'The value never changed'
);
assert.ok(
changeTriggered,
'The change event should be triggered'
);
assert.ok(
second.selected,
'The second duplicate is selected, not the first'
);
});
test('duplicates - single - different id on select triggers change',
function (assert) {
var $select = $('#qunit-fixture .duplicates');
var data = new SelectData($select, data);
var second = $('#qunit-fixture .duplicates option')[2];
var changeTriggered = false;
$select.val('two');
$select.on('change', function () {
changeTriggered = true;
});
data.select({
id: 'one',
text: 'Uno',
element: second
});
assert.equal(
$select.val(),
'one',
'The value changed to the duplicate id'
);
assert.ok(
changeTriggered,
'The change event should be triggered'
);
assert.ok(
second.selected,
'The second duplicate is selected, not the first'
);
});
test('duplicates - multiple - same id on select triggers change',
function (assert) {
var $select = $('#qunit-fixture .duplicates-multi');
var data = new SelectData($select, data);
var second = $('#qunit-fixture .duplicates-multi option')[2];
var changeTriggered = false;
$select.val(['one']);
$select.on('change', function () {
changeTriggered = true;
});
data.select({
id: 'one',
text: 'Uno',
element: second
});
assert.deepEqual(
$select.val(),
['one', 'one'],
'The value now has duplicates'
);
assert.ok(
changeTriggered,
'The change event should be triggered'
);
assert.ok(
second.selected,
'The second duplicate is selected, not the first'
);
});
test('duplicates - multiple - different id on select triggers change',
function (assert) {
var $select = $('#qunit-fixture .duplicates-multi');
var data = new SelectData($select, data);
var second = $('#qunit-fixture .duplicates-multi option')[2];
var changeTriggered = false;
$select.val(['two']);
$select.on('change', function () {
changeTriggered = true;
});
data.select({
id: 'one',
text: 'Uno',
element: second
});
assert.deepEqual(
$select.val(),
['two', 'one'],
'The value has the new id'
);
assert.ok(
changeTriggered,
'The change event should be triggered'
);
assert.ok(
second.selected,
'The second duplicate is selected, not the first'
);
});
module('Data adapter - Select - query');
test('all options are returned with no term', function (assert) {
var $select = $('#qunit-fixture .single');
var data = new SelectData($select, selectOptions);
data.query({}, function (data) {
assert.equal(
data.results.length,
1,
'The number of items returned should be equal to the number of options'
);
});
});
test('the matcher checks the text', function (assert) {
var $select = $('#qunit-fixture .single');
var data = new SelectData($select, selectOptions);
data.query({
term: 'One'
}, function (data) {
assert.equal(
data.results.length,
1,
'Only the "One" option should be found'
);
});
});
test('the matcher ignores case', function (assert) {
var $select = $('#qunit-fixture .single');
var data = new SelectData($select, selectOptions);
data.query({
term: 'one'
}, function (data) {
assert.equal(
data.results.length,
1,
'The "One" option should still be found'
);
});
});
test('no options may be returned with no matches', function (assert) {
var $select = $('#qunit-fixture .single');
var data = new SelectData($select, selectOptions);
data.query({
term: 'qwerty'
}, function (data) {
assert.equal(
data.results.length,
0,
'Only matching items should be returned'
);
});
});
test('optgroup tags are marked with children', function (assert) {
var $select = $('#qunit-fixture .groups');
var data = new SelectData($select, selectOptions);
data.query({}, function (data) {
assert.ok(
'children' in data.results[0],
'The optgroup element should have children when queried'
);
});
});
test('empty optgroups are still shown when queried', function (assert) {
var $select = $('#qunit-fixture .groups');
var data = new SelectData($select, selectOptions);
data.query({}, function (data) {
assert.equal(
data.results.length,
2,
'The empty optgroup element should still be returned when queried'
);
var item = data.results[1];
assert.equal(
item.text,
'Empty',
'The text of the empty optgroup should match the label'
);
assert.equal(
item.children.length,
0,
'There should be no children in the empty opgroup'
);
});
});
test('multiple options with the same value are returned', function (assert) {
var $select = $('#qunit-fixture .duplicates');
var data = new SelectData($select, selectOptions);
data.query({}, function (data) {
assert.equal(
data.results.length,
3,
'The duplicate option should still be returned when queried'
);
var first = data.results[0];
var duplicate = data.results[2];
assert.equal(
first.id,
duplicate.id,
'The duplicates should have the same id'
);
assert.notEqual(
first.text,
duplicate.text,
'The duplicates do not have the same text'
);
});
});
test('data objects use the text of the option', function (assert) {
var $select = $('#qunit-fixture .duplicates');
var data = new SelectData($select, selectOptions);
var $option = $('<option>&amp;</option>');
var item = data.item($option);
assert.equal(item.id, '&');
assert.equal(item.text, '&');
});
test('select option construction accepts id=0 (zero) value', function (assert) {
var $select = $('#qunit-fixture .single');
var selectOptions = [{ id: 0, text: 'Zero Value'}];
var data = new SelectData($select, selectOptions);
var optionElem = data.option(selectOptions[0]);
// If was "Zero Value"", then it ignored id property
assert.equal(
optionElem[0].value,
'0',
'Built option value should be "0" (zero as a string).'
);
});
test('select option construction accepts id="" (empty string) value',
function (assert) {
var $select = $('#qunit-fixture .single');
var selectOptions = [{ id: '', text: 'Empty String'}];
var data = new SelectData($select, selectOptions);
var optionElem = data.option(selectOptions[0]);
assert.equal(
optionElem[0].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'
);
});

View File

@ -0,0 +1,276 @@
module('Data adapters - Tags');
var SelectData = require('select2/data/select');
var Tags = require('select2/data/tags');
var $ = require('jquery');
var Options = require('select2/options');
var Utils = require('select2/utils');
var SelectTags = Utils.Decorate(SelectData, Tags);
var options = new Options({
tags: true
});
test('does not trigger on blank or null terms', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
term: ''
}, function (data) {
assert.equal(data.results.length, 1);
var item = data.results[0];
assert.equal(item.id, 'One');
assert.equal(item.text, 'One');
});
data.query({
term: null
}, function (data) {
assert.equal(data.results.length, 1);
var item = data.results[0];
assert.equal(item.id, 'One');
assert.equal(item.text, 'One');
});
});
test('white space is trimmed by default', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
term: ' '
}, function (data) {
assert.equal(data.results.length, 1);
var item = data.results[0];
assert.equal(item.id, 'One');
assert.equal(item.text, 'One');
});
data.query({
term: ' One '
}, function (data) {
assert.equal(data.results.length, 1);
var item = data.results[0];
assert.equal(item.id, 'One');
assert.equal(item.text, 'One');
});
});
test('does not create option if text is same but lowercase', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
term: 'one'
}, function (data) {
assert.equal(data.results.length, 1);
var item = data.results[0];
assert.equal(item.id, 'One');
assert.equal(item.text, 'One');
});
});
test('does not trigger for additional pages', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
page: 2
}, function (data) {
assert.equal(data.results.length, 1);
var item = data.results[0];
assert.equal(item.id, 'One');
assert.equal(item.text, 'One');
});
});
test('creates tag at beginning', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
term: 'o'
}, function (data) {
assert.equal(data.results.length, 2);
var first = data.results[0];
assert.equal(first.id, 'o');
assert.equal(first.text, 'o');
});
});
test('tags can be the only result', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
term: 'test'
}, function (data) {
assert.equal(data.results.length, 1);
var item = data.results[0];
assert.equal(item.id, 'test');
assert.equal(item.text, 'test');
});
});
test('tags are injected as options', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
term: 'test'
}, function (data) {
assert.equal(data.results.length, 1);
var $children = $('#qunit-fixture .single option');
assert.equal($children.length, 2);
var $tag = $children.last();
assert.equal($tag.val(), 'test');
assert.equal($tag.text(), 'test');
});
});
test('old tags are removed automatically', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
term: 'first'
}, function (data) {
assert.equal(data.results.length, 1);
var $children = $('#qunit-fixture .single option');
assert.equal($children.length, 2);
});
data.query({
term: 'second'
}, function (data) {
assert.equal(data.results.length, 1);
var $children = $('#qunit-fixture .single option');
assert.equal($children.length, 2);
var $tag = $children.last();
assert.equal($tag.val(), 'second');
assert.equal($tag.text(), 'second');
});
});
test('insertTag controls the tag location', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.insertTag = function (data, tag) {
data.push(tag);
};
data.query({
term: 'o'
}, function (data) {
assert.equal(data.results.length, 2);
var item = data.results[1];
assert.equal(item.id, 'o');
assert.equal(item.text, 'o');
});
});
test('insertTag can be controlled through the options', function (assert) {
var options = new Options({
insertTag: function (data, tag) {
data.push(tag);
}
});
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
term: 'o'
}, function (data) {
assert.equal(data.results.length, 2);
var item = data.results[1];
assert.equal(item.id, 'o');
assert.equal(item.text, 'o');
});
});
test('createTag controls the tag object', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.createTag = function (params) {
return {
id: 0,
text: params.term
};
};
data.query({
term: 'test'
}, function (data) {
assert.equal(data.results.length, 1);
var item = data.results[0];
assert.equal(item.id, 0);
assert.equal(item.text, 'test');
});
});
test('createTag returns null for no tag', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.createTag = function (params) {
return null;
};
data.query({
term: 'o'
}, function (data) {
assert.equal(data.results.length, 1);
});
});
test('the createTag options customizes the function', function (assert) {
var data = new SelectTags(
$('#qunit-fixture .single'),
new Options({
tags: true,
createTag: function (params) {
return {
id: params.term,
text: params.term,
tag: true
};
}
})
);
data.query({
term: 'test'
}, function (data) {
assert.equal(data.results.length, 1);
var item = data.results[0];
assert.equal(item.id, 'test');
assert.equal(item.text, 'test');
assert.equal(item.tag, true);
});
});

View File

@ -0,0 +1,219 @@
module('Data adaptor - Tokenizer');
test('triggers the select event', function (assert) {
assert.expect(2);
var SelectData = require('select2/data/select');
var Tokenizer = require('select2/data/tokenizer');
var Tags = require('select2/data/tags');
var Options = require('select2/options');
var Utils = require('select2/utils');
var $ = require('jquery');
var TokenizedSelect = Utils.Decorate(
Utils.Decorate(SelectData, Tags),
Tokenizer
);
var $select = $('#qunit-fixture .single');
var options = new Options({
tags: true,
tokenSeparators: [',']
});
var container = new MockContainer();
container.dropdown = container.selection = {};
var $container = $('<div></div>');
var data = new TokenizedSelect($select, options);
data.bind(container, $container);
data.on('select', function () {
assert.ok(true, 'The select event should be triggered');
});
data.query({
term: 'first,second'
}, function () {
assert.ok(true, 'The callback should have succeeded');
});
});
test('createTag can return null', function (assert) {
assert.expect(3);
var SelectData = require('select2/data/select');
var Tokenizer = require('select2/data/tokenizer');
var Tags = require('select2/data/tags');
var Options = require('select2/options');
var Utils = require('select2/utils');
var $ = require('jquery');
var TokenizedSelect = Utils.Decorate(
Utils.Decorate(SelectData, Tags),
Tokenizer
);
var $select = $('#qunit-fixture .single');
var options = new Options({
tags: true,
tokenSeparators: [','],
createTag: function () {
assert.ok(true, 'createTag should have been called');
return null;
}
});
var container = new MockContainer();
container.dropdown = container.selection = {};
var $container = $('<div></div>');
var data = new TokenizedSelect($select, options);
data.bind(container, $container);
data.on('select', function (params) {
if (params.data == null) {
assert.ok(false, 'Null data should never be selected');
}
});
data.query({
term: 'first,second'
}, function () {
assert.ok(true, 'The callback should have succeeded');
});
});
test('createTag returning null does not cut the term', function (assert) {
assert.expect(4);
var SelectData = require('select2/data/select');
var Tokenizer = require('select2/data/tokenizer');
var Tags = require('select2/data/tags');
var Options = require('select2/options');
var Utils = require('select2/utils');
var $ = require('jquery');
var TokenizedSelect = Utils.Decorate(
Utils.Decorate(SelectData, Tags),
Tokenizer
);
var $select = $('#qunit-fixture .single');
var options = new Options({
tags: true,
tokenSeparators: [',', '"'],
createTag: function (params) {
var term = params.term;
// Ignore blanks
if (term.length === 0) {
return null;
}
// Ignore the leading quote
if (term === '"') {
return null;
}
// If there is a leading quote, check for a second one
if (term[0] === '"' && term[term.length - 1] !== '"') {
return null;
}
var text = term.substr(1, term.length - 2);
return {
id: term,
text: text
};
}
});
var container = new MockContainer();
container.dropdown = container.selection = {};
var $container = $('<div></div>');
var data = new TokenizedSelect($select, options);
data.bind(container, $container);
data.on('select', function (params) {
assert.ok(params.data, 'Data should not be null');
assert.equal(
params.data.id,
'"first, second"',
'The id should have the quotes'
);
assert.equal(
params.data.text,
'first, second',
'The text should not have the quotes'
);
});
data.query({
term: '"first, second",abc'
}, function () {
assert.ok(true, 'The callback should have succeeded');
});
});
test('works with multiple tokens given', function (assert) {
assert.expect(4);
var SelectData = require('select2/data/select');
var Tokenizer = require('select2/data/tokenizer');
var Tags = require('select2/data/tags');
var Options = require('select2/options');
var Utils = require('select2/utils');
var $ = require('jquery');
var TokenizedSelect = Utils.Decorate(
Utils.Decorate(SelectData, Tags),
Tokenizer
);
var $select = $('#qunit-fixture .multiple');
var options = new Options({
tags: true,
tokenSeparators: [',']
});
var container = new MockContainer();
container.dropdown = container.selection = {};
var $container = $('<div></div>');
var data = new TokenizedSelect($select, options);
data.bind(container, $container);
data.on('select', function () {
assert.ok(true, 'The select event should be triggered');
});
data.query({
term: 'first,second,third'
}, function () {
assert.ok(true, 'The callback should have succeeded');
});
assert.equal(
$select.children('option').length,
3,
'The two new tags should have been created'
);
});