kempo-ui 0.0.5 → 0.0.6
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.
- package/package.json +2 -2
- package/tests/{HybridComponent.browser-test.js → components/HybridComponent.browser-test.js} +2 -2
- package/tests/{LightComponent.browser-test.js → components/LightComponent.browser-test.js} +2 -2
- package/tests/{ShadowComponent.browser-test.js → components/ShadowComponent.browser-test.js} +2 -2
- package/tests/utils/debounce.test.js +209 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kempo-ui",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A Lit based web-component library",
|
|
6
6
|
"main": "index.js",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"homepage": "https://github.com/dustinpoissant/kempo-ui#readme",
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"kempo-server": "^1.7.10",
|
|
29
|
-
"kempo-testing-framework": "^1.
|
|
29
|
+
"kempo-testing-framework": "^1.4.0",
|
|
30
30
|
"terser": "^5.43.1"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
package/tests/{HybridComponent.browser-test.js → components/HybridComponent.browser-test.js}
RENAMED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import HybridComponent from '
|
|
2
|
-
import { html } from '
|
|
1
|
+
import HybridComponent from '../../src/components/HybridComponent.js';
|
|
2
|
+
import { html } from '../../src/lit-all.min.js';
|
|
3
3
|
|
|
4
4
|
class TestHybridComponent extends HybridComponent {
|
|
5
5
|
render() {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import LightComponent from '
|
|
2
|
-
import { html } from '
|
|
1
|
+
import LightComponent from '../../src/components/LightComponent.js';
|
|
2
|
+
import { html } from '../../src/lit-all.min.js';
|
|
3
3
|
|
|
4
4
|
class TestLightComponent extends LightComponent {
|
|
5
5
|
renderLightDom() {
|
package/tests/{ShadowComponent.browser-test.js → components/ShadowComponent.browser-test.js}
RENAMED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import ShadowComponent from '
|
|
2
|
-
import { html } from '
|
|
1
|
+
import ShadowComponent from '../../src/components/ShadowComponent.js';
|
|
2
|
+
import { html } from '../../src/lit-all.min.js';
|
|
3
3
|
|
|
4
4
|
class TestShadowComponent extends ShadowComponent {
|
|
5
5
|
render() {
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import debounce from '../../src/utils/debounce.js';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
Utility Functions
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
Tests
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export default {
|
|
14
|
+
'should call the function after the specified timeout': async ({pass, fail, log}) => {
|
|
15
|
+
let callCount = 0;
|
|
16
|
+
const debouncedFn = debounce(() => {
|
|
17
|
+
callCount++;
|
|
18
|
+
}, 100);
|
|
19
|
+
|
|
20
|
+
debouncedFn();
|
|
21
|
+
|
|
22
|
+
if(callCount !== 0){
|
|
23
|
+
return fail('Function should not be called immediately');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
await wait(150);
|
|
27
|
+
|
|
28
|
+
if(callCount === 1){
|
|
29
|
+
log('✓ Function called once after timeout');
|
|
30
|
+
pass('Function was called after the specified timeout');
|
|
31
|
+
} else {
|
|
32
|
+
fail(`Expected 1 call, got ${callCount}`);
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
'should cancel previous calls when invoked multiple times': async ({pass, fail, log}) => {
|
|
37
|
+
let callCount = 0;
|
|
38
|
+
const debouncedFn = debounce(() => {
|
|
39
|
+
callCount++;
|
|
40
|
+
}, 100);
|
|
41
|
+
|
|
42
|
+
debouncedFn();
|
|
43
|
+
debouncedFn();
|
|
44
|
+
debouncedFn();
|
|
45
|
+
|
|
46
|
+
if(callCount !== 0){
|
|
47
|
+
return fail('Function should not be called immediately');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
await wait(150);
|
|
51
|
+
|
|
52
|
+
if(callCount === 1){
|
|
53
|
+
log('✓ Only the last call was executed');
|
|
54
|
+
pass('Previous calls were cancelled, only last call executed');
|
|
55
|
+
} else {
|
|
56
|
+
fail(`Expected 1 call after debouncing 3 invocations, got ${callCount}`);
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
'should pass arguments to the debounced function': async ({pass, fail, log}) => {
|
|
61
|
+
let receivedArgs = null;
|
|
62
|
+
const debouncedFn = debounce((...args) => {
|
|
63
|
+
receivedArgs = args;
|
|
64
|
+
}, 100);
|
|
65
|
+
|
|
66
|
+
debouncedFn('hello', 42, {key: 'value'});
|
|
67
|
+
|
|
68
|
+
await wait(150);
|
|
69
|
+
|
|
70
|
+
if(receivedArgs &&
|
|
71
|
+
receivedArgs[0] === 'hello' &&
|
|
72
|
+
receivedArgs[1] === 42 &&
|
|
73
|
+
receivedArgs[2]?.key === 'value'){
|
|
74
|
+
log('✓ Arguments passed correctly');
|
|
75
|
+
pass('All arguments were passed to the debounced function');
|
|
76
|
+
} else {
|
|
77
|
+
fail(`Arguments not passed correctly. Got: ${JSON.stringify(receivedArgs)}`);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
'should use default timeout of 300ms when not specified': async ({pass, fail, log}) => {
|
|
82
|
+
let callCount = 0;
|
|
83
|
+
const debouncedFn = debounce(() => {
|
|
84
|
+
callCount++;
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
debouncedFn();
|
|
88
|
+
|
|
89
|
+
await wait(250);
|
|
90
|
+
if(callCount !== 0){
|
|
91
|
+
return fail('Function should not be called before 300ms');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
await wait(100);
|
|
95
|
+
|
|
96
|
+
if(callCount === 1){
|
|
97
|
+
log('✓ Default timeout of 300ms works correctly');
|
|
98
|
+
pass('Function was called after default 300ms timeout');
|
|
99
|
+
} else {
|
|
100
|
+
fail(`Expected 1 call after 350ms, got ${callCount}`);
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
'should handle rapid successive calls': async ({pass, fail, log}) => {
|
|
105
|
+
let callCount = 0;
|
|
106
|
+
const debouncedFn = debounce(() => {
|
|
107
|
+
callCount++;
|
|
108
|
+
}, 100);
|
|
109
|
+
|
|
110
|
+
for(let i = 0; i < 10; i++){
|
|
111
|
+
debouncedFn();
|
|
112
|
+
await wait(20);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if(callCount !== 0){
|
|
116
|
+
return fail('Function should not be called during rapid invocations');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
await wait(150);
|
|
120
|
+
|
|
121
|
+
if(callCount === 1){
|
|
122
|
+
log('✓ Only called once after 10 rapid invocations');
|
|
123
|
+
pass('Debounce correctly handled rapid successive calls');
|
|
124
|
+
} else {
|
|
125
|
+
fail(`Expected 1 call after rapid invocations, got ${callCount}`);
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
'should allow multiple independent debounced functions': async ({pass, fail, log}) => {
|
|
130
|
+
let count1 = 0;
|
|
131
|
+
let count2 = 0;
|
|
132
|
+
|
|
133
|
+
const debouncedFn1 = debounce(() => {
|
|
134
|
+
count1++;
|
|
135
|
+
}, 100);
|
|
136
|
+
|
|
137
|
+
const debouncedFn2 = debounce(() => {
|
|
138
|
+
count2++;
|
|
139
|
+
}, 100);
|
|
140
|
+
|
|
141
|
+
debouncedFn1();
|
|
142
|
+
debouncedFn2();
|
|
143
|
+
|
|
144
|
+
await wait(150);
|
|
145
|
+
|
|
146
|
+
if(count1 === 1 && count2 === 1){
|
|
147
|
+
log('✓ Both independent debounced functions executed');
|
|
148
|
+
pass('Multiple debounced functions work independently');
|
|
149
|
+
} else {
|
|
150
|
+
fail(`Expected both counts to be 1, got count1: ${count1}, count2: ${count2}`);
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
'should work with different timeout values': async ({pass, fail, log}) => {
|
|
155
|
+
let shortCount = 0;
|
|
156
|
+
let longCount = 0;
|
|
157
|
+
|
|
158
|
+
const shortDebounce = debounce(() => {
|
|
159
|
+
shortCount++;
|
|
160
|
+
}, 50);
|
|
161
|
+
|
|
162
|
+
const longDebounce = debounce(() => {
|
|
163
|
+
longCount++;
|
|
164
|
+
}, 200);
|
|
165
|
+
|
|
166
|
+
shortDebounce();
|
|
167
|
+
longDebounce();
|
|
168
|
+
|
|
169
|
+
await wait(100);
|
|
170
|
+
|
|
171
|
+
if(shortCount !== 1){
|
|
172
|
+
return fail(`Short debounce should have been called. Got ${shortCount} calls`);
|
|
173
|
+
}
|
|
174
|
+
if(longCount !== 0){
|
|
175
|
+
return fail(`Long debounce should not have been called yet. Got ${longCount} calls`);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
await wait(150);
|
|
179
|
+
|
|
180
|
+
if(longCount === 1){
|
|
181
|
+
log('✓ Different timeout values work correctly');
|
|
182
|
+
pass('Both short and long debounce timeouts worked as expected');
|
|
183
|
+
} else {
|
|
184
|
+
fail(`Long debounce should have been called. Got ${longCount} calls`);
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
|
|
188
|
+
'should update arguments when called multiple times': async ({pass, fail, log}) => {
|
|
189
|
+
let receivedValue = null;
|
|
190
|
+
const debouncedFn = debounce((value) => {
|
|
191
|
+
receivedValue = value;
|
|
192
|
+
}, 100);
|
|
193
|
+
|
|
194
|
+
debouncedFn('first');
|
|
195
|
+
await wait(20);
|
|
196
|
+
debouncedFn('second');
|
|
197
|
+
await wait(20);
|
|
198
|
+
debouncedFn('third');
|
|
199
|
+
|
|
200
|
+
await wait(150);
|
|
201
|
+
|
|
202
|
+
if(receivedValue === 'third'){
|
|
203
|
+
log('✓ Most recent arguments were used');
|
|
204
|
+
pass('Debounced function used the last set of arguments');
|
|
205
|
+
} else {
|
|
206
|
+
fail(`Expected 'third', got '${receivedValue}'`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
};
|