|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' |
| 2 | +import { createDebounce, createThrottle } from './timing' |
| 3 | + |
| 4 | +beforeEach(() => { |
| 5 | + vi.useFakeTimers() |
| 6 | +}) |
| 7 | +afterEach(() => { |
| 8 | + vi.useRealTimers() |
| 9 | +}) |
| 10 | + |
| 11 | +describe('createDebounce', () => { |
| 12 | + it('fires after delay when called once', () => { |
| 13 | + const fn = vi.fn() |
| 14 | + const debounced = createDebounce(fn, 100) |
| 15 | + |
| 16 | + debounced.call() |
| 17 | + expect(fn).not.toHaveBeenCalled() |
| 18 | + |
| 19 | + vi.advanceTimersByTime(100) |
| 20 | + expect(fn).toHaveBeenCalledOnce() |
| 21 | + }) |
| 22 | + |
| 23 | + it('resets timer on repeated calls — only the last one fires', () => { |
| 24 | + const fn = vi.fn() |
| 25 | + const debounced = createDebounce(fn, 100) |
| 26 | + |
| 27 | + debounced.call() |
| 28 | + vi.advanceTimersByTime(50) |
| 29 | + debounced.call() |
| 30 | + vi.advanceTimersByTime(50) |
| 31 | + debounced.call() |
| 32 | + vi.advanceTimersByTime(50) |
| 33 | + |
| 34 | + expect(fn).not.toHaveBeenCalled() |
| 35 | + |
| 36 | + vi.advanceTimersByTime(50) |
| 37 | + expect(fn).toHaveBeenCalledOnce() |
| 38 | + }) |
| 39 | + |
| 40 | + it('cancel prevents pending call', () => { |
| 41 | + const fn = vi.fn() |
| 42 | + const debounced = createDebounce(fn, 100) |
| 43 | + |
| 44 | + debounced.call() |
| 45 | + vi.advanceTimersByTime(50) |
| 46 | + debounced.cancel() |
| 47 | + |
| 48 | + vi.advanceTimersByTime(200) |
| 49 | + expect(fn).not.toHaveBeenCalled() |
| 50 | + }) |
| 51 | + |
| 52 | + it('flush fires immediately and clears timer', () => { |
| 53 | + const fn = vi.fn() |
| 54 | + const debounced = createDebounce(fn, 100) |
| 55 | + |
| 56 | + debounced.call() |
| 57 | + debounced.flush() |
| 58 | + expect(fn).toHaveBeenCalledOnce() |
| 59 | + |
| 60 | + // No double-fire after the original delay |
| 61 | + vi.advanceTimersByTime(200) |
| 62 | + expect(fn).toHaveBeenCalledOnce() |
| 63 | + }) |
| 64 | + |
| 65 | + it('flush is a no-op when nothing is pending', () => { |
| 66 | + const fn = vi.fn() |
| 67 | + const debounced = createDebounce(fn, 100) |
| 68 | + |
| 69 | + debounced.flush() |
| 70 | + expect(fn).not.toHaveBeenCalled() |
| 71 | + }) |
| 72 | +}) |
| 73 | + |
| 74 | +describe('createThrottle', () => { |
| 75 | + it('fires immediately on first call', () => { |
| 76 | + const fn = vi.fn() |
| 77 | + const throttled = createThrottle(fn, 100) |
| 78 | + |
| 79 | + throttled.call() |
| 80 | + expect(fn).toHaveBeenCalledOnce() |
| 81 | + }) |
| 82 | + |
| 83 | + it('suppresses calls within the delay window, fires trailing', () => { |
| 84 | + const fn = vi.fn() |
| 85 | + const throttled = createThrottle(fn, 100) |
| 86 | + |
| 87 | + throttled.call() // fires immediately |
| 88 | + throttled.call() // suppressed, schedules trailing |
| 89 | + throttled.call() // suppressed, trailing already scheduled |
| 90 | + |
| 91 | + expect(fn).toHaveBeenCalledOnce() |
| 92 | + |
| 93 | + vi.advanceTimersByTime(100) |
| 94 | + expect(fn).toHaveBeenCalledTimes(2) // trailing fires |
| 95 | + }) |
| 96 | + |
| 97 | + it('allows another immediate call after delay has passed', () => { |
| 98 | + const fn = vi.fn() |
| 99 | + const throttled = createThrottle(fn, 100) |
| 100 | + |
| 101 | + throttled.call() // immediate |
| 102 | + vi.advanceTimersByTime(100) |
| 103 | + |
| 104 | + throttled.call() // immediate again (enough time passed) |
| 105 | + expect(fn).toHaveBeenCalledTimes(2) |
| 106 | + }) |
| 107 | + |
| 108 | + it('cancel prevents the trailing call', () => { |
| 109 | + const fn = vi.fn() |
| 110 | + const throttled = createThrottle(fn, 100) |
| 111 | + |
| 112 | + throttled.call() // immediate |
| 113 | + throttled.call() // schedules trailing |
| 114 | + throttled.cancel() |
| 115 | + |
| 116 | + vi.advanceTimersByTime(200) |
| 117 | + expect(fn).toHaveBeenCalledOnce() // only the immediate one |
| 118 | + }) |
| 119 | + |
| 120 | + it('handles rapid bursts with correct cadence', () => { |
| 121 | + const fn = vi.fn() |
| 122 | + const throttled = createThrottle(fn, 100) |
| 123 | + |
| 124 | + // Simulate 10 calls at 20ms intervals (burst over 200ms) |
| 125 | + for (let i = 0; i < 10; i++) { |
| 126 | + throttled.call() |
| 127 | + vi.advanceTimersByTime(20) |
| 128 | + } |
| 129 | + |
| 130 | + // Flush any remaining trailing |
| 131 | + vi.advanceTimersByTime(100) |
| 132 | + |
| 133 | + // Should fire at most ~3-4 times (immediate + trailing calls), not 10 |
| 134 | + expect(fn.mock.calls.length).toBeGreaterThanOrEqual(2) |
| 135 | + expect(fn.mock.calls.length).toBeLessThanOrEqual(5) |
| 136 | + }) |
| 137 | +}) |
0 commit comments