describe('API', function() { describe('disable()', function() { var test; before(function() { test = setup_test('', {}); test.instance.setMaxItems(2); window.setTimeout(function() { done(); }, 5); }); it_n('should set maxItems setting to 2', function() { expect(test.instance.settings.maxItems).to.be.equal(2); }); }); describe('setMaxItems(0)', function(){ var test; before(function(done) { test = setup_test('', {}); expect(String(test.instance.control_input.tabIndex)).to.be.equal('-1'); test.instance.enable(); }); it_n('should restore original "tabindex" prop', function() { expect(String(test.instance.control_input.tabIndex)).to.be.equal('2'); }); it_n('should remove "disabled" class', function() { expect(test.instance.control.classList.contains('disabled')).to.be.equal(false); }); it_n('should set isDisabled property to false', function() { expect(test.instance.isDisabled).to.be.equal(false); }); it_n('should remove "disabled" attribute on inputs', function() { expect(test.instance.input.disabled).to.be.equal(false); expect( $(test.instance.control_input).is(':disabled')).to.be.equal(false); }); }); describe('focus() and blur()', function() { it_n('should focus and blur',function(done) { var test = setup_test('', { valueField: 'value', labelField: 'value', create: function(input) { return false; } }); test.instance.control_input.value = 'test'; test.instance.createItem(); expect(test.instance.items.length).to.be.equal(0); }); it_n('should fail if string returned by "create" callback', function() { var test = setup_test('', { create: function(input) { return {value:null}; } }); test.instance.createItem('test'); expect(test.instance.items.length).to.be.equal(0); }); it_n('should add option upon completion (synchronous)', function() { var test = setup_test('', { valueField: 'value', labelField: 'value', create: function(input, callback) { window.setTimeout(function() { callback({value: input}); expect(test.instance.options).to.have.property('test'); done(); }, 0); } }); test.instance.control_input.value = 'test'; test.instance.createItem(); }); it_n('should not create two items', function(){ var test = setup_test('', {valueField: 'value', labelField: 'value', optgroupValueField: 'grpval'}); }); it_n('should register group', function() { var data = {label: 'Group Label'}; test.instance.addOptionGroup('group_id', data); expect(test.instance.optgroups).to.have.property('group_id'); }); it_n('should add implicit $order property', function() { test.instance.addOptionGroup('group1', {}); test.instance.addOptionGroup('group2', {}); assert.equal(test.instance.optgroups['group1'].$order, 2); assert.equal(test.instance.optgroups['group2'].$order, 3); }); }); describe('removeOptionGroup()', function() { var test; before(function() { test = setup_test('', {valueField: 'value', labelField: 'value'}); }); it_n('should clear all groups', function() { var data = {label: 'Group Label'}; test.instance.addOptionGroup('group_id', data); test.instance.addOptionGroup('group_id2', data); test.instance.clearOptionGroups(); expect(test.instance.optgroups).to.deep.equal({}); }); }); describe('clearOptions()', function() { it_n('options should be empty', function(done) { var test = setup_test('AB_Multi', {}); assert.equal( Object.keys(test.instance.options).length, 3); test.instance.clearOptions(); assert.equal( Object.keys(test.instance.options).length, 0); done(); }); it_n('options should not be empty', function(done) { var test = setup_test('AB_Multi', {}); assert.equal( Object.keys(test.instance.options).length, 3); test.instance.addItem('b'); test.instance.clearOptions(); assert.equal( Object.keys(test.instance.options).length, 1); done(); }); }); describe('selectAll()', function() { it_n('should select all', function(done) { var test = setup_test('AB_Multi', {}); assert.equal( test.instance.activeItems.length, 0 ); test.instance.addItem('a'); test.instance.addItem('b'); assert.equal( test.instance.activeItems.length, 0 ); test.instance.selectAll(); assert.equal( test.instance.activeItems.length, 2 ); done(); }); }); describe('deleteSelection()', function() { it_n('should select then delete two items', function(done) { var test = setup_test('AB_Multi', {}); assert.equal( test.instance.activeItems.length, 0 ); assert.equal( test.instance.items.length, 0 ); test.instance.addItem('a'); test.instance.addItem('b'); assert.equal( test.instance.items.length, 2 ); assert.equal( test.instance.activeItems.length, 0 ); test.instance.selectAll(); assert.equal( test.instance.activeItems.length, 2 ); test.instance.deleteSelection(); assert.equal( test.instance.activeItems.length, 0 ); assert.equal( test.instance.items.length, 0 ); done(); }); }); describe('addOption()', function() { var test; before(function() { test = setup_test('', { 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_n('should update "items" array', function() { test.instance.addItem('b'); expect(test.instance.items.indexOf('b')).to.be.equal(0); }); it_n('should not give control focus', function(done) { test.instance.addItem(0); window.setTimeout(function() { expect(test.instance.isFocused).to.be.equal(false); done(); }, 0); }); it_n('should not allow duplicate entries', function() { test.instance.addItem('a'); test.instance.addItem('a'); expect(test.instance.items.indexOf('a')).to.be.equal(test.instance.items.lastIndexOf('a')); }); it_n('should not allow undefined / null values', function() { test.instance.addItem(undefined); test.instance.addItem(null); expect(test.instance.items.indexOf('undefined')).to.be.equal(-1); expect(test.instance.items.indexOf('null')).to.be.equal(-1); }); it_n('should allow integer values', function() { test.instance.addItem(0); expect(test.instance.items.indexOf('0')).to.not.be.equal(-1); }); it_n('should not fire "change" if silent is truthy', function(done) { var watcher = function(e) { throw new Error('Change fired'); }; test.instance.on('change', watcher); test.instance.addItem('x', true); expect(test.instance.items.indexOf('x')).to.not.be.equal(-1); window.setTimeout(function() { test.instance.off('change', watcher); done(); }, 0); }); it_n('should update DOM (1)', function() { test.instance.addItem('c'); expect( $(test.instance.control).find('[data-value=c]').length).to.be.equal(1); test.instance.addItem('$1'); var found = false; $(test.instance.control).children().each(function() { if (this.getAttribute('data-value') === '$1') { found = true; return false; } }); expect(found).to.be.equal(true); }); }); describe('addItems()',function(){ // this test ensures ' + '' + '' + '' + '' + '' + '' + ''; var test = setup_test('', {}); test.instance.destroy(); expect(test.select.innerHTML,'restoring children failed').to.be.equal(children); expect(test.select.tabIndex,'restoring tabindex failed').to.be.equal(9999); }); }); describe('clearCache()', function() { var test; before(()=>{ test = setup_test('', { create: true, persist: false, }); var len_opts_before = Object.keys(test.instance.options).length; test.instance.createItem('test'); expect( Object.keys(test.instance.options).length).to.be.equal(len_opts_before+1); test.instance.removeItem('test'); expect( Object.keys(test.instance.options).length).to.be.equal(len_opts_before); }); it_n('should not remove user created option', function() { var test = setup_test('', { valueField: 'value', labelField: 'value', searchField:'value', options: [ {value: 2}, {value: 1}, {value: 0}, ], items: ['2','1','0'] }); test.instance.refreshOptions(true); assert.equal( test.instance.items.length, 3); test.instance.removeItem(0); assert.equal( test.instance.items.length, 2); assert.equal( test.instance.items[0], '2'); assert.equal( test.instance.items[1], '1'); }); }); describe('controlChilden()', function() { it_n('controlChilden() should return empty array',function(){ const test = setup_test('AB_Multi'); assert.equal(test.instance.controlChildren().length,0); }); }); });