49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
|
|
import { describe, expect, it, vi } from "vitest";
|
||
|
|
|
||
|
|
import { createInboundDebouncer } from "./inbound-debounce.js";
|
||
|
|
|
||
|
|
describe("createInboundDebouncer", () => {
|
||
|
|
it("debounces and combines items", async () => {
|
||
|
|
vi.useFakeTimers();
|
||
|
|
const calls: Array<string[]> = [];
|
||
|
|
|
||
|
|
const debouncer = createInboundDebouncer<{ key: string; id: string }>({
|
||
|
|
debounceMs: 10,
|
||
|
|
buildKey: (item) => item.key,
|
||
|
|
onFlush: async (items) => {
|
||
|
|
calls.push(items.map((entry) => entry.id));
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
await debouncer.enqueue({ key: "a", id: "1" });
|
||
|
|
await debouncer.enqueue({ key: "a", id: "2" });
|
||
|
|
|
||
|
|
expect(calls).toEqual([]);
|
||
|
|
await vi.advanceTimersByTimeAsync(10);
|
||
|
|
expect(calls).toEqual([["1", "2"]]);
|
||
|
|
|
||
|
|
vi.useRealTimers();
|
||
|
|
});
|
||
|
|
|
||
|
|
it("flushes buffered items before non-debounced item", async () => {
|
||
|
|
vi.useFakeTimers();
|
||
|
|
const calls: Array<string[]> = [];
|
||
|
|
|
||
|
|
const debouncer = createInboundDebouncer<{ key: string; id: string; debounce: boolean }>({
|
||
|
|
debounceMs: 50,
|
||
|
|
buildKey: (item) => item.key,
|
||
|
|
shouldDebounce: (item) => item.debounce,
|
||
|
|
onFlush: async (items) => {
|
||
|
|
calls.push(items.map((entry) => entry.id));
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
await debouncer.enqueue({ key: "a", id: "1", debounce: true });
|
||
|
|
await debouncer.enqueue({ key: "a", id: "2", debounce: false });
|
||
|
|
|
||
|
|
expect(calls).toEqual([["1"], ["2"]]);
|
||
|
|
|
||
|
|
vi.useRealTimers();
|
||
|
|
});
|
||
|
|
});
|