|
| 1 | +import { throttle } from '../throttle' |
| 2 | + |
| 3 | +// Utilities |
| 4 | +import { wait } from '@test' |
| 5 | + |
| 6 | +describe('throttle', () => { |
| 7 | + it('should execute only the calls right before the interval', async () => { |
| 8 | + const result = [] as number[] |
| 9 | + const pushThrottled = throttle((v: number) => result.push(v), 100, { leading: false, trailing: false }) |
| 10 | + |
| 11 | + let lastId = 0 |
| 12 | + const interval = setInterval(() => pushThrottled(++lastId), 30) |
| 13 | + await wait(280) |
| 14 | + clearInterval(interval) |
| 15 | + |
| 16 | + expect(result).toStrictEqual([5, 9]) |
| 17 | + }) |
| 18 | + |
| 19 | + it('should execute only the calls right before the interval + trailing one', async () => { |
| 20 | + const result = [] as number[] |
| 21 | + const pushThrottled = throttle((v: number) => result.push(v), 100, { leading: false, trailing: true }) |
| 22 | + |
| 23 | + let lastId = 0 |
| 24 | + const interval = setInterval(() => pushThrottled(++lastId), 30) |
| 25 | + await wait(280) |
| 26 | + clearInterval(interval) |
| 27 | + await wait(100) |
| 28 | + |
| 29 | + expect(result).toStrictEqual([4, 7, 9]) |
| 30 | + }) |
| 31 | + |
| 32 | + it('should keep throttling after executing trailing call', async () => { |
| 33 | + const result = [] as number[] |
| 34 | + const pushThrottled = throttle((v: number) => result.push(v), 100, { leading: false, trailing: true }) |
| 35 | + |
| 36 | + let lastId = 0 |
| 37 | + setTimeout(() => pushThrottled(++lastId), 0) |
| 38 | + setTimeout(() => pushThrottled(++lastId), 40) |
| 39 | + setTimeout(() => pushThrottled(++lastId), 180) |
| 40 | + setTimeout(() => pushThrottled(++lastId), 190) |
| 41 | + await wait(280) |
| 42 | + |
| 43 | + expect(result).toStrictEqual([2, 4]) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should execute only the calls right before the interval + leading and trailing', async () => { |
| 47 | + const result = [] as number[] |
| 48 | + const pushThrottled = throttle((v: number) => result.push(v), 100) |
| 49 | + |
| 50 | + let lastId = 0 |
| 51 | + const interval = setInterval(() => pushThrottled(++lastId), 30) |
| 52 | + await wait(280) |
| 53 | + clearInterval(interval) |
| 54 | + await wait(100) |
| 55 | + |
| 56 | + expect(result).toStrictEqual([1, 4, 7, 9]) |
| 57 | + }) |
| 58 | + |
| 59 | + it('should pass calls the same way when resumed', async () => { |
| 60 | + const result = [] as number[] |
| 61 | + const pushThrottled = throttle((v: number) => result.push(v), 100) |
| 62 | + |
| 63 | + let lastId = 0 |
| 64 | + let interval = setInterval(() => pushThrottled(++lastId), 30) |
| 65 | + await wait(280) |
| 66 | + clearInterval(interval) |
| 67 | + await wait(150) |
| 68 | + |
| 69 | + lastId = 200 |
| 70 | + interval = setInterval(() => pushThrottled(++lastId), 30) |
| 71 | + await wait(280) |
| 72 | + clearInterval(interval) |
| 73 | + await wait(100) |
| 74 | + |
| 75 | + expect(result).toStrictEqual([1, 4, 7, 9, 201, 204, 207, 209]) |
| 76 | + }) |
| 77 | +}) |
0 commit comments