ui / src / components / virtual-log / virtual-log-scroll.test.tsxmain at d68d7e1
// @vitest-environment jsdom
// Unit checks for VirtualLog scroll stability: pixel-based overscan over runs of 4 px rows and
// surviving-row fallback anchoring when a prepend re-keys the oldest row at scrollTop 0.
import { act, type ReactElement } from "react";
import { createRoot } from "react-dom/client";
import { beforeAll, expect, test } from "vitest";
import { VirtualLog, type VirtualLogRowInfo } from "./virtual-log.tsx";
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
beforeAll(() => {
if (typeof globalThis.ResizeObserver === "undefined") {
globalThis.ResizeObserver = class {
observe() {}
unobserve() {}
disconnect() {}
} as unknown as typeof ResizeObserver;
}
});
type TestRow = { id: string; offset?: number; estimate: number };
const ROW_INFO: VirtualLogRowInfo<TestRow> = {
key: (row) => row.id,
estimateSize: (row) => row.estimate,
offset: (row) => row.offset,
};
function mount(element: ReactElement, rowHeight = 0) {
const ownClientHeight = Object.getOwnPropertyDescriptor(HTMLElement.prototype, "clientHeight");
const ownOffsetHeight = Object.getOwnPropertyDescriptor(HTMLElement.prototype, "offsetHeight");
Object.defineProperty(HTMLElement.prototype, "clientHeight", {
configurable: true,
get: () => 600,
});
Object.defineProperty(HTMLElement.prototype, "offsetHeight", {
configurable: true,
get(this: HTMLElement) {
return this.hasAttribute("data-index") ? rowHeight : 0;
},
});
const container = document.createElement("div");
document.body.appendChild(container);
const root = createRoot(container);
act(() => {
root.render(element);
});
return {
container,
rerender(next: ReactElement) {
act(() => {
root.render(next);
});
},
[Symbol.dispose]() {
act(() => root.unmount());
container.remove();
document.body.replaceChildren();
if (ownClientHeight)
Object.defineProperty(HTMLElement.prototype, "clientHeight", ownClientHeight);
else delete (HTMLElement.prototype as unknown as Record<string, unknown>).clientHeight;
if (ownOffsetHeight)
Object.defineProperty(HTMLElement.prototype, "offsetHeight", ownOffsetHeight);
else delete (HTMLElement.prototype as unknown as Record<string, unknown>).offsetHeight;
},
};
}
test("pixel overscan mounts more than 16 tiny 4 px rows above the viewport", () => {
const rows: TestRow[] = Array.from({ length: 200 }, (_, i) => ({
id: `r-${i}`,
offset: i + 1,
estimate: 4,
}));
using view = mount(
<VirtualLog
rows={rows}
rowInfo={ROW_INFO}
renderRow={(row) => <div data-row={row.id}>{row.id}</div>}
label="Tiny rows"
/>,
);
const scroller = view.container.querySelector<HTMLDivElement>('[data-slot="virtual-log"]')!;
// Scroll away from end into the middle of the 4px rows.
act(() => {
scroller.dispatchEvent(new WheelEvent("wheel", { deltaY: -120, bubbles: true }));
scroller.scrollTop = 400;
scroller.dispatchEvent(new Event("scroll"));
});
const mounted = [...scroller.querySelectorAll<HTMLElement>("[data-index]")].map((el) =>
Number(el.dataset.index),
);
const minIndex = Math.min(...mounted);
// startIndex is around 100 (400px / 4px). Old overscan: 16 would only reach index 84;
// pixel overscan (1600px, capped at 120 rows) mounts all the way to index 0.
expect(minIndex).toBeLessThan(50);
});
test("prepend at scrollTop 0 advances scrollTop even when old row 0 is re-keyed", () => {
const initial: TestRow[] = Array.from({ length: 20 }, (_, i) => ({
id: `row-${100 + i}`,
offset: 100 + i,
estimate: 60,
}));
using view = mount(
<VirtualLog
rows={initial}
rowInfo={ROW_INFO}
renderRow={(row) => <div data-row={row.id}>{row.id}</div>}
label="Prepend test"
/>,
60,
);
const scroller = view.container.querySelector<HTMLDivElement>('[data-slot="virtual-log"]')!;
Object.defineProperty(scroller, "scrollHeight", {
configurable: true,
get: () => 2_400,
});
// Leave the end and stand at scrollTop 0.
act(() => {
scroller.dispatchEvent(new WheelEvent("wheel", { deltaY: -120, bubbles: true }));
scroller.scrollTop = 0;
scroller.dispatchEvent(new Event("scroll"));
});
expect(scroller.scrollTop).toBe(0);
// Prepend 5 rows (5 * 60 = 300 px) AND re-key old row 0 (row-100 -> merged-95-100), so
// virtual-core's single-key anchor misses while row-101..row-119 survive.
const prepended: TestRow[] = [
...Array.from({ length: 5 }, (_, i) => ({
id: `row-${95 + i}`,
offset: 95 + i,
estimate: 60,
})),
{ ...initial[0]!, id: "merged-95-100" },
...initial.slice(1),
];
view.rerender(
<VirtualLog
rows={prepended}
rowInfo={ROW_INFO}
renderRow={(row) => <div data-row={row.id}>{row.id}</div>}
label="Prepend test"
/>,
);
expect(scroller.scrollTop).toBe(300);
});
test("same-day prepend at scrollTop 0 advances scrollTop even when a floating day header stays at index 0", () => {
const initial: TestRow[] = [
{ id: "day:Thu", estimate: 32 },
...Array.from({ length: 30 }, (_, i) => ({
id: `event:${100 + i}`,
offset: 100 + i,
estimate: 26,
})),
];
using view = mount(
<VirtualLog
rows={initial}
rowInfo={ROW_INFO}
renderRow={(row) => <div data-row={row.id}>{row.id}</div>}
label="Day-header prepend test"
/>,
26,
);
const scroller = view.container.querySelector<HTMLDivElement>('[data-slot="virtual-log"]')!;
Object.defineProperty(scroller, "scrollHeight", {
configurable: true,
get: () => 4_000,
});
act(() => {
scroller.dispatchEvent(new WheelEvent("wheel", { deltaY: -120, bubbles: true }));
scroller.scrollTop = 0;
scroller.dispatchEvent(new Event("scroll"));
});
expect(scroller.scrollTop).toBe(0);
// Prepend 50 same-day events under the same `"day:Thu"` header at index 0 (50 * 26 = 1,300 px).
const prepended: TestRow[] = [
{ id: "day:Thu", estimate: 32 },
...Array.from({ length: 50 }, (_, i) => ({
id: `event:${50 + i}`,
offset: 50 + i,
estimate: 26,
})),
...initial.slice(1),
];
view.rerender(
<VirtualLog
rows={prepended}
rowInfo={ROW_INFO}
renderRow={(row) => <div data-row={row.id}>{row.id}</div>}
label="Day-header prepend test"
/>,
);
expect(scroller.scrollTop).toBe(1_300);
});
test("a chat's short log stands on the list's end, and another view's on its start", () => {
const rows: TestRow[] = [{ id: "r-0", offset: 1, estimate: 40 }];
const column = (shortLogAt?: "end") => {
using view = mount(
<VirtualLog
rows={rows}
rowInfo={ROW_INFO}
renderRow={(row) => <div>{row.id}</div>}
label="Short"
shortLogAt={shortLogAt}
/>,
);
return view.container.querySelector<HTMLElement>('[role="log"]')!.className;
};
expect(column("end")).toContain("justify-end");
expect(column()).not.toContain("justify-end");
});