Skip to content

Commit

Permalink
Updated debounce test spec to use jest timers
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBastin committed Aug 26, 2020
1 parent 3cd2a2a commit 4b87684
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions helpers/utils/__tests__/debounce.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
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()
Expand All @@ -17,12 +13,15 @@ describe("debounce", () => {
test("calls the function after the given timeout", async () => {
const fn = jest.fn()

jest.useFakeTimers()

const debFunc = debounce(fn, 100)
debFunc()

await sleep(1000)
jest.runAllTimers()

expect(fn).toHaveBeenCalled()
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 100)
})

test("calls the function only one time within the timeframe", async () => {
Expand All @@ -31,7 +30,8 @@ describe("debounce", () => {
const debFunc = debounce(fn, 1000)

for (let i = 0; i < 100; i++) debFunc()
await sleep(2000)

jest.runAllTimers()

expect(fn).toHaveBeenCalledTimes(1)
})
Expand Down

0 comments on commit 4b87684

Please sign in to comment.