mirror of
https://github.com/block/buzz.git
synced 2026-11-03 07:51:22 +00:00
Signed-off-by: Wes <wesbillman@users.noreply.github.com> Co-authored-by: Brain <21994759fc7a6fa6b965551d35cfd7897d262f2495467f2d78694ddcfa6a5c7e@sprout-oss.stage.blox.sqprod.co>
60 lines
1.5 KiB
JavaScript
60 lines
1.5 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import { refreshChannelsWhenIdle } from "./refreshChannelsWhenIdle.ts";
|
|
|
|
function spy() {
|
|
const fn = () => {
|
|
fn.calls++;
|
|
};
|
|
fn.calls = 0;
|
|
return fn;
|
|
}
|
|
|
|
test("invalidates when no fetch is in flight", () => {
|
|
const invalidate = spy();
|
|
const reArm = spy();
|
|
refreshChannelsWhenIdle({ isFetching: () => 0, invalidate, reArm });
|
|
assert.equal(invalidate.calls, 1);
|
|
assert.equal(reArm.calls, 0);
|
|
});
|
|
|
|
test("re-arms instead of invalidating while a fetch is in flight", () => {
|
|
const invalidate = spy();
|
|
const reArm = spy();
|
|
refreshChannelsWhenIdle({ isFetching: () => 1, invalidate, reArm });
|
|
assert.equal(
|
|
invalidate.calls,
|
|
0,
|
|
"must not invalidate mid-flight (would drop the dirty signal)",
|
|
);
|
|
assert.equal(reArm.calls, 1);
|
|
});
|
|
|
|
test("re-arm then idle eventually invalidates exactly once", () => {
|
|
const invalidate = spy();
|
|
const reArm = spy();
|
|
// First pass: fetch in flight -> re-arm.
|
|
let fetching = 1;
|
|
const deps = {
|
|
isFetching: () => fetching,
|
|
invalidate,
|
|
// Simulate the debounce re-firing the same action.
|
|
reArm: () => {
|
|
reArm.calls++;
|
|
refreshChannelsWhenIdle(deps);
|
|
},
|
|
};
|
|
// Once re-armed, the fetch has finished.
|
|
reArm.calls = 0;
|
|
const reArmOnce = deps.reArm;
|
|
deps.reArm = () => {
|
|
fetching = 0;
|
|
reArmOnce();
|
|
};
|
|
|
|
refreshChannelsWhenIdle(deps);
|
|
assert.equal(reArm.calls, 1, "re-armed once while fetching");
|
|
assert.equal(invalidate.calls, 1, "invalidated once after going idle");
|
|
});
|