refactor: executor protocol, fallback, debounce

This commit is contained in:
2026-09-26 02:20:34 +05:00
parent 5f77c097e2
commit 0933f1cdea
14 changed files with 739 additions and 301 deletions
+38
View File
@@ -0,0 +1,38 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { debounce } from "./debounce";
afterEach(() => {
vi.useRealTimers();
});
describe("debounce", () => {
it("coalesces calls until the deadline", () => {
vi.useFakeTimers();
const fn = vi.fn();
const debounced = debounce(fn, 200);
debounced();
vi.advanceTimersByTime(199);
debounced();
vi.advanceTimersByTime(199);
expect(fn).not.toHaveBeenCalled();
vi.advanceTimersByTime(1);
expect(fn).toHaveBeenCalledTimes(1);
});
it("cancels a pending call and can be scheduled again", () => {
vi.useFakeTimers();
const fn = vi.fn();
const debounced = debounce(fn, 200);
debounced();
debounced.cancel();
vi.advanceTimersByTime(200);
expect(fn).not.toHaveBeenCalled();
debounced();
vi.advanceTimersByTime(200);
expect(fn).toHaveBeenCalledTimes(1);
});
});