import { beforeEach, describe, it, vi } from 'vitest' import { debounce } from './debounce' vi.useFakeTimers() describe('debounce()', () => { let debouncedFunc: Function let testFunc: Function let testWait: number let testImmediate: boolean beforeEach(() => { testFunc = vi.fn() testWait = 1000 testImmediate = true debouncedFunc = debounce(testFunc, testWait, testImmediate) }) it('should be throw error if first argument passed is not function type', () => { testFunc = '' const resultFn = () => { debounce(testFunc, testWait, testImmediate)() } expect(resultFn).toThrow(/is not a function/) }) it('should execute testFunc', () => { debouncedFunc() expect(testFunc).toBeCalled() }) it('should be execute just once', () => { for (let i = 0; i < 100; i++) debouncedFunc() vi.runAllTimers() expect(testFunc).toBeCalledTimes(1) }) it('should not be executed twice with passed wait parametr equal 1000', () => { for (let i = 0; i < 100; i++) debouncedFunc() vi.runAllTimers() expect(testFunc).not.toBeCalledTimes(2) }) })