47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach, vitest } from 'vitest'
|
|
import { Throttler } from '../../src/lib/utilities/buffer-utilities'
|
|
|
|
describe('throttler', () => {
|
|
let throttleInstance: Throttler
|
|
let callback: () => void
|
|
|
|
beforeEach(() => {
|
|
vitest.useFakeTimers()
|
|
throttleInstance = new Throttler()
|
|
callback = vitest.fn()
|
|
})
|
|
|
|
afterEach(() => {
|
|
vitest.useRealTimers()
|
|
})
|
|
|
|
it('should call the callback function after the specified time', () => {
|
|
throttleInstance.throttle(callback, 1000)
|
|
expect(callback).not.toHaveBeenCalled()
|
|
|
|
vitest.advanceTimersByTime(1000)
|
|
expect(callback).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('should not call the callback function if throttle is called again within the timeout period', () => {
|
|
throttleInstance.throttle(callback, 1000)
|
|
throttleInstance.throttle(callback, 1000)
|
|
|
|
vitest.advanceTimersByTime(500)
|
|
expect(callback).not.toHaveBeenCalled()
|
|
|
|
vitest.advanceTimersByTime(500)
|
|
expect(callback).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('should allow the callback to be called again after the timeout period', () => {
|
|
throttleInstance.throttle(callback, 1000)
|
|
vitest.advanceTimersByTime(1000)
|
|
expect(callback).toHaveBeenCalledTimes(1)
|
|
|
|
throttleInstance.throttle(callback, 1000)
|
|
vitest.advanceTimersByTime(1000)
|
|
expect(callback).toHaveBeenCalledTimes(2)
|
|
})
|
|
})
|