|
| 1 | +import { expect } from '@vaadin/chai-plugins'; |
| 2 | +import { fixtureSync, nextRender } from '@vaadin/testing-helpers'; |
| 3 | +import '../vaadin-combo-box.js'; |
| 4 | +import { setInputValue } from './helpers.js'; |
| 5 | + |
| 6 | +describe('item-label-generator', () => { |
| 7 | + let comboBox; |
| 8 | + |
| 9 | + beforeEach(async () => { |
| 10 | + comboBox = fixtureSync('<vaadin-combo-box></vaadin-combo-box>'); |
| 11 | + await nextRender(); |
| 12 | + }); |
| 13 | + |
| 14 | + describe('basic functionality', () => { |
| 15 | + beforeEach(() => { |
| 16 | + comboBox.items = [ |
| 17 | + { id: 1, name: 'John', surname: 'Doe', age: 30 }, |
| 18 | + { id: 2, name: 'Jane', surname: 'Smith', age: 25 }, |
| 19 | + { id: 3, name: 'Bob', surname: 'Johnson', age: 35 }, |
| 20 | + { id: 4, name: 'Alice', surname: 'Williams', age: 28 }, |
| 21 | + ]; |
| 22 | + }); |
| 23 | + |
| 24 | + it('should generate labels using itemLabelGenerator', async () => { |
| 25 | + comboBox.itemLabelGenerator = (item) => `${item.name} ${item.surname}`; |
| 26 | + comboBox.opened = true; |
| 27 | + await nextRender(); |
| 28 | + |
| 29 | + const items = comboBox._scroller.querySelectorAll('vaadin-combo-box-item'); |
| 30 | + expect(items[0].textContent).to.equal('John Doe'); |
| 31 | + expect(items[1].textContent).to.equal('Jane Smith'); |
| 32 | + expect(items[2].textContent).to.equal('Bob Johnson'); |
| 33 | + expect(items[3].textContent).to.equal('Alice Williams'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should display generated label in input when item is selected', async () => { |
| 37 | + comboBox.itemLabelGenerator = (item) => `${item.name} ${item.surname} (${item.age})`; |
| 38 | + comboBox.itemValuePath = 'id'; |
| 39 | + comboBox.value = 2; |
| 40 | + await nextRender(); |
| 41 | + |
| 42 | + expect(comboBox.inputElement.value).to.equal('Jane Smith (25)'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should filter items using generated labels', () => { |
| 46 | + comboBox.itemLabelGenerator = (item) => `${item.name} ${item.surname}`; |
| 47 | + |
| 48 | + setInputValue(comboBox, 'john'); |
| 49 | + |
| 50 | + expect(comboBox.filteredItems.length).to.equal(2); |
| 51 | + expect(comboBox.filteredItems[0]).to.deep.equal({ id: 1, name: 'John', surname: 'Doe', age: 30 }); |
| 52 | + expect(comboBox.filteredItems[1]).to.deep.equal({ id: 3, name: 'Bob', surname: 'Johnson', age: 35 }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should use itemLabelGenerator over itemLabelPath', async () => { |
| 56 | + comboBox.items = [ |
| 57 | + { id: 1, label: 'Label from path', customLabel: 'Custom Label 1' }, |
| 58 | + { id: 2, label: 'Another label', customLabel: 'Custom Label 2' }, |
| 59 | + ]; |
| 60 | + comboBox.itemLabelPath = 'label'; |
| 61 | + comboBox.itemLabelGenerator = (item) => item.customLabel; |
| 62 | + comboBox.opened = true; |
| 63 | + await nextRender(); |
| 64 | + |
| 65 | + const items = comboBox._scroller.querySelectorAll('vaadin-combo-box-item'); |
| 66 | + expect(items[0].textContent).to.equal('Custom Label 1'); |
| 67 | + expect(items[1].textContent).to.equal('Custom Label 2'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should handle empty return from itemLabelGenerator', async () => { |
| 71 | + comboBox.itemLabelGenerator = (item) => { |
| 72 | + if (item.id === 2) { |
| 73 | + return ''; |
| 74 | + } |
| 75 | + return `${item.name} ${item.surname}`; |
| 76 | + }; |
| 77 | + comboBox.opened = true; |
| 78 | + await nextRender(); |
| 79 | + |
| 80 | + const items = comboBox._scroller.querySelectorAll('vaadin-combo-box-item'); |
| 81 | + expect(items[0].textContent).to.equal('John Doe'); |
| 82 | + expect(items[1].textContent).to.equal(''); |
| 83 | + expect(items[2].textContent).to.equal('Bob Johnson'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should update dropdown when itemLabelGenerator changes', async () => { |
| 87 | + comboBox.itemLabelGenerator = (item) => item.name; |
| 88 | + comboBox.opened = true; |
| 89 | + await nextRender(); |
| 90 | + |
| 91 | + let items = comboBox._scroller.querySelectorAll('vaadin-combo-box-item'); |
| 92 | + expect(items[0].textContent).to.equal('John'); |
| 93 | + |
| 94 | + comboBox.itemLabelGenerator = (item) => `${item.name} (${item.age})`; |
| 95 | + await nextRender(); |
| 96 | + |
| 97 | + items = comboBox._scroller.querySelectorAll('vaadin-combo-box-item'); |
| 98 | + expect(items[0].textContent).to.equal('John (30)'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should work with string items when itemLabelGenerator is not set', async () => { |
| 102 | + comboBox.items = ['Apple', 'Banana', 'Orange']; |
| 103 | + comboBox.opened = true; |
| 104 | + await nextRender(); |
| 105 | + |
| 106 | + const items = comboBox._scroller.querySelectorAll('vaadin-combo-box-item'); |
| 107 | + expect(items[0].textContent).to.equal('Apple'); |
| 108 | + expect(items[1].textContent).to.equal('Banana'); |
| 109 | + expect(items[2].textContent).to.equal('Orange'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should find items by generated label when typing', () => { |
| 113 | + comboBox.itemLabelGenerator = (item) => `${item.surname}, ${item.name}`; |
| 114 | + comboBox.itemValuePath = 'id'; |
| 115 | + |
| 116 | + setInputValue(comboBox, 'Smith, Jane'); |
| 117 | + |
| 118 | + expect(comboBox.filteredItems.length).to.equal(1); |
| 119 | + expect(comboBox.filteredItems[0]).to.deep.equal({ id: 2, name: 'Jane', surname: 'Smith', age: 25 }); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments