diff --git a/packages/pacer-lite/tests/lite-debouncer.test.ts b/packages/pacer-lite/tests/lite-debouncer.test.ts index e872772ae..1934581a7 100644 --- a/packages/pacer-lite/tests/lite-debouncer.test.ts +++ b/packages/pacer-lite/tests/lite-debouncer.test.ts @@ -16,7 +16,7 @@ describe('LiteDebouncer', () => { const debouncer = new LiteDebouncer(mockFn, { wait: 1000 }) debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should execute the function after the specified wait', () => { @@ -24,10 +24,10 @@ describe('LiteDebouncer', () => { const debouncer = new LiteDebouncer(mockFn, { wait: 1000 }) debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should debounce multiple calls', () => { @@ -37,10 +37,10 @@ describe('LiteDebouncer', () => { debouncer.maybeExecute() debouncer.maybeExecute() debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should pass arguments to the debounced function', () => { @@ -50,7 +50,7 @@ describe('LiteDebouncer', () => { debouncer.maybeExecute('test', 123) vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledWith('test', 123) + expect(mockFn).toHaveBeenCalledWith('test', 123) }) it('should use latest arguments from multiple calls', () => { @@ -62,7 +62,7 @@ describe('LiteDebouncer', () => { debouncer.maybeExecute('third') vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledWith('third') + expect(mockFn).toHaveBeenCalledWith('third') }) }) @@ -76,11 +76,11 @@ describe('LiteDebouncer', () => { }) debouncer.maybeExecute('test') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('test') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('test') vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should respect leading edge timing', () => { @@ -93,20 +93,20 @@ describe('LiteDebouncer', () => { // First call - executes immediately debouncer.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Call again before wait expires - should not execute vi.advanceTimersByTime(500) debouncer.maybeExecute('second') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Advance to end of second call's wait period - should not execute vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Now that the full wait has passed since last call, this should execute debouncer.maybeExecute('third') - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('third') }) @@ -120,11 +120,11 @@ describe('LiteDebouncer', () => { debouncer.maybeExecute('test1') debouncer.maybeExecute('test2') - expect(mockFn).toBeCalledTimes(1) // Leading call - expect(mockFn).toBeCalledWith('test1') + expect(mockFn).toHaveBeenCalledTimes(1) // Leading call + expect(mockFn).toHaveBeenCalledWith('test1') vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(2) // Trailing call + expect(mockFn).toHaveBeenCalledTimes(2) // Trailing call expect(mockFn).toHaveBeenLastCalledWith('test2') }) @@ -134,11 +134,11 @@ describe('LiteDebouncer', () => { debouncer.maybeExecute('test1') debouncer.maybeExecute('test2') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('test2') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('test2') }) it('should handle case where both leading and trailing are false', () => { @@ -150,14 +150,14 @@ describe('LiteDebouncer', () => { }) debouncer.maybeExecute('test') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() // Should still reset canLeadingExecute flag debouncer.maybeExecute('test2') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) }) @@ -170,7 +170,7 @@ describe('LiteDebouncer', () => { debouncer.cancel() vi.advanceTimersByTime(1000) - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should properly handle canLeadingExecute flag after cancellation', () => { @@ -183,7 +183,7 @@ describe('LiteDebouncer', () => { // First call - executes immediately debouncer.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Cancel before wait expires vi.advanceTimersByTime(500) @@ -191,7 +191,7 @@ describe('LiteDebouncer', () => { // Should be able to execute immediately again after cancellation debouncer.maybeExecute('second') - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('second') }) @@ -210,15 +210,15 @@ describe('LiteDebouncer', () => { debouncer.maybeExecute('fourth') // Only first call should execute immediately - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') // Wait for timeout vi.advanceTimersByTime(1000) // Next call should execute immediately debouncer.maybeExecute('fifth') - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('fifth') }) }) @@ -229,11 +229,11 @@ describe('LiteDebouncer', () => { const debouncer = new LiteDebouncer(mockFn, { wait: 1000 }) debouncer.maybeExecute('test') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() debouncer.flush() - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('test') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('test') }) it('should clear pending timeout when flushing', () => { @@ -246,7 +246,7 @@ describe('LiteDebouncer', () => { // Advance time to ensure timeout would have fired vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should do nothing when no pending execution', () => { @@ -254,7 +254,7 @@ describe('LiteDebouncer', () => { const debouncer = new LiteDebouncer(mockFn, { wait: 1000 }) debouncer.flush() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should work with leading and trailing execution', () => { @@ -266,12 +266,12 @@ describe('LiteDebouncer', () => { }) debouncer.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) debouncer.maybeExecute('second') debouncer.flush() - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('second') }) @@ -284,13 +284,13 @@ describe('LiteDebouncer', () => { }) debouncer.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) debouncer.maybeExecute('second') debouncer.flush() // Since we have lastArgs, flush will execute even with trailing: false - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('second') }) }) @@ -408,10 +408,10 @@ describe('LiteDebouncer', () => { const debouncer = new LiteDebouncer(mockFn, { wait: 0 }) debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(0) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should handle negative wait time', () => { @@ -419,10 +419,10 @@ describe('LiteDebouncer', () => { const debouncer = new LiteDebouncer(mockFn, { wait: -1000 }) debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(0) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should handle very large wait times', () => { @@ -432,10 +432,10 @@ describe('LiteDebouncer', () => { }) debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(Number.MAX_SAFE_INTEGER) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should handle NaN wait time', () => { @@ -443,10 +443,10 @@ describe('LiteDebouncer', () => { const debouncer = new LiteDebouncer(mockFn, { wait: NaN }) debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(0) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should handle undefined/null arguments', () => { @@ -455,7 +455,7 @@ describe('LiteDebouncer', () => { debouncer.maybeExecute(undefined, null) vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledWith(undefined, null) + expect(mockFn).toHaveBeenCalledWith(undefined, null) }) it('should prevent memory leaks by clearing timeouts', () => { @@ -472,7 +472,7 @@ describe('LiteDebouncer', () => { // Advance time to ensure no executions occur vi.advanceTimersByTime(1000) - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) }) }) @@ -492,11 +492,11 @@ describe('liteDebounce helper function', () => { const debouncedFn = liteDebounce(mockFn, { wait: 1000 }) debouncedFn('test') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('test') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('test') }) it('should pass arguments correctly', () => { @@ -506,7 +506,7 @@ describe('liteDebounce helper function', () => { debouncedFn(42, 'test', { foo: 'bar' }) vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledWith(42, 'test', { foo: 'bar' }) + expect(mockFn).toHaveBeenCalledWith(42, 'test', { foo: 'bar' }) }) }) @@ -520,17 +520,17 @@ describe('liteDebounce helper function', () => { }) debouncedFn('first') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') debouncedFn('second') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) debouncedFn('third') - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('third') }) @@ -541,15 +541,15 @@ describe('liteDebounce helper function', () => { debouncedFn('a') debouncedFn('b') debouncedFn('c') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(500) debouncedFn('d') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('d') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('d') }) it('should support both leading and trailing execution', () => { @@ -561,14 +561,14 @@ describe('liteDebounce helper function', () => { }) debouncedFn('first') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') debouncedFn('second') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('second') }) diff --git a/packages/pacer-lite/tests/lite-throttler.test.ts b/packages/pacer-lite/tests/lite-throttler.test.ts index 8ecdb19cf..fe2c0abcf 100644 --- a/packages/pacer-lite/tests/lite-throttler.test.ts +++ b/packages/pacer-lite/tests/lite-throttler.test.ts @@ -169,12 +169,12 @@ describe('LiteThrottler', () => { // Should execute immediately due to leading: true throttler.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') // Should execute immediately again since wait is 0 throttler.maybeExecute('second') - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('second') }) @@ -183,10 +183,10 @@ describe('LiteThrottler', () => { const throttler = new LiteThrottler(mockFn, { wait: -100 }) throttler.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) throttler.maybeExecute('second') - expect(mockFn).toBeCalledTimes(2) // Should execute immediately due to negative wait + expect(mockFn).toHaveBeenCalledTimes(2) // Should execute immediately due to negative wait }) it('should handle very large wait times', () => { @@ -195,21 +195,21 @@ describe('LiteThrottler', () => { // First call should execute immediately throttler.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') // Subsequent calls should be throttled throttler.maybeExecute('second') throttler.maybeExecute('third') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Advance time by half the wait period vi.advanceTimersByTime(500000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Complete the wait period vi.advanceTimersByTime(500000) - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('third') }) }) @@ -237,21 +237,21 @@ describe('LiteThrottler', () => { // First call throttler.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Cancel before trailing execution throttler.cancel() vi.advanceTimersByTime(100) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Second call throttler.maybeExecute('second') - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) // Cancel again throttler.cancel() vi.advanceTimersByTime(100) - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) }) }) @@ -261,13 +261,13 @@ describe('LiteThrottler', () => { const throttler = new LiteThrottler(mockFn, { wait: 1000 }) throttler.maybeExecute('test') - expect(mockFn).toBeCalledTimes(1) // Leading execution + expect(mockFn).toHaveBeenCalledTimes(1) // Leading execution throttler.maybeExecute('pending') - expect(mockFn).toBeCalledTimes(1) // Still throttled + expect(mockFn).toHaveBeenCalledTimes(1) // Still throttled throttler.flush() - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('pending') }) @@ -282,7 +282,7 @@ describe('LiteThrottler', () => { // Advance time to ensure timeout would have fired vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) }) it('should do nothing when no pending execution', () => { @@ -290,7 +290,7 @@ describe('LiteThrottler', () => { const throttler = new LiteThrottler(mockFn, { wait: 1000 }) throttler.flush() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should work with leading and trailing execution', () => { @@ -302,12 +302,12 @@ describe('LiteThrottler', () => { }) throttler.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) throttler.maybeExecute('second') throttler.flush() - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('second') }) @@ -320,11 +320,11 @@ describe('LiteThrottler', () => { }) throttler.maybeExecute('first') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() throttler.flush() - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') }) it('should not flush when leading only and no pending execution', () => { @@ -336,11 +336,11 @@ describe('LiteThrottler', () => { }) throttler.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // No pending execution to flush throttler.flush() - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) }) @@ -475,11 +475,11 @@ describe('LiteThrottler', () => { // With NaN wait, timeSinceLastExecution >= NaN is false, so no leading execution // But trailing execution will be scheduled with NaN timeout duration throttler.maybeExecute('first') - expect(mockFn).toBeCalledTimes(0) // No leading execution + expect(mockFn).toHaveBeenCalledTimes(0) // No leading execution // The trailing execution should happen with NaN timeout vi.advanceTimersByTime(0) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) expect(mockFn).toHaveBeenCalledWith('first') }) @@ -496,7 +496,7 @@ describe('LiteThrottler', () => { // Advance time to ensure no executions occur vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) // Only the leading execution + expect(mockFn).toHaveBeenCalledTimes(1) // Only the leading execution }) }) }) @@ -516,14 +516,14 @@ describe('liteThrottle helper function', () => { const throttledFn = liteThrottle(mockFn, { wait: 100 }) throttledFn('test') - expect(mockFn).toBeCalledTimes(1) // Leading edge - expect(mockFn).toBeCalledWith('test') + expect(mockFn).toHaveBeenCalledTimes(1) // Leading edge + expect(mockFn).toHaveBeenCalledWith('test') throttledFn('ignored') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) vi.advanceTimersByTime(100) - expect(mockFn).toBeCalledTimes(2) // Trailing edge + expect(mockFn).toHaveBeenCalledTimes(2) // Trailing edge expect(mockFn).toHaveBeenLastCalledWith('ignored') }) @@ -532,7 +532,7 @@ describe('liteThrottle helper function', () => { const throttledFn = liteThrottle(mockFn, { wait: 100 }) throttledFn(42, 'test', { foo: 'bar' }) - expect(mockFn).toBeCalledWith(42, 'test', { foo: 'bar' }) + expect(mockFn).toHaveBeenCalledWith(42, 'test', { foo: 'bar' }) }) }) @@ -546,7 +546,7 @@ describe('liteThrottle helper function', () => { }) throttledFn('first') - expect(mockFn).not.toBeCalled() // No leading edge execution + expect(mockFn).not.toHaveBeenCalled() // No leading edge execution throttledFn('second') // Add another call to ensure trailing edge triggers @@ -565,11 +565,11 @@ describe('liteThrottle helper function', () => { }) throttledFn('first') - expect(mockFn).toBeCalledTimes(1) // Leading edge + expect(mockFn).toHaveBeenCalledTimes(1) // Leading edge throttledFn('second') vi.advanceTimersByTime(100) - expect(mockFn).toBeCalledTimes(1) // No trailing edge + expect(mockFn).toHaveBeenCalledTimes(1) // No trailing edge expect(mockFn).toHaveBeenCalledWith('first') }) @@ -580,23 +580,23 @@ describe('liteThrottle helper function', () => { // First burst throttledFn('a') throttledFn('b') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('a') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('a') // Advance halfway and make another call vi.advanceTimersByTime(50) throttledFn('c') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Complete first wait period vi.advanceTimersByTime(50) - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('c') // Wait another period and make new call vi.advanceTimersByTime(100) throttledFn('d') - expect(mockFn).toBeCalledTimes(3) + expect(mockFn).toHaveBeenCalledTimes(3) expect(mockFn).toHaveBeenLastCalledWith('d') }) @@ -608,12 +608,12 @@ describe('liteThrottle helper function', () => { for (let i = 0; i < 5; i++) { throttledFn(`call-${i}`) } - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('call-0') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('call-0') // Should execute the last call after wait vi.advanceTimersByTime(100) - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('call-4') }) @@ -626,10 +626,10 @@ describe('liteThrottle helper function', () => { }) throttledFn('test') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(100) - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should work with onExecute callback', () => { diff --git a/packages/pacer/tests/async-debouncer.test.ts b/packages/pacer/tests/async-debouncer.test.ts index 393d6158c..542f4bc49 100644 --- a/packages/pacer/tests/async-debouncer.test.ts +++ b/packages/pacer/tests/async-debouncer.test.ts @@ -25,14 +25,14 @@ describe('AsyncDebouncer', () => { const debouncer = new AsyncDebouncer(mockFn, { wait: 1000 }) const promise = debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(999) - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1) await promise - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should execute the async function after the specified wait', async () => { @@ -40,11 +40,11 @@ describe('AsyncDebouncer', () => { const debouncer = new AsyncDebouncer(mockFn, { wait: 1000 }) const promise = debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) const result = await promise - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) expect(result).toBe('result') }) @@ -60,14 +60,14 @@ describe('AsyncDebouncer', () => { const promise3 = debouncer.maybeExecute() // Function should not be called yet - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() // Wait for the full delay vi.advanceTimersByTime(1000) await Promise.any([promise1, promise2, promise3]) // Should only execute once - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should pass arguments to the debounced async function', async () => { @@ -78,7 +78,7 @@ describe('AsyncDebouncer', () => { vi.advanceTimersByTime(1000) await promise - expect(mockFn).toBeCalledWith('arg1', 42, { foo: 'bar' }) + expect(mockFn).toHaveBeenCalledWith('arg1', 42, { foo: 'bar' }) }) it('should return a promise that resolves with the function result', async () => { @@ -104,12 +104,12 @@ describe('AsyncDebouncer', () => { }) const promise = debouncer.maybeExecute() - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith() + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith() vi.advanceTimersByTime(1000) await promise - expect(mockFn).toBeCalledTimes(1) // Should not execute again + expect(mockFn).toHaveBeenCalledTimes(1) // Should not execute again }) it('should respect leading edge timing', async () => { @@ -122,22 +122,22 @@ describe('AsyncDebouncer', () => { // First call - executes immediately const promise1 = debouncer.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') // Call again before wait expires - should not execute vi.advanceTimersByTime(500) const promise2 = debouncer.maybeExecute('second') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Advance to end of second call's wait period - should not execute vi.advanceTimersByTime(1000) await Promise.all([promise1, promise2]) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Now that the full wait has passed since last call, this should execute const promise3 = debouncer.maybeExecute('third') - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('third') await promise3 }) @@ -152,17 +152,17 @@ describe('AsyncDebouncer', () => { // First call - executes immediately (leading) const promise1 = debouncer.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') // Second call - should not execute immediately const promise2 = debouncer.maybeExecute('second') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // After wait, should execute again (trailing) vi.advanceTimersByTime(1000) await Promise.all([promise1, promise2]) - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('second') }) @@ -172,17 +172,17 @@ describe('AsyncDebouncer', () => { // First call - should not execute immediately const promise1 = debouncer.maybeExecute('first') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() // Second call - should not execute immediately const promise2 = debouncer.maybeExecute('second') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() // After wait, should execute once with last arguments vi.advanceTimersByTime(1000) await Promise.any([promise1, promise2]) - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('second') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('second') }) it('should handle case where both leading and trailing are false', async () => { @@ -195,18 +195,18 @@ describe('AsyncDebouncer', () => { // First call - should not execute debouncer.maybeExecute('test') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() // Second call - should cancel first promise and not execute const promise2 = debouncer.maybeExecute('test2') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() // Advance time and wait for the last promise vi.advanceTimersByTime(1000) await promise2 // Verify no executions occurred - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should handle rapid calls with leading edge execution', async () => { @@ -224,8 +224,8 @@ describe('AsyncDebouncer', () => { const promise4 = debouncer.maybeExecute('fourth') // Only first call should execute immediately - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') // Wait for timeout and last promise vi.advanceTimersByTime(1000) @@ -233,7 +233,7 @@ describe('AsyncDebouncer', () => { // Next call should execute immediately const promise5 = debouncer.maybeExecute('fifth') - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('fifth') await promise5 }) @@ -249,7 +249,7 @@ describe('AsyncDebouncer', () => { const result = await promise expect(result).toBe('resolved value') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should handle promise errors without rejecting', async () => { @@ -267,7 +267,7 @@ describe('AsyncDebouncer', () => { // The promise should resolve with undefined, not reject const result = await promise expect(result).toBeUndefined() - expect(onError).toBeCalledWith(error, [], debouncer) + expect(onError).toHaveBeenCalledWith(error, [], debouncer) }) it('should maintain execution order of promises', async () => { @@ -290,8 +290,8 @@ describe('AsyncDebouncer', () => { await promise3 // Should only execute once with the last value - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('third') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('third') expect(results).toEqual(['third']) }) @@ -319,7 +319,7 @@ describe('AsyncDebouncer', () => { await promise3 expect(mockFn).toHaveBeenCalledWith('third') - expect(mockFn).toBeCalledTimes(3) + expect(mockFn).toHaveBeenCalledTimes(3) }) it('should handle promise cancellation', () => { @@ -332,7 +332,7 @@ describe('AsyncDebouncer', () => { debouncer.maybeExecute('test') debouncer.cancel() vi.advanceTimersByTime(1100) - expect(mockFn).toBeCalledTimes(0) + expect(mockFn).toHaveBeenCalledTimes(0) }) }) @@ -352,8 +352,8 @@ describe('AsyncDebouncer', () => { vi.advanceTimersByTime(1000) await promise - expect(onError).toBeCalledWith(error, [], debouncer) - expect(onSettled).toBeCalledWith([], debouncer) + expect(onError).toHaveBeenCalledWith(error, [], debouncer) + expect(onSettled).toHaveBeenCalledWith([], debouncer) expect(debouncer.store.state.errorCount).toBe(1) expect(debouncer.store.state.settleCount).toBe(1) expect(debouncer.store.state.successCount).toBe(0) @@ -375,7 +375,7 @@ describe('AsyncDebouncer', () => { const promise1 = debouncer.maybeExecute() vi.advanceTimersByTime(1000) await promise1 - expect(onError).toBeCalledWith(error, [], debouncer) + expect(onError).toHaveBeenCalledWith(error, [], debouncer) expect(debouncer.store.state.errorCount).toBe(1) expect(debouncer.store.state.settleCount).toBe(1) expect(debouncer.store.state.successCount).toBe(0) @@ -385,7 +385,7 @@ describe('AsyncDebouncer', () => { vi.advanceTimersByTime(1000) const result = await promise2 expect(result).toBe('success') - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(debouncer.store.state.errorCount).toBe(1) expect(debouncer.store.state.settleCount).toBe(2) expect(debouncer.store.state.successCount).toBe(1) @@ -438,8 +438,8 @@ describe('AsyncDebouncer', () => { const promise = debouncer.maybeExecute() await vi.advanceTimersByTimeAsync(1000) await promise - expect(onError).toBeCalledWith(error, [], debouncer) - expect(onSettled).toBeCalledWith([], debouncer) + expect(onError).toHaveBeenCalledWith(error, [], debouncer) + expect(onSettled).toHaveBeenCalledWith([], debouncer) expect(debouncer.store.state.errorCount).toBe(1) expect(debouncer.store.state.settleCount).toBe(1) expect(debouncer.store.state.successCount).toBe(0) @@ -460,8 +460,8 @@ describe('AsyncDebouncer', () => { const promise = debouncer.maybeExecute() vi.advanceTimersByTime(1000) await promise - expect(onError).toBeCalledWith(error, [], debouncer) - expect(onSettled).toBeCalledWith([], debouncer) + expect(onError).toHaveBeenCalledWith(error, [], debouncer) + expect(onSettled).toHaveBeenCalledWith([], debouncer) expect(debouncer.store.state.errorCount).toBe(1) expect(debouncer.store.state.settleCount).toBe(1) expect(debouncer.store.state.successCount).toBe(0) @@ -518,7 +518,7 @@ describe('AsyncDebouncer', () => { vi.advanceTimersByTime(1000) await expect(promise).rejects.toThrow('test error') - expect(onError).toBeCalledWith(error, [], debouncer) + expect(onError).toHaveBeenCalledWith(error, [], debouncer) expect(debouncer.store.state.errorCount).toBe(1) }) @@ -589,7 +589,7 @@ describe('AsyncDebouncer', () => { const result = await promise expect(result).toBeUndefined() - expect(onError).toBeCalledWith(error, [], debouncer) + expect(onError).toHaveBeenCalledWith(error, [], debouncer) expect(debouncer.store.state.errorCount).toBe(1) }) @@ -607,7 +607,7 @@ describe('AsyncDebouncer', () => { const promise = debouncer.maybeExecute() const result = await promise expect(result).toBeUndefined() - expect(onError).toBeCalledWith(error, [], debouncer) + expect(onError).toHaveBeenCalledWith(error, [], debouncer) expect(debouncer.store.state.errorCount).toBe(1) }) }) @@ -644,7 +644,7 @@ describe('AsyncDebouncer', () => { const result = await debouncer.flush() expect(result).toBeUndefined() - expect(onError).toBeCalledWith(error, [], debouncer) + expect(onError).toHaveBeenCalledWith(error, [], debouncer) expect(debouncer.store.state.errorCount).toBe(1) expect(debouncer.store.state.isPending).toBe(false) }) @@ -663,7 +663,7 @@ describe('AsyncDebouncer', () => { const result = await promise expect(result).toBeUndefined() - expect(onError).toBeCalledWith(error, [], debouncer) + expect(onError).toHaveBeenCalledWith(error, [], debouncer) }) }) @@ -678,7 +678,7 @@ describe('AsyncDebouncer', () => { const result = await debouncer.maybeExecute() expect(result).toBeUndefined() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() expect(debouncer.store.state.errorCount).toBe(0) }) }) @@ -697,7 +697,7 @@ describe('AsyncDebouncer', () => { const result = await promise expect(result).toBeUndefined() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() expect(debouncer.store.state.errorCount).toBe(0) }) }) @@ -716,8 +716,8 @@ describe('AsyncDebouncer', () => { vi.advanceTimersByTime(1000) await promise - expect(onSuccess).toBeCalledTimes(1) - expect(onSuccess).toBeCalledWith('success', [], debouncer) + expect(onSuccess).toHaveBeenCalledTimes(1) + expect(onSuccess).toHaveBeenCalledWith('success', [], debouncer) }) it('should call onSettled after execution completes', async () => { @@ -732,8 +732,8 @@ describe('AsyncDebouncer', () => { vi.advanceTimersByTime(1000) await promise - expect(onSettled).toBeCalledTimes(1) - expect(onSettled).toBeCalledWith([], debouncer) + expect(onSettled).toHaveBeenCalledTimes(1) + expect(onSettled).toHaveBeenCalledWith([], debouncer) }) it('should call onError when execution fails', async () => { @@ -749,8 +749,8 @@ describe('AsyncDebouncer', () => { vi.advanceTimersByTime(1000) await promise - expect(onError).toBeCalledTimes(1) - expect(onError).toBeCalledWith(error, [], debouncer) + expect(onError).toHaveBeenCalledTimes(1) + expect(onError).toHaveBeenCalledWith(error, [], debouncer) }) it('should maintain correct callback order', async () => { @@ -799,11 +799,11 @@ describe('AsyncDebouncer', () => { await promise // onSuccess throws, which triggers onError, and onSettled is always called - expect(onSuccess).toBeCalledTimes(1) - expect(onError).toBeCalledTimes(1) - expect(onError).toBeCalledWith(callbackError, [], debouncer) - expect(onSettled).toBeCalledTimes(1) - expect(onSettled).toBeCalledWith([], debouncer) + expect(onSuccess).toHaveBeenCalledTimes(1) + expect(onError).toHaveBeenCalledTimes(1) + expect(onError).toHaveBeenCalledWith(callbackError, [], debouncer) + expect(onSettled).toHaveBeenCalledTimes(1) + expect(onSettled).toHaveBeenCalledWith([], debouncer) }) }) @@ -822,7 +822,7 @@ describe('AsyncDebouncer', () => { // Advance time and verify no execution vi.advanceTimersByTime(1000) - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should properly handle canLeadingExecute flag after cancellation', async () => { @@ -834,8 +834,8 @@ describe('AsyncDebouncer', () => { // First call - should execute immediately const promise1 = debouncer.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') // Cancel and verify canLeadingExecute is reset debouncer.cancel() @@ -843,8 +843,8 @@ describe('AsyncDebouncer', () => { // Next call should execute immediately again const promise2 = debouncer.maybeExecute('second') - expect(mockFn).toBeCalledTimes(2) - expect(mockFn).toBeCalledWith('second') + expect(mockFn).toHaveBeenCalledTimes(2) + expect(mockFn).toHaveBeenCalledWith('second') await Promise.all([promise1, promise2]) }) @@ -858,8 +858,8 @@ describe('AsyncDebouncer', () => { // First call - executes immediately const promise1 = debouncer.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') // Cancel during leading execution debouncer.cancel() @@ -867,8 +867,8 @@ describe('AsyncDebouncer', () => { // Next call should execute immediately again const promise2 = debouncer.maybeExecute('second') - expect(mockFn).toBeCalledTimes(2) - expect(mockFn).toBeCalledWith('second') + expect(mockFn).toHaveBeenCalledTimes(2) + expect(mockFn).toHaveBeenCalledWith('second') await Promise.all([promise1, promise2]) }) @@ -1012,7 +1012,7 @@ describe('AsyncDebouncer', () => { // Advance time and verify no execution vi.advanceTimersByTime(1000) await promise - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should not execute leading edge when disabled', async () => { @@ -1031,7 +1031,7 @@ describe('AsyncDebouncer', () => { // Advance time and verify no execution vi.advanceTimersByTime(1000) await promise - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should default to enabled', async () => { @@ -1045,7 +1045,7 @@ describe('AsyncDebouncer', () => { // Advance time and verify execution vi.advanceTimersByTime(1000) await promise - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should allow disabling mid-wait', async () => { @@ -1063,7 +1063,7 @@ describe('AsyncDebouncer', () => { // Advance time and verify no execution vi.advanceTimersByTime(1000) await promise - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should handle rapid enable/disable cycles', async () => { @@ -1086,7 +1086,7 @@ describe('AsyncDebouncer', () => { // Advance time and verify single execution vi.advanceTimersByTime(1000) await promise - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should maintain state when disabled', async () => { @@ -1128,13 +1128,13 @@ describe('AsyncDebouncer', () => { // Verify new options are applied const promise = debouncer.maybeExecute() - expect(mockFn).toBeCalledTimes(1) // Leading execution - expect(mockFn).toBeCalledWith() + expect(mockFn).toHaveBeenCalledTimes(1) // Leading execution + expect(mockFn).toHaveBeenCalledWith() // Advance time and verify no trailing execution vi.advanceTimersByTime(500) await promise - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should handle option changes during execution', async () => { @@ -1155,8 +1155,8 @@ describe('AsyncDebouncer', () => { // Advance time and verify new callback is used vi.advanceTimersByTime(1000) await promise - expect(onSuccess).toBeCalledTimes(1) - expect(onSuccess).toBeCalledWith('result', [], debouncer) + expect(onSuccess).toHaveBeenCalledTimes(1) + expect(onSuccess).toHaveBeenCalledWith('result', [], debouncer) }) it('should maintain state across option changes', async () => { @@ -1179,7 +1179,7 @@ describe('AsyncDebouncer', () => { // Second execution with new options const promise2 = debouncer.maybeExecute() - expect(mockFn).toBeCalledTimes(2) // Leading execution + expect(mockFn).toHaveBeenCalledTimes(2) // Leading execution vi.advanceTimersByTime(500) await promise2 expect(debouncer.store.state.successCount).toBe(2) @@ -1205,9 +1205,9 @@ describe('AsyncDebouncer', () => { const promise1 = debouncer.maybeExecute() vi.advanceTimersByTime(1000) await promise1 - expect(onSuccess1).toBeCalledTimes(1) - expect(onSettled1).toBeCalledTimes(1) - expect(onError1).not.toBeCalled() + expect(onSuccess1).toHaveBeenCalledTimes(1) + expect(onSettled1).toHaveBeenCalledTimes(1) + expect(onError1).not.toHaveBeenCalled() // Change callbacks debouncer.setOptions({ @@ -1220,9 +1220,9 @@ describe('AsyncDebouncer', () => { const promise2 = debouncer.maybeExecute() vi.advanceTimersByTime(1000) await promise2 - expect(onSuccess2).toBeCalledTimes(1) - expect(onSettled2).toBeCalledTimes(1) - expect(onError2).not.toBeCalled() + expect(onSuccess2).toHaveBeenCalledTimes(1) + expect(onSettled2).toHaveBeenCalledTimes(1) + expect(onError2).not.toHaveBeenCalled() }) it('should handle option changes during error handling', async () => { @@ -1252,12 +1252,12 @@ describe('AsyncDebouncer', () => { // Advance time and verify new callbacks are used vi.advanceTimersByTime(1000) await promise - expect(onError2).toBeCalledTimes(1) - expect(onError2).toBeCalledWith(error, [], debouncer) - expect(onSettled2).toBeCalledTimes(1) - expect(onSettled2).toBeCalledWith([], debouncer) - expect(onError1).not.toBeCalled() - expect(onSettled1).not.toBeCalled() + expect(onError2).toHaveBeenCalledTimes(1) + expect(onError2).toHaveBeenCalledWith(error, [], debouncer) + expect(onSettled2).toHaveBeenCalledTimes(1) + expect(onSettled2).toHaveBeenCalledWith([], debouncer) + expect(onError1).not.toHaveBeenCalled() + expect(onSettled1).not.toHaveBeenCalled() }) }) }) @@ -1277,11 +1277,11 @@ describe('asyncDebounce helper function', () => { const debounced = asyncDebounce(mockFn, { wait: 1000 }) const promise = debounced() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) const result = await promise - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) expect(result).toBe('result') }) @@ -1293,7 +1293,7 @@ describe('asyncDebounce helper function', () => { vi.advanceTimersByTime(1000) await promise - expect(mockFn).toBeCalledWith('arg1', 42, { foo: 'bar' }) + expect(mockFn).toHaveBeenCalledWith('arg1', 42, { foo: 'bar' }) }) it('should return a promise', () => { @@ -1311,11 +1311,11 @@ describe('asyncDebounce helper function', () => { const debounced = asyncDebounce(mockFn, { wait: 1000, leading: true }) const promise = debounced() - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) vi.advanceTimersByTime(1000) await promise - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should handle multiple calls with trailing edge', () => { @@ -1325,10 +1325,10 @@ describe('asyncDebounce helper function', () => { debounced() debounced() debounced() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should support both leading and trailing execution', async () => { @@ -1341,17 +1341,17 @@ describe('asyncDebounce helper function', () => { // First call - should execute immediately const promise1 = debounced('first') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') // Second call - should queue for trailing const promise2 = debounced('second') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) vi.advanceTimersByTime(1000) await Promise.all([promise1, promise2]) - expect(mockFn).toBeCalledTimes(2) - expect(mockFn).toBeCalledWith('second') + expect(mockFn).toHaveBeenCalledTimes(2) + expect(mockFn).toHaveBeenCalledWith('second') }) }) diff --git a/packages/pacer/tests/debouncer.test.ts b/packages/pacer/tests/debouncer.test.ts index b8d69ab3a..db4b7376b 100644 --- a/packages/pacer/tests/debouncer.test.ts +++ b/packages/pacer/tests/debouncer.test.ts @@ -16,7 +16,7 @@ describe('Debouncer', () => { const debouncer = new Debouncer(mockFn, { wait: 1000 }) debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should execute the function after the specified wait', () => { @@ -24,10 +24,10 @@ describe('Debouncer', () => { const debouncer = new Debouncer(mockFn, { wait: 1000 }) debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should debounce multiple calls', () => { @@ -37,10 +37,10 @@ describe('Debouncer', () => { debouncer.maybeExecute() debouncer.maybeExecute() debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should pass arguments to the debounced function', () => { @@ -50,7 +50,7 @@ describe('Debouncer', () => { debouncer.maybeExecute('test', 123) vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledWith('test', 123) + expect(mockFn).toHaveBeenCalledWith('test', 123) }) }) @@ -64,11 +64,11 @@ describe('Debouncer', () => { }) debouncer.maybeExecute('test') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('test') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('test') vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should respect leading edge timing', () => { @@ -81,20 +81,20 @@ describe('Debouncer', () => { // First call - executes immediately debouncer.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Call again before wait expires - should not execute vi.advanceTimersByTime(500) debouncer.maybeExecute('second') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Advance to end of second call's wait period - should not execute vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Now that the full wait has passed since last call, this should execute debouncer.maybeExecute('third') - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('third') }) @@ -108,10 +108,10 @@ describe('Debouncer', () => { debouncer.maybeExecute('test1') debouncer.maybeExecute('test2') - expect(mockFn).toBeCalledTimes(1) // Leading call + expect(mockFn).toHaveBeenCalledTimes(1) // Leading call vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(2) // Trailing call + expect(mockFn).toHaveBeenCalledTimes(2) // Trailing call }) it('should default to trailing-only execution', () => { @@ -120,11 +120,11 @@ describe('Debouncer', () => { debouncer.maybeExecute('test1') debouncer.maybeExecute('test2') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('test2') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('test2') }) it('should handle case where both leading and trailing are false', () => { @@ -136,14 +136,14 @@ describe('Debouncer', () => { }) debouncer.maybeExecute('test') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() // Should still reset canLeadingExecute flag debouncer.maybeExecute('test2') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) }) @@ -156,7 +156,7 @@ describe('Debouncer', () => { debouncer.cancel() vi.advanceTimersByTime(1000) - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should properly handle canLeadingExecute flag after cancellation', () => { @@ -169,7 +169,7 @@ describe('Debouncer', () => { // First call - executes immediately debouncer.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Cancel before wait expires vi.advanceTimersByTime(500) @@ -177,7 +177,7 @@ describe('Debouncer', () => { // Should be able to execute immediately again after cancellation debouncer.maybeExecute('second') - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('second') }) @@ -196,15 +196,15 @@ describe('Debouncer', () => { debouncer.maybeExecute('fourth') // Only first call should execute immediately - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') // Wait for timeout vi.advanceTimersByTime(1000) // Next call should execute immediately debouncer.maybeExecute('fifth') - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('fifth') }) }) @@ -215,11 +215,11 @@ describe('Debouncer', () => { const debouncer = new Debouncer(mockFn, { wait: 1000 }) debouncer.maybeExecute('test') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() debouncer.flush() - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('test') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('test') }) it('should clear pending timeout when flushing', () => { @@ -232,7 +232,7 @@ describe('Debouncer', () => { // Advance time to ensure timeout would have fired vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should do nothing when no pending execution', () => { @@ -240,7 +240,7 @@ describe('Debouncer', () => { const debouncer = new Debouncer(mockFn, { wait: 1000 }) debouncer.flush() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should work with leading and trailing execution', () => { @@ -252,12 +252,12 @@ describe('Debouncer', () => { }) debouncer.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) debouncer.maybeExecute('second') debouncer.flush() - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('second') }) @@ -270,13 +270,13 @@ describe('Debouncer', () => { }) debouncer.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) debouncer.maybeExecute('second') debouncer.flush() // With leading: true, trailing: false, flush should NOT cause another call - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) expect(mockFn).toHaveBeenLastCalledWith('first') }) @@ -304,7 +304,7 @@ describe('Debouncer', () => { debouncer.maybeExecute('test') vi.advanceTimersByTime(1000) - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should not execute leading edge when disabled', () => { @@ -316,9 +316,9 @@ describe('Debouncer', () => { }) debouncer.maybeExecute('test') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should default to enabled', () => { @@ -329,8 +329,8 @@ describe('Debouncer', () => { debouncer.maybeExecute('test') vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('test') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('test') }) it('should allow enabling/disabling after construction', () => { @@ -340,20 +340,20 @@ describe('Debouncer', () => { // Start enabled by default debouncer.maybeExecute('first') vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') // Disable and verify no execution debouncer.setOptions({ enabled: false }) debouncer.maybeExecute('second') vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) // Still only called once + expect(mockFn).toHaveBeenCalledTimes(1) // Still only called once // Re-enable and verify execution resumes debouncer.setOptions({ enabled: true }) debouncer.maybeExecute('third') vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('third') }) @@ -365,7 +365,7 @@ describe('Debouncer', () => { vi.advanceTimersByTime(500) // Half-way through wait debouncer.setOptions({ enabled: false }) vi.advanceTimersByTime(500) // Complete wait - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) }) @@ -380,11 +380,11 @@ describe('Debouncer', () => { // Verify new leading behavior debouncer.maybeExecute('test1') debouncer.maybeExecute('test2') - expect(mockFn).toBeCalledTimes(1) // Immediate execution due to leading: true + expect(mockFn).toHaveBeenCalledTimes(1) // Immediate execution due to leading: true // Verify new wait time vi.advanceTimersByTime(500) // Only need to wait 500ms now - expect(mockFn).toBeCalledTimes(2) // Trailing execution after shorter wait + expect(mockFn).toHaveBeenCalledTimes(2) // Trailing execution after shorter wait }) }) @@ -544,11 +544,11 @@ describe('Debouncer', () => { }) debouncer.maybeExecute() - expect(onExecute).not.toBeCalled() + expect(onExecute).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) - expect(onExecute).toBeCalledTimes(1) - expect(onExecute).toBeCalledWith([], debouncer) + expect(onExecute).toHaveBeenCalledTimes(1) + expect(onExecute).toHaveBeenCalledWith([], debouncer) }) it('should call onExecute callback with leading execution', () => { @@ -561,11 +561,11 @@ describe('Debouncer', () => { }) debouncer.maybeExecute() - expect(onExecute).toBeCalledTimes(1) - expect(onExecute).toBeCalledWith([], debouncer) + expect(onExecute).toHaveBeenCalledTimes(1) + expect(onExecute).toHaveBeenCalledWith([], debouncer) vi.advanceTimersByTime(1000) - expect(onExecute).toBeCalledTimes(1) // Should not be called again + expect(onExecute).toHaveBeenCalledTimes(1) // Should not be called again }) it('should not call onExecute callback when disabled', () => { @@ -579,7 +579,7 @@ describe('Debouncer', () => { debouncer.maybeExecute() vi.advanceTimersByTime(1000) - expect(onExecute).not.toBeCalled() + expect(onExecute).not.toHaveBeenCalled() }) it('should not call onExecute callback when cancelled', () => { @@ -593,7 +593,7 @@ describe('Debouncer', () => { debouncer.maybeExecute() debouncer.cancel() vi.advanceTimersByTime(1000) - expect(onExecute).not.toBeCalled() + expect(onExecute).not.toHaveBeenCalled() }) it('should call onExecute callback with correct debouncer instance', () => { @@ -606,7 +606,7 @@ describe('Debouncer', () => { debouncer.maybeExecute() vi.advanceTimersByTime(1000) - expect(onExecute).toBeCalledWith([], debouncer) + expect(onExecute).toHaveBeenCalledWith([], debouncer) expect(onExecute.mock.calls[0]?.[1]).toBe(debouncer) }) @@ -621,12 +621,12 @@ describe('Debouncer', () => { // First execution debouncer.maybeExecute() vi.advanceTimersByTime(1000) - expect(onExecute).toBeCalledTimes(1) + expect(onExecute).toHaveBeenCalledTimes(1) // Second execution debouncer.maybeExecute() vi.advanceTimersByTime(1000) - expect(onExecute).toBeCalledTimes(2) + expect(onExecute).toHaveBeenCalledTimes(2) }) }) @@ -636,10 +636,10 @@ describe('Debouncer', () => { const debouncer = new Debouncer(mockFn, { wait: 0 }) debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(0) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should handle negative wait time by using 0', () => { @@ -647,10 +647,10 @@ describe('Debouncer', () => { const debouncer = new Debouncer(mockFn, { wait: -1000 }) debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(0) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should handle very large wait times', () => { @@ -658,10 +658,10 @@ describe('Debouncer', () => { const debouncer = new Debouncer(mockFn, { wait: Number.MAX_SAFE_INTEGER }) debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(Number.MAX_SAFE_INTEGER) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should handle NaN wait time by using 0', () => { @@ -669,10 +669,10 @@ describe('Debouncer', () => { const debouncer = new Debouncer(mockFn, { wait: NaN }) debouncer.maybeExecute() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(0) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) it('should handle undefined/null arguments', () => { @@ -681,7 +681,7 @@ describe('Debouncer', () => { debouncer.maybeExecute(undefined, null) vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledWith(undefined, null) + expect(mockFn).toHaveBeenCalledWith(undefined, null) }) it('should prevent memory leaks by clearing timeouts', () => { @@ -698,7 +698,7 @@ describe('Debouncer', () => { // Advance time to ensure no executions occur vi.advanceTimersByTime(1000) - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should handle rapid option changes', () => { @@ -715,7 +715,7 @@ describe('Debouncer', () => { // Should still execute after the last wait time vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) }) }) }) @@ -735,11 +735,11 @@ describe('debounce helper function', () => { const debouncedFn = debounce(mockFn, { wait: 1000 }) debouncedFn('test') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('test') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('test') }) it('should pass arguments correctly', () => { @@ -749,7 +749,7 @@ describe('debounce helper function', () => { debouncedFn(42, 'test', { foo: 'bar' }) vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledWith(42, 'test', { foo: 'bar' }) + expect(mockFn).toHaveBeenCalledWith(42, 'test', { foo: 'bar' }) }) }) @@ -763,17 +763,17 @@ describe('debounce helper function', () => { }) debouncedFn('first') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') debouncedFn('second') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) debouncedFn('third') - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('third') }) @@ -784,15 +784,15 @@ describe('debounce helper function', () => { debouncedFn('a') debouncedFn('b') debouncedFn('c') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(500) debouncedFn('d') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('d') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('d') }) it('should support both leading and trailing execution', () => { @@ -803,14 +803,14 @@ describe('debounce helper function', () => { }) debouncedFn('first') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') debouncedFn('second') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('second') }) }) diff --git a/packages/pacer/tests/throttler.test.ts b/packages/pacer/tests/throttler.test.ts index fcd269ff7..4225faefe 100644 --- a/packages/pacer/tests/throttler.test.ts +++ b/packages/pacer/tests/throttler.test.ts @@ -166,12 +166,12 @@ describe('Throttler', () => { // Should execute immediately due to leading: true throttler.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') // Should execute immediately again since wait is 0 throttler.maybeExecute('second') - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('second') }) @@ -181,21 +181,21 @@ describe('Throttler', () => { // First call should execute immediately throttler.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') // Subsequent calls should be throttled throttler.maybeExecute('second') throttler.maybeExecute('third') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Advance time by half the wait period vi.advanceTimersByTime(500000) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Complete the wait period vi.advanceTimersByTime(500000) - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('third') }) @@ -205,19 +205,19 @@ describe('Throttler', () => { // First call executes immediately throttler.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Second call during wait period throttler.maybeExecute('second') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Cancel before trailing execution throttler.cancel() // Advance time - trailing execution should not occur vi.advanceTimersByTime(100) - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') }) it('should handle multiple cancellations', () => { @@ -226,21 +226,21 @@ describe('Throttler', () => { // First call throttler.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Cancel before trailing execution throttler.cancel() vi.advanceTimersByTime(100) - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Second call throttler.maybeExecute('second') - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) // Cancel again throttler.cancel() vi.advanceTimersByTime(100) - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) }) describe('Flush Method', () => { @@ -249,13 +249,13 @@ describe('Throttler', () => { const throttler = new Throttler(mockFn, { wait: 1000 }) throttler.maybeExecute('test') - expect(mockFn).toBeCalledTimes(1) // Leading execution + expect(mockFn).toHaveBeenCalledTimes(1) // Leading execution throttler.maybeExecute('pending') - expect(mockFn).toBeCalledTimes(1) // Still throttled + expect(mockFn).toHaveBeenCalledTimes(1) // Still throttled throttler.flush() - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('pending') }) @@ -270,7 +270,7 @@ describe('Throttler', () => { // Advance time to ensure timeout would have fired vi.advanceTimersByTime(1000) - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) }) it('should do nothing when no pending execution', () => { @@ -278,7 +278,7 @@ describe('Throttler', () => { const throttler = new Throttler(mockFn, { wait: 1000 }) throttler.flush() - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) it('should work with leading and trailing execution', () => { @@ -290,12 +290,12 @@ describe('Throttler', () => { }) throttler.maybeExecute('first') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) throttler.maybeExecute('second') throttler.flush() - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('second') }) @@ -308,11 +308,11 @@ describe('Throttler', () => { }) throttler.maybeExecute('first') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() throttler.flush() - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('first') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('first') }) it('should update state correctly after flush', () => { @@ -345,14 +345,14 @@ describe('throttle helper function', () => { const throttledFn = throttle(mockFn, { wait: 100 }) throttledFn('test') - expect(mockFn).toBeCalledTimes(1) // Leading edge - expect(mockFn).toBeCalledWith('test') + expect(mockFn).toHaveBeenCalledTimes(1) // Leading edge + expect(mockFn).toHaveBeenCalledWith('test') throttledFn('ignored') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) vi.advanceTimersByTime(100) - expect(mockFn).toBeCalledTimes(2) // Trailing edge + expect(mockFn).toHaveBeenCalledTimes(2) // Trailing edge expect(mockFn).toHaveBeenLastCalledWith('ignored') }) @@ -361,7 +361,7 @@ describe('throttle helper function', () => { const throttledFn = throttle(mockFn, { wait: 100 }) throttledFn(42, 'test', { foo: 'bar' }) - expect(mockFn).toBeCalledWith(42, 'test', { foo: 'bar' }) + expect(mockFn).toHaveBeenCalledWith(42, 'test', { foo: 'bar' }) }) it('should respect leading: false option', () => { @@ -373,7 +373,7 @@ describe('throttle helper function', () => { }) throttledFn('first') - expect(mockFn).not.toBeCalled() // No leading edge execution + expect(mockFn).not.toHaveBeenCalled() // No leading edge execution throttledFn('second') // Add another call to ensure trailing edge triggers @@ -392,11 +392,11 @@ describe('throttle helper function', () => { }) throttledFn('first') - expect(mockFn).toBeCalledTimes(1) // Leading edge + expect(mockFn).toHaveBeenCalledTimes(1) // Leading edge throttledFn('second') vi.advanceTimersByTime(100) - expect(mockFn).toBeCalledTimes(1) // No trailing edge + expect(mockFn).toHaveBeenCalledTimes(1) // No trailing edge expect(mockFn).toHaveBeenCalledWith('first') }) @@ -407,23 +407,23 @@ describe('throttle helper function', () => { // First burst throttledFn('a') throttledFn('b') - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('a') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('a') // Advance halfway and make another call vi.advanceTimersByTime(50) throttledFn('c') - expect(mockFn).toBeCalledTimes(1) + expect(mockFn).toHaveBeenCalledTimes(1) // Complete first wait period vi.advanceTimersByTime(50) - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('c') // Wait another period and make new call vi.advanceTimersByTime(100) throttledFn('d') - expect(mockFn).toBeCalledTimes(3) + expect(mockFn).toHaveBeenCalledTimes(3) expect(mockFn).toHaveBeenLastCalledWith('d') }) @@ -435,12 +435,12 @@ describe('throttle helper function', () => { for (let i = 0; i < 5; i++) { throttledFn(`call-${i}`) } - expect(mockFn).toBeCalledTimes(1) - expect(mockFn).toBeCalledWith('call-0') + expect(mockFn).toHaveBeenCalledTimes(1) + expect(mockFn).toHaveBeenCalledWith('call-0') // Should execute the last call after wait vi.advanceTimersByTime(100) - expect(mockFn).toBeCalledTimes(2) + expect(mockFn).toHaveBeenCalledTimes(2) expect(mockFn).toHaveBeenLastCalledWith('call-4') }) @@ -453,9 +453,9 @@ describe('throttle helper function', () => { }) throttledFn('test') - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() vi.advanceTimersByTime(100) - expect(mockFn).not.toBeCalled() + expect(mockFn).not.toHaveBeenCalled() }) })