|
| 1 | +import { expect } from '@esm-bundle/chai'; |
| 2 | +import { fixtureSync, focusout } from '@vaadin/testing-helpers'; |
| 3 | +import sinon from 'sinon'; |
| 4 | +import './not-animated-styles.js'; |
| 5 | +import '../vaadin-combo-box.js'; |
| 6 | +import { clickItem, outsideClick, setInputValue } from './helpers.js'; |
| 7 | + |
| 8 | +describe('events', () => { |
| 9 | + let comboBox; |
| 10 | + |
| 11 | + describe('custom-value-set event', () => { |
| 12 | + beforeEach(() => { |
| 13 | + comboBox = fixtureSync('<vaadin-combo-box></vaadin-combo-box>'); |
| 14 | + comboBox.allowCustomValue = true; |
| 15 | + comboBox.items = ['a', 'b']; |
| 16 | + comboBox.inputElement.focus(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should be fired when custom value is set', () => { |
| 20 | + const spy = sinon.spy(); |
| 21 | + comboBox.addEventListener('custom-value-set', spy); |
| 22 | + |
| 23 | + comboBox.open(); |
| 24 | + setInputValue(comboBox, 'foo'); |
| 25 | + outsideClick(); |
| 26 | + |
| 27 | + expect(spy.callCount).to.eql(1); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should not be fired when custom values are not allowed', () => { |
| 31 | + comboBox.allowCustomValue = false; |
| 32 | + |
| 33 | + const spy = sinon.spy(); |
| 34 | + comboBox.addEventListener('custom-value-set', spy); |
| 35 | + |
| 36 | + comboBox.open(); |
| 37 | + setInputValue(comboBox, 'foo'); |
| 38 | + outsideClick(); |
| 39 | + |
| 40 | + expect(spy.callCount).to.eql(0); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should not be fired when combo-box is read-only', () => { |
| 44 | + const spy = sinon.spy(); |
| 45 | + comboBox.addEventListener('custom-value-set', spy); |
| 46 | + |
| 47 | + comboBox.readonly = true; |
| 48 | + setInputValue(comboBox, 'foo'); |
| 49 | + comboBox.focus(); |
| 50 | + focusout(comboBox); |
| 51 | + |
| 52 | + expect(spy.called).to.be.false; |
| 53 | + }); |
| 54 | + |
| 55 | + it('should be cancelable', () => { |
| 56 | + comboBox.addEventListener('custom-value-set', (e) => e.preventDefault()); |
| 57 | + |
| 58 | + comboBox.open(); |
| 59 | + setInputValue(comboBox, 'foo'); |
| 60 | + outsideClick(); |
| 61 | + expect(comboBox.value).to.be.empty; |
| 62 | + }); |
| 63 | + |
| 64 | + it('should not be fired when clicking an item', () => { |
| 65 | + const spy = sinon.spy(); |
| 66 | + comboBox.addEventListener('custom-value-set', spy); |
| 67 | + |
| 68 | + comboBox.open(); |
| 69 | + setInputValue(comboBox, 'a'); |
| 70 | + clickItem(comboBox, 0); |
| 71 | + expect(spy.called).to.be.false; |
| 72 | + }); |
| 73 | + |
| 74 | + it('should not be fired when existing item is entered and overlay is closed', () => { |
| 75 | + const spy = sinon.spy(); |
| 76 | + comboBox.addEventListener('custom-value-set', spy); |
| 77 | + |
| 78 | + comboBox.open(); |
| 79 | + setInputValue(comboBox, 'a'); |
| 80 | + outsideClick(); |
| 81 | + expect(spy.called).to.be.false; |
| 82 | + }); |
| 83 | + |
| 84 | + it('should not be fired when the custom value equals the label of the selected item', () => { |
| 85 | + const spy = sinon.spy(); |
| 86 | + comboBox.addEventListener('custom-value-set', spy); |
| 87 | + comboBox.selectedItem = { |
| 88 | + label: 'foo', |
| 89 | + value: 'bar', |
| 90 | + }; |
| 91 | + |
| 92 | + comboBox.open(); |
| 93 | + setInputValue(comboBox, 'foo'); |
| 94 | + outsideClick(); |
| 95 | + |
| 96 | + expect(spy.called).to.be.false; |
| 97 | + }); |
| 98 | + |
| 99 | + it('should be fired when the custom value equals the value of the selected item', () => { |
| 100 | + const spy = sinon.spy(); |
| 101 | + comboBox.addEventListener('custom-value-set', spy); |
| 102 | + comboBox.selectedItem = { |
| 103 | + label: 'foo', |
| 104 | + value: 'bar', |
| 105 | + }; |
| 106 | + |
| 107 | + comboBox.open(); |
| 108 | + setInputValue(comboBox, 'bar'); |
| 109 | + outsideClick(); |
| 110 | + |
| 111 | + expect(spy.calledOnce).to.be.true; |
| 112 | + }); |
| 113 | + |
| 114 | + it('should not be fired twice when the custom value set listener causes blur', () => { |
| 115 | + const spy = sinon.spy(); |
| 116 | + comboBox.addEventListener('custom-value-set', spy); |
| 117 | + |
| 118 | + // Emulate opening the overlay that causes blur |
| 119 | + comboBox.addEventListener('custom-value-set', () => { |
| 120 | + comboBox.blur(); |
| 121 | + }); |
| 122 | + |
| 123 | + comboBox.open(); |
| 124 | + setInputValue(comboBox, 'foo'); |
| 125 | + outsideClick(); |
| 126 | + |
| 127 | + expect(spy.calledOnce).to.be.true; |
| 128 | + }); |
| 129 | + |
| 130 | + it('should be fired twice when another custom value is committed by the user', () => { |
| 131 | + const spy = sinon.spy(); |
| 132 | + comboBox.addEventListener('custom-value-set', spy); |
| 133 | + |
| 134 | + comboBox.open(); |
| 135 | + setInputValue(comboBox, 'foo'); |
| 136 | + outsideClick(); |
| 137 | + |
| 138 | + comboBox.inputElement.focus(); |
| 139 | + setInputValue(comboBox, 'bar'); |
| 140 | + outsideClick(); |
| 141 | + focusout(comboBox.inputElement); |
| 142 | + |
| 143 | + expect(spy.calledTwice).to.be.true; |
| 144 | + }); |
| 145 | + }); |
| 146 | +}); |
0 commit comments