|
| 1 | +import{afterEach,beforeEach,describe,expect,it,vi}from'vitest' |
| 2 | +import{debounce}from'./debounce' |
| 3 | + |
| 4 | +describe('debounce',()=>{ |
| 5 | +beforeEach(()=>{ |
| 6 | +vi.useFakeTimers() |
| 7 | +}) |
| 8 | + |
| 9 | +afterEach(()=>{ |
| 10 | +vi.useRealTimers() |
| 11 | +}) |
| 12 | + |
| 13 | +it('should delay function execution',()=>{ |
| 14 | +constmockFn=vi.fn() |
| 15 | +constdebouncedFn=debounce(mockFn,1000) |
| 16 | + |
| 17 | +debouncedFn() |
| 18 | +expect(mockFn).not.toBeCalled() |
| 19 | + |
| 20 | +vi.advanceTimersByTime(500) |
| 21 | +debouncedFn() |
| 22 | +expect(mockFn).not.toBeCalled() |
| 23 | + |
| 24 | +vi.advanceTimersByTime(999) |
| 25 | +expect(mockFn).not.toBeCalled() |
| 26 | + |
| 27 | +vi.advanceTimersByTime(1) |
| 28 | +expect(mockFn).toBeCalledTimes(1) |
| 29 | +}) |
| 30 | + |
| 31 | +it('should handle errors',()=>{ |
| 32 | +constmockError=newError('Sync error') |
| 33 | +constmockFn=vi.fn(()=>{ |
| 34 | +throwmockError |
| 35 | +}) |
| 36 | +constdebouncedFn=debounce(mockFn,1000) |
| 37 | + |
| 38 | +expect(()=>{ |
| 39 | +debouncedFn() |
| 40 | +vi.advanceTimersByTime(1000) |
| 41 | +}).toThrow(mockError) |
| 42 | +}) |
| 43 | + |
| 44 | +it('should call the function only once for multiple calls within the wait time',()=>{ |
| 45 | +constmockFn=vi.fn() |
| 46 | +constdebouncedFn=debounce(mockFn,1000) |
| 47 | + |
| 48 | +debouncedFn() |
| 49 | +debouncedFn() |
| 50 | +debouncedFn() |
| 51 | + |
| 52 | +vi.advanceTimersByTime(1000) |
| 53 | +expect(mockFn).toBeCalledTimes(1) |
| 54 | +}) |
| 55 | + |
| 56 | +it('should pass arguments to the debounced function',()=>{ |
| 57 | +constmockFn=vi.fn() |
| 58 | +constdebouncedFn=debounce(mockFn,1000) |
| 59 | + |
| 60 | +debouncedFn('test',123) |
| 61 | + |
| 62 | +vi.advanceTimersByTime(1000) |
| 63 | +expect(mockFn).toBeCalledWith('test',123) |
| 64 | +}) |
| 65 | + |
| 66 | +it('should use the latest arguments for delayed execution',()=>{ |
| 67 | +constmockFn=vi.fn() |
| 68 | +constdebouncedFn=debounce(mockFn,1000) |
| 69 | + |
| 70 | +debouncedFn('first',1) |
| 71 | +debouncedFn('second',2) |
| 72 | +debouncedFn('third',3) |
| 73 | + |
| 74 | +vi.advanceTimersByTime(1000) |
| 75 | +expect(mockFn).toBeCalledTimes(1) |
| 76 | +expect(mockFn).toBeCalledWith('third',3) |
| 77 | +}) |
| 78 | + |
| 79 | +it('should maintain the correct context',()=>{ |
| 80 | +constobj={ |
| 81 | +value:'test', |
| 82 | +method:vi.fn(function(this:{value:string}){ |
| 83 | +returnthis.value |
| 84 | +}) |
| 85 | +} |
| 86 | + |
| 87 | +constdebouncedMethod=debounce(obj.method,1000) |
| 88 | + |
| 89 | +debouncedMethod.call(obj) |
| 90 | +vi.advanceTimersByTime(1000) |
| 91 | + |
| 92 | +expect(obj.method).toBeCalledTimes(1) |
| 93 | +expect(obj.method.mock.results[0]?.value).toBe('test') |
| 94 | +}) |
| 95 | + |
| 96 | +it('should allow immediate execution after the wait time',()=>{ |
| 97 | +constmockFn=vi.fn() |
| 98 | +constdebouncedFn=debounce(mockFn,1000) |
| 99 | + |
| 100 | +debouncedFn() |
| 101 | +vi.advanceTimersByTime(1000) |
| 102 | +expect(mockFn).toBeCalledTimes(1) |
| 103 | + |
| 104 | +debouncedFn() |
| 105 | +vi.advanceTimersByTime(1000) |
| 106 | +expect(mockFn).toBeCalledTimes(2) |
| 107 | +}) |
| 108 | + |
| 109 | +it('should work with a wait time of 0',()=>{ |
| 110 | +constmockFn=vi.fn() |
| 111 | +constdebouncedFn=debounce(mockFn,0) |
| 112 | + |
| 113 | +debouncedFn() |
| 114 | +vi.advanceTimersByTime(0) |
| 115 | +expect(mockFn).toBeCalledTimes(1) |
| 116 | +}) |
| 117 | + |
| 118 | +it('should cancel pending executions',()=>{ |
| 119 | +constmockFn=vi.fn() |
| 120 | +constdebouncedFn=debounce(mockFn,1000) |
| 121 | + |
| 122 | +debouncedFn() |
| 123 | +vi.advanceTimersByTime(500) |
| 124 | +debouncedFn() |
| 125 | +vi.advanceTimersByTime(500) |
| 126 | +expect(mockFn).not.toBeCalled() |
| 127 | + |
| 128 | +vi.advanceTimersByTime(500) |
| 129 | +expect(mockFn).toBeCalledTimes(1) |
| 130 | +}) |
| 131 | + |
| 132 | +it('should handle multiple debounced functions independently',()=>{ |
| 133 | +constmockFn1=vi.fn() |
| 134 | +constmockFn2=vi.fn() |
| 135 | +constdebouncedFn1=debounce(mockFn1,1000) |
| 136 | +constdebouncedFn2=debounce(mockFn2,500) |
| 137 | + |
| 138 | +debouncedFn1() |
| 139 | +debouncedFn2() |
| 140 | + |
| 141 | +vi.advanceTimersByTime(500) |
| 142 | +expect(mockFn1).not.toBeCalled() |
| 143 | +expect(mockFn2).toBeCalledTimes(1) |
| 144 | + |
| 145 | +vi.advanceTimersByTime(500) |
| 146 | +expect(mockFn1).toBeCalledTimes(1) |
| 147 | +expect(mockFn2).toBeCalledTimes(1) |
| 148 | +}) |
| 149 | + |
| 150 | +it('should work with async functions',async()=>{ |
| 151 | +constmockFn=vi.fn().mockResolvedValue('async result') |
| 152 | +constdebouncedFn=debounce(mockFn,1000) |
| 153 | + |
| 154 | +debouncedFn() |
| 155 | +vi.advanceTimersByTime(1000) |
| 156 | + |
| 157 | +awaitvi.runAllTimersAsync() |
| 158 | + |
| 159 | +expect(mockFn).toHaveBeenCalledTimes(1) |
| 160 | +}) |
| 161 | + |
| 162 | +it('should debounce multiple calls to async functions',async()=>{ |
| 163 | +constmockFn=vi.fn().mockResolvedValue('async result') |
| 164 | +constdebouncedFn=debounce(mockFn,1000) |
| 165 | + |
| 166 | +debouncedFn() |
| 167 | +debouncedFn() |
| 168 | +debouncedFn() |
| 169 | + |
| 170 | +vi.advanceTimersByTime(1000) |
| 171 | + |
| 172 | +awaitvi.runAllTimersAsync() |
| 173 | + |
| 174 | +expect(mockFn).toHaveBeenCalledTimes(1) |
| 175 | +}) |
| 176 | + |
| 177 | +it('should pass arguments to async functions',async()=>{ |
| 178 | +constmockFn=vi.fn().mockResolvedValue('async result') |
| 179 | +constdebouncedFn=debounce(mockFn,1000) |
| 180 | + |
| 181 | +debouncedFn('arg1','arg2') |
| 182 | +vi.advanceTimersByTime(1000) |
| 183 | + |
| 184 | +awaitvi.runAllTimersAsync() |
| 185 | + |
| 186 | +expect(mockFn).toHaveBeenCalledWith('arg1','arg2') |
| 187 | +}) |
| 188 | + |
| 189 | +it('should maintain the correct context for async methods',async()=>{ |
| 190 | +constobj={ |
| 191 | +value:'test', |
| 192 | +method:vi.fn(asyncfunction(this:{value:string}){ |
| 193 | +returnthis.value |
| 194 | +}) |
| 195 | +} |
| 196 | + |
| 197 | +constdebouncedMethod=debounce(obj.method,1000) |
| 198 | + |
| 199 | +debouncedMethod.call(obj) |
| 200 | +vi.advanceTimersByTime(1000) |
| 201 | + |
| 202 | +awaitvi.runAllTimersAsync() |
| 203 | + |
| 204 | +expect(obj.method).toHaveBeenCalledTimes(1) |
| 205 | +expect(awaitobj.method.mock.results[0]?.value).toBe('test') |
| 206 | +}) |
| 207 | + |
| 208 | +it('should cancel pending async function calls',async()=>{ |
| 209 | +constmockFn=vi.fn().mockResolvedValue('async result') |
| 210 | +constdebouncedFn=debounce(mockFn,1000) |
| 211 | + |
| 212 | +debouncedFn() |
| 213 | +vi.advanceTimersByTime(500) |
| 214 | +debouncedFn() |
| 215 | +vi.advanceTimersByTime(500) |
| 216 | + |
| 217 | +awaitvi.runAllTimersAsync() |
| 218 | + |
| 219 | +expect(mockFn).toHaveBeenCalledTimes(1) |
| 220 | +}) |
| 221 | +}) |