|
| 1 | +import { expect } from '@vaadin/chai-plugins'; |
| 2 | +import { fixtureSync, nextFrame, nextRender } from '@vaadin/testing-helpers'; |
| 3 | +import '../src/vaadin-combo-box.js'; |
| 4 | + |
| 5 | +describe('combo-box itemLabelGenerator', () => { |
| 6 | + let comboBox; |
| 7 | + |
| 8 | + beforeEach(async () => { |
| 9 | + comboBox = fixtureSync('<vaadin-combo-box></vaadin-combo-box>'); |
| 10 | + await nextFrame(); |
| 11 | + }); |
| 12 | + |
| 13 | + describe('basic functionality', () => { |
| 14 | + beforeEach(async () => { |
| 15 | + comboBox.items = [ |
| 16 | + { label: 'Option 1', value: '1', customProp: 'Custom 1' }, |
| 17 | + { label: 'Option 2', value: '2', customProp: 'Custom 2' }, |
| 18 | + { label: 'Option 3', value: '3', customProp: 'Custom 3' }, |
| 19 | + ]; |
| 20 | + await nextFrame(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should use item label by default', async () => { |
| 24 | + comboBox.opened = true; |
| 25 | + await nextRender(); |
| 26 | + |
| 27 | + const items = comboBox._scroller.querySelectorAll('vaadin-combo-box-item'); |
| 28 | + expect(items[0].textContent.trim()).to.equal('Option 1'); |
| 29 | + expect(items[1].textContent.trim()).to.equal('Option 2'); |
| 30 | + expect(items[2].textContent.trim()).to.equal('Option 3'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should use itemLabelGenerator when provided', async () => { |
| 34 | + comboBox.itemLabelGenerator = (item) => `${item.customProp} - ${item.value}`; |
| 35 | + await nextFrame(); |
| 36 | + |
| 37 | + comboBox.opened = true; |
| 38 | + await nextRender(); |
| 39 | + |
| 40 | + const items = comboBox._scroller.querySelectorAll('vaadin-combo-box-item'); |
| 41 | + expect(items[0].textContent.trim()).to.equal('Custom 1 - 1'); |
| 42 | + expect(items[1].textContent.trim()).to.equal('Custom 2 - 2'); |
| 43 | + expect(items[2].textContent.trim()).to.equal('Custom 3 - 3'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should update items when itemLabelGenerator changes', async () => { |
| 47 | + comboBox.itemLabelGenerator = (item) => item.value; |
| 48 | + await nextFrame(); |
| 49 | + |
| 50 | + comboBox.opened = true; |
| 51 | + await nextRender(); |
| 52 | + |
| 53 | + let items = comboBox._scroller.querySelectorAll('vaadin-combo-box-item'); |
| 54 | + expect(items[0].textContent.trim()).to.equal('1'); |
| 55 | + |
| 56 | + comboBox.itemLabelGenerator = (item) => `Value: ${item.value}`; |
| 57 | + await nextFrame(); |
| 58 | + |
| 59 | + items = comboBox._scroller.querySelectorAll('vaadin-combo-box-item'); |
| 60 | + expect(items[0].textContent.trim()).to.equal('Value: 1'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should handle undefined return from itemLabelGenerator', async () => { |
| 64 | + comboBox.itemLabelGenerator = () => undefined; |
| 65 | + await nextFrame(); |
| 66 | + |
| 67 | + comboBox.opened = true; |
| 68 | + await nextRender(); |
| 69 | + |
| 70 | + const items = comboBox._scroller.querySelectorAll('vaadin-combo-box-item'); |
| 71 | + expect(items[0].textContent.trim()).to.equal(''); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should handle null return from itemLabelGenerator', async () => { |
| 75 | + comboBox.itemLabelGenerator = () => null; |
| 76 | + await nextFrame(); |
| 77 | + |
| 78 | + comboBox.opened = true; |
| 79 | + await nextRender(); |
| 80 | + |
| 81 | + const items = comboBox._scroller.querySelectorAll('vaadin-combo-box-item'); |
| 82 | + expect(items[0].textContent.trim()).to.equal(''); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should override itemLabelPath when itemLabelGenerator is set', async () => { |
| 86 | + comboBox.itemLabelPath = 'customProp'; |
| 87 | + comboBox.itemLabelGenerator = (item) => `Generated: ${item.value}`; |
| 88 | + await nextFrame(); |
| 89 | + |
| 90 | + comboBox.opened = true; |
| 91 | + await nextRender(); |
| 92 | + |
| 93 | + const items = comboBox._scroller.querySelectorAll('vaadin-combo-box-item'); |
| 94 | + expect(items[0].textContent.trim()).to.equal('Generated: 1'); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('with complex items', () => { |
| 99 | + it('should work with nested object properties', async () => { |
| 100 | + comboBox.items = [ |
| 101 | + { data: { name: 'John', age: 30 }, value: '1' }, |
| 102 | + { data: { name: 'Jane', age: 25 }, value: '2' }, |
| 103 | + ]; |
| 104 | + |
| 105 | + comboBox.itemLabelGenerator = (item) => `${item.data.name} (${item.data.age})`; |
| 106 | + await nextFrame(); |
| 107 | + |
| 108 | + comboBox.opened = true; |
| 109 | + await nextRender(); |
| 110 | + |
| 111 | + const items = comboBox._scroller.querySelectorAll('vaadin-combo-box-item'); |
| 112 | + expect(items[0].textContent.trim()).to.equal('John (30)'); |
| 113 | + expect(items[1].textContent.trim()).to.equal('Jane (25)'); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('selected value display', () => { |
| 118 | + it('should show generated label for selected item in input', async () => { |
| 119 | + comboBox.items = [ |
| 120 | + { label: 'Option 1', value: '1', displayName: 'First' }, |
| 121 | + { label: 'Option 2', value: '2', displayName: 'Second' }, |
| 122 | + ]; |
| 123 | + |
| 124 | + comboBox.itemLabelGenerator = (item) => item.displayName; |
| 125 | + comboBox.value = '1'; |
| 126 | + await nextFrame(); |
| 127 | + |
| 128 | + expect(comboBox.inputElement.value).to.equal('First'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should update input value when itemLabelGenerator changes and item is selected', async () => { |
| 132 | + comboBox.items = [{ label: 'Option 1', value: '1', customProp: 'Custom 1' }]; |
| 133 | + comboBox.value = '1'; |
| 134 | + await nextFrame(); |
| 135 | + |
| 136 | + expect(comboBox.inputElement.value).to.equal('Option 1'); |
| 137 | + |
| 138 | + comboBox.itemLabelGenerator = (item) => item.customProp; |
| 139 | + await nextFrame(); |
| 140 | + |
| 141 | + expect(comboBox.inputElement.value).to.equal('Custom 1'); |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + describe('filtering', () => { |
| 146 | + beforeEach(() => { |
| 147 | + comboBox.items = [ |
| 148 | + { label: 'Apple', value: 'apple', code: 'APL' }, |
| 149 | + { label: 'Banana', value: 'banana', code: 'BNN' }, |
| 150 | + { label: 'Cherry', value: 'cherry', code: 'CHR' }, |
| 151 | + ]; |
| 152 | + }); |
| 153 | + |
| 154 | + it('should filter items based on generated labels', async () => { |
| 155 | + comboBox.itemLabelGenerator = (item) => `${item.code} - ${item.label}`; |
| 156 | + await nextFrame(); |
| 157 | + |
| 158 | + comboBox.opened = true; |
| 159 | + comboBox.filter = 'APL'; |
| 160 | + await nextFrame(); |
| 161 | + |
| 162 | + const items = comboBox.filteredItems; |
| 163 | + expect(items).to.have.lengthOf(1); |
| 164 | + expect(items[0].value).to.equal('apple'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should filter with custom generated labels', async () => { |
| 168 | + comboBox.itemLabelGenerator = (item) => item.code; |
| 169 | + await nextFrame(); |
| 170 | + |
| 171 | + comboBox.opened = true; |
| 172 | + comboBox.filter = 'BNN'; |
| 173 | + await nextFrame(); |
| 174 | + |
| 175 | + const items = comboBox.filteredItems; |
| 176 | + expect(items).to.have.lengthOf(1); |
| 177 | + expect(items[0].value).to.equal('banana'); |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments