From 4b876846112851db83f1871caad21aa993f674bc Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Wed, 26 Aug 2020 12:14:47 -0400 Subject: [PATCH] Updated debounce test spec to use jest timers --- helpers/utils/__tests__/debounce.spec.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/helpers/utils/__tests__/debounce.spec.js b/helpers/utils/__tests__/debounce.spec.js index 30ca626fc..16701de31 100644 --- a/helpers/utils/__tests__/debounce.spec.js +++ b/helpers/utils/__tests__/debounce.spec.js @@ -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() @@ -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 () => { @@ -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) })