Skip to content

Commit

Permalink
improve test consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
nruffing committed Jan 5, 2024
1 parent fa9e75c commit a0f928c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
22 changes: 12 additions & 10 deletions lib/composables/useDebounce.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ async function executeDebounceTest(
//expectTimestampCloseEnough(actualCall.timestamp, expectedCall.timestampOffset + timestampStart)
expect(actualCall.args).toEqual(expectedCall.args)
}

debounced!.destroy()
}

describe('useDebounce', () => {
Expand All @@ -107,14 +109,14 @@ describe('useDebounce', () => {
})

test('successfully debounces - immediate and timeout mode', async () => {
const debounceMs = 100
const debounceMs = 200

const calls = [
{ delay: 0, arg1: 'a', arg2: 1 },
{ delay: 150, arg1: 'a1', arg2: 11 },
{ delay: 220, arg1: 'a1', arg2: 11 },
{ delay: 10, arg1: 'a', arg2: 1 },
{ delay: 50, arg1: 'b', arg2: 2 },
{ delay: 110, arg1: 'c', arg2: 3 },
{ delay: 10, arg1: 'b', arg2: 2 },
{ delay: 220, arg1: 'c', arg2: 3 },
] as DebounceTestCall[]

const expected = [{ args: ['a', 1] }, { args: ['a1', 11] }, { args: ['b', 2] }, { args: ['c', 3] }] as DebounceTestExpectedCall[]
Expand All @@ -123,15 +125,15 @@ describe('useDebounce', () => {
})

test('successfully debounces - maximum frequency', async () => {
const debounceMs = 100
const debounceMs = 200

const calls = [
{ delay: 0, arg1: 'a', arg2: 1 },
{ delay: 80, arg1: 'a1', arg2: 11 },
{ delay: 60, arg1: 'a', arg2: 1 },
{ delay: 10, arg1: 'a', arg2: 1 },
{ delay: 20, arg1: 'b', arg2: 2 },
{ delay: 110, arg1: 'c', arg2: 3 },
{ delay: 160, arg1: 'a1', arg2: 11 },
{ delay: 120, arg1: 'a', arg2: 1 },
{ delay: 20, arg1: 'a', arg2: 1 },
{ delay: 40, arg1: 'b', arg2: 2 },
{ delay: 220, arg1: 'c', arg2: 3 },
] as DebounceTestCall[]

const expected = [{ args: ['a', 1] }, { args: ['a1', 11] }, { args: ['b', 2] }, { args: ['c', 3] }] as DebounceTestExpectedCall[]
Expand Down
12 changes: 7 additions & 5 deletions lib/composables/useDebounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ export function useDebounce(
* If this is the first call, execute immediately
*/
if (!lastCallTimestamp.value) {
lastCallTimestamp.value = Date.now()
log('useDebounce | first call', { args, lastCallTimestamp: lastCallTimestamp.value })
log('useDebounce | first call', { args })
lastArgs.value = args
return execute()
}
Expand All @@ -83,7 +82,7 @@ export function useDebounce(
*/
const elapsed = Date.now() - lastCallTimestamp.value
if (!timeoutId.value && elapsed > timeoutMs) {
log('useDebounce | subsequent call within timeout', args)
log('useDebounce | subsequent call within timeout', { args, elapsed })
lastArgs.value = args
return execute()
}
Expand All @@ -98,7 +97,9 @@ export function useDebounce(
lastArgs.value = args
if (!timeoutId.value || mode === DebounceMode.Timeout || mode === DebounceMode.ImmediateAndTimeout) {
window.clearTimeout(timeoutId.value)
const timeout = lastCallTimestamp.value ? timeoutMs - (Date.now() - lastCallTimestamp.value) : timeoutMs

const timeout =
mode === DebounceMode.MaximumFrequency && lastCallTimestamp.value ? timeoutMs - (Date.now() - lastCallTimestamp.value) : timeoutMs

timeoutId.value = window.setTimeout(() => {
execute()
Expand All @@ -124,8 +125,9 @@ export function useDebounce(

debounced.destroy = () => {
log('useDebounce | destroy', {})
lastCallTimestamp.value = undefined
window.clearTimeout(timeoutId.value)
clear()
lastCallTimestamp.value = undefined
isDestroyed.value = true
}

Expand Down

0 comments on commit a0f928c

Please sign in to comment.