kempo-ui 0.0.81 → 0.0.82

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.
@@ -13,12 +13,13 @@ Use this skill any time you are asked to create a new component or add a new cus
13
13
 
14
14
  ## Overview
15
15
 
16
- Creating a component involves four steps:
16
+ Creating a component involves five steps:
17
17
 
18
18
  1. **Choose the base component** — pick the right rendering strategy
19
19
  2. **Write the source file** in `src/components/`
20
20
  3. **Register the custom element** at the bottom of the source file
21
21
  4. **Add documentation** in `docs/components/`
22
+ 5. **Write and run unit tests** in `tests/components/`
22
23
 
23
24
  ---
24
25
 
@@ -193,6 +194,72 @@ Key requirements:
193
194
 
194
195
  ---
195
196
 
197
+ ## Step 5: Write and Run Unit Tests
198
+
199
+ Create `tests/components/MyComponent.browser-test.js`. Use an existing test file (e.g. `tests/components/Toggle.browser-test.js`) as a reference.
200
+
201
+ Key conventions:
202
+ - Import the component class at the top.
203
+ - Define an async `createMyComponent()` helper that builds the DOM, appends it to `document.body`, awaits `el.updateComplete`, and returns `{ container, el }`.
204
+ - Define a `cleanup(container)` helper that removes the container from the DOM.
205
+ - Export a default plain object where each key is a test description and each value is an `async ({pass, fail}) => {}` function.
206
+ - Always call `cleanup(container)` before every `pass()` or `fail()` call.
207
+ - Use multi-line comments to group related tests (e.g. `/* Element Creation */`, `/* Properties */`).
208
+
209
+ Tests to include at minimum:
210
+ - Element is created and is an instance of the component class
211
+ - Element has a shadow root
212
+ - Default property values are correct
213
+ - Attribute reflection works (if applicable)
214
+ - Public methods behave correctly
215
+ - Events are dispatched correctly (if applicable)
216
+
217
+ Example skeleton:
218
+
219
+ ```javascript
220
+ import MyComponent from '../../src/components/MyComponent.js';
221
+
222
+ const createMyComponent = async () => {
223
+ const container = document.createElement('div');
224
+ container.innerHTML = `<k-my-component></k-my-component>`;
225
+ document.body.appendChild(container);
226
+ const el = container.querySelector('k-my-component');
227
+ await el.updateComplete;
228
+ return { container, el };
229
+ };
230
+
231
+ const cleanup = (container) => {
232
+ if(container && container.parentNode){
233
+ container.parentNode.removeChild(container);
234
+ }
235
+ };
236
+
237
+ export default {
238
+ /*
239
+ Element Creation
240
+ */
241
+ 'should create my-component element': async ({pass, fail}) => {
242
+ const { container, el } = await createMyComponent();
243
+ if(!(el instanceof MyComponent)){
244
+ cleanup(container);
245
+ return fail('Element should be instance of MyComponent');
246
+ }
247
+ cleanup(container);
248
+ pass('MyComponent element created correctly');
249
+ },
250
+ };
251
+ ```
252
+
253
+ After writing the tests, run them to confirm they all pass:
254
+
255
+ ```
256
+ npm run test -- MyComponent
257
+ ```
258
+
259
+ The partial string `MyComponent` will match any test file whose path contains that string. Fix any failures before considering the component complete.
260
+
261
+ ---
262
+
196
263
  ## Component Architecture and Communication
197
264
 
198
265
  - Use **methods** to trigger actions. Events notify that something already happened; they should not be used to trigger logic.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kempo-ui",
3
- "version": "0.0.81",
3
+ "version": "0.0.82",
4
4
  "type": "module",
5
5
  "description": "A Lit based web-component library",
6
6
  "main": "index.js",
@@ -0,0 +1,104 @@
1
+ import FilterItem from '../../src/components/FilterItem.js';
2
+
3
+ const createFilterItem = async (content = 'item') => {
4
+ const container = document.createElement('div');
5
+ container.innerHTML = `<k-filter-item>${content}</k-filter-item>`;
6
+ document.body.appendChild(container);
7
+ const el = container.querySelector('k-filter-item');
8
+ await el.updateComplete;
9
+ return { container, el };
10
+ };
11
+
12
+ const cleanup = (container) => {
13
+ if(container && container.parentNode){
14
+ container.parentNode.removeChild(container);
15
+ }
16
+ };
17
+
18
+ export default {
19
+ /*
20
+ Element Creation
21
+ */
22
+ 'should create filter-item element': async ({pass, fail}) => {
23
+ const { container, el } = await createFilterItem();
24
+ if(!el){
25
+ cleanup(container);
26
+ return fail('k-filter-item element should be created');
27
+ }
28
+ if(!(el instanceof FilterItem)){
29
+ cleanup(container);
30
+ return fail('Element should be instance of FilterItem');
31
+ }
32
+ cleanup(container);
33
+ pass('FilterItem element created correctly');
34
+ },
35
+
36
+ 'should have shadow root': async ({pass, fail}) => {
37
+ const { container, el } = await createFilterItem();
38
+ if(!el.shadowRoot){
39
+ cleanup(container);
40
+ return fail('FilterItem should have shadow root');
41
+ }
42
+ cleanup(container);
43
+ pass('FilterItem has shadow root');
44
+ },
45
+
46
+ /*
47
+ Visibility
48
+ */
49
+ 'should be visible by default': async ({pass, fail}) => {
50
+ const { container, el } = await createFilterItem();
51
+ if(el.hidden){
52
+ cleanup(container);
53
+ return fail('FilterItem should be visible by default');
54
+ }
55
+ cleanup(container);
56
+ pass('FilterItem is visible by default');
57
+ },
58
+
59
+ 'setting hidden should hide the element': async ({pass, fail}) => {
60
+ const { container, el } = await createFilterItem();
61
+ el.hidden = true;
62
+ await el.updateComplete;
63
+ const style = getComputedStyle(el);
64
+ if(style.display !== 'none'){
65
+ cleanup(container);
66
+ return fail(`Expected display:none when hidden, got ${style.display}`);
67
+ }
68
+ cleanup(container);
69
+ pass('FilterItem is hidden when hidden attribute is set');
70
+ },
71
+
72
+ 'removing hidden should show the element': async ({pass, fail}) => {
73
+ const { container, el } = await createFilterItem();
74
+ el.hidden = true;
75
+ await el.updateComplete;
76
+ el.hidden = false;
77
+ await el.updateComplete;
78
+ const style = getComputedStyle(el);
79
+ if(style.display === 'none'){
80
+ cleanup(container);
81
+ return fail('FilterItem should be visible after removing hidden');
82
+ }
83
+ cleanup(container);
84
+ pass('FilterItem is visible after removing hidden');
85
+ },
86
+
87
+ /*
88
+ filter-keywords attribute
89
+ */
90
+ 'should accept filter-keywords attribute': async ({pass, fail}) => {
91
+ const container = document.createElement('div');
92
+ container.innerHTML = `<k-filter-item filter-keywords="foo bar">content</k-filter-item>`;
93
+ document.body.appendChild(container);
94
+ const el = container.querySelector('k-filter-item');
95
+ await el.updateComplete;
96
+ const kw = el.getAttribute('filter-keywords');
97
+ if(kw !== 'foo bar'){
98
+ cleanup(container);
99
+ return fail(`Expected filter-keywords "foo bar", got "${kw}"`);
100
+ }
101
+ cleanup(container);
102
+ pass('filter-keywords attribute set correctly');
103
+ },
104
+ };
@@ -0,0 +1,162 @@
1
+ import FilterList from '../../src/components/FilterList.js';
2
+ import '../../src/components/FilterItem.js';
3
+
4
+ const createFilterList = async (items = []) => {
5
+ const container = document.createElement('div');
6
+ container.innerHTML = `<k-filter-list>${items.map(kw => `<k-filter-item filter-keywords="${kw}">${kw}</k-filter-item>`).join('')}</k-filter-list>`;
7
+ document.body.appendChild(container);
8
+ const el = container.querySelector('k-filter-list');
9
+ await el.updateComplete;
10
+ return { container, el };
11
+ };
12
+
13
+ const cleanup = (container) => {
14
+ if(container && container.parentNode){
15
+ container.parentNode.removeChild(container);
16
+ }
17
+ };
18
+
19
+ export default {
20
+ /*
21
+ Element Creation
22
+ */
23
+ 'should create filter-list element': async ({pass, fail}) => {
24
+ const { container, el } = await createFilterList();
25
+ if(!el){
26
+ cleanup(container);
27
+ return fail('k-filter-list element should be created');
28
+ }
29
+ if(!(el instanceof FilterList)){
30
+ cleanup(container);
31
+ return fail('Element should be instance of FilterList');
32
+ }
33
+ cleanup(container);
34
+ pass('FilterList element created correctly');
35
+ },
36
+
37
+ 'should have shadow root': async ({pass, fail}) => {
38
+ const { container, el } = await createFilterList();
39
+ if(!el.shadowRoot){
40
+ cleanup(container);
41
+ return fail('FilterList should have shadow root');
42
+ }
43
+ cleanup(container);
44
+ pass('FilterList has shadow root');
45
+ },
46
+
47
+ /*
48
+ filter() — empty term
49
+ */
50
+ 'filter("") should show all items': async ({pass, fail}) => {
51
+ const { container, el } = await createFilterList(['apple', 'banana', 'cherry']);
52
+ el.filter('');
53
+ const items = [...el.querySelectorAll('k-filter-item')];
54
+ const hidden = items.filter(i => i.hidden);
55
+ if(hidden.length !== 0){
56
+ cleanup(container);
57
+ return fail(`Expected 0 hidden items, got ${hidden.length}`);
58
+ }
59
+ cleanup(container);
60
+ pass('All items visible when term is empty');
61
+ },
62
+
63
+ /*
64
+ filter() — single word
65
+ */
66
+ 'filter("apple") should hide non-matching items': async ({pass, fail}) => {
67
+ const { container, el } = await createFilterList(['apple', 'banana', 'cherry']);
68
+ el.filter('apple');
69
+ const banana = el.querySelector('[filter-keywords="banana"]');
70
+ const cherry = el.querySelector('[filter-keywords="cherry"]');
71
+ if(!banana.hidden || !cherry.hidden){
72
+ cleanup(container);
73
+ return fail('Non-matching items should be hidden');
74
+ }
75
+ cleanup(container);
76
+ pass('Non-matching items are hidden');
77
+ },
78
+
79
+ 'filter("apple") should show matching item': async ({pass, fail}) => {
80
+ const { container, el } = await createFilterList(['apple', 'banana', 'cherry']);
81
+ el.filter('apple');
82
+ const apple = el.querySelector('[filter-keywords="apple"]');
83
+ if(apple.hidden){
84
+ cleanup(container);
85
+ return fail('Matching item should not be hidden');
86
+ }
87
+ cleanup(container);
88
+ pass('Matching item is visible');
89
+ },
90
+
91
+ /*
92
+ filter() — multi-word (AND logic)
93
+ */
94
+ 'filter("apple pie") should require all words to match': async ({pass, fail}) => {
95
+ const { container, el } = await createFilterList(['apple pie', 'apple', 'pie']);
96
+ el.filter('apple pie');
97
+ const applePie = el.querySelector('[filter-keywords="apple pie"]');
98
+ const appleOnly = el.querySelector('[filter-keywords="apple"]');
99
+ const pieOnly = el.querySelector('[filter-keywords="pie"]');
100
+ if(applePie.hidden){
101
+ cleanup(container);
102
+ return fail('Item matching all words should be visible');
103
+ }
104
+ if(!appleOnly.hidden || !pieOnly.hidden){
105
+ cleanup(container);
106
+ return fail('Items matching only one word should be hidden');
107
+ }
108
+ cleanup(container);
109
+ pass('Multi-word filter requires all words to match');
110
+ },
111
+
112
+ /*
113
+ filter() — case insensitivity
114
+ */
115
+ 'filter() should be case-insensitive': async ({pass, fail}) => {
116
+ const { container, el } = await createFilterList(['Apple', 'banana']);
117
+ el.filter('APPLE');
118
+ const apple = el.querySelector('[filter-keywords="Apple"]');
119
+ if(apple.hidden){
120
+ cleanup(container);
121
+ return fail('Matching should be case-insensitive');
122
+ }
123
+ cleanup(container);
124
+ pass('filter() is case-insensitive');
125
+ },
126
+
127
+ /*
128
+ filter() — no keywords attribute
129
+ */
130
+ 'items with no filter-keywords should be hidden when term is provided': async ({pass, fail}) => {
131
+ const container = document.createElement('div');
132
+ container.innerHTML = `<k-filter-list><k-filter-item>no keywords</k-filter-item></k-filter-list>`;
133
+ document.body.appendChild(container);
134
+ const el = container.querySelector('k-filter-list');
135
+ await el.updateComplete;
136
+ el.filter('something');
137
+ const item = el.querySelector('k-filter-item');
138
+ if(!item.hidden){
139
+ cleanup(container);
140
+ return fail('Item with no keywords should be hidden when term is given');
141
+ }
142
+ cleanup(container);
143
+ pass('Item with no keywords is hidden when term is provided');
144
+ },
145
+
146
+ /*
147
+ filter() — restore all after filtering
148
+ */
149
+ 'filter("") after filtering should restore all items': async ({pass, fail}) => {
150
+ const { container, el } = await createFilterList(['apple', 'banana']);
151
+ el.filter('apple');
152
+ el.filter('');
153
+ const items = [...el.querySelectorAll('k-filter-item')];
154
+ const hidden = items.filter(i => i.hidden);
155
+ if(hidden.length !== 0){
156
+ cleanup(container);
157
+ return fail(`Expected all items visible after clearing filter, got ${hidden.length} hidden`);
158
+ }
159
+ cleanup(container);
160
+ pass('All items restored after clearing filter');
161
+ },
162
+ };