-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathvscode-multi-select.test.ts
More file actions
457 lines (378 loc) · 14.7 KB
/
Copy pathvscode-multi-select.test.ts
File metadata and controls
457 lines (378 loc) · 14.7 KB
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
446
447
448
449
450
451
452
453
454
455
456
457
/* eslint-disable @typescript-eslint/no-unused-expressions */
import {expect, fixture, html} from '@open-wc/testing';
import {sendKeys} from '@web/test-runner-commands';
import sinon from 'sinon';
import '../vscode-option/index.js';
import {clickOnElement, moveMouseOnElement} from '../includes/test-helpers.js';
import {VscodeOption} from '../vscode-option/index.js';
import {VscodeMultiSelect} from './index.js';
describe('vscode-multi-select', () => {
it('is defined', () => {
const el = document.createElement('vscode-multi-select');
expect(el).to.instanceOf(VscodeMultiSelect);
});
it('should display selected value', async () => {
const el = (await fixture(html`
<vscode-multi-select>
<vscode-option>Lorem</vscode-option>
<vscode-option selected>Ipsum</vscode-option>
<vscode-option>Dolor</vscode-option>
</vscode-multi-select>
`)) as VscodeMultiSelect;
const badge = el.shadowRoot?.querySelector('.select-face-badge');
expect(badge).lightDom.to.eq('1 Selected');
expect(el.selectedIndexes).to.eql([1]);
expect(el.value).to.eql(['Ipsum']);
});
it('selectedIndexes should be set', async () => {
const el = (await fixture(html`
<vscode-multi-select>
<vscode-option>Lorem</vscode-option>
<vscode-option>Ipsum</vscode-option>
<vscode-option>Dolor</vscode-option>
</vscode-multi-select>
`)) as VscodeMultiSelect;
el.selectedIndexes = [0, 1];
expect(el.selectedIndexes).to.eql([0, 1]);
});
it('values should be set', async () => {
const el = (await fixture(html`
<vscode-multi-select>
<vscode-option value="1">One</vscode-option>
<vscode-option value="2">Two</vscode-option>
<vscode-option value="3">Three</vscode-option>
</vscode-multi-select>
`)) as VscodeMultiSelect;
el.value = ['2', '3'];
const selectFace =
el.shadowRoot?.querySelector<HTMLDivElement>('.select-face');
selectFace!.click();
await el.updateComplete;
const checkboxes = el.shadowRoot?.querySelectorAll('li .checkbox-icon');
expect(el.value).to.eql(['2', '3']);
expect(el.selectedIndexes).to.eql([1, 2]);
expect(checkboxes?.item(0).classList.contains('checked')).to.eq(false);
expect(checkboxes?.item(1).classList.contains('checked')).to.eq(true);
expect(checkboxes?.item(2).classList.contains('checked')).to.eq(true);
});
it('when an option is clicked the selectedIndex and the value should be set', async () => {
const el = (await fixture(html`
<vscode-multi-select>
<vscode-option value="1">One</vscode-option>
<vscode-option value="2">Two</vscode-option>
<vscode-option value="3">Three</vscode-option>
</vscode-multi-select>
`)) as VscodeMultiSelect;
const selectFace =
el.shadowRoot?.querySelector<HTMLDivElement>('.select-face');
selectFace!.click();
await el.updateComplete;
const firstOption =
el.shadowRoot?.querySelectorAll<HTMLLIElement>('.option')[0];
firstOption!.click();
await el.updateComplete;
expect(el.value).to.deep.equal(['1']);
expect(el.selectedIndexes).to.deep.equal([0]);
});
it('should apply combobox mode', async () => {
const el = await fixture(
html`<vscode-multi-select combobox>
<vscode-option value="1">One</vscode-option>
<vscode-option value="2">Two</vscode-option>
<vscode-option value="3">Three</vscode-option>
</vscode-multi-select>`
);
const comboboxFace = el.shadowRoot?.querySelector('.combobox-face');
expect(comboboxFace).to.be.ok;
});
it('selects all options', async () => {
const el = await fixture<VscodeMultiSelect>(
html`<vscode-multi-select>
<vscode-option value="1">One</vscode-option>
<vscode-option value="2">Two</vscode-option>
<vscode-option value="3">Three</vscode-option>
</vscode-multi-select>`
);
el.selectAll();
await el.updateComplete;
expect(el.shadowRoot?.querySelector('.select-face-badge')).lightDom.to.eq(
'3 Selected'
);
});
it('de-selects all options', async () => {
const el = await fixture<VscodeMultiSelect>(
html`<vscode-multi-select>
<vscode-option value="1" selected>One</vscode-option>
<vscode-option value="2" selected>Two</vscode-option>
<vscode-option value="3" selected>Three</vscode-option>
</vscode-multi-select>`
);
expect(el.shadowRoot?.querySelector('.select-face-badge')).lightDom.to.eq(
'3 Selected'
);
el.selectNone();
await el.updateComplete;
expect(el.shadowRoot?.querySelector('.select-face-badge')).lightDom.to.eq(
'0 Selected'
);
});
it('should be unfocusable when it is disabled', () => {
const el = document.createElement('vscode-multi-select');
el.tabIndex = 2;
el.disabled = true;
expect(el.tabIndex).to.eq(-1);
});
it('should aria-disabled attribute applied when it is disabled', () => {
const el = document.createElement('vscode-multi-select');
el.disabled = true;
expect(el.getAttribute('aria-disabled')).to.eq('true');
});
it('should original tabindex restored when enabled again', () => {
const el = document.createElement('vscode-multi-select');
el.tabIndex = 2;
el.disabled = true;
expect(el.tabIndex).to.eq(-1);
el.disabled = false;
expect(el.tabIndex).to.eq(2);
});
it('should selectedIndexes reflect the selected options when value set through property', async () => {
const el = await fixture<VscodeMultiSelect>(
html`<vscode-multi-select>
<vscode-option value="A">A</vscode-option>
<vscode-option value="B">B</vscode-option>
<vscode-option value="C">C</vscode-option>
</vscode-multi-select>`
);
el.value = ['A', 'B', 'C'];
expect(el.selectedIndexes).to.eql([0, 1, 2]);
});
it('Set the "value" property before adding selectable options', async () => {
const el = await fixture<VscodeMultiSelect>(
html`<vscode-multi-select></vscode-multi-select>`
);
el.value = ['dolor'];
const op1 = document.createElement('vscode-option');
const op2 = document.createElement('vscode-option');
const op3 = document.createElement('vscode-option');
op1.innerHTML = 'lorem';
op2.innerHTML = 'ipsum';
op3.innerHTML = 'dolor';
el.appendChild(op1);
el.appendChild(op2);
el.appendChild(op3);
await el.updateComplete;
expect(el.value).to.eql(['dolor']);
});
it('open by default', async () => {
const sl = await fixture(
html`<vscode-multi-select open>
<vscode-option>Lorem</vscode-option>
<vscode-option>Ipsum</vscode-option>
<vscode-option>Dolor</vscode-option>
</vscode-multi-select>`
);
expect(sl.shadowRoot?.querySelector('ul.options')).to.be.ok;
});
it('shows selected option when opened by default', async () => {
const sl = await fixture(
html`<vscode-multi-select open>
<vscode-option>Lorem</vscode-option>
<vscode-option selected>Ipsum</vscode-option>
<vscode-option>Dolor</vscode-option>
</vscode-multi-select>`
);
const op = sl.shadowRoot?.querySelector<HTMLLIElement>(
'ul.options li:nth-child(2)'
);
expect(op).lightDom.to.eq(`
<span class="checkbox-icon checked"></span>
<span class="option-label">Ipsum</span>
`);
expect(op?.classList.contains('selected')).to.be.true;
});
it('changes the description of an option in an existing select', async () => {
const el = await fixture<VscodeMultiSelect>(html`
<vscode-multi-select>
<vscode-option>Lorem</vscode-option>
<vscode-option>Ipsum</vscode-option>
<vscode-option>Dolor</vscode-option>
</vscode-multi-select>
`);
const secondOption = el.querySelectorAll<VscodeOption>('vscode-option')[1];
secondOption.description = 'Test description';
await el.updateComplete;
await clickOnElement(el);
await el.updateComplete;
await moveMouseOnElement(el.shadowRoot!.querySelectorAll('li')[1]);
await el.updateComplete;
const desc = el.shadowRoot!.querySelector<HTMLDivElement>('.description');
expect(desc).lightDom.to.eq('Test description');
});
it('changes the label of an option in an existing select', async () => {
const el = await fixture<VscodeMultiSelect>(html`
<vscode-multi-select>
<vscode-option>Lorem</vscode-option>
<vscode-option>Ipsum</vscode-option>
<vscode-option>Dolor</vscode-option>
</vscode-multi-select>
`);
const secondOption = el.querySelectorAll<VscodeOption>('vscode-option')[1];
secondOption.innerHTML = 'Test label';
await el.updateComplete;
await clickOnElement(el);
await el.updateComplete;
const li = el.shadowRoot!.querySelectorAll<HTMLLIElement>('li')[1];
const label = li.querySelector('.option-label');
expect(label).lightDom.to.eq('Test label');
});
it('changes the disabled state of an option in an existing select', async () => {
const el = await fixture<VscodeMultiSelect>(html`
<vscode-multi-select>
<vscode-option>Lorem</vscode-option>
<vscode-option>Ipsum</vscode-option>
<vscode-option>Dolor</vscode-option>
</vscode-multi-select>
`);
const secondOption = el.querySelectorAll<VscodeOption>('vscode-option')[1];
secondOption.disabled = true;
await el.updateComplete;
await clickOnElement(el);
await el.updateComplete;
const li = el.shadowRoot!.querySelectorAll<HTMLLIElement>('li')[1];
expect(li.classList.contains('disabled')).to.be.true;
});
it('checks validity when required property is changed', async () => {
const el = await fixture<VscodeMultiSelect>(
html`<vscode-multi-select combobox>
<vscode-option>Lorem</vscode-option>
<vscode-option>Ipsum</vscode-option>
</vscode-multi-select>`
);
const isValidBefore = el.checkValidity();
el.setAttribute('required', '');
await el.updateComplete;
const isValidAfter = el.checkValidity();
expect(isValidBefore).to.be.true;
expect(isValidAfter).to.be.false;
});
it('creates and select suggested option', async () => {
const el = await fixture<VscodeMultiSelect>(
html`<vscode-multi-select combobox creatable
><vscode-option>Lorem</vscode-option></vscode-multi-select
>`
);
const createOptionHandlerSpy = sinon.spy();
el.addEventListener(
'vsc-multi-select-create-option',
createOptionHandlerSpy
);
const changeHandlerSpy = sinon.spy();
el.addEventListener('change', changeHandlerSpy);
el.focus();
await el.updateComplete;
await sendKeys({type: 'asdf'});
await sendKeys({down: 'ArrowDown'});
await sendKeys({down: 'Enter'});
expect(createOptionHandlerSpy.getCalls()[0].args[0].detail.value).to.eq(
'asdf'
);
expect(changeHandlerSpy.called).to.be.true;
expect(el.value).to.eql(['asdf']);
});
describe('i18n override props', () => {
it('selected-text overrides the badge label', async () => {
const el = await fixture<VscodeMultiSelect>(html`
<vscode-multi-select selected-text="Ausgewählt">
<vscode-option selected>One</vscode-option>
<vscode-option>Two</vscode-option>
</vscode-multi-select>
`);
expect(el.shadowRoot?.querySelector('.select-face-badge')).lightDom.to.eq(
'1 Ausgewählt'
);
});
it('selected-text overrides the badge label when nothing is selected', async () => {
const el = await fixture<VscodeMultiSelect>(html`
<vscode-multi-select selected-text="Ausgewählt">
<vscode-option>One</vscode-option>
<vscode-option>Two</vscode-option>
</vscode-multi-select>
`);
expect(el.shadowRoot?.querySelector('.select-face-badge')).lightDom.to.eq(
'0 Ausgewählt'
);
});
it('open-button-aria-label overrides the combobox button aria-label', async () => {
const el = await fixture<VscodeMultiSelect>(html`
<vscode-multi-select combobox open-button-aria-label="Öffnen">
<vscode-option>One</vscode-option>
</vscode-multi-select>
`);
const btn =
el.shadowRoot?.querySelector<HTMLButtonElement>('.combobox-button');
expect(btn?.getAttribute('aria-label')).to.eq('Öffnen');
});
it('no-options-text is shown when the filter matches nothing', async () => {
const el = await fixture<VscodeMultiSelect>(html`
<vscode-multi-select combobox no-options-text="Keine Optionen">
<vscode-option>Lorem</vscode-option>
</vscode-multi-select>
`);
el.focus();
await el.updateComplete;
await sendKeys({type: 'zzz'});
await el.updateComplete;
const noOpts = el.shadowRoot?.querySelector('.no-options');
expect(noOpts?.textContent?.trim()).to.eq('Keine Optionen');
});
it('create-option-prefix overrides the creatable suggestion text', async () => {
const el = await fixture<VscodeMultiSelect>(html`
<vscode-multi-select
combobox
creatable
create-option-prefix="Hinzufügen"
>
<vscode-option>Lorem</vscode-option>
</vscode-multi-select>
`);
el.focus();
await el.updateComplete;
await sendKeys({type: 'Neu'});
await el.updateComplete;
const placeholder = el.shadowRoot?.querySelector('.option.placeholder');
expect(placeholder?.textContent?.trim()).to.eq('Hinzufügen "Neu"');
});
it('select-all-title overrides the select-all button tooltip', async () => {
const el = await fixture<VscodeMultiSelect>(html`
<vscode-multi-select select-all-title="Alle auswählen">
<vscode-option>One</vscode-option>
<vscode-option>Two</vscode-option>
</vscode-multi-select>
`);
expect(el.selectAllTitle).to.eq('Alle auswählen');
});
it('deselect-all-title overrides the deselect-all button tooltip', async () => {
const el = await fixture<VscodeMultiSelect>(html`
<vscode-multi-select deselect-all-title="Alle abwählen">
<vscode-option>One</vscode-option>
<vscode-option>Two</vscode-option>
</vscode-multi-select>
`);
expect(el.deselectAllTitle).to.eq('Alle abwählen');
});
it('accept-button-text overrides the OK button label', async () => {
const el = await fixture<VscodeMultiSelect>(html`
<vscode-multi-select accept-button-text="Bestätigen">
<vscode-option>One</vscode-option>
<vscode-option>Two</vscode-option>
</vscode-multi-select>
`);
expect(el.acceptButtonText).to.eq('Bestätigen');
});
});
it('selects multiple options with keyboard');
it('selectedIndexes sync with values');
it(
'dispatch change event, set form value, manage required state (enter key press)'
);
});