@wordpress/compose 5.15.2 → 5.16.1-next.4d3b314fd5.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.
Files changed (86) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +86 -8
  3. package/build/higher-order/compose.js +5 -4
  4. package/build/higher-order/compose.js.map +1 -1
  5. package/build/higher-order/pipe.js +93 -0
  6. package/build/higher-order/pipe.js.map +1 -0
  7. package/build/hooks/use-debounce/index.js +11 -10
  8. package/build/hooks/use-debounce/index.js.map +1 -1
  9. package/build/hooks/use-disabled/index.js +3 -1
  10. package/build/hooks/use-disabled/index.js.map +1 -1
  11. package/build/hooks/use-drop-zone/index.js +4 -10
  12. package/build/hooks/use-drop-zone/index.js.map +1 -1
  13. package/build/hooks/use-fixed-window-list/index.js +5 -5
  14. package/build/hooks/use-fixed-window-list/index.js.map +1 -1
  15. package/build/hooks/use-instance-id/index.js +22 -20
  16. package/build/hooks/use-instance-id/index.js.map +1 -1
  17. package/build/hooks/use-throttle/index.js +12 -8
  18. package/build/hooks/use-throttle/index.js.map +1 -1
  19. package/build/index.js +37 -0
  20. package/build/index.js.map +1 -1
  21. package/build/index.native.js +37 -0
  22. package/build/index.native.js.map +1 -1
  23. package/build/utils/debounce/index.js +230 -0
  24. package/build/utils/debounce/index.js.map +1 -0
  25. package/build/utils/throttle/index.js +98 -0
  26. package/build/utils/throttle/index.js.map +1 -0
  27. package/build-module/higher-order/compose.js +4 -3
  28. package/build-module/higher-order/compose.js.map +1 -1
  29. package/build-module/higher-order/pipe.js +85 -0
  30. package/build-module/higher-order/pipe.js.map +1 -0
  31. package/build-module/hooks/use-debounce/index.js +10 -9
  32. package/build-module/hooks/use-debounce/index.js.map +1 -1
  33. package/build-module/hooks/use-disabled/index.js +3 -2
  34. package/build-module/hooks/use-disabled/index.js.map +1 -1
  35. package/build-module/hooks/use-drop-zone/index.js +4 -10
  36. package/build-module/hooks/use-drop-zone/index.js.map +1 -1
  37. package/build-module/hooks/use-fixed-window-list/index.js +5 -5
  38. package/build-module/hooks/use-fixed-window-list/index.js.map +1 -1
  39. package/build-module/hooks/use-instance-id/index.js +20 -19
  40. package/build-module/hooks/use-instance-id/index.js.map +1 -1
  41. package/build-module/hooks/use-throttle/index.js +11 -7
  42. package/build-module/hooks/use-throttle/index.js.map +1 -1
  43. package/build-module/index.js +7 -2
  44. package/build-module/index.js.map +1 -1
  45. package/build-module/index.native.js +7 -2
  46. package/build-module/index.native.js.map +1 -1
  47. package/build-module/utils/debounce/index.js +221 -0
  48. package/build-module/utils/debounce/index.js.map +1 -0
  49. package/build-module/utils/throttle/index.js +88 -0
  50. package/build-module/utils/throttle/index.js.map +1 -0
  51. package/build-types/higher-order/compose.d.ts +2 -5
  52. package/build-types/higher-order/compose.d.ts.map +1 -1
  53. package/build-types/higher-order/pipe.d.ts +61 -0
  54. package/build-types/higher-order/pipe.d.ts.map +1 -0
  55. package/build-types/hooks/use-debounce/index.d.ts +6 -6
  56. package/build-types/hooks/use-debounce/index.d.ts.map +1 -1
  57. package/build-types/hooks/use-disabled/index.d.ts.map +1 -1
  58. package/build-types/hooks/use-drop-zone/index.d.ts.map +1 -1
  59. package/build-types/hooks/use-instance-id/index.d.ts +11 -6
  60. package/build-types/hooks/use-instance-id/index.d.ts.map +1 -1
  61. package/build-types/hooks/use-throttle/index.d.ts +6 -6
  62. package/build-types/hooks/use-throttle/index.d.ts.map +1 -1
  63. package/build-types/index.d.ts +3 -0
  64. package/build-types/utils/debounce/index.d.ts +104 -0
  65. package/build-types/utils/debounce/index.d.ts.map +1 -0
  66. package/build-types/utils/throttle/index.d.ts +77 -0
  67. package/build-types/utils/throttle/index.d.ts.map +1 -0
  68. package/package.json +8 -8
  69. package/src/higher-order/compose.ts +5 -3
  70. package/src/higher-order/pipe.ts +76 -0
  71. package/src/higher-order/test/compose.ts +39 -0
  72. package/src/higher-order/test/pipe.ts +37 -0
  73. package/src/hooks/use-debounce/index.js +11 -9
  74. package/src/hooks/use-disabled/index.js +3 -2
  75. package/src/hooks/use-drop-zone/index.js +6 -13
  76. package/src/hooks/use-fixed-window-list/index.js +5 -5
  77. package/src/hooks/use-instance-id/index.ts +59 -0
  78. package/src/hooks/use-throttle/index.js +11 -7
  79. package/src/index.js +6 -1
  80. package/src/index.native.js +6 -1
  81. package/src/utils/debounce/index.ts +260 -0
  82. package/src/utils/debounce/test/index.ts +367 -0
  83. package/src/utils/throttle/index.ts +95 -0
  84. package/src/utils/throttle/test/index.ts +256 -0
  85. package/tsconfig.tsbuildinfo +1 -1
  86. package/src/hooks/use-instance-id/index.js +0 -42
@@ -0,0 +1,367 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import { debounce } from '../index';
5
+
6
+ const identity = ( value ) => value;
7
+
8
+ beforeAll( () => {
9
+ jest.useRealTimers();
10
+ } );
11
+
12
+ afterAll( () => {
13
+ jest.useFakeTimers();
14
+ } );
15
+
16
+ describe( 'debounce', () => {
17
+ it( 'should debounce a function', () => {
18
+ return new Promise( ( done ) => {
19
+ let callCount = 0;
20
+
21
+ const debounced = debounce( function ( value ) {
22
+ ++callCount;
23
+ return value;
24
+ }, 32 );
25
+
26
+ let results = [
27
+ debounced( 'a' ),
28
+ debounced( 'b' ),
29
+ debounced( 'c' ),
30
+ ];
31
+ expect( results ).toStrictEqual( [
32
+ undefined,
33
+ undefined,
34
+ undefined,
35
+ ] );
36
+ expect( callCount ).toBe( 0 );
37
+
38
+ setTimeout( function () {
39
+ expect( callCount ).toBe( 1 );
40
+
41
+ results = [
42
+ debounced( 'd' ),
43
+ debounced( 'e' ),
44
+ debounced( 'f' ),
45
+ ];
46
+ expect( results ).toStrictEqual( [ 'c', 'c', 'c' ] );
47
+ expect( callCount ).toBe( 1 );
48
+ }, 128 );
49
+
50
+ setTimeout( function () {
51
+ expect( callCount ).toBe( 2 );
52
+ done( null );
53
+ }, 256 );
54
+ } );
55
+ } );
56
+
57
+ it( 'should return the last `func` result on subsequent debounced calls', () => {
58
+ return new Promise( ( done ) => {
59
+ const debounced = debounce( identity, 32 );
60
+ debounced( 'a' );
61
+
62
+ setTimeout( function () {
63
+ expect( debounced( 'b' ) ).not.toBe( 'b' );
64
+ }, 64 );
65
+
66
+ setTimeout( function () {
67
+ expect( debounced( 'c' ) ).not.toBe( 'c' );
68
+ done( null );
69
+ }, 128 );
70
+ } );
71
+ } );
72
+
73
+ it( 'should not immediately call `func` when `wait` is `0`', () => {
74
+ return new Promise( ( done ) => {
75
+ let callCount = 0;
76
+ const debounced = debounce( function () {
77
+ ++callCount;
78
+ }, 0 );
79
+
80
+ debounced();
81
+ debounced();
82
+ expect( callCount ).toBe( 0 );
83
+
84
+ setTimeout( function () {
85
+ expect( callCount ).toBe( 1 );
86
+ done( null );
87
+ }, 5 );
88
+ } );
89
+ } );
90
+
91
+ it( 'should apply default options', () => {
92
+ return new Promise( ( done ) => {
93
+ let callCount = 0;
94
+ const debounced = debounce(
95
+ function () {
96
+ callCount++;
97
+ },
98
+ 32,
99
+ {}
100
+ );
101
+
102
+ debounced();
103
+ expect( callCount ).toBe( 0 );
104
+
105
+ setTimeout( function () {
106
+ expect( callCount ).toBe( 1 );
107
+ done( null );
108
+ }, 64 );
109
+ } );
110
+ } );
111
+
112
+ it( 'should support a `leading` option', () => {
113
+ return new Promise( ( done ) => {
114
+ const callCounts = [ 0, 0 ];
115
+
116
+ const withLeading = debounce(
117
+ function () {
118
+ callCounts[ 0 ]++;
119
+ },
120
+ 32,
121
+ { leading: true }
122
+ );
123
+
124
+ const withLeadingAndTrailing = debounce(
125
+ function () {
126
+ callCounts[ 1 ]++;
127
+ },
128
+ 32,
129
+ { leading: true }
130
+ );
131
+
132
+ withLeading();
133
+ expect( callCounts[ 0 ] ).toBe( 1 );
134
+
135
+ withLeadingAndTrailing();
136
+ withLeadingAndTrailing();
137
+ expect( callCounts[ 1 ] ).toBe( 1 );
138
+
139
+ setTimeout( function () {
140
+ expect( callCounts ).toStrictEqual( [ 1, 2 ] );
141
+
142
+ withLeading();
143
+ expect( callCounts[ 0 ] ).toBe( 2 );
144
+
145
+ done( null );
146
+ }, 64 );
147
+ } );
148
+ } );
149
+
150
+ it( 'should return the last `func` result for subsequent leading debounced calls', () => {
151
+ return new Promise( ( done ) => {
152
+ const debounced = debounce( identity, 32, {
153
+ leading: true,
154
+ trailing: false,
155
+ } );
156
+ let results = [ debounced( 'a' ), debounced( 'b' ) ];
157
+
158
+ expect( results ).toStrictEqual( [ 'a', 'a' ] );
159
+
160
+ setTimeout( function () {
161
+ results = [ debounced( 'c' ), debounced( 'd' ) ];
162
+ expect( results ).toStrictEqual( [ 'c', 'c' ] );
163
+ done( null );
164
+ }, 64 );
165
+ } );
166
+ } );
167
+
168
+ it( 'should support a `trailing` option', () => {
169
+ return new Promise( ( done ) => {
170
+ let withCount = 0;
171
+ let withoutCount = 0;
172
+
173
+ const withTrailing = debounce(
174
+ function () {
175
+ withCount++;
176
+ },
177
+ 32,
178
+ { trailing: true }
179
+ );
180
+
181
+ const withoutTrailing = debounce(
182
+ function () {
183
+ withoutCount++;
184
+ },
185
+ 32,
186
+ { trailing: false }
187
+ );
188
+
189
+ withTrailing();
190
+ expect( withCount ).toBe( 0 );
191
+
192
+ withoutTrailing();
193
+ expect( withCount ).toBe( 0 );
194
+
195
+ setTimeout( function () {
196
+ expect( withCount ).toBe( 1 );
197
+ expect( withoutCount ).toBe( 0 );
198
+ done( null );
199
+ }, 64 );
200
+ } );
201
+ } );
202
+
203
+ it( 'should support a `maxWait` option', () => {
204
+ return new Promise( ( done ) => {
205
+ let callCount = 0;
206
+
207
+ const debounced = debounce(
208
+ function ( value ) {
209
+ ++callCount;
210
+ return value;
211
+ },
212
+ 32,
213
+ { maxWait: 64 }
214
+ );
215
+
216
+ debounced( 42 );
217
+ debounced( 42 );
218
+ expect( callCount ).toBe( 0 );
219
+
220
+ setTimeout( function () {
221
+ expect( callCount ).toBe( 1 );
222
+ debounced( 42 );
223
+ debounced( 42 );
224
+ expect( callCount ).toBe( 1 );
225
+ }, 128 );
226
+
227
+ setTimeout( function () {
228
+ expect( callCount ).toBe( 2 );
229
+ done( null );
230
+ }, 256 );
231
+ } );
232
+ } );
233
+
234
+ it( 'should support `maxWait` in a tight loop', () => {
235
+ return new Promise( ( done ) => {
236
+ const limit = 320;
237
+ let withCount = 0;
238
+ let withoutCount = 0;
239
+
240
+ const withMaxWait = debounce(
241
+ function () {
242
+ withCount++;
243
+ },
244
+ 64,
245
+ { maxWait: 128 }
246
+ );
247
+
248
+ const withoutMaxWait = debounce( function () {
249
+ withoutCount++;
250
+ }, 96 );
251
+
252
+ const start = Date.now();
253
+ while ( Date.now() - start < limit ) {
254
+ withMaxWait();
255
+ withoutMaxWait();
256
+ }
257
+ const actual = [ Boolean( withoutCount ), Boolean( withCount ) ];
258
+ setTimeout( function () {
259
+ expect( actual ).toStrictEqual( [ false, true ] );
260
+ done( null );
261
+ }, 1 );
262
+ } );
263
+ } );
264
+
265
+ it( 'should queue a trailing call for subsequent debounced calls after `maxWait`', () => {
266
+ return new Promise( ( done ) => {
267
+ let callCount = 0;
268
+
269
+ const debounced = debounce(
270
+ function () {
271
+ ++callCount;
272
+ },
273
+ 200,
274
+ { maxWait: 200 }
275
+ );
276
+
277
+ debounced();
278
+
279
+ setTimeout( debounced, 190 );
280
+ setTimeout( debounced, 200 );
281
+ setTimeout( debounced, 210 );
282
+
283
+ setTimeout( function () {
284
+ expect( callCount ).toBe( 2 );
285
+ done( null );
286
+ }, 500 );
287
+ } );
288
+ } );
289
+
290
+ it( 'should cancel when `cancel` is invoked', () => {
291
+ return new Promise( ( done ) => {
292
+ let callCount = 0;
293
+
294
+ const debounced = debounce( function () {
295
+ callCount++;
296
+ }, 32 );
297
+
298
+ debounced();
299
+
300
+ setTimeout( function () {
301
+ debounced.cancel();
302
+ expect( callCount ).toBe( 0 );
303
+ }, 16 );
304
+
305
+ setTimeout( function () {
306
+ expect( callCount ).toBe( 0 );
307
+ done( null );
308
+ }, 64 );
309
+ } );
310
+ } );
311
+
312
+ it( 'should cancel `maxDelayed` when `delayed` is invoked', () => {
313
+ return new Promise( ( done ) => {
314
+ let callCount = 0;
315
+
316
+ const debounced = debounce(
317
+ function () {
318
+ callCount++;
319
+ },
320
+ 32,
321
+ { maxWait: 64 }
322
+ );
323
+
324
+ debounced();
325
+
326
+ setTimeout( function () {
327
+ debounced();
328
+ expect( callCount ).toBe( 1 );
329
+ }, 128 );
330
+
331
+ setTimeout( function () {
332
+ expect( callCount ).toBe( 2 );
333
+ done( null );
334
+ }, 192 );
335
+ } );
336
+ } );
337
+
338
+ it( 'should invoke the trailing call with the correct arguments and `this` binding', () => {
339
+ return new Promise( ( done ) => {
340
+ let actual;
341
+ let callCount = 0;
342
+ const object = {};
343
+
344
+ const debounced = debounce(
345
+ function ( ...args ) {
346
+ actual = [ this ];
347
+ Array.prototype.push.apply( actual, args );
348
+ return ++callCount !== 2;
349
+ },
350
+ 32,
351
+ { leading: true, maxWait: 64 }
352
+ );
353
+
354
+ while ( true ) {
355
+ if ( ! debounced.call( object, 'a' ) ) {
356
+ break;
357
+ }
358
+ }
359
+
360
+ setTimeout( function () {
361
+ expect( callCount ).toBe( 2 );
362
+ expect( actual ).toStrictEqual( [ object, 'a' ] );
363
+ done( null );
364
+ }, 64 );
365
+ } );
366
+ } );
367
+ } );
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Parts of this source were derived and modified from lodash,
3
+ * released under the MIT license.
4
+ *
5
+ * https://github.com/lodash/lodash
6
+ *
7
+ * Copyright JS Foundation and other contributors <https://js.foundation/>
8
+ *
9
+ * Based on Underscore.js, copyright Jeremy Ashkenas,
10
+ * DocumentCloud and Investigative Reporters & Editors <http://underscorejs.org/>
11
+ *
12
+ * This software consists of voluntary contributions made by many
13
+ * individuals. For exact contribution history, see the revision history
14
+ * available at https://github.com/lodash/lodash
15
+ *
16
+ * The following license applies to all parts of this software except as
17
+ * documented below:
18
+ *
19
+ * ====
20
+ *
21
+ * Permission is hereby granted, free of charge, to any person obtaining
22
+ * a copy of this software and associated documentation files (the
23
+ * "Software"), to deal in the Software without restriction, including
24
+ * without limitation the rights to use, copy, modify, merge, publish,
25
+ * distribute, sublicense, and/or sell copies of the Software, and to
26
+ * permit persons to whom the Software is furnished to do so, subject to
27
+ * the following conditions:
28
+ *
29
+ * The above copyright notice and this permission notice shall be
30
+ * included in all copies or substantial portions of the Software.
31
+ *
32
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39
+ */
40
+
41
+ /**
42
+ * Internal dependencies
43
+ */
44
+ import { debounce } from '../debounce';
45
+
46
+ export interface ThrottleOptions {
47
+ leading?: boolean;
48
+ trailing?: boolean;
49
+ }
50
+
51
+ /**
52
+ * A simplified and properly typed version of lodash's `throttle`, that
53
+ * always uses timers instead of sometimes using rAF.
54
+ *
55
+ * Creates a throttled function that only invokes `func` at most once per
56
+ * every `wait` milliseconds. The throttled function comes with a `cancel`
57
+ * method to cancel delayed `func` invocations and a `flush` method to
58
+ * immediately invoke them. Provide `options` to indicate whether `func`
59
+ * should be invoked on the leading and/or trailing edge of the `wait`
60
+ * timeout. The `func` is invoked with the last arguments provided to the
61
+ * throttled function. Subsequent calls to the throttled function return
62
+ * the result of the last `func` invocation.
63
+ *
64
+ * **Note:** If `leading` and `trailing` options are `true`, `func` is
65
+ * invoked on the trailing edge of the timeout only if the throttled function
66
+ * is invoked more than once during the `wait` timeout.
67
+ *
68
+ * If `wait` is `0` and `leading` is `false`, `func` invocation is deferred
69
+ * until the next tick, similar to `setTimeout` with a timeout of `0`.
70
+ *
71
+ * @param {Function} func The function to throttle.
72
+ * @param {number} wait The number of milliseconds to throttle invocations to.
73
+ * @param {Partial< ThrottleOptions >} options The options object.
74
+ * @param {boolean} options.leading Specify invoking on the leading edge of the timeout.
75
+ * @param {boolean} options.trailing Specify invoking on the trailing edge of the timeout.
76
+ * @return Returns the new throttled function.
77
+ */
78
+ export const throttle = < FunctionT extends ( ...args: unknown[] ) => unknown >(
79
+ func: FunctionT,
80
+ wait: number,
81
+ options?: ThrottleOptions
82
+ ) => {
83
+ let leading = true;
84
+ let trailing = true;
85
+
86
+ if ( options ) {
87
+ leading = 'leading' in options ? !! options.leading : leading;
88
+ trailing = 'trailing' in options ? !! options.trailing : trailing;
89
+ }
90
+ return debounce( func, wait, {
91
+ leading,
92
+ trailing,
93
+ maxWait: wait,
94
+ } );
95
+ };
@@ -0,0 +1,256 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import { throttle } from '../index';
5
+
6
+ const identity = ( value ) => value;
7
+
8
+ describe( 'throttle', () => {
9
+ beforeEach( () => {
10
+ jest.useFakeTimers();
11
+ } );
12
+
13
+ afterEach( () => {
14
+ jest.useRealTimers();
15
+ } );
16
+
17
+ it( 'should throttle a function', () => {
18
+ let callCount = 0;
19
+ const throttled = throttle( function () {
20
+ callCount++;
21
+ }, 32 );
22
+
23
+ throttled();
24
+ throttled();
25
+ throttled();
26
+
27
+ const lastCount = callCount;
28
+ expect( callCount ).toBeGreaterThan( 0 );
29
+
30
+ jest.advanceTimersByTime( 64 );
31
+
32
+ expect( callCount ).toBeGreaterThan( lastCount );
33
+ } );
34
+
35
+ it( 'should return the result of the first call on subsequent calls', () => {
36
+ const throttled = throttle( identity, 32 );
37
+ let results = [ throttled( 'a' ), throttled( 'b' ) ];
38
+
39
+ expect( results ).toStrictEqual( [ 'a', 'a' ] );
40
+
41
+ jest.advanceTimersByTime( 64 );
42
+
43
+ results = [ throttled( 'c' ), throttled( 'd' ) ];
44
+ expect( results[ 0 ] ).not.toBe( 'a' );
45
+ expect( results[ 0 ] ).not.toBe( undefined );
46
+
47
+ expect( results[ 1 ] ).not.toBe( 'd' );
48
+ expect( results[ 1 ] ).not.toBe( undefined );
49
+ } );
50
+
51
+ it( 'should clear timeout when `func` is called', () => {
52
+ let callCount = 0;
53
+ let dateCount = 0;
54
+
55
+ const globalDateNow = global.Date.now;
56
+ global.Date.now = function () {
57
+ return ++dateCount === 5 ? Infinity : +new Date();
58
+ };
59
+
60
+ const throttled = throttle( () => {
61
+ callCount++;
62
+ }, 32 );
63
+
64
+ throttled();
65
+ throttled();
66
+
67
+ jest.advanceTimersByTime( 64 );
68
+
69
+ expect( callCount ).toBe( 2 );
70
+ global.Date.now = globalDateNow;
71
+ } );
72
+
73
+ it( 'should not trigger a trailing call when invoked once', () => {
74
+ let callCount = 0;
75
+ const throttled = throttle( () => {
76
+ callCount++;
77
+ }, 32 );
78
+
79
+ throttled();
80
+ expect( callCount ).toBe( 1 );
81
+
82
+ jest.advanceTimersByTime( 64 );
83
+
84
+ expect( callCount ).toBe( 1 );
85
+ } );
86
+
87
+ [ true, false ].forEach( ( leading ) => {
88
+ it(
89
+ 'should trigger a call when invoked repeatedly' +
90
+ ( ! leading ? ' and `leading` is `false`' : '' ),
91
+ () => {
92
+ let callCount = 0;
93
+ const limit = 320;
94
+ const options = leading ? {} : { leading: false };
95
+ const throttled = throttle(
96
+ () => {
97
+ callCount++;
98
+ },
99
+ 32,
100
+ options
101
+ );
102
+
103
+ const start = Date.now();
104
+ while ( Date.now() - start < limit ) {
105
+ throttled();
106
+ jest.advanceTimersByTime( 1 );
107
+ }
108
+ const actual = callCount;
109
+
110
+ jest.advanceTimersByTime( 1 );
111
+
112
+ expect( actual ).toBeGreaterThan( 1 );
113
+ }
114
+ );
115
+ } );
116
+
117
+ it( 'should trigger a second throttled call as soon as possible', () => {
118
+ let callCount = 0;
119
+
120
+ const throttled = throttle(
121
+ () => {
122
+ callCount++;
123
+ },
124
+ 128,
125
+ { leading: false }
126
+ );
127
+
128
+ throttled();
129
+
130
+ jest.advanceTimersByTime( 192 );
131
+
132
+ expect( callCount ).toBe( 1 );
133
+ throttled();
134
+
135
+ jest.advanceTimersByTime( 64 );
136
+
137
+ expect( callCount ).toBe( 1 );
138
+
139
+ jest.advanceTimersByTime( 130 );
140
+
141
+ expect( callCount ).toBe( 2 );
142
+ } );
143
+
144
+ it( 'should apply default options', () => {
145
+ let callCount = 0;
146
+ const throttled = throttle(
147
+ () => {
148
+ callCount++;
149
+ },
150
+ 32,
151
+ {}
152
+ );
153
+
154
+ throttled();
155
+ throttled();
156
+ expect( callCount ).toBe( 1 );
157
+
158
+ jest.advanceTimersByTime( 128 );
159
+
160
+ expect( callCount ).toBe( 2 );
161
+ } );
162
+
163
+ it( 'should support a `leading` option', () => {
164
+ const withLeading = throttle( identity, 32, { leading: true } );
165
+ expect( withLeading( 'a' ) ).toBe( 'a' );
166
+
167
+ const withoutLeading = throttle( identity, 32, { leading: false } );
168
+ expect( withoutLeading( 'a' ) ).toBeUndefined();
169
+ } );
170
+
171
+ it( 'should support a `trailing` option', () => {
172
+ let withCount = 0;
173
+ let withoutCount = 0;
174
+
175
+ const withTrailing = throttle(
176
+ ( value ) => {
177
+ withCount++;
178
+ return value;
179
+ },
180
+ 64,
181
+ { trailing: true }
182
+ );
183
+
184
+ const withoutTrailing = throttle(
185
+ ( value ) => {
186
+ withoutCount++;
187
+ return value;
188
+ },
189
+ 64,
190
+ { trailing: false }
191
+ );
192
+
193
+ expect( withTrailing( 'a' ) ).toBe( 'a' );
194
+ expect( withTrailing( 'b' ) ).toBe( 'a' );
195
+
196
+ expect( withoutTrailing( 'a' ) ).toBe( 'a' );
197
+ expect( withoutTrailing( 'b' ) ).toBe( 'a' );
198
+
199
+ jest.advanceTimersByTime( 256 );
200
+
201
+ expect( withCount ).toBe( 2 );
202
+ expect( withoutCount ).toBe( 1 );
203
+ } );
204
+
205
+ it( 'should not update `lastCalled`, at the end of the timeout, when `trailing` is `false`', () => {
206
+ let callCount = 0;
207
+
208
+ const throttled = throttle(
209
+ function () {
210
+ callCount++;
211
+ },
212
+ 64,
213
+ { trailing: false }
214
+ );
215
+
216
+ throttled();
217
+ throttled();
218
+
219
+ jest.advanceTimersByTime( 96 );
220
+
221
+ throttled();
222
+ throttled();
223
+
224
+ jest.advanceTimersByTime( 96 );
225
+
226
+ expect( callCount ).toBeGreaterThan( 1 );
227
+ } );
228
+
229
+ it( 'should work with a system time of `0`', () => {
230
+ let callCount = 0;
231
+ let dateCount = 0;
232
+
233
+ const globalDateNow = global.Date.now;
234
+ global.Date.now = function () {
235
+ return ++dateCount < 4 ? 0 : +new Date();
236
+ };
237
+
238
+ const throttled = throttle( ( value ) => {
239
+ callCount++;
240
+ return value;
241
+ }, 32 );
242
+
243
+ const results = [
244
+ throttled( 'a' ),
245
+ throttled( 'b' ),
246
+ throttled( 'c' ),
247
+ ];
248
+ expect( results ).toStrictEqual( [ 'a', 'a', 'a' ] );
249
+ expect( callCount ).toBe( 1 );
250
+
251
+ jest.advanceTimersByTime( 64 );
252
+
253
+ expect( callCount ).toBe( 2 );
254
+ global.Date.now = globalDateNow;
255
+ } );
256
+ } );