Typeahead for tags with selectize.

Fixes #12
This commit is contained in:
Tim Schumacher 2015-04-11 11:29:53 +02:00
parent 3a862e7432
commit faad41f5e2
94 changed files with 37138 additions and 4 deletions

723
web/vendor/selectize.js/test/api.js vendored Normal file
View file

@ -0,0 +1,723 @@
(function() {
describe('API', function() {
describe('disable()', function() {
var test;
before(function() {
test = setup_test('<select tabindex="4">', {});
expect(String(test.selectize.$control_input.attr('tabindex'))).to.be.equal('4');
test.selectize.disable();
});
it('should set "tabindex" prop to -1', function() {
expect(String(test.selectize.$control_input.attr('tabindex'))).to.be.equal('-1');
});
it('should set "disabled" class', function() {
expect(test.selectize.$control.hasClass('disabled')).to.be.equal(true);
});
it('should set isDisabled property to true', function() {
expect(test.selectize.isDisabled).to.be.equal(true);
});
it('should add "disabled" attribute on inputs', function() {
expect(test.selectize.$input.is(':disabled')).to.be.equal(true);
expect(test.selectize.$control_input.is(':disabled')).to.be.equal(true);
});
});
describe('enable()', function() {
var test;
before(function() {
test = setup_test('<select disabled tabindex="2">', {});
expect(String(test.selectize.$control_input.attr('tabindex'))).to.be.equal('-1');
test.selectize.enable();
});
it('should restore original "tabindex" prop', function() {
expect(String(test.selectize.$control_input.attr('tabindex'))).to.be.equal('2');
});
it('should remove "disabled" class', function() {
expect(test.selectize.$control.hasClass('disabled')).to.be.equal(false);
});
it('should set isDisabled property to false', function() {
expect(test.selectize.isDisabled).to.be.equal(false);
});
it('should remove "disabled" attribute on inputs', function() {
expect(test.selectize.$input.is(':disabled')).to.be.equal(false);
expect(test.selectize.$control_input.is(':disabled')).to.be.equal(false);
});
});
describe('focus()', function() {
var test;
before(function(done) {
test = setup_test('<select>', {});
test.selectize.focus();
window.setTimeout(function() { done(); }, 5);
});
it('should set isFocused property to true', function() {
expect(test.selectize.isFocused).to.be.equal(true);
});
it('should give the control focus', function() {
expect(has_focus(test.selectize.$control_input[0])).to.be.equal(true);
});
});
describe('blur()', function() {
var test;
before(function(done) {
test = setup_test('<select>', {});
test.selectize.focus();
window.setTimeout(function() {
test.selectize.blur();
window.setTimeout(done, 100);
}, 50);
});
it('should set isFocused property to false', function() {
expect(test.selectize.isFocused).to.be.equal(false);
});
it('should remove focus from the control', function() {
expect(has_focus(test.selectize.$control_input[0])).to.be.equal(false);
});
});
describe('createItem()', function() {
it('should fail if non-object returned by "create" callback', function() {
var test = setup_test('<select>', {
valueField: 'value',
labelField: 'value',
create: function(input) {
return false;
}
});
test.selectize.$control_input.val('test');
test.selectize.createItem();
expect(test.selectize.items.length).to.be.equal(0);
test = setup_test('<select>', {
valueField: 'value',
labelField: 'value',
create: function(input) {
return 'hello';
}
});
test.selectize.$control_input.val('test');
test.selectize.createItem();
expect(test.selectize.items.length).to.be.equal(0);
});
it('should add option upon completion (synchronous)', function() {
var test = setup_test('<select>', {
valueField: 'value',
labelField: 'value',
create: function(input) {
return {value: input};
}
});
test.selectize.$control_input.val('test');
test.selectize.createItem();
expect(test.selectize.options).to.have.property('test');
});
it('should add option upon completion (asynchronous)', function(done) {
var test = setup_test('<select>', {
valueField: 'value',
labelField: 'value',
create: function(input, callback) {
window.setTimeout(function() {
callback({value: input});
expect(test.selectize.options).to.have.property('test');
done();
}, 0);
}
});
test.selectize.$control_input.val('test');
test.selectize.createItem();
});
});
describe('addOptionGroup()', function() {
var test;
before(function() {
test = setup_test('<select>', {valueField: 'value', labelField: 'value', optgroupValueField: 'grpval'});
});
it('should register group', function() {
var data = {label: 'Group Label'};
test.selectize.addOptionGroup('group_id', data);
expect(test.selectize.optgroups).to.have.property('group_id');
});
it('should add implicit $order property', function() {
test.selectize.addOptionGroup('group1', {});
test.selectize.addOptionGroup('group2', {});
assert.equal(test.selectize.optgroups['group1'].$order, 2);
assert.equal(test.selectize.optgroups['group2'].$order, 3);
});
});
describe('removeOptionGroup()', function() {
var test;
before(function() {
test = setup_test('<select>', {valueField: 'value', labelField: 'value'});
});
it('should remove group', function() {
var data = {label: 'Group Label'};
test.selectize.addOptionGroup('group_id', data);
test.selectize.removeOptionGroup('group_id');
expect(test.selectize.optgroups).to.not.have.property('group_id');
});
});
describe('clearOptionGroups()', function() {
var test;
before(function() {
test = setup_test('<select>', {valueField: 'value', labelField: 'value'});
});
it('should clear all groups', function() {
var data = {label: 'Group Label'};
test.selectize.addOptionGroup('group_id', data);
test.selectize.addOptionGroup('group_id2', data);
test.selectize.clearOptionGroups();
expect(test.selectize.optgroups).to.deep.equal({});
});
});
describe('addOption()', function() {
var test;
before(function() {
test = setup_test('<select>', {valueField: 'value', labelField: 'value'});
});
it('should add implicit $order property', function() {
var opt1 = {value: 'hello'};
var opt2 = {value: 'world'};
test.selectize.addOption(opt1);
test.selectize.addOption(opt2);
assert.deepEqual(test.selectize.options, {
'hello': {value: 'hello', $order: 1},
'world': {value: 'world', $order: 2}
});
});
it('should allow string values', function() {
test.selectize.addOption({value: 'stringtest'});
expect(test.selectize.options).to.have.property('stringtest');
});
it('should not allow null / undefined values', function() {
test.selectize.addOption({value: undefined});
test.selectize.addOption({value: null});
expect(test.selectize.options).to.not.have.property('undefined');
expect(test.selectize.options).to.not.have.property('null');
expect(test.selectize.options).to.not.have.property('');
});
it('should allow integer values', function() {
test.selectize.addOption({value: 0});
test.selectize.addOption({value: 1});
expect(test.selectize.options).to.have.property('0');
expect(test.selectize.options).to.have.property('1');
});
it('should allow arrays of options', function() {
test.selectize.addOption([{value: 'a'}, {value: 'b'}]);
expect(test.selectize.options).to.have.property('a');
expect(test.selectize.options).to.have.property('b');
});
it('should not override existing options', function() {
test.selectize.addOption([{value: 'a'}, {value: 'b'}]);
test.selectize.addOption({value: 'a', test: 'hello'});
expect(test.selectize.options.a).to.not.have.property('test');
});
});
describe('addItem()', function() {
var test;
before(function() {
test = setup_test('<select multiple>', {
valueField: 'value',
labelField: 'value',
options: [
{value: 0},
{value: 1},
{value: 'undefined'},
{value: 'null'},
{value: 'a'},
{value: 'b'},
{value: 'c'},
{value: 'x'},
{value: '$1'},
{value: '\''},
{value: '"'},
{value: '\\\''},
{value: '\\"'},
]
});
});
it('should update "items" array', function() {
test.selectize.addItem('b');
expect(test.selectize.items.indexOf('b')).to.be.equal(0);
});
it('should not give control focus', function(done) {
test.selectize.addItem(0);
window.setTimeout(function() {
expect(test.selectize.isFocused).to.be.equal(false);
done();
}, 0);
});
it('should not allow duplicate entries', function() {
test.selectize.addItem('a');
test.selectize.addItem('a');
expect(test.selectize.items.indexOf('a')).to.be.equal(test.selectize.items.lastIndexOf('a'));
});
it('should not allow undefined / null values', function() {
test.selectize.addItem(undefined);
test.selectize.addItem(null);
expect(test.selectize.items.indexOf('undefined')).to.be.equal(-1);
expect(test.selectize.items.indexOf('null')).to.be.equal(-1);
});
it('should allow integer values', function() {
test.selectize.addItem(0);
expect(test.selectize.items.indexOf('0')).to.not.be.equal(-1);
});
it('should not fire "change" if silent is truthy', function(done) {
var watcher = function(e) { throw new Error('Change fired'); };
test.$select.on('change', watcher);
test.selectize.addItem('x', true);
expect(test.selectize.items.indexOf('x')).to.not.be.equal(-1);
window.setTimeout(function() {
test.$select.off('change', watcher);
done();
}, 0);
});
it('should update DOM', function() {
test.selectize.addItem('c');
expect(test.selectize.$control.find('[data-value=c]').length).to.be.equal(1);
test.selectize.addItem('$1');
var found = false;
test.selectize.$control.children().each(function() {
if (this.getAttribute('data-value') === '$1') {
found = true;
return false;
}
});
expect(found).to.be.equal(true);
});
});
describe('updateOption()', function() {
var test;
before(function() {
test = setup_test('<select multiple>', {
valueField: 'value',
labelField: 'value',
options: [
{value: 0},
{value: 1},
{value: 'a'},
{value: 'b'},
{value: 'c'},
{value: 'd'},
{value: 'e'},
{value: 'f'},
{value: 'x'},
{value: 'null'},
{value: 'undefined'},
{value: '\''},
{value: '"'},
{value: '\\\''},
{value: '\\"'},
],
items: ['e','f']
});
});
it('should update option data', function() {
test.selectize.updateOption('a', {value: 'a', test: 'test'});
expect(test.selectize.options).to.have.property('a');
expect(test.selectize.options['a'].test).to.equal('test');
});
it('should update indexes', function() {
test.selectize.updateOption('e', {value: 'e_updated'});
expect(test.selectize.options).to.not.have.property('e');
expect(test.selectize.options).to.have.property('e_updated');
expect(test.selectize.items.indexOf('e')).to.be.equal(-1);
expect(test.selectize.items.indexOf('e_updated')).to.be.equal(0);
});
it('should maintain implicit $order property', function() {
var order_orig = test.selectize.options['x'].$order;
assert.isNumber(order_orig);
test.selectize.updateOption('x', {value: 'x', something: 'x'});
assert.equal(test.selectize.options['x'].$order, order_orig);
});
it('should allow integer values', function() {
test.selectize.updateOption(0, {value: '0_updated'});
test.selectize.updateOption(1, {value: '1_updated'});
expect(test.selectize.options).to.not.have.property('0');
expect(test.selectize.options).to.not.have.property('1');
expect(test.selectize.options).to.have.property('0_updated');
expect(test.selectize.options).to.have.property('1_updated');
});
it('should throw error if value not set in data', function() {
expect(function() {
test.selectize.updateOption('c', {value: undefined, test: 'test'});
test.selectize.updateOption('d', {value: null, test: 'test'});
}).to.throw(Error);
});
it('should ignore undefined / null value references', function() {
test.selectize.updateOption(undefined, {value: 'undefined', test: 'test'});
test.selectize.updateOption(null, {value: 'null', test: 'test'});
expect(test.selectize.options['undefined']).to.not.have.property('test');
expect(test.selectize.options['null']).to.not.have.property('test');
});
it('should update DOM', function() {
test.selectize.updateOption('f', {value: 'f_updated'});
expect(test.selectize.$control.find('[data-value=f]').length).to.be.equal(0);
expect(test.selectize.$control.find('[data-value=f_updated]').length).to.be.equal(1);
});
});
describe('getOption()', function() {
var test;
before(function() {
test = setup_test('<select>', {
valueField: 'value',
labelField: 'value',
options: [
{value: 0},
{value: 1},
{value: 'a'},
{value: 'b'},
{value: '\''},
{value: '\\'},
{value: '"'},
{value: '\\\''},
{value: '\\"'},
]
});
test.selectize.refreshOptions(true);
});
it('should allow string values', function() {
expect(test.selectize.getOption('a')).to.be.ok;
expect(test.selectize.getOption('a').length).to.be.equal(1);
expect(test.selectize.getOption('b')).to.be.ok;
expect(test.selectize.getOption('b').length).to.be.equal(1);
});
it('should allow integer values', function() {
expect(test.selectize.getOption(0)).to.be.ok;
expect(test.selectize.getOption(0).length).to.be.equal(1);
expect(test.selectize.getOption(1)).to.be.ok;
expect(test.selectize.getOption(1).length).to.be.equal(1);
});
it('should allow values with quotation marks', function() {
expect(test.selectize.getOption('\'')).to.be.ok;
expect(test.selectize.getOption('\'').length).to.be.equal(1);
expect(test.selectize.getOption('"')).to.be.ok;
expect(test.selectize.getOption('"').length).to.be.equal(1);
});
it('should allow values with backslashes', function() {
expect(test.selectize.getOption('\\')).to.be.ok;
expect(test.selectize.getOption('\\').length).to.be.equal(1);
expect(test.selectize.getOption('\\\'')).to.be.ok;
expect(test.selectize.getOption('\\\'').length).to.be.equal(1);
expect(test.selectize.getOption('\\"')).to.be.ok;
expect(test.selectize.getOption('\\"').length).to.be.equal(1);
});
it('should not allow undefined / null values', function() {
expect(test.selectize.getOption(null)).to.be.ok;
expect(test.selectize.getOption(null).length).to.be.equal(0);
expect(test.selectize.getOption(undefined)).to.be.ok;
expect(test.selectize.getOption(undefined).length).to.be.equal(0);
});
});
describe('getItem()', function() {
var test;
before(function() {
test = setup_test('<select multiple>', {
valueField: 'value',
labelField: 'value',
options: [
{value: 0},
{value: 1},
{value: 'a'},
{value: 'b'},
{value: '\''},
{value: '"'},
{value: '\\\''},
{value: '\\"'}
],
items: ['0','1','a','b','\'','"','\\\'','\\"']
});
});
it('should allow string values', function() {
expect(test.selectize.getItem('a')).to.be.ok;
expect(test.selectize.getItem('a').length).to.be.equal(1);
expect(test.selectize.getItem('b')).to.be.ok;
expect(test.selectize.getItem('b').length).to.be.equal(1);
});
it('should allow integer values', function() {
expect(test.selectize.getItem(0)).to.be.ok;
expect(test.selectize.getItem(0).length).to.be.equal(1);
expect(test.selectize.getItem(1)).to.be.ok;
expect(test.selectize.getItem(1).length).to.be.equal(1);
});
it('should allow values with quotation marks', function() {
expect(test.selectize.getItem('\'')).to.be.ok;
expect(test.selectize.getItem('\'').length).to.be.equal(1);
expect(test.selectize.getItem('"')).to.be.ok;
expect(test.selectize.getItem('"').length).to.be.equal(1);
});
it('should allow values with backslashes', function() {
expect(test.selectize.getItem('\\\'')).to.be.ok;
expect(test.selectize.getItem('\\\'').length).to.be.equal(1);
expect(test.selectize.getItem('\\"')).to.be.ok;
expect(test.selectize.getItem('\\"').length).to.be.equal(1);
});
it('should not allow undefined / null values', function() {
expect(test.selectize.getItem(null)).to.be.ok;
expect(test.selectize.getItem(null).length).to.be.equal(0);
expect(test.selectize.getItem(undefined)).to.be.ok;
expect(test.selectize.getItem(undefined).length).to.be.equal(0);
});
});
describe('clear()', function() {
var test;
beforeEach(function() {
test = setup_test('<select multiple>', {
valueField: 'value',
labelField: 'value',
options: [
{value: 0},
{value: 1},
{value: 2},
{value: 3},
],
items: ['1','2','3']
});
});
it('should empty "$activeItems" array', function() {
test.selectize.setActiveItem(test.selectize.getItem('1'));
expect(test.selectize.$activeItems.length).to.be.equal(1);
test.selectize.clear();
expect(test.selectize.$activeItems.length).to.be.equal(0);
});
it('should refresh option list (dropdown)', function(done) {
// test = setup_test('<select multiple>', {
// valueField: 'value',
// labelField: 'value',
// options: [
// {value: 0},
// {value: 1},
// {value: 2},
// {value: 3},
// ],
// items: ['1','2','3']
// });
test.selectize.focus();
window.setTimeout(function() {
test.selectize.clear();
test.selectize.focus();
window.setTimeout(function() {
expect(test.selectize.$dropdown_content.find('[data-value=1]').length).to.be.equal(1);
expect(test.selectize.$dropdown_content.find('[data-value=2]').length).to.be.equal(1);
expect(test.selectize.$dropdown_content.find('[data-value=3]').length).to.be.equal(1);
done();
}, 0);
}, 0);
});
it('should empty "items" array', function() {
test.selectize.clear();
expect(test.selectize.items.length).to.be.equal(0);
});
it('should update DOM', function() {
test.selectize.clear();
expect(test.selectize.$control.find('[data-value=1]').length).to.be.equal(0);
expect(test.selectize.$control.find('[data-value=2]').length).to.be.equal(0);
expect(test.selectize.$control.find('[data-value=3]').length).to.be.equal(0);
});
it('should not fire "change" if silent is truthy', function(done) {
var watcher = function(e) { throw new Error('Change fired'); };
test.$select.on('change', watcher);
test.selectize.clear(true);
window.setTimeout(function() {
test.$select.off('change', watcher);
done();
}, 0);
});
it('should not give control focus', function(done) {
test.selectize.clear();
window.setTimeout(function() {
expect(test.selectize.isFocused).to.be.equal(false);
done();
}, 0);
});
it('should empty "items" array', function() {
test.selectize.clear();
expect(test.selectize.items.length).to.be.equal(0);
});
});
describe('search()', function() {
it('should throw error if "score" setting does not return a function', function() {
var test;
expect(function() {
test = setup_test('<select multiple>', {
valueField: 'value',
labelField: 'value',
options: [
{value: 0},
{value: 1}
],
score: function() { }
});
test.selectize.search('hello');
}).to.throw(Error);
});
it('should not throw error if "score" setting does return a function', function() {
var test;
expect(function() {
test = setup_test('<select multiple>', {
valueField: 'value',
labelField: 'value',
options: [
{value: 0},
{value: 1}
],
score: function(query) {
return function(item) { return 0; };
}
});
test.selectize.search('hello');
}).to.not.throw(Error);
});
});
describe('getScoreFunction()', function() {
it('should return an function that returns a number', function() {
var test = setup_test('<select multiple>', {
valueField: 'value',
labelField: 'value',
searchField: 'value',
options: []
});
var fn = test.selectize.getScoreFunction('test');
expect(typeof fn).to.be.equal('function');
expect(typeof fn({value: 'test'})).to.be.equal('number');
expect(fn({value: 'test'})).to.be.above(0);
});
});
describe('destroy()', function() {
var has_namespaced_event = function($el, ns) {
var i, n, key;
var data = ($._data || $.data).apply($, [$(window)[0], 'events']);
ns = ns.replace(/^./, '');
for (key in data) {
if (data.hasOwnProperty(key)) {
for (i = 0, n = data[key].length; i < n; i++) {
if (data[key][i].namespace.indexOf(ns) !== -1) {
return true;
}
}
}
}
return false;
};
it('should remove control from DOM', function() {
var test = setup_test('<select>', {});
test.selectize.destroy();
expect($.contains(document.documentElement, test.selectize.$wrapper[0])).to.be.equal(false);
});
it('should delete "selectize" reference on original input element', function() {
var test = setup_test('<select>', {});
test.selectize.destroy();
expect(test.selectize.$input[0].selectize).to.be.equal(undefined);
});
it('should unbind events on window', function() {
var test = setup_test('<select>', {});
test.selectize.destroy();
expect(has_namespaced_event($(window), test.selectize.eventNS)).to.be.equal(false);
});
it('should unbind events on document', function() {
var test = setup_test('<select>', {});
test.selectize.destroy();
expect(has_namespaced_event($(document), test.selectize.eventNS)).to.be.equal(false);
});
it('should unbind events on <body>', function() {
var test = setup_test('<select>', {});
test.selectize.destroy();
expect(has_namespaced_event($('body'), test.selectize.eventNS)).to.be.equal(false);
});
it('should restore original options and tabindex', function() {
var children = '<optgroup label="Swedish Cars">' +
'<option value="volvo">Volvo</option>' +
'<option value="saab">Saab</option>' +
'</optgroup>' +
'<optgroup label="German Cars">' +
'<option value="mercedes">Mercedes</option>' +
'<option value="audi">Audi</option>' +
'</optgroup>';
var test = setup_test('<select tabindex="9999">' + children + '</select>', {});
test.selectize.destroy();
expect(test.$select.html()).to.be.equal(children);
expect(test.$select.attr('tabindex')).to.be.equal('9999');
});
it('should remove tabindex if it was originally undefined', function() {
var test = setup_test('<select>', {});
test.selectize.destroy();
expect(test.$select.attr('tabindex')).to.be.equal(undefined);
});
});
describe('clearCache()', function() {
var test;
before(function() {
test = setup_test('<select multiple>', {
valueField: 'value',
labelField: 'value',
options: [
{value: 0},
{value: 1},
{value: 2},
{value: 3},
],
items: ['1','2','3']
});
test.selectize.advanceSelection(1);
test.selectize.refreshOptions(true);
test.selectize.refreshItems();
});
it('should clear the whole renderCache', function () {
expect($.isEmptyObject(test.selectize.renderCache)).to.be.equal(false);
test.selectize.clearCache();
expect($.isEmptyObject(test.selectize.renderCache)).to.be.equal(true);
});
it('should allow clearing just one template type from the renderCache', function () {
test.selectize.render('item', test.selectize.options[0]);
test.selectize.refreshOptions();
expect($.isEmptyObject(test.selectize.renderCache['option'])).to.be.equal(false);
expect($.isEmptyObject(test.selectize.renderCache['item'])).to.be.equal(false);
test.selectize.clearCache('option');
expect($.isEmptyObject(test.selectize.renderCache['option'])).to.be.equal(true);
expect($.isEmptyObject(test.selectize.renderCache['item'])).to.be.equal(false);
});
});
});
})();

274
web/vendor/selectize.js/test/events.js vendored Normal file
View file

@ -0,0 +1,274 @@
describe('Events', function() {
describe('focus', function() {
it('should work as expected', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b"></option><option value="c"></option></select>', {});
var counter = 0;
test.selectize.on('focus', function() { counter++; });
test.selectize.focus();
syn.click(test.selectize.$control).delay(0, function() {
window.setTimeout(function() {
expect(counter).to.be.equal(1);
done();
}, 0);
});
});
});
describe('blur', function() {
it('should work as expected', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b"></option><option value="c"></option></select>', {});
var counter = 0;
test.selectize.on('blur', function() { counter++; });
test.selectize.focus();
syn.click(test.selectize.$control).delay(0, function() {
syn.click($('body')).delay(0, function() {
window.setTimeout(function() {
expect(counter).to.be.equal(1);
done();
}, 0);
});
});
});
});
describe('change', function() {
it('should be triggered once', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b"></option><option value="c"></option></select>', {});
var counter = 0;
test.selectize.on('change', function() { counter++; });
test.selectize.setValue('b');
window.setTimeout(function() {
expect(counter).to.be.equal(1);
done();
}, 0);
});
it('should contain current value', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b"></option><option value="c"></option></select>', {});
test.selectize.on('change', function(value) {
expect(value).to.be.equal('c');
done();
});
test.selectize.setValue('c');
});
it('should not be triggered when the selected item has not changed', function(done) {
var test = setup_test('<select><option value="a" selected="selected">a</option></select>');
var counter = 0;
test.$select.on('change', function() { counter++; });
syn.click(test.selectize.$control).delay(0, function() {
syn
.click($('[data-value="a"]', test.selectize.$dropdown))
.delay(0, function() {
expect(counter).to.be.equal(0);
done();
});
});
});
});
describe('item_add', function() {
it('should be triggered', function(done) {
var test = setup_test('<select><option value="a"></option><option value="b"></option><option value="c"></option></select>', {});
test.selectize.on('item_add', function() {
done();
});
test.selectize.addItem('b');
});
it('should contain item\'s value and element', function(done) {
var test = setup_test('<select><option value="a"></option><option value="b"></option><option value="c"></option></select>', {});
test.selectize.on('item_add', function(value, $item) {
expect(value).to.be.equal('b');
assert.equal($item.length, 1);
done();
});
test.selectize.addItem('b');
});
});
describe('item_remove', function() {
it('should be triggered', function(done) {
var test = setup_test('<select multiple><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('item_remove', function() {
done();
});
test.selectize.removeItem('a');
});
it('should contain item\'s value and element', function(done) {
var test = setup_test('<select multiple><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('item_remove', function(value, $item) {
expect(value).to.be.equal('b');
assert.equal($item.length, 1);
done();
});
test.selectize.removeItem('b');
});
});
describe('clear', function() {
it('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('clear', function() {
done();
});
test.selectize.clear();
});
});
describe('optgroup_add', function() {
it('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('optgroup_add', function() { done(); });
test.selectize.addOptionGroup('id', {label: 'Group'});
});
it('should contain optgroup id', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('optgroup_add', function(id, data) {
expect(id).to.be.equal('id');
done();
});
test.selectize.addOptionGroup('id', {label: 'Group'});
});
it('should contain outgroup data', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
var optgroup = {label: 'Group'};
test.selectize.on('optgroup_add', function(id, data) {
expect(data).to.eql(optgroup);
done();
});
test.selectize.addOptionGroup('id', optgroup);
});
});
describe('optgroup_remove', function() {
it('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('optgroup_remove', function(id) {
expect(id).to.be.equal('id');
done();
});
test.selectize.addOptionGroup('id', {label: 'Group'});
test.selectize.removeOptionGroup('id');
});
});
describe('optgroup_clear', function() {
it('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('optgroup_clear', function() {
done();
});
test.selectize.addOptionGroup('id', {label: 'Group'});
test.selectize.clearOptionGroups();
});
});
describe('option_add', function() {
it('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('option_add', function() {
done();
});
test.selectize.addOption({value: 'e'});
});
it('should contain option value', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('option_add', function(value, data) {
expect(value).to.be.equal('e');
done();
});
test.selectize.addOption({value: 'e'});
});
it('should contain option data', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
var option = {value: 'e'};
test.selectize.on('option_add', function(value, data) {
expect(option).to.eql(data);
done();
});
test.selectize.addOption(option);
});
});
describe('option_remove', function() {
it('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('option_remove', function() {
done();
});
test.selectize.removeOption('a');
});
it('should contain option value', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('option_remove', function(value) {
expect(value).to.be.equal('a');
done();
});
test.selectize.removeOption('a');
});
});
describe('option_clear', function() {
it('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('option_clear', function() {
done();
});
test.selectize.clearOptions();
});
});
describe('dropdown_open', function() {
it('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('dropdown_open', function() {
done();
});
test.selectize.open();
});
});
describe('dropdown_close', function() {
it('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('dropdown_close', function() {
done();
});
test.selectize.open();
test.selectize.close();
});
});
describe('destroy', function() {
it('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('destroy', function() {
done();
});
test.selectize.destroy();
});
});
describe('type', function() {
it('should be triggered', function(done) {
var test = setup_test('<select></select>', {create: true});
test.selectize.on('type', function() {
done();
});
syn.click(test.selectize.$control).type('a', test.selectize.$control_input);
});
it('should contain current value', function(done) {
var test = setup_test('<select></select>', {create: true});
test.selectize.on('type', function(value) {
expect(value).to.be.equal('a');
done();
});
syn.click(test.selectize.$control).type('a', test.selectize.$control_input);
});
});
});

View file

@ -0,0 +1,64 @@
describe('DOM Events', function() {
describe('"change"', function() {
it('should be triggered once by addItem()', function(done) {
var test = setup_test('<select>', {
valueField: 'value',
labelField: 'value',
options: [
{value: 'a'},
{value: 'b'},
],
items: ['a']
});
var counter = 0;
test.$select.on('change', function() { counter++; });
test.selectize.addItem('b');
window.setTimeout(function() {
expect(counter).to.be.equal(1);
done();
}, 0);
});
it('should be triggered once by removeItem()', function(done) {
var test = setup_test('<select multiple>', {
valueField: 'value',
labelField: 'value',
options: [
{value: 'a'},
{value: 'b'},
],
items: ['a','b']
});
var counter = 0;
test.$select.on('change', function() { counter++; });
test.selectize.removeItem('b');
window.setTimeout(function() {
expect(counter).to.be.equal(1);
done();
}, 0);
});
it('should be triggered once by clear()', function(done) {
var test = setup_test('<select multiple>', {
valueField: 'value',
labelField: 'value',
options: [
{value: 'a'},
{value: 'b'},
],
items: ['a','b']
});
var counter = 0;
test.$select.on('change', function() { counter++; });
test.selectize.clear();
window.setTimeout(function() {
expect(counter).to.be.equal(1);
done();
}, 0);
});
});
});

View file

@ -0,0 +1,323 @@
(function() {
var click = function(el, cb) {
syn.click(el).delay(350, cb);
};
// These tests are functional simulations of
// user interaction, using syn.js. For more information:
//
// @see http://v3.javascriptmvc.com/docs.html#&who=syn
// @see http://bitovi.com/blog/2010/07/syn-a-standalone-synthetic-event-library.html
describe('Interaction', function() {
it('should keep dropdown open after selection made if closeAfterSelect: false', function(done) {
var test = setup_test('<select multiple>' +
'<option value="a">A</option>' +
'<option value="b">B</option>' +
'</select>', {});
click(test.selectize.$control, function() {
click($('[data-value=a]', test.selectize.$dropdown_content), function() {
expect(test.selectize.isOpen).to.be.equal(true);
expect(test.selectize.isFocused).to.be.equal(true);
done();
});
});
});
it('should close dropdown after selection made if closeAfterSelect: true', function(done) {
var test = setup_test('<select multiple>' +
'<option value="a">A</option>' +
'<option value="b">B</option>' +
'</select>', {closeAfterSelect: true});
click(test.selectize.$control, function() {
click($('[data-value=a]', test.selectize.$dropdown_content), function() {
expect(test.selectize.isOpen).to.be.equal(false);
expect(test.selectize.isFocused).to.be.equal(true);
done();
});
});
});
describe('clicking control', function() {
it('should give it focus', function(done) {
var test = setup_test('<select>' +
'<option value="a">A</option>' +
'<option value="b">B</option>' +
'</select>', {});
click(test.selectize.$control, function() {
expect(test.selectize.isFocused).to.be.equal(true);
done();
});
});
it('should start loading results if preload:"focus"', function(done) {
var calls_focus = 0;
var calls_load = 0;
var test = setup_test('<select>' +
'<option value="a">A</option>' +
'<option value="b">B</option>' +
'</select>', {
preload: 'focus',
load: function(query, done) {
calls_load++;
assert.equal(query, '');
setTimeout(function() {
done([{value: 'c', text: 'C'}]);
});
}
});
test.selectize.on('focus', function() {
calls_focus++;
});
click(test.selectize.$control, function() {
setTimeout(function() {
assert.equal(calls_focus, 1);
assert.equal(calls_load, 1);
done();
}, 300);
});
});
it('should open dropdown menu', function(done) {
var test = setup_test('<select>' +
'<option value="a">A</option>' +
'<option value="b">B</option>' +
'</select>', {});
click(test.selectize.$control, function() {
expect(test.selectize.isOpen).to.be.equal(true);
expect(test.selectize.$dropdown.is(':visible')).to.be.equal(true);
done();
});
});
});
describe('clicking option', function() {
it('should select it', function(done) {
var test = setup_test('<select>' +
'<option value="">Select an option...</option>' +
'<option value="a">A</option>' +
'<option value="b">B</option>' +
'</select>', {});
click(test.selectize.$control, function() {
click($('[data-value="b"]', test.selectize.$dropdown), function() {
expect(test.selectize.$input.val()).to.be.equal('b');
expect(test.selectize.$input.text()).to.be.equal('B');
done();
});
});
});
it('should close dropdown', function(done) {
var test = setup_test('<select>' +
'<option value="">Select an option...</option>' +
'<option value="a">A</option>' +
'<option value="b">B</option>' +
'</select>', {});
click(test.selectize.$control, function() {
click($('[data-value="b"]', test.selectize.$dropdown), function() {
expect(test.selectize.isOpen).to.be.equal(false);
expect(test.selectize.$dropdown.is(':visible')).to.be.equal(false);
done();
});
});
});
});
describe('typing in input', function() {
it('should filter results', function(done) {
var test = setup_test('<select>' +
'<option value="">Select an option...</option>' +
'<option value="a">A</option>' +
'<option value="b">B</option>' +
'</select>', {});
click(test.selectize.$control, function() {
syn.type('a', test.selectize.$control_input)
.delay(0, function() {
expect($('[data-value="a"]', test.selectize.$dropdown).length).to.be.equal(1);
expect($('[data-value="b"]', test.selectize.$dropdown).length).to.be.equal(0);
done();
});
});
});
it('should hide dropdown if no results present', function(done) {
var test = setup_test('<select>' +
'<option value="">Select an option...</option>' +
'<option value="a">A</option>' +
'<option value="b">B</option>' +
'</select>', {});
click(test.selectize.$control, function() {
syn.type('awaw', test.selectize.$control_input)
.delay(0, function() {
expect(test.selectize.isOpen).to.be.equal(false);
expect(test.selectize.$dropdown.is(':visible')).to.be.equal(false);
done();
});
});
});
it('should not hide dropdown if "create" option enabled and no results present', function(done) {
var test = setup_test('<select>' +
'<option value="">Select an option...</option>' +
'<option value="a">A</option>' +
'<option value="b">B</option>' +
'</select>', {create: true});
click(test.selectize.$control, function() {
syn.type('awaw', test.selectize.$control_input)
.delay(0, function() {
expect(test.selectize.isOpen).to.be.equal(true);
expect(test.selectize.$dropdown.is(':visible')).to.be.equal(true);
done();
});
});
});
it('should restore dropdown visibility when backing out of a query without results (backspace)', function(done) {
var test = setup_test('<select>' +
'<option value="">Select an option...</option>' +
'<option value="a">A</option>' +
'<option value="b">B</option>' +
'</select>', {});
click(test.selectize.$control, function() {
syn.type('awf', test.selectize.$control_input)
.type('\b\b\b', test.selectize.$control_input)
.delay(0, function() {
expect(test.selectize.isOpen).to.be.equal(true);
expect(test.selectize.$dropdown.is(':visible')).to.be.equal(true);
done();
});
});
});
it('should move caret when [left] or [right] pressed', function(done) {
var test = setup_test('<input type="text" value="a,b,c,d">', {create: true});
click(test.selectize.$control, function() {
syn.type('[left][left]whatt', test.selectize.$control_input)
.delay(0, function() {
expect(test.selectize.caretPos).to.be.equal(2);
done();
});
});
});
it('should not create input if comma entered in single select mode', function(done) {
var test = setup_test('<select>' +
'<option value="">Select an option...</option>' +
'<option value="a">A</option>' +
'<option value="b">B</option>' +
'</select>', {create: true});
click(test.selectize.$control, function() {
syn.type('asdf,asdf', test.selectize.$control_input)
.delay(0, function() {
expect(test.selectize.isOpen).to.be.equal(true);
expect(test.selectize.options).to.not.have.property('asdf');
done();
});
});
});
});
describe('blurring the input', function() {
it('should close dropdown when createOnBlur is true', function(done) {
var test = setup_test('<select multiple="multiple">' +
'<option></option>' +
'<option value="a">A</option>' +
'<option value="b">B</option>' +
'</select>', {
createOnBlur: true,
create: function(value){
return {
value: value,
text: value
};
}
});
click(test.selectize.$control, function() {
syn
.type('fooo', test.selectize.$control_input)
.delay(0, function() {
expect(test.selectize.isOpen).to.be.equal(true);
expect(test.selectize.$dropdown.is(':visible')).to.be.equal(true);
syn
.click($("body"))
.delay(5, function() {
expect(test.selectize.isOpen).to.be.equal(false);
expect(test.selectize.$dropdown.is(':visible')).to.be.equal(false);
done();
});
});
});
});
});
describe('filtering created items', function() {
function createFilterTest(createFilter) {
return setup_test('<select multiple="multiple"></select>', {create: true, createFilter: createFilter});
}
var text = 'abc';
function execFilterTest(test, done, expectation) {
var selectize = test.selectize;
click(selectize.$control, function() {
syn
.type(text, selectize.$control_input)
.type(selectize.settings.delimiter, selectize.$control_input)
.delay(0, function() {
expectation(selectize);
done();
})
});
}
function execFilterTests(heading, filters, expectation) {
for (var i = 0; i < filters.length; i++) {
(function(filter) {
it(heading, function(done) {
execFilterTest(createFilterTest(filter), done, expectation);
});
})(filters[i]);
}
}
execFilterTests('should add an item normally if there is no createFilter', [undefined, null, ''], function(selectize) {
expect(selectize.getItem(text).length).to.be.equal(1);
});
execFilterTests('should add an item if the input matches the createFilter', ['a', /a/, function() { return true; }], function(selectize) {
expect(selectize.getItem(text).length).to.be.equal(1);
});
execFilterTests('should not add an item or display the create label if the input does not match the createFilter', ['foo', /foo/, function() { return false; }], function(selectize) {
expect(selectize.getItem(text).length).to.be.equal(0);
expect($(selectize.$dropdown_content).filter('.create').length).to.be.equal(0);
});
});
});
})();

283
web/vendor/selectize.js/test/setup.js vendored Normal file
View file

@ -0,0 +1,283 @@
(function() {
describe('Setup', function() {
it('should not allow duplicate initialization', function() {
var instance_before, instance_after, test;
test = setup_test('<input type="text">', {});
instance_before = test.$select[0].selectize;
test.$select.selectize();
instance_after = test.$select[0].selectize;
expect(instance_before).to.be.equal(instance_after);
});
describe('<input type="text">', function() {
it('should complete without exceptions', function() {
var test = setup_test('<input type="text">', {});
});
it('should populate items,options from "dataAttr" if available', function() {
var data = [{val: 'a', lbl: 'Hello'}, {val: 'b', lbl: 'World'}];
var test = setup_test('<input type="text" value="c,d,e" data-hydrate="' + JSON.stringify(data).replace(/"/g,'&quot;') + '">', {
dataAttr: 'data-hydrate',
valueField: 'val',
labelField: 'lbl'
});
expect(test.selectize.getValue()).to.be.equal('a,b');
assert.deepEqual(test.selectize.items, ['a','b']);
assert.deepEqual(test.selectize.options, {
'a': {val: 'a', lbl: 'Hello', $order: 1},
'b': {val: 'b', lbl: 'World', $order: 2}
});
});
describe('getValue()', function() {
it('should return value as a string', function() {
var test = setup_test('<input type="text" value="a,b">', {delimiter: ','});
expect(test.selectize.getValue()).to.be.a('string');
});
it('should return "" when empty', function() {
var test = setup_test('<input type="text" value="">', {delimiter: ','});
expect(test.selectize.getValue()).to.be.equal('');
});
it('should return proper value when not empty', function() {
var test = setup_test('<input type="text" value="a,b">', {delimiter: ','});
expect(test.selectize.getValue()).to.be.equal('a,b');
});
});
describe('<input type="text" attributes>', function() {
it('should propagate original input attributes to the generated input', function() {
var test = setup_test('<input type="text" autocorrect="off" autocapitalize="none">', {});
expect(test.selectize.$control_input.attr('autocorrect')).to.be.equal('off');
expect(test.selectize.$control_input.attr('autocapitalize')).to.be.equal('none');
});
it('should not add attributes if not present in the original', function() {
var test = setup_test('<input type="text">', {});
expect(test.selectize.$control_input.attr('autocorrect')).to.be.equal(undefined);
expect(test.selectize.$control_input.attr('autocapitalize')).to.be.equal(undefined);
});
});
});
describe('<select>', function() {
it('should complete without exceptions', function() {
var test = setup_test('<select></select>', {});
});
it('should allow for values optgroups with duplicated options', function() {
var test = setup_test(['<select>',
'<optgroup label="Group 1">',
'<option value="a">Item A</option>',
'<option value="b">Item B</option>',
'</optgroup>',
'<optgroup label="Group 2">',
'<option value="a">Item A</option>',
'<option value="b">Item B</option>',
'</optgroup>',
'</select>'].join(''), {
optgroupValueField: 'val',
optgroupField: 'grp'
});
assert.deepEqual(test.selectize.options, {
'a': {text: 'Item A', value: 'a', grp: ['Group 1', 'Group 2'], $order: 1},
'b': {text: 'Item B', value: 'b', grp: ['Group 1', 'Group 2'], $order: 2}
});
assert.deepEqual(test.selectize.optgroups, {
'Group 1': {label: 'Group 1', val: 'Group 1', $order: 3},
'Group 2': {label: 'Group 2', val: 'Group 2', $order: 4}
});
});
it('should add options in text form (no html entities)', function() {
var test = setup_test('<select><option selected value="a">&lt;hi&gt;</option></select>', {});
expect(test.selectize.options['a'].text).to.be.equal('<hi>');
});
it('should keep options in original order if no sort given', function(done) {
var test = setup_test([
'<select multiple>',
'<option value="">Select a state...</option>',
'<option value="AL">Alabama</option>',
'<option value="AK">Alaska</option>',
'<option value="AZ">Arizona</option>',
'<option value="AR">Arkansas</option>',
'<option value="CA" selected>California</option>',
'<option value="CO">Colorado</option>',
'<option value="CT">Connecticut</option>',
'<option value="DE">Delaware</option>',
'<option value="DC">District of Columbia</option>',
'<option value="FL">Florida</option>',
'<option value="GA">Georgia</option>',
'<option value="HI">Hawaii</option>',
'<option value="ID">Idaho</option>',
'<option value="IL">Illinois</option>',
'<option value="IN">Indiana</option>',
'<option value="IA">Iowa</option>',
'<option value="KS">Kansas</option>',
'<option value="KY">Kentucky</option>',
'<option value="LA">Louisiana</option>',
'<option value="ME">Maine</option>',
'<option value="MD">Maryland</option>',
'<option value="MA">Massachusetts</option>',
'<option value="MI">Michigan</option>',
'<option value="MN">Minnesota</option>',
'<option value="MS">Mississippi</option>',
'<option value="MO">Missouri</option>',
'<option value="MT">Montana</option>',
'<option value="NE">Nebraska</option>',
'<option value="NV">Nevada</option>',
'<option value="NH">New Hampshire</option>',
'<option value="NJ">New Jersey</option>',
'<option value="NM">New Mexico</option>',
'<option value="NY">New York</option>',
'<option value="NC">North Carolina</option>',
'<option value="ND">North Dakota</option>',
'<option value="OH">Ohio</option>',
'<option value="OK">Oklahoma</option>',
'<option value="OR">Oregon</option>',
'<option value="PA">Pennsylvania</option>',
'<option value="RI">Rhode Island</option>',
'<option value="SC">South Carolina</option>',
'<option value="SD">South Dakota</option>',
'<option value="TN">Tennessee</option>',
'<option value="TX">Texas</option>',
'<option value="UT">Utah</option>',
'<option value="VT">Vermont</option>',
'<option value="VA">Virginia</option>',
'<option value="WA">Washington</option>',
'<option value="WV">West Virginia</option>',
'<option value="WI">Wisconsin</option>',
'<option value="01">01</option>',
'<option value="10">10</option>',
'<option value="WY" selected>Wyoming</option>',
'</select>'
].join(), {});
var order_expected = ['AL','AK','AZ','AR','CO','CT','DE','DC','FL','GA','HI','ID','IL','IN','IA','KS','KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','01','10'];
var order_actual = [];
test.selectize.refreshOptions(true);
window.setTimeout(function() {
test.selectize.$dropdown.find('[data-value]').each(function(i, el) {
order_actual.push($(el).attr('data-value'));
});
expect(order_actual).to.eql(order_expected);
done();
}, 0);
});
describe('getValue()', function() {
it('should return "" when empty', function() {
var test = setup_test('<select>', {});
expect(test.selectize.getValue()).to.be.equal('');
});
it('should return proper value when not empty', function() {
var test = setup_test('<select><option selected value="a">A</option></select>', {});
expect(test.selectize.getValue()).to.be.equal('a');
});
});
});
describe('<select multiple>', function() {
it('should complete without exceptions', function() {
var test = setup_test('<select>', {});
});
describe('getValue()', function() {
it('should return [] when empty', function() {
var test = setup_test('<select multiple>', {});
expect(test.selectize.getValue()).to.deep.equal([]);
});
it('should return proper value as array when not empty', function() {
var test = setup_test('<select multiple><option selected value="a">A</option></select>', {});
expect(test.selectize.getValue()).to.deep.equal(['a']);
});
});
});
describe('<select disabled>', function() {
var test;
before(function() {
test = setup_test('<select disabled>', {});
});
it('should have "disabled" class', function() {
expect(test.selectize.$control.hasClass('disabled')).to.be.equal(true);
});
it('should have isDisabled property set to true', function() {
expect(test.selectize.isDisabled).to.be.equal(true);
});
});
describe('<select required>', function(){
var $form, $button, test;
beforeEach(function() {
test = setup_test('<select required>' +
'<option value="">Select an option...</option>' +
'<option value="a">A</option>' +
'</select>', {});
$form = test.$select.parents('form');
$button = $('<button type="submit">').appendTo($form);
});
afterEach(function() {
$form.off('.test_required');
$button.remove();
});
it('should have isRequired property set to true', function() {
expect(test.selectize.isRequired).to.be.equal(true);
});
it('should have the required class', function() {
expect(test.selectize.$control.hasClass('required')).to.be.equal(true);
});
if ($.fn.selectize.support.validity) {
it('should have "invalid" class when validation fails', function(done) {
test.$select[0].checkValidity();
window.setTimeout(function() {
expect(test.selectize.$control.hasClass('invalid')).to.be.equal(true);
done();
}, 250);
});
it('should clear the invalid class after an item is selected', function(done) {
syn.click($button).delay(0, function() {
test.selectize.addItem('a');
expect(test.selectize.$control.hasClass('invalid')).to.be.equal(false);
done();
});
});
it('should pass validation if an element is selected', function(done) {
test.selectize.addItem('a');
$form.one('submit.test_required', function(e) {
done();
});
syn.click($button);
});
}
});
describe('<select> (not required)', function(){
var $form, $button, test;
beforeEach(function() {
test = setup_test('<select>' +
'<option value="">Select an option...</option>' +
'<option value="a">A</option>' +
'</select>', {});
$form = test.$select.parents('form');
$button = $('<button type="submit">').appendTo($form);
});
afterEach(function() {
$form.off('.test_required');
$button.remove();
});
it('should have isRequired property set to false', function() {
expect(test.selectize.isRequired).to.be.equal(false);
});
it('should not have the required class', function() {
expect(test.selectize.$control.hasClass('required')).to.be.equal(false);
});
});
});
})();

View file

@ -0,0 +1,35 @@
window.expect = chai.expect;
window.assert = chai.assert;
window.has_focus = function(elem) {
return !!(elem === document.activeElement);
};
var sandbox = document.createElement('form');
document.body.appendChild(sandbox);
window.setup_test = function(html, options, callback) {
if (window.test_last) window.test_last.teardown();
var $select = $(html).appendTo(sandbox).selectize(options);
var instance = $select[0].selectize;
var test = window.test_last = {
$select: $select,
callback: callback,
selectize: instance,
teardown: function() {
instance.destroy();
$select.remove();
window.test_last = null;
}
};
return test;
};
after(function() {
if (window.test_last) {
window.test_last.teardown();
}
});
$(sandbox).on('submit', function(e) { e.preventDefault(); });

File diff suppressed because it is too large Load diff

61
web/vendor/selectize.js/test/xss.js vendored Normal file
View file

@ -0,0 +1,61 @@
(function() {
window.setup_xss_test = function(html, options, done) {
window.xss = function() {
window.clearTimeout(timeout);
complete(new Error('Exploit executed'));
};
var test = setup_test(html, options);
var complete = function(err) {
window.xss = function() {};
done(err);
};
var timeout = window.setTimeout(complete, 75);
return test;
};
describe('XSS', function() {
describe('Raw HTML in original input value', function() {
it('should not trigger exploit', function(done) {
setup_xss_test('<input type="text" value="&lt;img src=&quot;x&quot; onerror=&quot;xss()&quot;&gt;">', {}, done);
});
});
describe('Raw HTML in optgroup label', function() {
it('should not trigger exploit', function(done) {
var test = setup_xss_test('<select><optgroup label="&lt;img src=&quot;x&quot; onerror=&quot;xss()&quot;&gt;"><option>Test</option></optgroup></select>', {}, done);
test.selectize.refreshOptions();
test.selectize.open();
});
});
describe('Raw HTML in option label should not trigger exploit', function() {
it('should not trigger exploit', function(done) {
setup_xss_test('<input type="text" value="">', {
options: [
{value: '1', label: '<img src="x" onerror="xss()">'}
],
items: ['1'],
labelField: 'label',
valueField: 'value'
}, done);
});
});
describe('Raw HTML in option value should not trigger exploit', function() {
it('should not trigger exploit', function(done) {
setup_xss_test('<input type="text" value="">', {
options: [
{value: '<img src="x" onerror="xss()">', label: '1'}
],
items: ['<img src="x" onerror="xss()">'],
labelField: 'label',
valueField: 'value'
}, done);
});
});
});
})();