// 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() { describe('dropdown', function() { it_n('should keep dropdown open after selection made if closeAfterSelect: false', function(done) { var test = setup_test('AB_Multi',{}); click(test.instance.control, function() { click($('[data-value=a]', test.instance.dropdown_content), function() { expect(test.instance.isOpen).to.be.equal(true); expect(test.instance.isFocused).to.be.equal(true); done(); }); }); }); it_n('should keep dropdown open when clicking on dropdown if dropdownParent=body', function(done) { var test = setup_test('AB_Multi',{dropdownParent:'body'}); click(test.instance.control, function() { click($('[data-value=a]', test.instance.dropdown_content), function() { expect(test.instance.isOpen).to.be.equal(true); expect(test.instance.isFocused).to.be.equal(true); done(); }); }); }); it_n('should reopen dropdown if clicked after being closed by closeAfterSelect: true', function(done) { var test = setup_test('AB_Multi',{closeAfterSelect: true}); click(test.instance.control, function() { click($('[data-value=a]', test.instance.dropdown_content), function() { assert.equal(test.instance.isOpen, false, 'should be closed after select'); assert.equal(test.instance.isFocused, true, 'should be closed w/ focus after select'); click(test.instance.control, function () { assert.equal(test.instance.isOpen, true, 'should be open after click'); assert.equal(test.instance.isFocused, true, 'should be focused after click'); done(); }); }); }); }); it_n('should close dropdown after selection made if closeAfterSelect: true and in single mode' , function(done) { var test = setup_test('AB_Single',{closeAfterSelect: true}); click(test.instance.control, function() { expect(test.instance.isOpen).to.be.equal(true); click($('[data-value=a]', test.instance.dropdown_content), function() { expect(test.instance.isOpen).to.be.equal(false); done(); }); }); }); it_n('should blur dropdown after selection made if closeAfterSelect: true and in single mode' , function(done) { var test = setup_test('AB_Single',{closeAfterSelect: true}); click(test.instance.control, function() { expect(test.instance.isFocused).to.be.equal(true); click($('[data-value=a]', test.instance.dropdown_content), function() { expect(test.instance.isFocused).to.be.equal(false); done(); }); }); }); it_n('should close dropdown and clear active items after [escape] key press', async () => { var test = setup_test('AB_Multi',{items:['a']}); await asyncClick(test.instance.control); test.instance.setActiveItem(test.instance.getItem('a')); assert.equal( test.instance.isOpen, true, 'not open' ); assert.equal( test.instance.items.length, 1 , 'should have 1 item' ); assert.equal( test.instance.activeItems.length, 1 , 'should 1 active item' ); await asyncType('[escape]', test.instance.control_input); assert.equal( test.instance.isOpen, false, 'not closed' ); assert.equal( test.instance.activeItems.length, 0 , 'not cleared' ); assert.equal( test.instance.control_input.value,'','should clear control_input'); }); it_n('should close dropdown and clear control_input after [escape] key press', async () => { var test = setup_test('AB_Multi',{items:['a']}); await asyncClick(test.instance.control); await asyncType('b', test.instance.control_input); assert.equal( test.instance.isOpen, true, 'not open' ); assert.equal( test.instance.control_input.value,'b','should type "b"'); await asyncType('[escape]', test.instance.control_input); assert.equal( test.instance.isOpen, false, 'not closed' ); assert.equal( test.instance.control_input.value,'','should clear control_input'); }); it_n('should change activeOption with [down] and [up] keypress', function(done) { var test = setup_test(` `); click(test.instance.control, function() { assert.equal( test.instance.activeOption.dataset.value, 'a', 'activeOption should be "a" to start'); syn.type('[down]', test.instance.control_input, function() { assert.equal( test.instance.activeOption.dataset.value, 'b', 'activeOption should be changed to "b"'); syn.type('[up]', test.instance.control_input, function() { assert.equal( test.instance.activeOption.dataset.value, 'a', 'activeOption should be changed back to "a"'); done(); }); }); }); }); it_n('should not close dropdown when clicking on dropdown header', function(done) { var select = ``; var test = setup_test(select,{ render:{ 'optgroup_header': function(data, escape) { return ''; }, } }); click(test.instance.control, function() { expect(test.instance.isOpen).to.be.equal(true); var header = test.instance.dropdown.querySelector('.optgroup-header') click(header,function(){ expect(test.instance.isOpen).to.be.equal(true); done(); }); }); }); }); describe('clicking control', function() { it_n('should toggle focus', function(done) { var test = setup_test('AB_Single_Long',{ items: ['p'] }); click(test.instance.control, function() { assert.equal(test.instance.isFocused,true); assert.equal(test.instance.isOpen,true); expect( isVisible(test.instance.dropdown) ).to.be.equal(true); assert.equal( test.instance.activeOption.dataset.value,'p','activeOption should be "p"'); assert.isAbove(test.instance.dropdown_content.scrollTop, 0, 'dropdown should be scrolled to activeOption'); click(test.instance.control, function() { assert.equal(test.instance.isFocused,false); assert.equal(test.instance.isOpen,false); done(); }); }); }); it_n('should toggle focus clicking item in single mode control', function(done) { var test = setup_test('AB_Single',{ items:['a'] }); var itema = test.instance.getItem('a'); click(itema, function() { assert.equal(test.instance.isFocused,true); assert.equal(test.instance.isOpen,true); click(itema, function() { assert.equal(test.instance.isFocused,false); assert.equal(test.instance.isOpen,false); done(); }); }); }); it_n('should remain open but clear active item on click', function(done) { var test = setup_test('AB_Multi'); click(test.instance.control, () => { test.instance.addItem('a'); test.instance.setActiveItem(test.instance.getItem('a')); assert.equal( test.instance.activeItems.length, 1); assert.equal( test.instance.isOpen, true); click(test.instance.control_input, () => { assert.equal( test.instance.activeItems.length, 0); assert.equal( test.instance.isOpen, true); assert.equal( test.instance.isFocused, true); done(); }); }); }); }); describe('clicking label', function() { it_n('should give focus to select', function(done) { var test = setup_test(``); var label = document.getElementById('select-label'); label.click(); setTimeout(()=>{ expect(test.instance.isFocused).to.be.equal(true); done(); },1); }); it_n('should give focus to input', function(done) { var test = setup_test(` `); var label = document.getElementById('input-label'); label.click(); setTimeout(()=>{ expect(test.instance.isFocused).to.be.equal(true); done(); },1); }); }); describe('selecting option', function() { it_n('should select option when clicked', async () =>{ var test = setup_test('
'); var clicked = false; document.getElementById('test-wrap').addEventListener('click',()=>{ clicked = true; }); await asyncClick(test.instance.control); assert.equal(test.instance.input.querySelectorAll('option').length, 3,'should keep original options'); assert.isTrue( test.instance.isOpen, 'Should be open' ); var option_before = test.instance.input.querySelector('option[value="b"]'); await asyncClick( test.instance.dropdown.querySelector('[data-value="b"]') ); var option_after = test.instance.input.querySelector('option[value="b"]'); assert.equal(option_before, option_after,'should not recreate original '; var option_b = document.createElement('option'); option_b.value = "b"; option_b.textContent = "b"; option_b.selected = true; select.append(option_b); var test = setup_test(select); //test.instance.addItem('a'); //test.instance.addItem('b'); assert.equal( test.instance.items.length, 2 ,'items.length should = 2' ); assert.equal( Array.from(test.instance.input.options).filter(option => option.getAttribute('selected')).length, 2,'getAttribute(selected).length should = 2' ); assert.equal( Array.from(test.instance.input.options).filter(option => option.selected).length, 2,'option.selected.length should = 2' ); click(test.instance.control, function() { syn.type('\b', test.instance.control_input, function() { var option_after = test.instance.input.querySelector('option[value="b"]'); assert.equal( test.instance.items.length, 1 ); assert.equal( test.instance.items[0], 'a' ); assert.equal( option_b, option_after, 'should not remove original