@@ -2,7 +2,7 @@ import { expect } from '@vaadin/chai-plugins';
2
2
import { fixtureSync , nextRender } from '@vaadin/testing-helpers' ;
3
3
import sinon from 'sinon' ;
4
4
import '../src/vaadin-multi-select-combo-box.js' ;
5
- import { getAllItems , getFirstItem } from './helpers.js' ;
5
+ import { getAllItems , getChips , getFirstItem , setInputValue } from './helpers.js' ;
6
6
7
7
describe ( 'items' , ( ) => {
8
8
let comboBox ;
@@ -81,10 +81,6 @@ describe('items', () => {
81
81
} ) ;
82
82
83
83
describe ( 'itemClassNameGenerator' , ( ) => {
84
- let comboBox ;
85
-
86
- const getChips = ( combo ) => combo . querySelectorAll ( 'vaadin-multi-select-combo-box-chip' ) ;
87
-
88
84
beforeEach ( async ( ) => {
89
85
comboBox = fixtureSync ( '<vaadin-multi-select-combo-box></vaadin-multi-select-combo-box>' ) ;
90
86
await nextRender ( ) ;
@@ -184,4 +180,95 @@ describe('items', () => {
184
180
expect ( chips [ 2 ] . className ) . to . equal ( '' ) ;
185
181
} ) ;
186
182
} ) ;
183
+
184
+ describe ( 'itemLabelGenerator' , ( ) => {
185
+ beforeEach ( async ( ) => {
186
+ comboBox = fixtureSync ( `
187
+ <vaadin-multi-select-combo-box
188
+ auto-expand-horizontally
189
+ item-id-path="id"
190
+ ></vaadin-multi-select-combo-box>
191
+ ` ) ;
192
+ comboBox . items = [
193
+ { id : 1 , name : 'John' , surname : 'Doe' , age : 30 } ,
194
+ { id : 2 , name : 'Jane' , surname : 'Smith' , age : 25 } ,
195
+ { id : 3 , name : 'Bob' , surname : 'Johnson' , age : 35 } ,
196
+ ] ;
197
+ comboBox . itemLabelGenerator = ( item ) => `${ item . name } ${ item . surname } ` ;
198
+ await nextRender ( ) ;
199
+ } ) ;
200
+
201
+ it ( 'should generate items text content using itemLabelGenerator' , async ( ) => {
202
+ comboBox . open ( ) ;
203
+ await nextRender ( ) ;
204
+
205
+ const items = getAllItems ( comboBox ) ;
206
+ expect ( items [ 0 ] . textContent ) . to . equal ( 'John Doe' ) ;
207
+ expect ( items [ 1 ] . textContent ) . to . equal ( 'Jane Smith' ) ;
208
+ expect ( items [ 2 ] . textContent ) . to . equal ( 'Bob Johnson' ) ;
209
+ } ) ;
210
+
211
+ it ( 'should generate chips text content using itemLabelGenerator' , async ( ) => {
212
+ comboBox . selectedItems = [ comboBox . items [ 0 ] , comboBox . items [ 1 ] ] ;
213
+ await nextRender ( ) ;
214
+
215
+ const chips = getChips ( comboBox ) ;
216
+ expect ( chips [ 1 ] . label ) . to . equal ( 'John Doe' ) ;
217
+ expect ( chips [ 2 ] . label ) . to . equal ( 'Jane Smith' ) ;
218
+ } ) ;
219
+
220
+ it ( 'should filter items using generated label' , ( ) => {
221
+ setInputValue ( comboBox , 'john' ) ;
222
+
223
+ expect ( comboBox . filteredItems . length ) . to . equal ( 2 ) ;
224
+ expect ( comboBox . filteredItems [ 0 ] ) . to . deep . equal ( comboBox . items [ 0 ] ) ;
225
+ expect ( comboBox . filteredItems [ 1 ] ) . to . deep . equal ( comboBox . items [ 2 ] ) ;
226
+ } ) ;
227
+
228
+ it ( 'should use itemLabelGenerator over itemLabelPath' , async ( ) => {
229
+ comboBox . itemLabelPath = 'surname' ;
230
+ comboBox . itemLabelGenerator = ( item ) => item . name ;
231
+ comboBox . open ( ) ;
232
+ await nextRender ( ) ;
233
+
234
+ const items = getAllItems ( comboBox ) ;
235
+ expect ( items [ 0 ] . textContent ) . to . equal ( 'John' ) ;
236
+ expect ( items [ 1 ] . textContent ) . to . equal ( 'Jane' ) ;
237
+ } ) ;
238
+
239
+ it ( 'should accept empty string returned from itemLabelGenerator' , async ( ) => {
240
+ comboBox . itemLabelGenerator = ( item ) => ( item . id === 2 ? '' : `${ item . name } ${ item . surname } ` ) ;
241
+ comboBox . open ( ) ;
242
+ await nextRender ( ) ;
243
+
244
+ const items = getAllItems ( comboBox ) ;
245
+ expect ( items [ 0 ] . textContent ) . to . equal ( 'John Doe' ) ;
246
+ expect ( items [ 1 ] . textContent ) . to . equal ( '' ) ;
247
+ expect ( items [ 2 ] . textContent ) . to . equal ( 'Bob Johnson' ) ;
248
+ } ) ;
249
+
250
+ it ( 'should update dropdown items when itemLabelGenerator changes' , async ( ) => {
251
+ comboBox . open ( ) ;
252
+ await nextRender ( ) ;
253
+
254
+ expect ( getFirstItem ( comboBox ) . textContent ) . to . equal ( 'John Doe' ) ;
255
+
256
+ comboBox . itemLabelGenerator = ( item ) => `${ item . name } (${ item . age } )` ;
257
+ await nextRender ( ) ;
258
+
259
+ expect ( getFirstItem ( comboBox ) . textContent ) . to . equal ( 'John (30)' ) ;
260
+ } ) ;
261
+
262
+ it ( 'should update chips when itemLabelGenerator changes' , async ( ) => {
263
+ comboBox . selectedItems = [ comboBox . items [ 0 ] ] ;
264
+ await nextRender ( ) ;
265
+
266
+ expect ( getChips ( comboBox ) [ 1 ] . label ) . to . equal ( 'John Doe' ) ;
267
+
268
+ comboBox . itemLabelGenerator = ( item ) => `${ item . name } (${ item . age } )` ;
269
+ await nextRender ( ) ;
270
+
271
+ expect ( getChips ( comboBox ) [ 1 ] . label ) . to . equal ( 'John (30)' ) ;
272
+ } ) ;
273
+ } ) ;
187
274
} ) ;
0 commit comments