Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

Added test spec for helpers/utils/debounce.js

Andrew Bastin4b73ee3d41a0
diff --git a/helpers/utils/__tests__/debounce.spec.js b/helpers/utils/__tests__/debounce.spec.js
new file mode 100644
index 000000000..30ca626fc
--- /dev/null
+++ b/helpers/utils/__tests__/debounce.spec.js
@@ -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)
+ })
+})