Skip to content

Commit

Permalink
test: ✅ add tests for different parameter types of the throttleFilter…
Browse files Browse the repository at this point in the history
… function
  • Loading branch information
17359898647 committed Feb 6, 2024
1 parent 29598e1 commit f93b05e
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/shared/utils/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function debounceFilter(ms: MaybeRefOrGetter<number>, options: DebounceFi
return filter
}

interface throttleFilterOptions {
interface ThrottleFilterOptions {
/**
* The maximum time allowed to be delayed before it's invoked.
*/
Expand Down Expand Up @@ -143,7 +143,7 @@ interface throttleFilterOptions {
* @param [rejectOnCancel]
*/
export function throttleFilter(ms: MaybeRefOrGetter<number>, trailing?: boolean, leading?: boolean, rejectOnCancel?: boolean): EventFilter
export function throttleFilter(options: throttleFilterOptions): EventFilter
export function throttleFilter(options: ThrottleFilterOptions): EventFilter
export function throttleFilter(...args: any[]) {
let lastExec = 0
let timer: ReturnType<typeof setTimeout> | undefined
Expand Down
142 changes: 141 additions & 1 deletion packages/shared/utils/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ describe('filters', () => {
it('should throttle', () => {
const throttledFilterSpy = vi.fn()
const filter = createFilterWrapper(throttleFilter(1000), throttledFilterSpy)

setTimeout(filter, 500)
setTimeout(filter, 500)
setTimeout(filter, 500)
Expand Down Expand Up @@ -353,3 +352,144 @@ describe('compatibility', () => {
}
})
})

describe('optionsFilters', () => {
beforeEach(() => {
vi.useFakeTimers()
})
it('optionsThrottleFilter should throttle', () => {
const throttledFilterSpy = vi.fn()
const filter = createFilterWrapper(throttleFilter({
delay: 1000,
}), throttledFilterSpy)
setTimeout(filter, 500)
setTimeout(filter, 500)
setTimeout(filter, 500)
setTimeout(filter, 500)

vi.runAllTimers()

expect(throttledFilterSpy).toHaveBeenCalledTimes(2)
})

it('optionsThrottleFilter should throttle evenly', () => {
const debouncedFilterSpy = vi.fn()

const filter = createFilterWrapper(throttleFilter({
delay: 1000,
}), debouncedFilterSpy)

setTimeout(() => filter(1), 500)
setTimeout(() => filter(2), 1000)
setTimeout(() => filter(3), 2000)

vi.runAllTimers()

expect(debouncedFilterSpy).toHaveBeenCalledTimes(3)
expect(debouncedFilterSpy).toHaveBeenCalledWith(1)
expect(debouncedFilterSpy).toHaveBeenCalledWith(2)
expect(debouncedFilterSpy).toHaveBeenCalledWith(3)
})

it('optionsThrottleFilter should throttle with ref', () => {
const debouncedFilterSpy = vi.fn()
const throttle = ref(0)
const filter = createFilterWrapper(throttleFilter(throttle), debouncedFilterSpy)

filter()
throttle.value = 1000

setTimeout(filter, 300)
setTimeout(filter, 600)
setTimeout(filter, 900)

vi.runAllTimers()

expect(debouncedFilterSpy).toHaveBeenCalledTimes(2)
})

it('optionsThrottleFilter should not duplicate single event', () => {
const debouncedFilterSpy = vi.fn()
const filter = createFilterWrapper(throttleFilter({
delay: 1000,
}), debouncedFilterSpy)

setTimeout(filter, 500)

vi.runAllTimers()

expect(debouncedFilterSpy).toHaveBeenCalledTimes(1)
})

it('optionsThrottleFilter should get trailing value', () => {
const sumSpy = vi.fn((a: number, b: number) => a + b)
const throttledSum = createFilterWrapper(
throttleFilter({
delay: 1000,
trailing: true,
}),
sumSpy,
)

let result = throttledSum(2, 3)
setTimeout(() => {
result = throttledSum(4, 5)
}, 600)
setTimeout(() => {
result = throttledSum(6, 7)
}, 900)

vi.runAllTimers()

expect(sumSpy).toHaveBeenCalledTimes(2)
expect(result).resolves.toBe(6 + 7)

setTimeout(() => {
result = throttledSum(8, 9)
}, 1200)
setTimeout(() => {
result = throttledSum(10, 11)
}, 1800)

vi.runAllTimers()

expect(sumSpy).toHaveBeenCalledTimes(4)
expect(result).resolves.toBe(10 + 11)
})

it('optionsThrottleFilter should get leading value', () => {
const sumSpy = vi.fn((a: number, b: number) => a + b)
const throttledSum = createFilterWrapper(
throttleFilter({
delay: 1000,
trailing: false,
}),
sumSpy,
)

let result = throttledSum(2, 3)
setTimeout(() => {
result = throttledSum(4, 5)
}, 600)
setTimeout(() => {
result = throttledSum(6, 7)
}, 900)

vi.runAllTimers()

expect(sumSpy).toHaveBeenCalledTimes(1)
expect(result).resolves.toBe(2 + 3)

setTimeout(() => {
result = throttledSum(8, 9)
}, 1200)
setTimeout(() => {
result = throttledSum(10, 11)
}, 1800)

vi.runAllTimers()

expect(sumSpy).toHaveBeenCalledTimes(2)
expect(result).resolves.toBe(8 + 9)
})
})

0 comments on commit f93b05e

Please sign in to comment.