Added test spec for helpers/utils/debounce.js
This commit is contained in:
38
helpers/utils/__tests__/debounce.spec.js
Normal file
38
helpers/utils/__tests__/debounce.spec.js
Normal file
@@ -0,0 +1,38 @@
|
||||
import debounce from "../debounce"
|
||||
|
||||
function sleep(millis) {
|
||||
return new Promise((resolve) => setTimeout(resolve, millis))
|
||||
}
|
||||
|
||||
describe("debounce", () => {
|
||||
test("doesn't call function right after calling", () => {
|
||||
const fn = jest.fn()
|
||||
|
||||
const debFunc = debounce(fn, 100)
|
||||
debFunc()
|
||||
|
||||
expect(fn).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test("calls the function after the given timeout", async () => {
|
||||
const fn = jest.fn()
|
||||
|
||||
const debFunc = debounce(fn, 100)
|
||||
debFunc()
|
||||
|
||||
await sleep(1000)
|
||||
|
||||
expect(fn).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test("calls the function only one time within the timeframe", async () => {
|
||||
const fn = jest.fn()
|
||||
|
||||
const debFunc = debounce(fn, 1000)
|
||||
|
||||
for (let i = 0; i < 100; i++) debFunc()
|
||||
await sleep(2000)
|
||||
|
||||
expect(fn).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user