-
Notifications
You must be signed in to change notification settings - Fork 88
/
selecting-items.html
407 lines (307 loc) · 11.6 KB
/
selecting-items.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>vaadin-combo basic tests</title>
<script src="../../web-component-tester/browser.js"></script>
<script src='../../webcomponentsjs/webcomponents-lite.js'></script>
<link rel="import" href="../../test-fixture/test-fixture.html">
<link rel="import" href="common-imports.html">
<script src="common.js"></script>
</head>
<body>
<test-fixture id="combobox">
<template>
<vaadin-combo-box style="width: 320px;"></vaadin-combo-box>
</template>
</test-fixture>
<test-fixture id="combobox-clear">
<template>
<vaadin-combo-box clear-button-visible style="width: 320px;"></vaadin-combo-box>
</template>
</test-fixture>
<script>
describe('selecting a value', () => {
let combobox;
let input;
let valueChangedSpy, selectedItemChangedSpy, selectionChangedSpy, changeSpy;
function dispatchClick(elm) {
elm.dispatchEvent(new CustomEvent('click', {
bubbles: true
}));
}
beforeEach(() => {
combobox = fixture('combobox');
combobox.items = ['foo', 'bar'];
input = combobox.root.querySelector('input');
valueChangedSpy = sinon.spy();
combobox.addEventListener('value-changed', valueChangedSpy);
selectedItemChangedSpy = sinon.spy();
combobox.addEventListener('selected-item-changed', selectedItemChangedSpy);
selectionChangedSpy = sinon.spy();
combobox.$.overlay.addEventListener('selection-changed', selectionChangedSpy);
changeSpy = sinon.spy();
combobox.addEventListener('change', changeSpy);
});
it('should stop click events from bubbling outside the overlay', () => {
combobox.open();
combobox.close();
const clickSpy = sinon.spy();
document.addEventListener('click', clickSpy);
combobox.$.overlay._selector.dispatchEvent(new CustomEvent('click', {
bubbles: true
}));
document.removeEventListener('click', clickSpy);
expect(clickSpy.calledOnce).not.to.be.true;
});
it('should fire `selection-changed` when clicked on an item', () => {
combobox.opened = true;
Polymer.flush();
combobox.$.overlay._selector.querySelector('vaadin-combo-box-item').dispatchEvent(new CustomEvent('click', {
bubbles: true
}));
expect(selectionChangedSpy.calledOnce).to.be.true;
expect(selectionChangedSpy.args[0][0].detail.item).to.eql(combobox.items[0]);
});
it('should fire `selection-changed` after the scrolling grace period', done => {
combobox.open();
combobox.close();
const items = [];
for (let i = 1; i < 50; i++) {
items.push(i);
}
combobox.items = items;
combobox.opened = true;
function listener() {
// Ensure the listener is invoked only once. Otherwise, `done()` might
// be called multiple times, which produces error for the test.
combobox.$.overlay._scroller.removeEventListener('scroll', listener);
setTimeout(() => {
const firstItem = combobox.$.overlay._selector.querySelector('vaadin-combo-box-item');
dispatchClick(firstItem);
expect(selectionChangedSpy.calledOnce).to.be.true;
done();
}, 500);
}
// Scroll start could delay, for example, with full SD polyfill
combobox.$.overlay._scroller.addEventListener('scroll', listener);
combobox.$.overlay._selector.scrollToIndex(20);
});
it('should select by using JS api', () => {
combobox.value = 'foo';
combobox.open();
expect(combobox.selectedItem).to.equal('foo');
expect(combobox.$.overlay._selectedItem).to.equal('foo');
expect(combobox.inputElement.value).to.equal('foo');
});
it('should close the dropdown on selection', () => {
combobox.open();
Polymer.flush();
combobox.$.overlay._selector.querySelector('vaadin-combo-box-item').click();
expect(combobox.opened).to.equal(false);
});
it('should fire exactly one `value-changed` event', () => {
combobox.value = 'foo';
expect(valueChangedSpy.callCount).to.equal(1);
});
it('should close the dropdown when reselecting the current value', () => {
combobox.value = 'foo';
combobox.open();
combobox.$.overlay.dispatchEvent(new CustomEvent('selection-changed', {detail: {item: combobox.items[0]}}));
expect(combobox.opened).to.be.false;
});
it('should not fire an event when reselecting the current value', () => {
combobox.value = 'foo';
valueChangedSpy.reset();
combobox.$.overlay.dispatchEvent(new CustomEvent('selection-changed', {detail: {item: combobox.items[0]}}));
expect(valueChangedSpy.callCount).to.equal(0);
});
it('should not throw an error if value was undefined', () => {
combobox.allowCustomValue = true;
combobox.value = undefined;
combobox.open();
combobox.close();
});
it('should be possible to set selectedItem before attaching to the DOM', () => {
var newComboBox = combobox.cloneNode(true);
newComboBox.items = ['foo', 'bar'];
newComboBox.selectedItem = 'foo';
combobox.parentElement.appendChild(newComboBox);
expect(newComboBox.value).to.equal('foo');
});
it('should be possible to set selectedItem after attaching to the DOM and before setting items', () => {
var newComboBox = combobox.cloneNode(true);
combobox.parentElement.appendChild(newComboBox);
newComboBox.selectedItem = 'foo';
newComboBox.items = ['foo', 'bar'];
expect(newComboBox.value).to.equal('foo');
});
describe('`change` event', () => {
it('should fire `value-changed` and `selected-item-changed` before `changed`', done => {
combobox.addEventListener('change', () => {
expect(valueChangedSpy.called).to.be.true;
expect(selectedItemChangedSpy.called).to.be.true;
done();
});
combobox.open();
combobox.value = 'foo';
combobox.close();
});
it('should fire exactly one `change` event', () => {
combobox.open();
combobox.value = 'foo';
combobox.close();
expect(changeSpy.callCount).to.equal(1);
});
it('should not fire `change` event when not commiting a value by closing', () => {
combobox.value = 'foo';
expect(changeSpy.callCount).to.equal(0);
});
it('should fire on clear', () => {
combobox.value = 'foo';
fire('click', combobox.$.input.$.clearButton);
expect(changeSpy.callCount).to.equal(1);
});
it('should not fire until close', () => {
combobox.value = 'foo';
combobox.open();
combobox.value = 'bar';
expect(changeSpy.callCount).to.equal(0);
});
it('should not fire on cancel', () => {
combobox.open();
combobox.value = 'foo';
combobox.cancel();
expect(changeSpy.callCount).to.equal(0);
});
it('should not fire for changes when closed', () => {
combobox.value = 'foo';
combobox.open();
combobox.close();
it('should fire exactly one `change` event', () => {
combobox.value = 'foo';
expect(changeSpy.callCount).to.equal(1);
});
expect(changeSpy.callCount).to.equal(0);
});
it('should stop input `change` event from bubbling', () => {
combobox.inputElement.dispatchEvent(new CustomEvent('change'));
expect(changeSpy.callCount).to.equal(0);
});
it('should not set composed flag to true on `change` event', done => {
combobox.addEventListener('change', e => {
expect(e.composed).to.be.false;
done();
});
combobox.open();
combobox.value = 'foo';
combobox.close();
});
it('should fire when selecting an item via click', () => {
combobox.open();
const firstItem = combobox.$.overlay._selector.querySelector('vaadin-combo-box-item');
fire('click', firstItem);
expect(changeSpy.callCount).to.equal(1);
});
});
});
describe('clearing a selection - clear icon', () => {
let combobox;
let clearIcon;
beforeEach(() => {
combobox = fixture('combobox-clear');
combobox.items = ['foo', 'bar'];
combobox.value = 'foo';
clearIcon = combobox.inputElement.$.clearButton;
// needed for Edge <= 14 in Polymer 2
combobox.updateStyles();
});
it('should show the clearing icon only when combobox has value', done => {
// Edge and IE have an issue where the correct styles are not applied by the browser
// on the clear icon before triggering a reflow/re-render. Opening/Closing the combobox
// seems to be the most reliable way to fix this.
combobox.open();
expect(window.getComputedStyle(clearIcon).display).not.to.contain('none');
combobox.close();
combobox.value = '';
combobox.open();
setTimeout(() => {
expect(window.getComputedStyle(clearIcon).display).to.contain('none');
done();
}, 1);
});
it('should clear the selection when clicking on the icon', () => {
combobox.open();
fire('click', clearIcon);
expect(combobox.value).to.eql('');
expect(combobox.$.overlay._selectedItem).to.be.null;
expect(combobox.selectedItem).to.be.null;
});
it('should not close the dropdown after clearing a selection', () => {
combobox.open();
fire('click', clearIcon);
expect(combobox.opened).to.eql(true);
});
it('should cancel click event to avoid input blur', () => {
combobox.open();
const event = fire('click', clearIcon);
expect(event.defaultPrevented).to.eql(true);
});
});
describe('selecting a custom value', () => {
let combobox;
function filter(value) {
combobox.inputElement.value = value;
combobox.inputElement.dispatchEvent(new CustomEvent('input'));
}
beforeEach(done => {
combobox = fixture('combobox');
combobox.allowCustomValue = true;
combobox.items = ['foo', 'bar', 'barbar'];
combobox.open();
setTimeout(done);
});
it('should select a value when closing when having a single exact match', () => {
filter('barbar');
combobox.close();
expect(combobox.value).to.eql('barbar');
});
it('should select a value when closing when having multiple matches', () => {
filter('BAR');
combobox.close();
expect(combobox.value).to.eql('bar');
});
it('should clear the selection when closing the overlay and input is cleared', () => {
combobox.value = 'foo';
filter('');
combobox.close();
expect(combobox.value).to.be.empty;
expect(combobox.selectedItem).to.be.null;
expect(combobox.hasAttribute('has-value')).to.be.false;
});
it('should select a custom value', () => {
combobox.value = 'foobar';
combobox.close();
expect(combobox.value).to.eql('foobar');
expect(combobox.selectedItem).to.be.null;
expect(combobox._inputElementValue).to.eql('foobar');
expect(combobox.hasAttribute('has-value')).to.be.true;
});
it('should clear the custom value on clear', () => {
combobox.value = 'foobar';
filter('');
combobox.close();
expect(combobox.value).to.be.empty;
expect(combobox.selectedItem).to.be.null;
});
it('should allow changing value to existing item when custom value is set', () => {
combobox.value = 'foobar';
combobox.close();
combobox.value = 'foo';
expect(combobox.value).to.eql('foo');
expect(combobox.selectedItem).to.eql('foo');
});
});
</script>
</body>
</html>