1
0
mirror of synced 2024-11-25 06:16:08 +03:00

Fixed jshint errors as a result of prepending QUnit

This commit is contained in:
Kevin Brown 2019-08-21 22:21:52 -04:00
parent 0241a48a8c
commit f9f43c854c
21 changed files with 1031 additions and 891 deletions

View File

@ -259,23 +259,26 @@ QUnit.test('option tags are automatically generated', function (assert) {
); );
}); });
QUnit.test('automatically generated option tags have a result id', function (assert) { QUnit.test(
var $select = $('#qunit-fixture .single-empty'); 'automatically generated option tags have a result id',
function (assert) {
var $select = $('#qunit-fixture .single-empty');
var data = new ArrayData($select, arrayOptions); var data = new ArrayData($select, arrayOptions);
var container = new MockContainer(); var container = new MockContainer();
data.bind(container, $('<div></div>')); data.bind(container, $('<div></div>'));
data.select({ data.select({
id: 'default' id: 'default'
}); });
assert.ok( assert.ok(
Utils.GetData($select.find(':selected')[0], 'data')._resultId, Utils.GetData($select.find(':selected')[0], 'data')._resultId,
'<option> default should have a result ID assigned' '<option> default should have a result ID assigned'
); );
}); }
);
QUnit.test('option tags can receive new data', function(assert) { QUnit.test('option tags can receive new data', function(assert) {
var $select = $('#qunit-fixture .single'); var $select = $('#qunit-fixture .single');
@ -354,9 +357,11 @@ QUnit.test('optgroup tags have the right properties', function (assert) {
); );
}); });
QUnit.test('existing selections are respected on initialization', function (assert) { QUnit.test(
var $select = $( 'existing selections are respected on initialization',
'<select>' + function (assert) {
var $select = $(
'<select>' +
'<option>First</option>' + '<option>First</option>' +
'<option selected>Second</option>' + '<option selected>Second</option>' +
'</select>' '</select>'
@ -383,4 +388,5 @@ QUnit.test('existing selections are respected on initialization', function (asse
data.bind(container, $('<div></div>')); data.bind(container, $('<div></div>'));
assert.equal($select.val(), 'Second'); assert.equal($select.val(), 'Second');
}); }
);

View File

@ -37,33 +37,36 @@ QUnit.test('test that options can be selected', function (assert) {
); );
}); });
QUnit.test('unselect the single selected option clears the value', function (assert) { QUnit.test(
var options = new Options({ 'unselect the single selected option clears the value',
data: [ function (assert) {
{ var options = new Options({
id: 'test', data: [
text: 'Test', {
selected: true id: 'test',
} text: 'Test',
] selected: true
}); }
var $element = $('<input />'); ]
});
var $element = $('<input />');
var adapter = new InputAdapter($element, options); var adapter = new InputAdapter($element, options);
var container = new MockContainer(); var container = new MockContainer();
adapter.bind(container, $('<div></div>')); adapter.bind(container, $('<div></div>'));
adapter.unselect({ adapter.unselect({
id: 'test' id: 'test'
}); });
assert.equal( assert.equal(
$element.val(), $element.val(),
'', '',
'The id should no longer be in the value' 'The id should no longer be in the value'
); );
}); }
);
QUnit.test('options can be unselected individually', function (assert) { QUnit.test('options can be unselected individually', function (assert) {
var options = new Options({ var options = new Options({

View File

@ -417,34 +417,37 @@ QUnit.test('empty optgroups are still shown when queried', function (assert) {
}); });
}); });
QUnit.test('multiple options with the same value are returned', function (assert) { QUnit.test(
var $select = $('#qunit-fixture .duplicates'); 'multiple options with the same value are returned',
function (assert) {
var $select = $('#qunit-fixture .duplicates');
var data = new SelectData($select, selectOptions); var data = new SelectData($select, selectOptions);
data.query({}, function (data) { data.query({}, function (data) {
assert.equal( assert.equal(
data.results.length, data.results.length,
3, 3,
'The duplicate option should still be returned when queried' 'The duplicate option should still be returned when queried'
); );
var first = data.results[0]; var first = data.results[0];
var duplicate = data.results[2]; var duplicate = data.results[2];
assert.equal( assert.equal(
first.id, first.id,
duplicate.id, duplicate.id,
'The duplicates should have the same id' 'The duplicates should have the same id'
); );
assert.notEqual( assert.notEqual(
first.text, first.text,
duplicate.text, duplicate.text,
'The duplicates do not have the same text' 'The duplicates do not have the same text'
); );
}); });
}); }
);
QUnit.test('data objects use the text of the option', function (assert) { QUnit.test('data objects use the text of the option', function (assert) {
var $select = $('#qunit-fixture .duplicates'); var $select = $('#qunit-fixture .duplicates');
@ -459,21 +462,24 @@ QUnit.test('data objects use the text of the option', function (assert) {
assert.equal(item.text, '&'); assert.equal(item.text, '&');
}); });
QUnit.test('select option construction accepts id=0 (zero) value', function (assert) { QUnit.test(
var $select = $('#qunit-fixture .single'); 'select option construction accepts id=0 (zero) value',
function (assert) {
var $select = $('#qunit-fixture .single');
var selectOptions = [{ id: 0, text: 'Zero Value'}]; var selectOptions = [{ id: 0, text: 'Zero Value'}];
var data = new SelectData($select, selectOptions); var data = new SelectData($select, selectOptions);
var optionElem = data.option(selectOptions[0]); var optionElem = data.option(selectOptions[0]);
// If was "Zero Value"", then it ignored id property // If was "Zero Value"", then it ignored id property
assert.equal( assert.equal(
optionElem[0].value, optionElem[0].value,
'0', '0',
'Built option value should be "0" (zero as a string).' 'Built option value should be "0" (zero as a string).'
); );
}); }
);
QUnit.test('select option construction accepts id="" (empty string) value', QUnit.test('select option construction accepts id="" (empty string) value',
function (assert) { function (assert) {

View File

@ -64,20 +64,23 @@ QUnit.test('white space is trimmed by default', function (assert) {
}); });
}); });
QUnit.test('does not create option if text is same but lowercase', function (assert) { QUnit.test(
var data = new SelectTags($('#qunit-fixture .single'), options); 'does not create option if text is same but lowercase',
function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({ data.query({
term: 'one' term: 'one'
}, function (data) { }, function (data) {
assert.equal(data.results.length, 1); assert.equal(data.results.length, 1);
var item = data.results[0]; var item = data.results[0];
assert.equal(item.id, 'One'); assert.equal(item.id, 'One');
assert.equal(item.text, 'One'); assert.equal(item.text, 'One');
}); });
}); }
);
QUnit.test('does not trigger for additional pages', function (assert) { QUnit.test('does not trigger for additional pages', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options); var data = new SelectTags($('#qunit-fixture .single'), options);
@ -191,25 +194,28 @@ QUnit.test('insertTag controls the tag location', function (assert) {
}); });
}); });
QUnit.test('insertTag can be controlled through the options', function (assert) { QUnit.test(
var options = new Options({ 'insertTag can be controlled through the options',
insertTag: function (data, tag) { function (assert) {
data.push(tag); var options = new Options({
} insertTag: function (data, tag) {
}); data.push(tag);
var data = new SelectTags($('#qunit-fixture .single'), options); }
});
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({ data.query({
term: 'o' term: 'o'
}, function (data) { }, function (data) {
assert.equal(data.results.length, 2); assert.equal(data.results.length, 2);
var item = data.results[1]; var item = data.results[1];
assert.equal(item.id, 'o'); assert.equal(item.id, 'o');
assert.equal(item.text, 'o'); assert.equal(item.text, 'o');
}); });
}); }
);
QUnit.test('createTag controls the tag object', function (assert) { QUnit.test('createTag controls the tag object', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options); var data = new SelectTags($('#qunit-fixture .single'), options);

View File

@ -58,120 +58,126 @@ QUnit.test('appends to the dropdown parent', function (assert) {
); );
}); });
QUnit.test('dropdown is positioned down with static margins', function (assert) { QUnit.test(
var $ = require('jquery'); 'dropdown is positioned down with static margins',
var $select = $('<select></select>'); function (assert) {
var $parent = $('<div></div>'); var $ = require('jquery');
$parent.css({ var $select = $('<select></select>');
position: 'static', var $parent = $('<div></div>');
marginTop: '5px', $parent.css({
marginLeft: '10px' position: 'static',
}); marginTop: '5px',
marginLeft: '10px'
});
var $container = $('<span>test</span>'); var $container = $('<span>test</span>');
var container = new MockContainer(); var container = new MockContainer();
$('#qunit-fixture').empty(); $('#qunit-fixture').empty();
$parent.appendTo($('#qunit-fixture')); $parent.appendTo($('#qunit-fixture'));
$container.appendTo($parent); $container.appendTo($parent);
var Utils = require('select2/utils'); var Utils = require('select2/utils');
var Options = require('select2/options'); var Options = require('select2/options');
var Dropdown = require('select2/dropdown'); var Dropdown = require('select2/dropdown');
var AttachBody = require('select2/dropdown/attachBody'); var AttachBody = require('select2/dropdown/attachBody');
var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody); var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody);
var dropdown = new DropdownAdapter($select, new Options({ var dropdown = new DropdownAdapter($select, new Options({
dropdownParent: $parent dropdownParent: $parent
})); }));
var $dropdown = dropdown.render(); var $dropdown = dropdown.render();
assert.equal( assert.equal(
$dropdown[0].style.top, $dropdown[0].style.top,
0, 0,
'The drodpown should not have any offset before it is displayed' 'The drodpown should not have any offset before it is displayed'
); );
dropdown.bind(container, $container); dropdown.bind(container, $container);
dropdown.position($dropdown, $container); dropdown.position($dropdown, $container);
dropdown._showDropdown(); dropdown._showDropdown();
assert.ok( assert.ok(
dropdown.$dropdown.hasClass('select2-dropdown--below'), dropdown.$dropdown.hasClass('select2-dropdown--below'),
'The dropdown should be forced down' 'The dropdown should be forced down'
); );
assert.equal( assert.equal(
$dropdown.css('top').replace(/\D+/, ''), $dropdown.css('top').replace(/\D+/, ''),
$container.outerHeight() + 5, $container.outerHeight() + 5,
'The offset should be 5px at the top' 'The offset should be 5px at the top'
); );
assert.equal( assert.equal(
$dropdown.css('left'), $dropdown.css('left'),
'10px', '10px',
'The offset should be 10px on the left' 'The offset should be 10px on the left'
); );
}); }
);
QUnit.test('dropdown is positioned down with absolute offsets', function (assert) { QUnit.test(
var $ = require('jquery'); 'dropdown is positioned down with absolute offsets',
var $select = $('<select></select>'); function (assert) {
var $parent = $('<div></div>'); var $ = require('jquery');
$parent.css({ var $select = $('<select></select>');
position: 'absolute', var $parent = $('<div></div>');
top: '10px', $parent.css({
left: '5px' position: 'absolute',
}); top: '10px',
left: '5px'
});
var $container = $('<span>test</span>'); var $container = $('<span>test</span>');
var container = new MockContainer(); var container = new MockContainer();
$parent.appendTo($('#qunit-fixture')); $parent.appendTo($('#qunit-fixture'));
$container.appendTo($parent); $container.appendTo($parent);
var Utils = require('select2/utils'); var Utils = require('select2/utils');
var Options = require('select2/options'); var Options = require('select2/options');
var Dropdown = require('select2/dropdown'); var Dropdown = require('select2/dropdown');
var AttachBody = require('select2/dropdown/attachBody'); var AttachBody = require('select2/dropdown/attachBody');
var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody); var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody);
var dropdown = new DropdownAdapter($select, new Options({ var dropdown = new DropdownAdapter($select, new Options({
dropdownParent: $parent dropdownParent: $parent
})); }));
var $dropdown = dropdown.render(); var $dropdown = dropdown.render();
assert.equal( assert.equal(
$dropdown[0].style.top, $dropdown[0].style.top,
0, 0,
'The drodpown should not have any offset before it is displayed' 'The drodpown should not have any offset before it is displayed'
); );
dropdown.bind(container, $container); dropdown.bind(container, $container);
dropdown.position($dropdown, $container); dropdown.position($dropdown, $container);
dropdown._showDropdown(); dropdown._showDropdown();
assert.ok( assert.ok(
dropdown.$dropdown.hasClass('select2-dropdown--below'), dropdown.$dropdown.hasClass('select2-dropdown--below'),
'The dropdown should be forced down' 'The dropdown should be forced down'
); );
assert.equal( assert.equal(
$dropdown.css('top').replace(/\D+/, ''), $dropdown.css('top').replace(/\D+/, ''),
$container.outerHeight(), $container.outerHeight(),
'There should not be an extra top offset' 'There should not be an extra top offset'
); );
assert.equal( assert.equal(
$dropdown.css('left'), $dropdown.css('left'),
'0px', '0px',
'There should not be an extra left offset' 'There should not be an extra left offset'
); );
}); }
);

View File

@ -45,88 +45,100 @@ QUnit.test('aria-autocomplete attribute is present', function (assert) {
); );
}); });
QUnit.test('aria-activedescendant should not be set initiailly', function (assert) { QUnit.test(
var $select = $('#qunit-fixture .single'); 'aria-activedescendant should not be set initiailly',
function (assert) {
var $select = $('#qunit-fixture .single');
var dropdown = new DropdownSearch($select, options); var dropdown = new DropdownSearch($select, options);
var $dropdown = dropdown.render(); var $dropdown = dropdown.render();
var container = new MockContainer(); var container = new MockContainer();
dropdown.bind(container, $('<span></span>')); dropdown.bind(container, $('<span></span>'));
var $search = $dropdown.find('input'); var $search = $dropdown.find('input');
assert.ok( assert.ok(
!$search.attr('aria-activedescendant'), !$search.attr('aria-activedescendant'),
'The search box should not point to anything when it is first rendered' 'The search box should not point to anything when it is first rendered'
); );
}); }
);
QUnit.test('aria-activedescendant should be set after highlight', function (assert) { QUnit.test(
var $select = $('#qunit-fixture .single'); 'aria-activedescendant should be set after highlight',
function (assert) {
var $select = $('#qunit-fixture .single');
var dropdown = new DropdownSearch($select, options); var dropdown = new DropdownSearch($select, options);
var $dropdown = dropdown.render(); var $dropdown = dropdown.render();
var container = new MockContainer(); var container = new MockContainer();
dropdown.bind(container, $('<span></span>')); dropdown.bind(container, $('<span></span>'));
container.trigger('results:focus', { container.trigger('results:focus', {
data: { data: {
_resultId: 'test' _resultId: 'test'
} }
}); });
var $search = $dropdown.find('input'); var $search = $dropdown.find('input');
assert.equal( assert.equal(
$search.attr('aria-activedescendant'), $search.attr('aria-activedescendant'),
'test', 'test',
'The search is pointing to the focused result' 'The search is pointing to the focused result'
); );
}); }
);
QUnit.test('activedescendant should remove if there is no ID', function (assert) { QUnit.test(
var $select = $('#qunit-fixture .single'); 'activedescendant should remove if there is no ID',
function (assert) {
var $select = $('#qunit-fixture .single');
var dropdown = new DropdownSearch($select, options); var dropdown = new DropdownSearch($select, options);
var $dropdown = dropdown.render(); var $dropdown = dropdown.render();
var container = new MockContainer(); var container = new MockContainer();
dropdown.bind(container, $('<span></span>')); dropdown.bind(container, $('<span></span>'));
var $search = $dropdown.find('input'); var $search = $dropdown.find('input');
$search.attr('aria-activedescendant', 'test'); $search.attr('aria-activedescendant', 'test');
container.trigger('results:focus', { container.trigger('results:focus', {
data: {} data: {}
}); });
assert.ok( assert.ok(
!$search.attr('aria-activedescendant'), !$search.attr('aria-activedescendant'),
'There is no result for the search to be pointing to' 'There is no result for the search to be pointing to'
); );
}); }
);
QUnit.test('aria-activedescendant should be removed when closed', function (assert) { QUnit.test(
var $select = $('#qunit-fixture .single'); 'aria-activedescendant should be removed when closed',
function (assert) {
var $select = $('#qunit-fixture .single');
var dropdown = new DropdownSearch($select, options); var dropdown = new DropdownSearch($select, options);
var $dropdown = dropdown.render(); var $dropdown = dropdown.render();
var container = new MockContainer(); var container = new MockContainer();
dropdown.bind(container, $('<span></span>')); dropdown.bind(container, $('<span></span>'));
var $search = $dropdown.find('input'); var $search = $dropdown.find('input');
$search.attr('aria-activedescendant', 'something'); $search.attr('aria-activedescendant', 'something');
container.trigger('close'); container.trigger('close');
assert.ok( assert.ok(
!$search.attr('aria-activedescendant'), !$search.attr('aria-activedescendant'),
'There is no active descendant when the dropdown is closed' 'There is no active descendant when the dropdown is closed'
); );
}); }
);
QUnit.test('aria-controls should not be set initiailly', function (assert) { QUnit.test('aria-controls should not be set initiailly', function (assert) {
var $select = $('#qunit-fixture .single'); var $select = $('#qunit-fixture .single');

View File

@ -62,44 +62,49 @@ QUnit.test('will not trigger if the results list is empty', function (assert) {
container.trigger('close'); container.trigger('close');
}); });
QUnit.test('will not trigger if no results here highlighted', function (assert) { QUnit.test(
assert.expect(2); 'will not trigger if no results here highlighted',
function (assert) {
assert.expect(2);
var $element = $('<select></select>'); var $element = $('<select></select>');
var select = new ModifiedResults($element, options, new SelectData($element)); var select = new ModifiedResults(
$element, options, new SelectData($element)
);
var $dropdown = select.render(); var $dropdown = select.render();
var container = new MockContainer(); var container = new MockContainer();
select.bind(container, $('<div></div>')); select.bind(container, $('<div></div>'));
select.on('select', function () { select.on('select', function () {
assert.ok(false, 'The select event should not have been triggered'); assert.ok(false, 'The select event should not have been triggered');
}); });
select.append({ select.append({
results: [ results: [
{ {
id: '1', id: '1',
text: 'Test' text: 'Test'
} }
] ]
}); });
assert.equal( assert.equal(
$dropdown.find('li').length, $dropdown.find('li').length,
1, 1,
'There should be one result in the dropdown' 'There should be one result in the dropdown'
); );
assert.equal( assert.equal(
$.trim($dropdown.find('li').text()), $.trim($dropdown.find('li').text()),
'Test', 'Test',
'The result should be the same as the one we appended' 'The result should be the same as the one we appended'
); );
container.trigger('close'); container.trigger('close');
}); }
);
QUnit.test('will trigger if there is a highlighted result', function (assert) { QUnit.test('will trigger if there is a highlighted result', function (assert) {
assert.expect(2); assert.expect(2);

View File

@ -1,50 +1,56 @@
QUnit.module('Defaults - Ajax'); QUnit.module('Defaults - Ajax');
QUnit.test('options are merged recursively with default options', function (assert) { QUnit.test(
var defaults = require('select2/defaults'); 'options are merged recursively with default options',
function (assert) {
var defaults = require('select2/defaults');
var ajaxDelay = 250; var ajaxDelay = 250;
var ajaxUrl = 'http://www.test.com'; var ajaxUrl = 'http://www.test.com';
var mergedOptions; var mergedOptions;
defaults.set('ajax--delay', ajaxDelay); defaults.set('ajax--delay', ajaxDelay);
mergedOptions = defaults.apply({ mergedOptions = defaults.apply({
ajax: { ajax: {
url: ajaxUrl url: ajaxUrl
} }
}); });
assert.equal( assert.equal(
mergedOptions.ajax.delay, mergedOptions.ajax.delay,
ajaxDelay,
'Ajax default options are present on the merged options'
);
assert.equal(
mergedOptions.ajax.url,
ajaxUrl,
'Ajax provided options are present on the merged options'
);
defaults.reset();
});
QUnit.test('more than one default option can be changed via set()', function(assert) {
var defaults = require('select2/defaults');
var ajaxDelay = 123;
var dataDataType = 'xml';
defaults.set('ajax--delay', ajaxDelay);
defaults.set('ajax--data-type', dataDataType);
assert.equal(
defaults.defaults.ajax.delay,
ajaxDelay, ajaxDelay,
'Ajax default options are present on the merged options'
);
assert.equal(
mergedOptions.ajax.url,
ajaxUrl,
'Ajax provided options are present on the merged options'
);
defaults.reset();
}
);
QUnit.test(
'more than one default option can be changed via set()',
function(assert) {
var defaults = require('select2/defaults');
var ajaxDelay = 123;
var dataDataType = 'xml';
defaults.set('ajax--delay', ajaxDelay);
defaults.set('ajax--data-type', dataDataType);
assert.equal(
defaults.defaults.ajax.delay,
ajaxDelay,
'Both ajax.delay and ajax.dataType present in defaults');
assert.equal(
defaults.defaults.ajax.dataType,
dataDataType,
'Both ajax.delay and ajax.dataType present in defaults'); 'Both ajax.delay and ajax.dataType present in defaults');
assert.equal( defaults.reset();
defaults.defaults.ajax.dataType, }
dataDataType, );
'Both ajax.delay and ajax.dataType present in defaults');
defaults.reset();
});

View File

@ -234,17 +234,20 @@ QUnit.test('converted ajax-url to ajax--url automatically', function (assert) {
); );
}); });
QUnit.test('converted select2-tags to data/tags automatically', function (assert) { QUnit.test(
var $test = $('<select data-select2-tags="original data"></select>'); 'converted select2-tags to data/tags automatically',
var options = new Options({}, $test); function (assert) {
var $test = $('<select data-select2-tags="original data"></select>');
var options = new Options({}, $test);
assert.ok( assert.ok(
options.get('tags'), options.get('tags'),
'The `tags` key is automatically set to true' 'The `tags` key is automatically set to true'
); );
assert.equal( assert.equal(
options.get('data'), options.get('data'),
'original data', 'original data',
'The `data` key is created with the original data' 'The `data` key is created with the original data'
); );
}); }
);

View File

@ -4,7 +4,7 @@ var $ = require('jquery');
var Options = require('select2/options'); var Options = require('select2/options');
var Defaults = require('select2/defaults'); var Defaults = require('select2/defaults');
module('Options - Translations', { QUnit.module('Options - Translations', {
beforeEach: function () { beforeEach: function () {
Defaults.reset(); Defaults.reset();
}, },
@ -13,20 +13,23 @@ module('Options - Translations', {
} }
}); });
test('partial dictonaries are reset when default reset', function (assert) { QUnit.test(
Defaults.set('language', { 'partial dictonaries are reset when default reset',
test: 'testing' function (assert) {
}); Defaults.set('language', {
test: 'testing'
});
Defaults.reset(); Defaults.reset();
assert.ok( assert.ok(
!Defaults.defaults.language.test, !Defaults.defaults.language.test,
'The partial dictionary should have been reset' 'The partial dictionary should have been reset'
); );
}); }
);
test('default language chain is English', function (assert) { QUnit.test('default language chain is English', function (assert) {
var $element = $('<select></select>'); var $element = $('<select></select>');
var options = new Options({}, $element); var options = new Options({}, $element);
@ -37,7 +40,7 @@ test('default language chain is English', function (assert) {
); );
}); });
test( QUnit.test(
'default translation includes all of the required messages', 'default translation includes all of the required messages',
function (assert) { function (assert) {
var $element = $('<select></select>'); var $element = $('<select></select>');
@ -86,84 +89,90 @@ QUnit.test('partial dictionaries can be passed', function (assert) {
); );
}); });
test('partial dictionaries can be combined with defaults', function (assert) { QUnit.test(
var $element = $('<select></select>'); 'partial dictionaries can be combined with defaults',
function (assert) {
var $element = $('<select></select>');
Defaults.set('language', { Defaults.set('language', {
test: function () { test: function () {
return 'Testing'; return 'Testing';
}
});
var options = new Options({
language: {
searching: function () {
return 'Something';
} }
} });
}, $element);
var translations = options.get('translations'); var options = new Options({
language: {
assert.equal(
translations.get('searching')(),
'Something',
'The partial dictionary still overrides translations'
);
assert.equal(
translations.get('test')(),
'Testing',
'The defaults were included in the fallback chain'
);
assert.equal(
translations.get('noResults')(),
'No results found',
'You can still get English translations for keys not passed in'
);
});
test('partial dictionaries can used in fallback chains', function (assert) {
var $element = $('<select></select>');
var options = new Options({
language: [
{
searching: function () { searching: function () {
return 'Something'; return 'Something';
} }
},
{
test: function () {
return 'Testing';
}
} }
] }, $element);
}, $element);
var translations = options.get('translations'); var translations = options.get('translations');
assert.equal( assert.equal(
translations.get('searching')(), translations.get('searching')(),
'Something', 'Something',
'The partial dictionary still overrides translations' 'The partial dictionary still overrides translations'
); );
assert.equal( assert.equal(
translations.get('test')(), translations.get('test')(),
'Testing', 'Testing',
'The defaults were included in the fallback chain' 'The defaults were included in the fallback chain'
); );
assert.equal( assert.equal(
translations.get('noResults')(), translations.get('noResults')(),
'No results found', 'No results found',
'You can still get English translations for keys not passed in' 'You can still get English translations for keys not passed in'
); );
}); }
);
test('language can be set via the options', function (assert) { QUnit.test(
'partial dictionaries can used in fallback chains',
function (assert) {
var $element = $('<select></select>');
var options = new Options({
language: [
{
searching: function () {
return 'Something';
}
},
{
test: function () {
return 'Testing';
}
}
]
}, $element);
var translations = options.get('translations');
assert.equal(
translations.get('searching')(),
'Something',
'The partial dictionary still overrides translations'
);
assert.equal(
translations.get('test')(),
'Testing',
'The defaults were included in the fallback chain'
);
assert.equal(
translations.get('noResults')(),
'No results found',
'You can still get English translations for keys not passed in'
);
}
);
QUnit.test('language can be set via the options', function (assert) {
var $element = $('<select></select>'); var $element = $('<select></select>');
var options = new Options({ var options = new Options({
@ -176,7 +185,7 @@ test('language can be set via the options', function (assert) {
); );
}); });
test('multi-part language is broken out', function (assert) { QUnit.test('multi-part language is broken out', function (assert) {
var $element = $('<select></select>'); var $element = $('<select></select>');
var options = new Options({ var options = new Options({
@ -189,7 +198,7 @@ test('multi-part language is broken out', function (assert) {
); );
}); });
test('default language can be set', function (assert) { QUnit.test('default language can be set', function (assert) {
var $element = $('<select></select>'); var $element = $('<select></select>');
Defaults.set('language', 'es'); Defaults.set('language', 'es');
@ -202,7 +211,7 @@ test('default language can be set', function (assert) {
); );
}); });
test('lanugage set via options adds to default chain', function (assert) { QUnit.test('lanugage set via options adds to default chain', function (assert) {
var $element = $('<select></select>'); var $element = $('<select></select>');
Defaults.set('language', 'es'); Defaults.set('language', 'es');
@ -217,7 +226,7 @@ test('lanugage set via options adds to default chain', function (assert) {
); );
}); });
test('default language chain can be set', function (assert) { QUnit.test('default language chain can be set', function (assert) {
var $element = $('<select></select>'); var $element = $('<select></select>');
Defaults.set('language', ['es', 'it', 'en']); Defaults.set('language', ['es', 'it', 'en']);
@ -230,7 +239,7 @@ test('default language chain can be set', function (assert) {
); );
}); });
test('language can be set by lang attr', function (assert) { QUnit.test('language can be set by lang attr', function (assert) {
var $element = $('<select lang="es"></select>'); var $element = $('<select lang="es"></select>');
var options = new Options({}, $element); var options = new Options({}, $element);
@ -241,7 +250,7 @@ test('language can be set by lang attr', function (assert) {
); );
}); });
test('language can be inherited by lang attr', function (assert) { QUnit.test('language can be inherited by lang attr', function (assert) {
var $element = $('<div lang="es"><select></select></div>').find('select'); var $element = $('<div lang="es"><select></select></div>').find('select');
var options = new Options({}, $element); var options = new Options({}, $element);
@ -252,18 +261,23 @@ test('language can be inherited by lang attr', function (assert) {
); );
}); });
test('multi-part language can be inherited by lang attr', function (assert) { QUnit.test(
var $element = $('<div lang="pt-BR"><select></select></div>').find('select'); 'multi-part language can be inherited by lang attr',
function (assert) {
var $element = $(
'<div lang="pt-BR"><select></select></div>'
).find('select');
var options = new Options({}, $element); var options = new Options({}, $element);
assert.deepEqual( assert.deepEqual(
options.get('language'), options.get('language'),
['pt-BR', 'pt', 'en'] ['pt-BR', 'pt', 'en']
); );
}); }
);
test('lang attr overrides default language', function (assert) { QUnit.test('lang attr overrides default language', function (assert) {
var $element = $('<select lang="it"></select>'); var $element = $('<select lang="it"></select>');
Defaults.set('language', 'es'); Defaults.set('language', 'es');
@ -276,7 +290,7 @@ test('lang attr overrides default language', function (assert) {
); );
}); });
test('default language overrides inherited lang attr', function (assert) { QUnit.test('default language overrides inherited lang attr', function (assert) {
var $element = $('<div lang="it"><select></select></div>').find('select'); var $element = $('<div lang="it"><select></select></div>').find('select');
Defaults.set('language', 'es'); Defaults.set('language', 'es');

View File

@ -21,13 +21,16 @@ QUnit.test('width from style attribute', function (assert) {
assert.equal(width, '50%'); assert.equal(width, '50%');
}); });
QUnit.test('width from style returns null if nothing is found', function (assert) { QUnit.test(
var $test = $('<select></selct>'); 'width from style returns null if nothing is found',
function (assert) {
var $test = $('<select></selct>');
var width = select._resolveWidth($test, 'style'); var width = select._resolveWidth($test, 'style');
assert.equal(width, null); assert.equal(width, null);
}); }
);
QUnit.test('width from computed element width', function (assert) { QUnit.test('width from computed element width', function (assert) {
var $style = $( var $style = $(
@ -51,34 +54,40 @@ QUnit.test('resolve gets the style if it is there', function (assert) {
assert.equal(width, '20%'); assert.equal(width, '20%');
}); });
QUnit.test('resolve falls back to element if there is no style', function (assert) { QUnit.test(
var $style = $( 'resolve falls back to element if there is no style',
'<style type="text/css">.css-set-width { width: 500px; }</style>' function (assert) {
); var $style = $(
var $test = $('<select class="css-set-width"></select>'); '<style type="text/css">.css-set-width { width: 500px; }</style>'
);
var $test = $('<select class="css-set-width"></select>');
$('#qunit-fixture').append($style); $('#qunit-fixture').append($style);
$('#qunit-fixture').append($test); $('#qunit-fixture').append($test);
var width = select._resolveWidth($test, 'resolve'); var width = select._resolveWidth($test, 'resolve');
assert.equal(width, '500px'); assert.equal(width, '500px');
}); }
);
QUnit.test('computedstyle gets the style if parent is invisible', function (assert) { QUnit.test(
var $style = $( 'computedstyle gets the style if parent is invisible',
'<style type="text/css">.css-set-width { width: 500px; }</style>' function (assert) {
); var $style = $(
var $test = $( '<style type="text/css">.css-set-width { width: 500px; }</style>'
'<div style="display:none;">' + );
'<select class="css-set-width"></select>' + var $test = $(
'</div>' '<div style="display:none;">' +
); '<select class="css-set-width"></select>' +
'</div>'
);
$('#qunit-fixture').append($style); $('#qunit-fixture').append($style);
$('#qunit-fixture').append($test); $('#qunit-fixture').append($test);
var width = select._resolveWidth($test.find('select'), 'computedstyle'); var width = select._resolveWidth($test.find('select'), 'computedstyle');
assert.equal(width, '500px'); assert.equal(width, '500px');
}); }
);

View File

@ -14,12 +14,15 @@ QUnit.test('role of results should be a listbox', function (assert) {
assert.equal($results.attr('role'), 'listbox'); assert.equal($results.attr('role'), 'listbox');
}); });
QUnit.test('multiple select should have aria-multiselectable', function (assert) { QUnit.test(
var results = new Results($('<select></select>'), new Options({ 'multiple select should have aria-multiselectable',
multiple: true function (assert) {
})); var results = new Results($('<select></select>'), new Options({
multiple: true
}));
var $results = results.render(); var $results = results.render();
assert.equal($results.attr('aria-multiselectable'), 'true'); assert.equal($results.attr('aria-multiselectable'), 'true');
}); }
);

View File

@ -42,53 +42,56 @@ QUnit.test('results:all with no data skips results:focus', function (assert) {
}); });
}); });
QUnit.test('results:all triggers results:focus on the first item', function (assert) { QUnit.test(
assert.expect(2); 'results:all triggers results:focus on the first item',
function (assert) {
assert.expect(2);
var $ = require('jquery'); var $ = require('jquery');
var $select = $('<select></select>'); var $select = $('<select></select>');
var $parent = $('<div></div>'); var $parent = $('<div></div>');
var $container = $('<span></span>'); var $container = $('<span></span>');
var container = new MockContainer(); var container = new MockContainer();
$parent.appendTo($('#qunit-fixture')); $parent.appendTo($('#qunit-fixture'));
$select.appendTo($parent); $select.appendTo($parent);
var Utils = require('select2/utils'); var Utils = require('select2/utils');
var Options = require('select2/options'); var Options = require('select2/options');
var Results = require('select2/results'); var Results = require('select2/results');
var results = new Results($select, new Options({})); var results = new Results($select, new Options({}));
// Fake the data adapter for the `setClasses` method // Fake the data adapter for the `setClasses` method
results.data = {}; results.data = {};
results.data.current = function (callback) { results.data.current = function (callback) {
callback([{ id: 'test' }]); callback([{ id: 'test' }]);
}; };
results.render(); results.render();
results.bind(container, $container); results.bind(container, $container);
results.on('results:focus', function (params) { results.on('results:focus', function (params) {
assert.equal(params.data.id, 'test'); assert.equal(params.data.id, 'test');
assert.equal(params.data.text, 'Test'); assert.equal(params.data.text, 'Test');
}); });
container.trigger('results:all', { container.trigger('results:all', {
data: { data: {
results: [ results: [
{ {
id: 'test', id: 'test',
text: 'Test' text: 'Test'
} }
] ]
} }
}); });
}); }
);
QUnit.test('results:append does not trigger results:focus', function (assert) { QUnit.test('results:append does not trigger results:focus', function (assert) {
assert.expect(0); assert.expect(0);
@ -189,53 +192,56 @@ QUnit.test('scrollAfterSelect triggers results:focus', function (assert) {
container.trigger('select', {}); container.trigger('select', {});
}); });
QUnit.test('!scrollAfterSelect does not trigger results:focus', function (assert) { QUnit.test(
assert.expect(1); '!scrollAfterSelect does not trigger results:focus',
function (assert) {
assert.expect(1);
var $ = require('jquery'); var $ = require('jquery');
var $select = $('<select></select>'); var $select = $('<select></select>');
var $parent = $('<div></div>'); var $parent = $('<div></div>');
var $container = $('<span></span>'); var $container = $('<span></span>');
var container = new MockContainer(); var container = new MockContainer();
$parent.appendTo($('#qunit-fixture')); $parent.appendTo($('#qunit-fixture'));
$select.appendTo($parent); $select.appendTo($parent);
var Utils = require('select2/utils'); var Utils = require('select2/utils');
var Options = require('select2/options'); var Options = require('select2/options');
var Results = require('select2/results'); var Results = require('select2/results');
var options = new Options({ scrollAfterSelect: false }); var options = new Options({ scrollAfterSelect: false });
var results = new Results($select, options); var results = new Results($select, options);
// Fake the data adapter for the `setClasses` method // Fake the data adapter for the `setClasses` method
results.data = {}; results.data = {};
results.data.current = function (callback) { results.data.current = function (callback) {
callback([{ id: 'test' }]); callback([{ id: 'test' }]);
}; };
results.render(); results.render();
results.bind(container, $container); results.bind(container, $container);
// check that default for scrollAfterSelect is false // check that default for scrollAfterSelect is false
assert.equal(options.get('scrollAfterSelect'), false); assert.equal(options.get('scrollAfterSelect'), false);
results.append({ results.append({
results: [ results: [
{ {
id: 'test', id: 'test',
text: 'Test' text: 'Test'
} }
] ]
}); });
results.on('results:focus', function () { results.on('results:focus', function () {
assert.ok(false, 'The results:focus event was triggered'); assert.ok(false, 'The results:focus event was triggered');
}); });
container.trigger('select', {}); container.trigger('select', {});
}); }
);

View File

@ -1,53 +1,56 @@
QUnit.module('Results - Infinite scrolling'); QUnit.module('Results - Infinite scrolling');
QUnit.test('loadingMore is triggered even without a scrollbar', function (assert) { QUnit.test(
assert.expect(1); 'loadingMore is triggered even without a scrollbar',
function (assert) {
assert.expect(1);
var $ = require('jquery'); var $ = require('jquery');
var $select = $('<select></select>'); var $select = $('<select></select>');
var $container = $('<span></span>'); var $container = $('<span></span>');
var container = new MockContainer(); var container = new MockContainer();
var Utils = require('select2/utils'); var Utils = require('select2/utils');
var Options = require('select2/options'); var Options = require('select2/options');
var Results = require('select2/results'); var Results = require('select2/results');
var InfiniteScroll = require('select2/dropdown/infiniteScroll'); var InfiniteScroll = require('select2/dropdown/infiniteScroll');
var InfiniteScrollResults = Utils.Decorate(Results, InfiniteScroll); var InfiniteScrollResults = Utils.Decorate(Results, InfiniteScroll);
var results = new InfiniteScrollResults($select, new Options({})); var results = new InfiniteScrollResults($select, new Options({}));
// Fake the data adapter for the `setClasses` method // Fake the data adapter for the `setClasses` method
results.data = {}; results.data = {};
results.data.current = function (callback) { results.data.current = function (callback) {
callback([{ id: 'test' }]); callback([{ id: 'test' }]);
}; };
$('#qunit-fixture').append(results.render()); $('#qunit-fixture').append(results.render());
results.bind(container, $container); results.bind(container, $container);
results.on('query:append', function () { results.on('query:append', function () {
assert.ok(true, 'It tried to load more immediately'); assert.ok(true, 'It tried to load more immediately');
}); });
container.trigger('results:all', { container.trigger('results:all', {
data: { data: {
results: [ results: [
{ {
id: 'test', id: 'test',
text: 'Test' text: 'Test'
}
],
pagination: {
more: true
} }
],
pagination: {
more: true
} }
} });
}); }
}); );
QUnit.test('loadingMore is not triggered without scrolling', function (assert) { QUnit.test('loadingMore is not triggered without scrolling', function (assert) {
assert.expect(0); assert.expect(0);

View File

@ -6,49 +6,61 @@ var Options = require('select2/options');
var Results = require('select2/results'); var Results = require('select2/results');
QUnit.test('disabled property on option is respected - enabled', function (assert) { QUnit.test(
var results = new Results($('<select></select>'), new Options({})); 'disabled property on option is respected - enabled',
function (assert) {
var results = new Results($('<select></select>'), new Options({}));
var $option = $('<option></option>'); var $option = $('<option></option>');
var option = results.option({ var option = results.option({
element: $option[0] element: $option[0]
}); });
assert.notEqual(option.getAttribute('aria-disabled'), 'true'); assert.notEqual(option.getAttribute('aria-disabled'), 'true');
}); }
);
QUnit.test('disabled property on option is respected - disabled', function (assert) { QUnit.test(
var results = new Results($('<select></select>'), new Options({})); 'disabled property on option is respected - disabled',
function (assert) {
var results = new Results($('<select></select>'), new Options({}));
var $option = $('<option disabled></option>'); var $option = $('<option disabled></option>');
var option = results.option({ var option = results.option({
element: $option[0] element: $option[0]
}); });
assert.equal(option.getAttribute('aria-disabled'), 'true'); assert.equal(option.getAttribute('aria-disabled'), 'true');
}); }
);
QUnit.test('disabled property on enabled optgroup is respected', function (assert) { QUnit.test(
var results = new Results($('<select></select>'), new Options({})); 'disabled property on enabled optgroup is respected',
function (assert) {
var results = new Results($('<select></select>'), new Options({}));
var $option = $('<optgroup></optgroup>'); var $option = $('<optgroup></optgroup>');
var option = results.option({ var option = results.option({
element: $option[0] element: $option[0]
}); });
assert.notEqual(option.getAttribute('aria-disabled'), 'true'); assert.notEqual(option.getAttribute('aria-disabled'), 'true');
}); }
);
QUnit.test('disabled property on disabled optgroup is respected', function (assert) { QUnit.test(
var results = new Results($('<select></select>'), new Options({})); 'disabled property on disabled optgroup is respected',
function (assert) {
var results = new Results($('<select></select>'), new Options({}));
var $option = $('<optgroup disabled></optgroup>'); var $option = $('<optgroup disabled></optgroup>');
var option = results.option({ var option = results.option({
element: $option[0] element: $option[0]
}); });
assert.equal(option.getAttribute('aria-disabled'), 'true'); assert.equal(option.getAttribute('aria-disabled'), 'true');
}); }
);
QUnit.test('option in disabled optgroup is disabled', function (assert) { QUnit.test('option in disabled optgroup is disabled', function (assert) {
var results = new Results($('<select></select>'), new Options({})); var results = new Results($('<select></select>'), new Options({}));

View File

@ -42,23 +42,25 @@ QUnit.test('clear is not displayed for single placeholder', function (assert) {
); );
}); });
QUnit.test('clear is not displayed for multiple placeholder', function (assert) { QUnit.test(
var selection = new AllowClearPlaceholder( 'clear is not displayed for multiple placeholder',
$('#qunit-fixture .multiple'), function (assert) {
allowClearOptions var selection = new AllowClearPlaceholder(
); $('#qunit-fixture .multiple'),
allowClearOptions
);
var $selection = selection.render(); var $selection = selection.render();
selection.update([]); selection.update([]);
assert.equal(
$selection.find('.select2-selection__clear').length,
0,
'The clear icon should not be displayed'
);
});
assert.equal(
$selection.find('.select2-selection__clear').length,
0,
'The clear icon should not be displayed'
);
}
);
QUnit.test('clear is displayed for placeholder', function (assert) { QUnit.test('clear is displayed for placeholder', function (assert) {
var selection = new AllowClearPlaceholder( var selection = new AllowClearPlaceholder(
@ -178,38 +180,41 @@ QUnit.test('clicking clear will trigger the unselect event', function (assert) {
$remove.trigger('mousedown'); $remove.trigger('mousedown');
}); });
QUnit.test('preventing the unselect event cancels the clearing', function (assert) { QUnit.test(
var $element = $('#qunit-fixture .single-with-placeholder'); 'preventing the unselect event cancels the clearing',
function (assert) {
var $element = $('#qunit-fixture .single-with-placeholder');
var selection = new AllowClearPlaceholder( var selection = new AllowClearPlaceholder(
$element, $element,
allowClearOptions allowClearOptions
); );
var container = new MockContainer(); var container = new MockContainer();
var $selection = selection.render(); var $selection = selection.render();
selection.bind(container, $('<div></div>')); selection.bind(container, $('<div></div>'));
$element.val('One'); $element.val('One');
selection.update([{ selection.update([{
id: 'One', id: 'One',
text: 'One' text: 'One'
}]); }]);
selection.on('unselect', function (ev) { selection.on('unselect', function (ev) {
ev.prevented = true; ev.prevented = true;
}); });
var $remove = $selection.find('.select2-selection__clear'); var $remove = $selection.find('.select2-selection__clear');
$remove.trigger('mousedown'); $remove.trigger('mousedown');
assert.equal( assert.equal(
$element.val(), $element.val(),
'One', 'One',
'The placeholder should not have been set' 'The placeholder should not have been set'
); );
}); }
);
QUnit.test('clicking clear will trigger the clear event', function (assert) { QUnit.test('clicking clear will trigger the clear event', function (assert) {
assert.expect(5); assert.expect(5);
@ -266,38 +271,41 @@ QUnit.test('clicking clear will trigger the clear event', function (assert) {
$remove.trigger('mousedown'); $remove.trigger('mousedown');
}); });
QUnit.test('preventing the clear event cancels the clearing', function (assert) { QUnit.test(
var $element = $('#qunit-fixture .single-with-placeholder'); 'preventing the clear event cancels the clearing',
function (assert) {
var $element = $('#qunit-fixture .single-with-placeholder');
var selection = new AllowClearPlaceholder( var selection = new AllowClearPlaceholder(
$element, $element,
allowClearOptions allowClearOptions
); );
var container = new MockContainer(); var container = new MockContainer();
var $selection = selection.render(); var $selection = selection.render();
selection.bind(container, $('<div></div>')); selection.bind(container, $('<div></div>'));
$element.val('One'); $element.val('One');
selection.update([{ selection.update([{
id: 'One', id: 'One',
text: 'One' text: 'One'
}]); }]);
selection.on('clear', function (ev) { selection.on('clear', function (ev) {
ev.prevented = true; ev.prevented = true;
}); });
var $remove = $selection.find('.select2-selection__clear'); var $remove = $selection.find('.select2-selection__clear');
$remove.trigger('mousedown'); $remove.trigger('mousedown');
assert.equal( assert.equal(
$element.val(), $element.val(),
'One', 'One',
'The placeholder should not have been set' 'The placeholder should not have been set'
); );
}); }
);
QUnit.test('clear does not work when disabled', function (assert) { QUnit.test('clear does not work when disabled', function (assert) {
var $element = $('#qunit-fixture .single-with-placeholder'); var $element = $('#qunit-fixture .single-with-placeholder');
@ -330,71 +338,74 @@ QUnit.test('clear does not work when disabled', function (assert) {
); );
}); });
QUnit.test('clear button doesnt visually break selected options', function (assert) { QUnit.test(
var $element = $('<select></select>'); 'clear button doesnt visually break selected options',
function (assert) {
var $element = $('<select></select>');
var Selection = Utils.Decorate( var Selection = Utils.Decorate(
Utils.Decorate(MultipleSelection, Placeholder), Utils.Decorate(MultipleSelection, Placeholder),
AllowClear AllowClear
); );
var selection = new Selection( var selection = new Selection(
$element, $element,
allowClearOptions allowClearOptions
); );
var container = new MockContainer(); var container = new MockContainer();
var $container = $( var $container = $(
'<span class="select2-container select2-container--default"></span>' '<span class="select2-container select2-container--default"></span>'
); );
$('#qunit-fixture').append($container); $('#qunit-fixture').append($container);
var $selection = selection.render(); var $selection = selection.render();
$container.append($selection); $container.append($selection);
$container.css('width', '100px'); $container.css('width', '100px');
selection.bind(container, $container); selection.bind(container, $container);
selection.update([{ selection.update([{
id: '1',
text: '1'
}]);
var singleHeight = $container.height();
selection.update([
{
id: '10',
text: '10'
},
{
id: '20',
text: '20'
}
]);
var doubleHeight = $container.height();
selection.update([
{
id: '1', id: '1',
text: '1' text: '1'
}, }]);
{
id: '2',
text: '2'
}
]);
assert.notEqual( var singleHeight = $container.height();
singleHeight,
doubleHeight,
'The height of the two different rows should be different'
);
assert.equal( selection.update([
$container.height(), {
doubleHeight, id: '10',
'There should be two full lines of selections' text: '10'
); },
}); {
id: '20',
text: '20'
}
]);
var doubleHeight = $container.height();
selection.update([
{
id: '1',
text: '1'
},
{
id: '2',
text: '2'
}
]);
assert.notEqual(
singleHeight,
doubleHeight,
'The height of the two different rows should be different'
);
assert.equal(
$container.height(),
doubleHeight,
'There should be two full lines of selections'
);
}
);

View File

@ -144,29 +144,32 @@ QUnit.test('update sets the title to the data title', function (assert) {
); );
}); });
QUnit.test('update should clear title for placeholder options', function (assert) { QUnit.test('update should clear title for placeholder options',
var selection = new MultipleSelection( function (assert) {
$('#qunit-fixture .multiple'), var selection = new MultipleSelection(
options $('#qunit-fixture .multiple'),
); options
);
var $selection = selection.render(); var $selection = selection.render();
selection.update([{ selection.update([{
id: '', id: '',
text: '' text: ''
}]); }]);
var $rendered = $selection.find('.select2-selection__rendered li'); var $rendered = $selection.find('.select2-selection__rendered li');
assert.equal( assert.equal(
$rendered.attr('title'), $rendered.attr('title'),
undefined, undefined,
'The title should be removed if a placeholder is rendered' 'The title should be removed if a placeholder is rendered'
); );
}); }
);
QUnit.test('update should clear title for options without text', function (assert) { QUnit.test(
'update should clear title for options without text', function (assert) {
var selection = new MultipleSelection( var selection = new MultipleSelection(
$('#qunit-fixture .multiple'), $('#qunit-fixture .multiple'),
options options

View File

@ -32,18 +32,20 @@ QUnit.test('normalizing placeholder ignores objects', function (assert) {
assert.equal(original, normalized); assert.equal(original, normalized);
}); });
QUnit.test('normalizing placeholder gives object for string', function (assert) { QUnit.test(
var selection = new SinglePlaceholder( 'normalizing placeholder gives object for string',
$('#qunit-fixture .single'), function (assert) {
placeholderOptions var selection = new SinglePlaceholder(
); $('#qunit-fixture .single'),
placeholderOptions
);
var normalized = selection.normalizePlaceholder('placeholder'); var normalized = selection.normalizePlaceholder('placeholder');
assert.equal(normalized.id, '');
assert.equal(normalized.text, 'placeholder');
});
assert.equal(normalized.id, '');
assert.equal(normalized.text, 'placeholder');
}
);
QUnit.test('text is shown for placeholder option on single', function (assert) { QUnit.test('text is shown for placeholder option on single', function (assert) {
var selection = new SinglePlaceholder( var selection = new SinglePlaceholder(
@ -60,15 +62,18 @@ QUnit.test('text is shown for placeholder option on single', function (assert) {
assert.equal($selection.text(), 'This is the placeholder'); assert.equal($selection.text(), 'This is the placeholder');
}); });
QUnit.test('placeholder is shown when no options are selected', function (assert) { QUnit.test(
var selection = new SinglePlaceholder( 'placeholder is shown when no options are selected',
$('#qunit-fixture .multiple'), function (assert) {
placeholderOptions var selection = new SinglePlaceholder(
); $('#qunit-fixture .multiple'),
placeholderOptions
);
var $selection = selection.render(); var $selection = selection.render();
selection.update([]); selection.update([]);
assert.equal($selection.text(), 'This is the placeholder'); assert.equal($selection.text(), 'This is the placeholder');
}); }
);

View File

@ -49,104 +49,116 @@ QUnit.test('aria-autocomplete attribute is present', function (assert) {
); );
}); });
QUnit.test('aria-activedescendant should not be set initiailly', function (assert) { QUnit.test(
var $select = $('#qunit-fixture .multiple'); 'aria-activedescendant should not be set initiailly',
function (assert) {
var $select = $('#qunit-fixture .multiple');
var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch); var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
var selection = new CustomSelection($select, options); var selection = new CustomSelection($select, options);
var $selection = selection.render(); var $selection = selection.render();
var container = new MockContainer(); var container = new MockContainer();
selection.bind(container, $('<span></span>')); selection.bind(container, $('<span></span>'));
// Update the selection so the search is rendered // Update the selection so the search is rendered
selection.update([]); selection.update([]);
var $search = $selection.find('input'); var $search = $selection.find('input');
assert.ok( assert.ok(
!$search.attr('aria-activedescendant'), !$search.attr('aria-activedescendant'),
'The search box should not point to anything when it is first rendered' 'The search box should not point to anything when it is first rendered'
); );
}); }
);
QUnit.test('aria-activedescendant should be set after highlight', function (assert) { QUnit.test(
var $select = $('#qunit-fixture .multiple'); 'aria-activedescendant should be set after highlight',
function (assert) {
var $select = $('#qunit-fixture .multiple');
var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch); var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
var selection = new CustomSelection($select, options); var selection = new CustomSelection($select, options);
var $selection = selection.render(); var $selection = selection.render();
var container = new MockContainer(); var container = new MockContainer();
selection.bind(container, $('<span></span>')); selection.bind(container, $('<span></span>'));
// Update the selection so the search is rendered // Update the selection so the search is rendered
selection.update([]); selection.update([]);
container.trigger('results:focus', { container.trigger('results:focus', {
data: { data: {
_resultId: 'test' _resultId: 'test'
} }
}); });
var $search = $selection.find('input'); var $search = $selection.find('input');
assert.equal( assert.equal(
$search.attr('aria-activedescendant'), $search.attr('aria-activedescendant'),
'test', 'test',
'The search is pointing to the focused result' 'The search is pointing to the focused result'
); );
}); }
);
QUnit.test('activedescendant should remove if there is no ID', function (assert) { QUnit.test(
var $select = $('#qunit-fixture .multiple'); 'activedescendant should remove if there is no ID',
function (assert) {
var $select = $('#qunit-fixture .multiple');
var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch); var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
var selection = new CustomSelection($select, options); var selection = new CustomSelection($select, options);
var $selection = selection.render(); var $selection = selection.render();
var container = new MockContainer(); var container = new MockContainer();
selection.bind(container, $('<span></span>')); selection.bind(container, $('<span></span>'));
// Update the selection so the search is rendered // Update the selection so the search is rendered
selection.update([]); selection.update([]);
var $search = $selection.find('input'); var $search = $selection.find('input');
$search.attr('aria-activedescendant', 'test'); $search.attr('aria-activedescendant', 'test');
container.trigger('results:focus', { container.trigger('results:focus', {
data: {} data: {}
}); });
assert.ok( assert.ok(
!$search.attr('aria-activedescendant'), !$search.attr('aria-activedescendant'),
'There is no result for the search to be pointing to' 'There is no result for the search to be pointing to'
); );
}); }
);
QUnit.test('aria-activedescendant should be removed when closed', function (assert) { QUnit.test(
var $select = $('#qunit-fixture .multiple'); 'aria-activedescendant should be removed when closed',
function (assert) {
var $select = $('#qunit-fixture .multiple');
var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch); var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
var selection = new CustomSelection($select, options); var selection = new CustomSelection($select, options);
var $selection = selection.render(); var $selection = selection.render();
var container = new MockContainer(); var container = new MockContainer();
selection.bind(container, $('<span></span>')); selection.bind(container, $('<span></span>'));
// Update the selection so the search is rendered // Update the selection so the search is rendered
selection.update([]); selection.update([]);
var $search = $selection.find('input'); var $search = $selection.find('input');
$search.attr('aria-activedescendant', 'something'); $search.attr('aria-activedescendant', 'something');
container.trigger('close'); container.trigger('close');
assert.ok( assert.ok(
!$search.attr('aria-activedescendant'), !$search.attr('aria-activedescendant'),
'There is no active descendant when the dropdown is closed' 'There is no active descendant when the dropdown is closed'
); );
}); }
);
QUnit.test('aria-controls should not be set initiailly', function (assert) { QUnit.test('aria-controls should not be set initiailly', function (assert) {
var $select = $('#qunit-fixture .multiple'); var $select = $('#qunit-fixture .multiple');

View File

@ -218,34 +218,37 @@ QUnit.test('search box without text should propagate click', function (assert) {
$search.trigger('click'); $search.trigger('click');
}); });
QUnit.test('search box with text should not propagate click', function (assert) { QUnit.test(
assert.expect(0); 'search box with text should not propagate click',
function (assert) {
assert.expect(0);
var $container = $('#qunit-fixture .event-container'); var $container = $('#qunit-fixture .event-container');
var container = new MockContainer(); var container = new MockContainer();
var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch); var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
var $element = $('#qunit-fixture .multiple'); var $element = $('#qunit-fixture .multiple');
var selection = new CustomSelection($element, options); var selection = new CustomSelection($element, options);
var $selection = selection.render(); var $selection = selection.render();
selection.bind(container, $container); selection.bind(container, $container);
// Update the selection so the search is rendered // Update the selection so the search is rendered
selection.update([]); selection.update([]);
// Make it visible so the browser can place focus on the search // Make it visible so the browser can place focus on the search
$container.append($selection); $container.append($selection);
$selection.on('click', function () { $selection.on('click', function () {
assert.ok(false, 'The click event should have been trapped'); assert.ok(false, 'The click event should have been trapped');
}); });
var $search = $selection.find('input'); var $search = $selection.find('input');
$search.val('test'); $search.val('test');
$search.trigger('click'); $search.trigger('click');
}); }
);
QUnit.test('search box with text should not close dropdown', function (assert) { QUnit.test('search box with text should not close dropdown', function (assert) {
assert.expect(0); assert.expect(0);

View File

@ -159,50 +159,56 @@ QUnit.test('update sets the title to the data title', function (assert) {
); );
}); });
QUnit.test('update should clear title for placeholder options', function (assert) { QUnit.test(
var selection = new SingleSelection( 'update should clear title for placeholder options',
$('#qunit-fixture .single'), function (assert) {
options var selection = new SingleSelection(
); $('#qunit-fixture .single'),
options
);
var $selection = selection.render(); var $selection = selection.render();
var $rendered = $selection.find('.select2-selection__rendered'); var $rendered = $selection.find('.select2-selection__rendered');
$rendered.attr('title', 'testing'); $rendered.attr('title', 'testing');
selection.update([{ selection.update([{
id: '', id: '',
text: '' text: ''
}]); }]);
assert.equal( assert.equal(
$rendered.attr('title'), $rendered.attr('title'),
undefined, undefined,
'The title should be removed if a placeholder is rendered' 'The title should be removed if a placeholder is rendered'
); );
}); }
);
QUnit.test('update should clear title for options without text', function (assert) { QUnit.test(
var selection = new SingleSelection( 'update should clear title for options without text',
$('#qunit-fixture .single'), function (assert) {
options var selection = new SingleSelection(
); $('#qunit-fixture .single'),
options
);
var $selection = selection.render(); var $selection = selection.render();
var $rendered = $selection.find('.select2-selection__rendered'); var $rendered = $selection.find('.select2-selection__rendered');
$rendered.attr('title', 'testing'); $rendered.attr('title', 'testing');
selection.update([{ selection.update([{
id: '' id: ''
}]); }]);
assert.equal( assert.equal(
$rendered.attr('title'), $rendered.attr('title'),
undefined, undefined,
'The title should be removed if there is no text or title property' 'The title should be removed if there is no text or title property'
); );
}); }
);
QUnit.test('escapeMarkup is being used', function (assert) { QUnit.test('escapeMarkup is being used', function (assert) {
var selection = new SingleSelection( var selection = new SingleSelection(