Skip to content

Commit 739ad1c

Browse files
committed
feat: tests for the debounce util function
1 parent fc4f264 commit 739ad1c

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

vis/test/utils/debounce.test.ts

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import { describe, it, expect, vi } from "vitest";
2+
import debounce from "../../js/utils/debounce";
3+
4+
describe("Debounce function", () => {
5+
const getDelayTimeouts = () => {
6+
const BASE_DELAY = 100;
7+
const DELAY_WITH_TIMEOUT = BASE_DELAY + 50;
8+
const TIMEOUT_BELOW_BASE_DELAY = BASE_DELAY - 50;
9+
10+
return { BASE_DELAY, DELAY_WITH_TIMEOUT, TIMEOUT_BELOW_BASE_DELAY };
11+
};
12+
13+
it("Should call a callback after delay timeout", async () => {
14+
// Getting delay values
15+
const { BASE_DELAY, DELAY_WITH_TIMEOUT } = getDelayTimeouts();
16+
17+
// Mocking callback function to check
18+
// how many times it would be called
19+
const mockFunc = vi.fn();
20+
21+
// Create debounced function
22+
const debouncedFunc = debounce(mockFunc, BASE_DELAY, false);
23+
24+
// Call function two times (without delay)
25+
debouncedFunc();
26+
debouncedFunc();
27+
28+
// Check that function was not called immediately
29+
expect(mockFunc).not.toHaveBeenCalled();
30+
31+
// Waiting more then delay was configured
32+
await new Promise((resolve) => setTimeout(resolve, DELAY_WITH_TIMEOUT));
33+
34+
// Check that mocked callback function was called once
35+
expect(mockFunc).toHaveBeenCalledTimes(1);
36+
});
37+
38+
it("Callback function must be called immediately if immediate is true", async () => {
39+
// Getting delay values
40+
const { BASE_DELAY, DELAY_WITH_TIMEOUT } = getDelayTimeouts();
41+
42+
// Mocking callback function to check
43+
// how many times it would be called
44+
const mockFunc = vi.fn();
45+
46+
// Create debounced function
47+
const debouncedFunc = debounce(mockFunc, BASE_DELAY, true);
48+
49+
// Call function
50+
debouncedFunc();
51+
52+
// Check that function was called immediately
53+
expect(mockFunc).toHaveBeenCalledTimes(1);
54+
55+
// Waiting more then delay was configured
56+
await new Promise((resolve) => setTimeout(resolve, DELAY_WITH_TIMEOUT));
57+
58+
// Check that mocked callback function was called once
59+
expect(mockFunc).toHaveBeenCalledTimes(1);
60+
});
61+
62+
it("Callback function should be called during a delay time", async () => {
63+
// Getting delay values
64+
const { BASE_DELAY, TIMEOUT_BELOW_BASE_DELAY } = getDelayTimeouts();
65+
66+
// Mocking callback function to check
67+
// how many times it would be called
68+
const mockFunc = vi.fn();
69+
70+
// Create debounced function
71+
const debouncedFunc = debounce(mockFunc, BASE_DELAY, false);
72+
73+
// Call function two times (without delay)
74+
debouncedFunc();
75+
debouncedFunc();
76+
77+
// Waiting less the delay was configured
78+
await new Promise((resolve) =>
79+
setTimeout(resolve, TIMEOUT_BELOW_BASE_DELAY)
80+
);
81+
82+
// Check that mocked callback function was NOT called
83+
expect(mockFunc).not.toHaveBeenCalled();
84+
85+
// Waiting until the end of delay
86+
await new Promise((resolve) => setTimeout(resolve, BASE_DELAY));
87+
88+
// Check that mocked callback function was called once
89+
expect(mockFunc).toHaveBeenCalledTimes(1);
90+
});
91+
92+
it("Maintaining the context of this", async () => {
93+
// Getting delay values
94+
const { BASE_DELAY, DELAY_WITH_TIMEOUT } = getDelayTimeouts();
95+
96+
// Defining context value
97+
const VALUE = 5;
98+
99+
// Mock object to test that this is working correctly
100+
const MOCK_OBJECT_WITH_CONTEXT = { value: VALUE };
101+
102+
// Mocking callback function to check
103+
// how many times it would be called
104+
const mockFunc = vi.fn(function () {
105+
// @ts-ignore
106+
return this.value;
107+
});
108+
109+
// Create debounced function
110+
const debouncedFunc = debounce(
111+
mockFunc.bind(MOCK_OBJECT_WITH_CONTEXT),
112+
BASE_DELAY,
113+
false
114+
);
115+
116+
// Calling function once
117+
debouncedFunc();
118+
119+
// Function must not been called
120+
expect(mockFunc).not.toHaveBeenCalled();
121+
122+
// Waiting until the end of delay
123+
await new Promise((resolve) => setTimeout(resolve, DELAY_WITH_TIMEOUT));
124+
125+
// Check that callback function was called once
126+
expect(mockFunc).toHaveBeenCalledTimes(1);
127+
128+
// Check that callback function was called with correct context
129+
expect(mockFunc).toHaveReturnedWith(VALUE);
130+
});
131+
});

0 commit comments

Comments
 (0)