Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,4 @@ This package provides a number of utility functions.
| `removePhoneNumberCountryPrefix(phoneNumber, [, prefix])` | Removes a country prefix from a phone number based on the station config `country_code`. |
| `onLocalStorageChange(key, callback)` | Calls `callback` when the value of `key` in `localStorage` changes. |
| `decodeRadioToken(token)` | Decodes a JWT radio token. |
| `debounce(ms, fn)` | Returns a debounced version of `fn` that delays invocation until `ms` after the last call. |
60 changes: 60 additions & 0 deletions src/__test__/utils/debounce.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, expect, test, jest } from '@jest/globals'
import debounce from '../../utils/debounce.js'

describe('debounce', () => {
beforeEach(() => {

Check failure on line 5 in src/__test__/utils/debounce.test.js

View workflow job for this annotation

GitHub Actions / lint-format

'beforeEach' is not defined
jest.useFakeTimers()
})

afterEach(() => {

Check failure on line 9 in src/__test__/utils/debounce.test.js

View workflow job for this annotation

GitHub Actions / lint-format

'afterEach' is not defined
jest.useRealTimers()
})

test('it calls the function after the delay', () => {
const fn = jest.fn()
const debounced = debounce(100, fn)

debounced()
expect(fn).not.toHaveBeenCalled()

jest.advanceTimersByTime(100)
expect(fn).toHaveBeenCalledTimes(1)
})

test('it resets the timer on subsequent calls', () => {
const fn = jest.fn()
const debounced = debounce(100, fn)

debounced()
jest.advanceTimersByTime(50)
debounced()
jest.advanceTimersByTime(50)

expect(fn).not.toHaveBeenCalled()

jest.advanceTimersByTime(50)
expect(fn).toHaveBeenCalledTimes(1)
})

test('it passes arguments to the function', () => {
const fn = jest.fn()
const debounced = debounce(100, fn)

debounced('hello', 42)
jest.advanceTimersByTime(100)

expect(fn).toHaveBeenCalledWith('hello', 42)
})

test('it uses the arguments from the last call', () => {
const fn = jest.fn()
const debounced = debounce(100, fn)

debounced('first')
debounced('second')
jest.advanceTimersByTime(100)

expect(fn).toHaveBeenCalledTimes(1)
expect(fn).toHaveBeenCalledWith('second')
})
})
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export { cdnImageUrl, cdnUrl } from './utils/cdnUrl.js'
export { removePhoneNumberCountryPrefix } from './utils/phoneNumber.js'
export { setupAirbrake, gtmFilter } from './utils/setupAirbrake.js'
export { onLocalStorageChange } from './utils/onLocalStorageChange.js'
export { default as debounce } from './utils/debounce.js'
7 changes: 7 additions & 0 deletions src/utils/debounce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function debounce(ms, fn) {
let timer
return (...args) => {
clearTimeout(timer)
timer = setTimeout(() => fn(...args), ms)
}
}
Loading