File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
src/packages/core/utils/debounce Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { expect } from '@open-wc/testing' ;
2
+ import { debounce } from './debounce.function.js' ;
3
+
4
+ describe ( 'debounce' , ( ) => {
5
+ it ( 'should call the function only once after the timeout' , async ( ) => {
6
+ let count = 0 ;
7
+ const debounced = debounce ( ( ) => count ++ , 100 ) ;
8
+
9
+ debounced ( ) ;
10
+ debounced ( ) ;
11
+ debounced ( ) ;
12
+ debounced ( ) ;
13
+ debounced ( ) ;
14
+
15
+ expect ( count ) . to . equal ( 0 ) ;
16
+
17
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 200 ) ) ;
18
+
19
+ expect ( count ) . to . equal ( 1 ) ;
20
+ } ) ;
21
+
22
+ it ( 'should call the function with the latest arguments' , async ( ) => {
23
+ let count = 0 ;
24
+ const debounced = debounce ( ( value : number ) => ( count = value ) , 100 ) ;
25
+
26
+ debounced ( 1 ) ;
27
+ debounced ( 2 ) ;
28
+ debounced ( 3 ) ;
29
+ debounced ( 4 ) ;
30
+ debounced ( 5 ) ;
31
+
32
+ expect ( count ) . to . equal ( 0 ) ;
33
+
34
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 200 ) ) ;
35
+
36
+ expect ( count ) . to . equal ( 5 ) ;
37
+ } ) ;
38
+ } ) ;
You can’t perform that action at this time.
0 commit comments