/
toggling-dropdown.html
445 lines (348 loc) · 14.1 KB
/
toggling-dropdown.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<!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>
<link rel="import" href="../../iron-input/iron-input.html">
<link rel="import" href="../../polymer/lib/elements/dom-if.html">
</head>
<body>
<test-fixture id="combobox">
<template>
<vaadin-combo-box label="Label" items="[1, 2]"></vaadin-combo-box>
</template>
</test-fixture>
<test-fixture id="combobox-light">
<template>
<vaadin-combo-box-light>
<iron-input>
<input>
</iron-input>
</vaadin-combo-box-light>
</template>
</test-fixture>
<test-fixture id="combobox-if">
<template>
<div>
<dom-if if="">
<template>
<vaadin-combo-box label="Label" items="[1, 2]"></vaadin-combo-box>
</template>
</dom-if>
</div>
</template>
</test-fixture>
<script>
describe('toggling the dropdown', () => {
let combobox;
function setInputValue(value) {
combobox.inputElement.value = value;
combobox.inputElement.dispatchEvent(new CustomEvent('input'));
}
function clickToggleIcon() {
fireMousedownMouseupClick(combobox._toggleElement);
}
function overlay() {
return combobox.$.overlay.$.dropdown.$.overlay;
}
beforeEach(() => {
combobox = fixture('combobox');
});
describe('opening', () => {
it('should open synchronously by clicking label', () => {
expect(combobox.opened).to.be.false;
fireMousedownMouseupClick(combobox.inputElement.root.querySelector('label'));
expect(combobox.opened).to.be.true;
});
it('should not open synchronously by clicking label when autoOpenDisabled is true', () => {
combobox.autoOpenDisabled = true;
expect(combobox.opened).to.be.false;
fireMousedownMouseupClick(combobox.inputElement.root.querySelector('label'));
expect(combobox.opened).to.be.false;
});
it('should restore attribute focus-ring if it was initially set before opening and combo-box is focused', () => {
combobox.setAttribute('focus-ring', '');
combobox.opened = true;
combobox.opened = false;
expect(!combobox.hasAttribute('focused') || combobox.focusElement.hasAttribute('focus-ring')).to.be.true;
});
it('should open synchronously by clicking input', () => {
expect(combobox.opened).to.be.false;
fireMousedownMouseupClick(combobox.inputElement);
expect(combobox.opened).to.be.true;
});
it('should not open synchronously by clicking input when autoOpenDisabled is true', () => {
combobox.autoOpenDisabled = true;
expect(combobox.opened).to.be.false;
fireMousedownMouseupClick(combobox.inputElement);
expect(combobox.opened).to.be.false;
});
it('should open by clicking icon', () => {
clickToggleIcon();
expect(combobox.opened).to.be.true;
});
it('should open by clicking icon when autoOpenDisabled is true and input is invalid', () => {
combobox.autoOpenDisabled = true;
combobox.inputElement.value = 3;
combobox.inputElement.dispatchEvent(new CustomEvent('input'));
clickToggleIcon();
expect(combobox.opened).to.be.true;
});
it('should open on function call', () => {
combobox.open();
expect(combobox.opened).to.be.true;
});
it('should set body `pointer-events: none` on open and restore initial value on close', () => {
document.body.style.pointerEvents = 'painted';
combobox.open();
if (!(linux && window.chrome)) { // FIXME: randomly fails on Chrome + Travis
expect(getComputedStyle(document.body).pointerEvents).to.be.equal('none');
expect(getComputedStyle(combobox).pointerEvents).to.be.equal('auto');
// The actual overlay part of the overlay moved to body should dispatch pointer events
expect(getComputedStyle(combobox.$.overlay.$.dropdown.$.overlay.$.overlay).pointerEvents).to.be.equal('auto');
}
combobox.close();
expect(getComputedStyle(document.body).pointerEvents).to.be.equal('painted');
});
it('should not close an open popup', () => {
combobox.open();
combobox.open();
expect(combobox.opened).to.be.true;
});
it('should be hidden with null items', () => {
combobox.items = null;
combobox.open();
expect(combobox.opened);
expect(combobox.$.overlay.$.dropdown.$.overlay.hidden).to.be.true;
});
it('should be hidden with no items', () => {
combobox.items = [];
combobox.open();
expect(combobox.opened);
expect(combobox.$.overlay.$.dropdown.$.overlay.hidden).to.be.true;
});
// When using Shadow DOM polyfill, the style scope of the combobox might
// not get removed from the overlay after it's moved to the body. The
// following test ensures that the overlay doesn't keep the style scope
// of the combobox when moved during open.
it('should not leak combobox style scope to the overlay', () => {
// Test only when style scope classes are used
combobox.open();
combobox.close();
if (overlay().classList.contains('style-scope') &&
overlay().classList.contains('vaadin-combo-box')) {
combobox.open();
Polymer.flush();
expect(overlay().classList.contains('style-scope')).to.be.false;
expect(overlay().classList.contains('vaadin-combo-box')).to.be.false;
}
});
it('should extract the iron-list border mixin into custom CSS properties', () => {
const elem = fixture('combobox-if');
// render dom-if template
Polymer.flush();
combobox = elem.querySelector('vaadin-combo-box');
combobox.open();
const selector = combobox.$.overlay._selector;
['border-width', 'border-style', 'border-color'].forEach(prop => {
expect(getCustomPropertyValue(selector, `--iron-list-items-container_-_${prop}`)).to.be.ok;
});
});
describe('after opening', () => {
beforeEach(() => combobox.open());
function createMouseDown(preventDefaultSpy) {
const mouseDownEvent = new CustomEvent('mousedown', {
bubbles: true,
cancelable: true
});
mouseDownEvent.preventDefault = preventDefaultSpy;
return mouseDownEvent;
}
if (touchDevice) {
it('should not focus input on dropdown open', () => {
expect(combobox.inputElement.hasAttribute('focused')).to.equal(false);
});
it('should not refocus the input field when closed from icon', () => {
clickToggleIcon();
expect(combobox.inputElement.hasAttribute('focused')).to.equal(false);
});
} else {
it('should focus input on dropdown open', done => {
setTimeout(() => { // IE11
expect(combobox.inputElement.hasAttribute('focused')).to.be.true;
done();
}, 1);
});
it('should refocus the input field when closed from icon', done => {
clickToggleIcon();
setTimeout(() => { // IE11
expect(combobox.inputElement.hasAttribute('focused')).to.be.true;
done();
}, 1);
});
}
if (!touchDevice) {
it('should prevent default on overlay mousedown', () => {
const preventDefaultSpy = sinon.spy();
combobox.$.overlay.$.dropdown.$.overlay.dispatchEvent(createMouseDown(preventDefaultSpy));
expect(preventDefaultSpy).to.have.been.called;
});
it('should prevent default on overlay mousedown (vaadin-combo-box-light)', () => {
combobox = fixture('combobox-light');
const preventDefaultSpy = sinon.spy();
combobox.open();
combobox.$.overlay.$.dropdown.$.overlay.dispatchEvent(createMouseDown(preventDefaultSpy));
expect(preventDefaultSpy).to.have.been.called;
});
}
});
});
describe('closing', () => {
if (touchDevice) {
it('should close popup when clicking outside overlay', () => {
combobox.open();
fire('click', document.body);
expect(combobox.opened).to.be.false;
});
} else {
it('should close popup when clicking outside overlay', () => {
combobox.open();
document.body.click();
expect(combobox.opened).to.be.false;
});
}
it('should not close when clicking on the overlay', () => {
combobox.open();
fire('click', combobox.$.overlay.$.dropdown.$.overlay);
expect(combobox.opened).to.be.true;
});
it('should not close popup when clicking on any overlay children', () => {
combobox.open();
combobox.$.overlay._scroller.click();
expect(combobox.opened).to.be.true;
});
it('should close on clicking icon', () => {
combobox.open();
clickToggleIcon();
expect(combobox.opened).to.be.false;
});
it('should close the overlay when focus is lost', () => {
combobox.open();
fire('focusout', combobox.inputElement);
expect(combobox.opened).to.equal(false);
});
describe('filtered items are empty', () => {
it('should close the dropdown on non-existent values', () => {
combobox.open();
// Existent value
setInputValue('1');
expect(combobox.$.overlay.$.dropdown.$.overlay.opened).to.be.true;
expect(combobox.opened).to.be.true;
// Non-existent value
setInputValue('3');
expect(combobox.$.overlay.$.dropdown.$.overlay.opened).to.be.false;
expect(combobox.opened).to.be.true;
});
it('should not commit value the input on dropdown closing', () => {
combobox.open();
setInputValue('3');
expect(combobox.inputElement.value).to.equal('3');
expect(combobox.value).to.be.empty;
fire('focusout', combobox.inputElement);
expect(combobox.inputElement.value).to.be.empty;
});
});
// When using Shadow DOM polyfill, the style scope of the combobox might
// not get added to the overlay after it's moved back to the combobox.
// The following test ensures that the overlay doesn't keep the style
// scope of the combobox when moved during close.
it('should add combobox style scope to the overlay', () => {
// Test only when style scope classes are used
if (combobox.$.overlay.classList.contains('style-scope') &&
combobox.$.overlay.classList.contains('vaadin-combo-box')) {
combobox.open();
combobox.close();
Polymer.flush();
expect(combobox.$.overlay.classList.contains('style-scope')).to.be.true;
expect(combobox.$.overlay.classList.contains('vaadin-combo-box')).to.be.true;
}
});
});
describe('disabled', () => {
beforeEach(() => {
combobox.disabled = true;
});
it('toggleIcon should not be hidden when disabled', () => {
expect(getComputedStyle(combobox._toggleElement).display).not.to.equal('none');
});
it('dropdown should not be shown when disabled', () => {
combobox.inputElement.dispatchEvent(new CustomEvent('click', {bubbles: true, composed: true}));
expect(combobox.opened).to.be.false;
});
});
describe('read-only', () => {
beforeEach(() => {
combobox.readonly = true;
});
it('toggleIcon should not be hidden when read-only', () => {
expect(getComputedStyle(combobox._toggleElement).display).not.to.equal('none');
});
it('dropdown should not be shown when read-only', () => {
combobox.inputElement.dispatchEvent(new CustomEvent('click', {bubbles: true, composed: true}));
expect(combobox.opened).to.be.false;
});
});
describe('empty list', () => {
it('vaadin-combo-box-overlay should not be attached when filteredItems is empty', () => {
combobox.open();
expect(combobox.opened).to.be.true;
expect(document.querySelector('vaadin-combo-box-overlay')).to.be.ok;
combobox.close();
expect(combobox.opened).to.be.false;
expect(document.querySelector('vaadin-combo-box-overlay')).not.to.be.ok;
combobox.filteredItems = [];
combobox.open();
expect(combobox.opened).to.be.true;
expect(document.querySelector('vaadin-combo-box-overlay')).not.to.be.ok;
combobox.close();
expect(combobox.opened).to.be.false;
});
});
describe('lazy upgrade dropdown', () => {
const getDropdown = () => combobox.$.overlay.shadowRoot.querySelector('vaadin-combo-box-dropdown');
it('should have disable-upgrade attribute initially', () => {
const dropdown = getDropdown();
expect(dropdown.hasAttribute('disable-upgrade')).to.be.true;
expect(dropdown.$).to.be.not.ok;
});
it('should remove disable-upgrade attribute on open', () => {
combobox.open();
const dropdown = getDropdown();
expect(dropdown.hasAttribute('disable-upgrade')).to.be.false;
expect(dropdown.$).to.be.ok;
});
});
describe('external focus (initially)', () => {
let input, blurSpy;
beforeEach(() => {
input = document.createElement('input');
combobox.insertAdjacentElement('beforebegin', input);
input.focus();
blurSpy = sinon.spy(input, 'blur');
});
if (touchDevice) {
it('should blur previously focused element when clicking on toggle button', () => {
clickToggleIcon();
expect(blurSpy).to.be.called;
});
}
});
});
</script>
</body>
</html>