rob-revisited/ide/ui/js/hover-model.test.mjs

66 lines
2.6 KiB
JavaScript

// The hover card's life: it stays while hovered or while a read is pending, and closes only after the pointer is away for > 300 ms.
// Run: docker run --rm -v $PWD:/w -w /w node:22-alpine node --test ide/ui/js/hover-model.test.mjs
import test from 'node:test';
import assert from 'node:assert/strict';
import { LeaveGuard, scrollMoved, pickSide, LEAVE } from './hover-model.js';
/** A manual clock. */
function clock() {
let now = 0; let id = 0; const timers = new Map();
return {
set: (fn, ms) => { timers.set(++id, { at: now + ms, fn }); return id; },
clear: (i) => timers.delete(i),
advance(ms) { now += ms; for (const [k, t] of [...timers]) if (t.at <= now) { timers.delete(k); t.fn(); } },
};
}
const make = () => { const c = clock(); let closed = 0; const g = new LeaveGuard(() => closed++, { set: c.set, clear: c.clear }); return { c, g, closed: () => closed }; };
test('closes only after more than 300 ms away', () => {
const { c, g, closed } = make();
assert.ok(LEAVE > 300);
g.leave();
c.advance(300);
assert.equal(closed(), 0);
c.advance(100);
assert.equal(closed(), 1);
});
test('moving onto the card cancels the close', () => {
const { c, g, closed } = make();
g.leave(); c.advance(200); g.enter(); c.advance(2000);
assert.equal(closed(), 0);
g.leave(); c.advance(500);
assert.equal(closed(), 1);
});
test('a pending read keeps it open, and the end of the read judges again', () => {
const { c, g, closed } = make();
g.busy(1);
g.leave(); c.advance(5000);
assert.equal(closed(), 0, 'pending: stays');
g.busy(-1);
c.advance(400);
assert.equal(closed(), 1, 'pointer away and nothing pending: closes');
});
test('a read ending while the pointer never left does not close it (keyboard-opened card)', () => {
const { c, g, closed } = make();
g.busy(1); g.busy(-1); c.advance(5000);
assert.equal(closed(), 0);
});
test('a read ending while hovered never closes it; typing in a field keeps it', () => {
const { c, g, closed } = make();
g.enter(); g.busy(1); g.busy(-1); c.advance(5000);
assert.equal(closed(), 0);
g.typing = () => true; g.leave(); c.advance(5000);
assert.equal(closed(), 0);
});
test('a layout change of the editor is not a scroll; a card keeps its side while it grows', () => {
assert.equal(scrollMoved({ scrollTopChanged: false, scrollWidthChanged: true }), false);
assert.equal(scrollMoved({ scrollTopChanged: true }), true);
assert.equal(pickSide(null, 100, 300, 50), 'below');
assert.equal(pickSide(null, 400, 100, 300), 'above');
assert.equal(pickSide('above', 50, 900, 10), 'above', 'kept: a flip would move the card from under the pointer');
});