Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions packages/cli-kit/src/public/common/function.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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)
})
})
Loading