@duskmoon-dev/el-select 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,305 @@
1
+ import { expect, test, describe, beforeEach, afterEach } from 'bun:test';
2
+ import { ElDmSelect, register } from './index';
3
+
4
+ register();
5
+
6
+ describe('ElDmSelect', () => {
7
+ let container: HTMLDivElement;
8
+
9
+ beforeEach(() => {
10
+ container = document.createElement('div');
11
+ document.body.appendChild(container);
12
+ });
13
+
14
+ afterEach(() => {
15
+ container.remove();
16
+ });
17
+
18
+ test('is defined', () => {
19
+ expect(customElements.get('el-dm-select')).toBe(ElDmSelect);
20
+ });
21
+
22
+ test('creates a shadow root', () => {
23
+ const el = document.createElement('el-dm-select') as ElDmSelect;
24
+ container.appendChild(el);
25
+
26
+ expect(el.shadowRoot).toBeDefined();
27
+ });
28
+
29
+ test('has combobox role', () => {
30
+ const el = document.createElement('el-dm-select') as ElDmSelect;
31
+ container.appendChild(el);
32
+
33
+ const combobox = el.shadowRoot?.querySelector('[role="combobox"]');
34
+ expect(combobox).toBeDefined();
35
+ });
36
+
37
+ test('reflects disabled attribute', () => {
38
+ const el = document.createElement('el-dm-select') as ElDmSelect;
39
+ el.disabled = true;
40
+ container.appendChild(el);
41
+
42
+ expect(el.hasAttribute('disabled')).toBe(true);
43
+ });
44
+
45
+ test('sets placeholder property', () => {
46
+ const el = document.createElement('el-dm-select') as ElDmSelect;
47
+ el.placeholder = 'Choose...';
48
+ container.appendChild(el);
49
+
50
+ expect(el.placeholder).toBe('Choose...');
51
+ });
52
+
53
+ test('reflects size attribute', () => {
54
+ const el = document.createElement('el-dm-select') as ElDmSelect;
55
+ el.size = 'lg';
56
+ container.appendChild(el);
57
+
58
+ expect(el.getAttribute('size')).toBe('lg');
59
+ });
60
+
61
+ test('reflects multiple attribute', () => {
62
+ const el = document.createElement('el-dm-select') as ElDmSelect;
63
+ el.multiple = true;
64
+ container.appendChild(el);
65
+
66
+ expect(el.hasAttribute('multiple')).toBe(true);
67
+ });
68
+
69
+ test('reflects clearable attribute', () => {
70
+ const el = document.createElement('el-dm-select') as ElDmSelect;
71
+ el.clearable = true;
72
+ container.appendChild(el);
73
+
74
+ expect(el.hasAttribute('clearable')).toBe(true);
75
+ });
76
+
77
+ test('reflects searchable attribute', () => {
78
+ const el = document.createElement('el-dm-select') as ElDmSelect;
79
+ el.searchable = true;
80
+ container.appendChild(el);
81
+
82
+ expect(el.hasAttribute('searchable')).toBe(true);
83
+ });
84
+
85
+ test('reflects tree-data attribute', () => {
86
+ const el = document.createElement('el-dm-select') as ElDmSelect;
87
+ el.treeData = true;
88
+ container.appendChild(el);
89
+
90
+ expect(el.hasAttribute('tree-data')).toBe(true);
91
+ });
92
+
93
+ test('reflects readonly attribute', () => {
94
+ const el = document.createElement('el-dm-select') as ElDmSelect;
95
+ el.readonly = true;
96
+ container.appendChild(el);
97
+
98
+ expect(el.hasAttribute('readonly')).toBe(true);
99
+ });
100
+
101
+ test('reflects validation-state attribute', () => {
102
+ const el = document.createElement('el-dm-select') as ElDmSelect;
103
+ (el as any).validationState = 'invalid';
104
+ container.appendChild(el);
105
+
106
+ expect(el.getAttribute('validation-state')).toBe('invalid');
107
+ });
108
+
109
+ test('renders placeholder text', () => {
110
+ const el = document.createElement('el-dm-select') as ElDmSelect;
111
+ el.placeholder = 'Pick one';
112
+ container.appendChild(el);
113
+
114
+ const placeholder = el.shadowRoot?.querySelector('.select-placeholder');
115
+ expect(placeholder?.textContent?.trim()).toBe('Pick one');
116
+ });
117
+
118
+ test('setOptions renders options', () => {
119
+ const el = document.createElement('el-dm-select') as ElDmSelect;
120
+ container.appendChild(el);
121
+
122
+ el.setOptions([
123
+ { value: 'a', label: 'Alpha' },
124
+ { value: 'b', label: 'Beta' },
125
+ ]);
126
+
127
+ const options = el.shadowRoot?.querySelectorAll('.select-option');
128
+ expect(options?.length).toBe(2);
129
+ });
130
+
131
+ test('renders dropdown with listbox role', () => {
132
+ const el = document.createElement('el-dm-select') as ElDmSelect;
133
+ container.appendChild(el);
134
+
135
+ el.setOptions([{ value: 'a', label: 'Alpha' }]);
136
+
137
+ const listbox = el.shadowRoot?.querySelector('[role="listbox"]');
138
+ expect(listbox).toBeDefined();
139
+ });
140
+
141
+ test('has aria-expanded on trigger', () => {
142
+ const el = document.createElement('el-dm-select') as ElDmSelect;
143
+ container.appendChild(el);
144
+
145
+ const trigger = el.shadowRoot?.querySelector('.select-trigger');
146
+ expect(trigger?.getAttribute('aria-expanded')).toBe('false');
147
+ });
148
+
149
+ test('setOptions with group headers', () => {
150
+ const el = document.createElement('el-dm-select') as ElDmSelect;
151
+ container.appendChild(el);
152
+
153
+ el.setOptions([
154
+ { value: 'a', label: 'Alpha', group: 'Greek' },
155
+ { value: '1', label: 'One', group: 'Numbers' },
156
+ ]);
157
+
158
+ const groupHeaders = el.shadowRoot?.querySelectorAll('.select-group-header');
159
+ expect(groupHeaders?.length).toBe(2);
160
+ });
161
+
162
+ test('shows empty state when no options match', () => {
163
+ const el = document.createElement('el-dm-select') as ElDmSelect;
164
+ container.appendChild(el);
165
+
166
+ el.setOptions([]);
167
+
168
+ const empty = el.shadowRoot?.querySelector('.select-empty');
169
+ expect(empty).toBeDefined();
170
+ });
171
+
172
+ test('renders selected value in trigger', () => {
173
+ const el = document.createElement('el-dm-select') as ElDmSelect;
174
+ el.value = 'a';
175
+ container.appendChild(el);
176
+
177
+ el.setOptions([
178
+ { value: 'a', label: 'Alpha' },
179
+ { value: 'b', label: 'Beta' },
180
+ ]);
181
+
182
+ const valueDisplay = el.shadowRoot?.querySelector('.select-value');
183
+ expect(valueDisplay?.textContent?.trim()).toBe('Alpha');
184
+ });
185
+
186
+ test('renders tags in multiple mode', () => {
187
+ const el = document.createElement('el-dm-select') as ElDmSelect;
188
+ el.multiple = true;
189
+ el.value = '["a","b"]';
190
+ container.appendChild(el);
191
+
192
+ el.setOptions([
193
+ { value: 'a', label: 'Alpha' },
194
+ { value: 'b', label: 'Beta' },
195
+ { value: 'c', label: 'Charlie' },
196
+ ]);
197
+
198
+ const tags = el.shadowRoot?.querySelectorAll('.select-tag');
199
+ expect(tags?.length).toBe(2);
200
+ });
201
+
202
+ test('renders search input when searchable', () => {
203
+ const el = document.createElement('el-dm-select') as ElDmSelect;
204
+ el.searchable = true;
205
+ container.appendChild(el);
206
+
207
+ el.setOptions([{ value: 'a', label: 'Alpha' }]);
208
+
209
+ const searchInput = el.shadowRoot?.querySelector('.select-search-input');
210
+ expect(searchInput).toBeDefined();
211
+ });
212
+
213
+ test('renders clear button when clearable and has value', () => {
214
+ const el = document.createElement('el-dm-select') as ElDmSelect;
215
+ el.clearable = true;
216
+ el.value = 'a';
217
+ container.appendChild(el);
218
+
219
+ el.setOptions([{ value: 'a', label: 'Alpha' }]);
220
+
221
+ const clearBtn = el.shadowRoot?.querySelector('.select-clear');
222
+ expect(clearBtn).toBeDefined();
223
+ });
224
+
225
+ test('setTreeOptions renders tree nodes', () => {
226
+ const el = document.createElement('el-dm-select') as ElDmSelect;
227
+ el.treeData = true;
228
+ el.defaultExpandAll = true;
229
+ container.appendChild(el);
230
+
231
+ el.setTreeOptions([
232
+ {
233
+ value: 'parent',
234
+ label: 'Parent',
235
+ children: [
236
+ { value: 'child1', label: 'Child 1' },
237
+ { value: 'child2', label: 'Child 2' },
238
+ ],
239
+ },
240
+ ]);
241
+
242
+ const treeNodes = el.shadowRoot?.querySelectorAll('.select-tree-node');
243
+ expect(treeNodes?.length).toBeGreaterThanOrEqual(1);
244
+ });
245
+
246
+ test('setOptions with 3 items renders 3 options', () => {
247
+ const el = document.createElement('el-dm-select') as ElDmSelect;
248
+ container.appendChild(el);
249
+
250
+ el.setOptions([
251
+ { value: 'x', label: 'X-Ray' },
252
+ { value: 'y', label: 'Yankee' },
253
+ { value: 'z', label: 'Zulu' },
254
+ ]);
255
+
256
+ const options = el.shadowRoot?.querySelectorAll('.select-option');
257
+ expect(options?.length).toBe(3);
258
+ });
259
+
260
+ test('handles invalid JSON options gracefully', () => {
261
+ const el = document.createElement('el-dm-select') as ElDmSelect;
262
+ el.options = 'not-json';
263
+ container.appendChild(el);
264
+
265
+ // Should not throw, renders empty
266
+ const options = el.shadowRoot?.querySelectorAll('.select-option');
267
+ expect(options?.length).toBe(0);
268
+ });
269
+
270
+ test('arrow icon present in trigger', () => {
271
+ const el = document.createElement('el-dm-select') as ElDmSelect;
272
+ container.appendChild(el);
273
+
274
+ const arrow = el.shadowRoot?.querySelector('.select-arrow');
275
+ expect(arrow).toBeDefined();
276
+ });
277
+
278
+ test('marks selected option', () => {
279
+ const el = document.createElement('el-dm-select') as ElDmSelect;
280
+ el.value = 'b';
281
+ container.appendChild(el);
282
+
283
+ el.setOptions([
284
+ { value: 'a', label: 'Alpha' },
285
+ { value: 'b', label: 'Beta' },
286
+ ]);
287
+
288
+ const selected = el.shadowRoot?.querySelector('.select-option.selected');
289
+ expect(selected).toBeDefined();
290
+ expect(selected?.textContent).toContain('Beta');
291
+ });
292
+
293
+ test('renders disabled options', () => {
294
+ const el = document.createElement('el-dm-select') as ElDmSelect;
295
+ container.appendChild(el);
296
+
297
+ el.setOptions([
298
+ { value: 'a', label: 'Alpha', disabled: true },
299
+ { value: 'b', label: 'Beta' },
300
+ ]);
301
+
302
+ const disabledOpt = el.shadowRoot?.querySelector('.select-option.disabled');
303
+ expect(disabledOpt).toBeDefined();
304
+ });
305
+ });
@@ -4,8 +4,8 @@
4
4
  * A unified select component supporting single, multi-select, and tree-select modes.
5
5
  */
6
6
 
7
- import { BaseElement, css } from '@duskmoon-dev/el-core';
8
- import type { Size, ValidationState } from '@duskmoon-dev/el-core';
7
+ import { BaseElement, css } from '@duskmoon-dev/el-base';
8
+ import type { Size, ValidationState } from '@duskmoon-dev/el-base';
9
9
 
10
10
  // Icons with explicit dimensions for proper rendering
11
11
  const chevronDownIcon = `<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m6 9 6 6 6-6"/></svg>`;