describe('Setup', function() { it_n('should not allow duplicate initialization', function() { let test = setup_test('', {}); let errors = 0 ; try { tomSelect(test.select); } catch (error) { errors++; } expect(errors).to.be.equal(1); }); describe('', function() { it_n('should complete without exceptions', function() { setup_test(''); }); it_n('should complete without exceptions - id with quotes', function() { setup_test(''); }); it_n('should populate items,options from "dataAttr" if available', function() { var data = [{val: 'a', lbl: 'Hello'}, {val: 'b', lbl: 'World'}]; var test = setup_test('', { dataAttr: 'data-hydrate', valueField: 'val', labelField: 'lbl' }); expect(test.instance.getValue()).to.be.equal('a,b'); assert.deepEqual(test.instance.items, ['a','b']); assert.equal( test.instance.options['a'].val,'a'); assert.equal( test.instance.options['a'].lbl,'Hello'); assert.equal( test.instance.options['a'].$order,1); assert.equal( test.instance.options['b'].val,'b'); assert.equal( test.instance.options['b'].lbl,'World'); assert.equal( test.instance.options['b'].$order,2); }); it_n('should populate options from data attributes',function(){ var test = setup_test(''); var option = test.instance.input.querySelector('option[value="a"]'); assert.equal( test.instance.options['a'].value,'a'); assert.equal( test.instance.options['a'].test,'b'); assert.equal( test.instance.options['a'].text,'c'); assert.equal( test.instance.options['a'].$order,1); assert.equal( test.instance.options['a'].disabled,false); assert.equal( test.instance.options['a'].optgroup,undefined); assert.equal( test.instance.options['a'].$option,option); }); it_n('should treat bool values as integers', function() { var test = setup_test('', { options:[{value: true, label: 'Hello'}, {value: false, label: 'World'}] }); assert.equal( test.instance.options[1].value,true); assert.equal( test.instance.options[1].label,'Hello'); assert.equal( test.instance.options[1].$order,1); assert.equal( test.instance.options[0].value,false); assert.equal( test.instance.options[0].label,'World'); assert.equal( test.instance.options[0].$order,2); }); describe('getValue()', function() { it_n('should return value as a string', function() { var test = setup_test('', {delimiter: ','}); expect(test.instance.getValue()).to.be.a('string'); }); it_n('should return "" when empty', function() { var test = setup_test('', {delimiter: ','}); expect(test.instance.getValue()).to.be.equal(''); }); it_n('should return proper value when not empty', function() { var test = setup_test('', {delimiter: ','}); expect(test.instance.getValue()).to.be.equal('a,b'); }); }); describe('', function() { it_n('should propagate original input attributes to the generated input', function() { var test = setup_test('', {}); expect(test.instance.control_input.getAttribute('autocorrect')).to.be.equal('off'); expect(test.instance.control_input.getAttribute('autocapitalize')).to.be.equal('none'); expect(test.instance.control_input.getAttribute('autocomplete')).to.be.equal('new-password'); }); it_n('should not add attributes if not present in the original', function() { var test = setup_test('', {}); expect(test.instance.control_input.getAttribute('autocorrect')).to.be.equal(null); expect(test.instance.control_input.getAttribute('autocapitalize')).to.be.equal(null); expect(test.instance.control_input.getAttribute('autocomplete')).to.be.equal('off'); }); }); }); describe('', function() { it_n('should complete without exceptions', function(done) { var test = setup_test('', {}); assert.equal(test.instance.control_input.getAttribute('type'), 'number'); done(); }); }); describe('', {}); }); it_n('should allow for values optgroups with duplicated options', function() { var test = setup_test([''].join(''), { optgroupValueField: 'val', optgroupField: 'grp', disabledField: 'dis' }); assert.equal( test.instance.options['a'].text,'Item A'); assert.equal( test.instance.options['a'].value,'a'); assert.deepEqual( test.instance.options['a'].grp,['Group 1', 'Group 2']); assert.equal( test.instance.options['a'].$order,1); assert.equal( test.instance.options['a'].dis,false); assert.equal( test.instance.options['b'].text,'Item B'); assert.equal( test.instance.options['b'].value,'b'); assert.deepEqual( test.instance.options['b'].grp,['Group 1', 'Group 2']); assert.equal( test.instance.options['b'].$order,2); assert.equal( test.instance.options['b'].dis,false); assert.deepEqual(test.instance.optgroups, { 'Group 1': {label: 'Group 1', val: 'Group 1', $order: 3, dis: false}, 'Group 2': {label: 'Group 2', val: 'Group 2', $order: 4, dis: false} }, '2'); }); it_n('should respect disabled flags of option and optgroup', function() { var test = setup_test([''].join(''), { optgroupValueField: 'val', optgroupField: 'grp', disabledField: 'dis' }); assert.equal( test.instance.options['a'].text,'Item A'); assert.equal( test.instance.options['a'].value,'a'); assert.deepEqual( test.instance.options['a'].grp,['Group 1', 'Group 2']); assert.equal( test.instance.options['a'].$order,1); assert.equal( test.instance.options['a'].dis,true); assert.equal( test.instance.options['b'].text,'Item B'); assert.equal( test.instance.options['b'].value,'b'); assert.deepEqual( test.instance.options['b'].grp,['Group 1', 'Group 2']); assert.equal( test.instance.options['b'].$order,2); assert.equal( test.instance.options['b'].dis,false); assert.deepEqual(test.instance.optgroups, { 'Group 1': {label: 'Group 1', val: 'Group 1', $order: 3, dis: false}, 'Group 2': {label: 'Group 2', val: 'Group 2', $order: 4, dis: true} }, '2'); }); it_n('should render optgroups with duplicated options correctly', function(done) { var test = setup_test([''].join(''), {}); test.instance.refreshOptions(true); assert.equal(test.instance.dropdown_content.querySelectorAll('.optgroup').length, 2, 'expect 2 optgroups'); assert.equal(test.instance.dropdown_content.querySelectorAll('.option').length, 4, 'expect 4 options'); done(); }); it_n('display non-optgroup items and optgroups with lockOptgroupOrder = true', function(done) { var test = setup_test(``,{lockOptgroupOrder:true}); test.instance.refreshOptions(true); assert.equal(test.instance.dropdown_content.querySelectorAll('.optgroup').length, 2, 'expect 2 optgroups'); assert.equal(test.instance.dropdown_content.querySelectorAll('.option').length, 5, 'expect 5 options'); done(); }); it_n('optgroup without label', function(done) { var test = setup_test(``); test.instance.refreshOptions(true); assert.equal(test.instance.dropdown_content.querySelectorAll('.optgroup').length, 1, 'expect 1 optgroups'); assert.equal(test.instance.dropdown_content.querySelectorAll('.option').length, 1, 'expect 1 options'); done(); }); it_n('optgroups with the same label', function(done) { var test = setup_test(``); test.instance.refreshOptions(true); assert.equal(test.instance.dropdown_content.querySelectorAll('.optgroup').length, 2, 'expect 2 optgroups'); assert.equal(test.instance.dropdown_content.querySelectorAll('.option').length, 2, 'expect 2 options'); done(); }); it_n('should add options in text form (no html entities)', function() { var test = setup_test('', {}); expect(test.instance.options['a'].text).to.be.equal(''); }); it_n('should keep options in original order if no sort given', function(done) { var test = setup_test([ '' ].join(), {maxOptions:51}); 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.instance.refreshOptions(true); $(test.instance.dropdown).find('[data-value]').each(function(i, el) { order_actual.push($(el).attr('data-value')); }); expect(order_actual).to.eql(order_expected); done(); }); it_n('should respect option disabled flag', function (done) { var test = setup_test([''].join(''), {}); test.instance.refreshOptions(true); expect($(test.instance.dropdown).find('.option')).to.has.length(2); expect($(test.instance.dropdown).find('[data-selectable]')).to.has.length(1); done(); }); describe('getValue()', function() { it_n('should return "" when empty', function() { var test = setup_test('', {}); expect(test.instance.getValue()).to.be.equal('a'); }); }); }); describe('', {}); }); describe('getValue()', function() { it_n('should return [] when empty', function() { var test = setup_test('', {}); expect(test.instance.getValue()).to.deep.equal(['a']); }); }); }); describe('', {}); }); it_n('should have "disabled" class', function() { expect(test.instance.control.classList.contains('disabled')).to.be.equal(true); }); it_n('should have isDisabled property set to true', function() { expect(test.instance.isDisabled).to.be.equal(true); }); }); describe(' `, { render: { option: function(item, escape) { return '
' + escape(item.text) + '
' }, optgroup_header: null, } }); test.instance.focus(); window.setTimeout(function() { assert.equal( test.instance.dropdown.querySelectorAll('.custom-option').length, 2); done(); }, 5); }); }); describe('' + '' + '' + '', { render: { option: function(item, escape) { var div = document.createElement('div'); div.className = 'option custom-option'; div.innerHTML = escape(item.text); return div; } } }); }); it_n('should render the custom option element', function(done) { test.instance.focus(); window.setTimeout(function() { expect($(test.instance.dropdown_content).find('.custom-option').length).to.be.equal(1); done(); }, 5); }); }); describe('' + '' + '' + '', { render: { option: function(item, escape) { return $('
').text(item.text); } } }); }); it_n('should render the custom option element', function(done) { test.instance.focus(); window.setTimeout(function() { expect($(test.instance.dropdown_content).find('.custom-option').length).to.be.equal(1); done(); }, 5); }); }); describe('rtl detection', function() { it_n('should not be rtl', function() { var test = setup_test('', {}); expect(test.instance.rtl).to.be.equal(true); }); it_n('should detect parent rtl', function() { var test = setup_test('
', {}); expect(test.instance.rtl).to.be.equal(true); }); }); describe('external control input', function() { var test, control; before(()=>{ test = setup_test('
',{controlInput:'#external-control'}); control = document.getElementById('external-control'); }); it_n('should filter options when typing in external control', function(done) { syn.type('a',control,function(){ assert.equal(test.instance.dropdown_content.children.length, 1); done(); }); }); it_n('should not hide external control', function() { test.instance.hideInput(); assert.equal(test.instance.isInputHidden, false); }); it_n('should not move caret position', function(done) { test.instance.addItem('a'); test.instance.addItem('b'); var caretpos = test.instance.caretPos; syn.type('[left]',control,function(){ assert.equal(test.instance.caretPos, caretpos); done(); }); }); }); });