diff --git a/packages/cli-kit/src/public/common/function.test.ts b/packages/cli-kit/src/public/common/function.test.ts index 4a64391ed79..05df7d5f285 100644 --- a/packages/cli-kit/src/public/common/function.test.ts +++ b/packages/cli-kit/src/public/common/function.test.ts @@ -1,5 +1,5 @@ -import {debounce, memoize} from './function.js' -import {describe, test, expect} from 'vitest' +import {debounce, memoize, throttle} from './function.js' +import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest' describe('memoize', () => { test('memoizes the function value', () => { @@ -35,3 +35,33 @@ describe('debounce', () => { expect(value).toEqual(1) }) }) + +describe('throttle', () => { + beforeEach(() => { + vi.useFakeTimers() + }) + + afterEach(() => { + vi.useRealTimers() + }) + + test('throttles function executions over time', () => { + // Given + let count = 0 + const throttled = throttle(() => { + count += 1 + }, 100) + + // When + throttled() + throttled() + throttled() + + // Then + expect(count).toBe(1) + + // Advance time past the wait interval + vi.advanceTimersByTime(100) + expect(count).toBe(2) + }) +})