import { createSignal } from "solid-js";
import { afterEach, describe, expect, it } from "vitest";
import { cleanup, fireEvent, render, waitFor } from "@solidjs/testing-library";
import { VirtualList } from "./VirtualList";
afterEach(() => cleanup()); // vitest globals:false disables autoloading cleanup
describe("VirtualList", () => {
const items = Array.from({ length: 1000 }, (_, i) => `item-${i}`);
const mount = (props: Partial<{ buffer: number }> = {}) =>
render(() => (
{(item) => {item}
}
));
it("renders only the visible window (+buffer)", () => {
mount({ buffer: 20 });
// window = ceil((0+100+20)/20) = 6 rows out of 1000
const rows = document.querySelectorAll(".row");
expect(rows.length).toBeGreaterThan(4);
expect(rows.length).toBeLessThanOrEqual(7);
expect(rows[0].textContent).toBe("item-0");
});
it("sizes the sizer to the full list", () => {
mount();
const sizer = document.querySelector(".vlist-sizer") as HTMLElement;
// 1000 rows × 20px. This empty div is what gives the scroller a real
// scrollHeight — native scrollbar, wheel, PgDn and momentum all work off it.
// The rows live in an absolutely positioned window, so they add no height:
// the sizer is the only thing setting it, which is why the panes around it
// must be height-bounded (.lib-panes) or it inflates the whole document.
expect(sizer.style.height).toBe("20000px");
const win = document.querySelector(".vlist-window")!;
expect(win.getAttribute("style")).toContain("translateY(0px)");
});
it("re-windows on scroll", async () => {
mount();
const container = document.querySelector(".vlist-test")!; // the scroller itself
Object.defineProperty(container, "scrollTop", { value: 400, configurable: true });
fireEvent.scroll(container);
await waitFor(() => {
const rows = document.querySelectorAll(".row");
expect(rows[0].textContent).toBe("item-20");
});
const win = document.querySelector(".vlist-window") as HTMLElement;
expect(win.style.transform).toBe("translateY(400px)");
});
it("renders ahead of the viewport, not just inside it", () => {
// Fast scrolling outruns the scroll handler: the compositor moves the view
// before JS can create the rows entering it. The buffer is the lead time
// that keeps rows already in the DOM when they come into view — without it,
// text pops in a frame behind the row backgrounds. (This is also why the
// rows carry no content-visibility: applying it here would skip rendering
// precisely the rows in this lead zone.)
mount({ buffer: 400 });
const rows = [...document.querySelectorAll(".row")];
// viewport 100px / 20px rows = 5 visible; the buffer must reach far past them
expect(rows[0].textContent).toBe("item-0");
expect(Number(rows.at(-1)!.textContent!.replace("item-", ""))).toBeGreaterThanOrEqual(20);
});
it("keeps the same DOM node for a row that stays on screen while scrolling", async () => {
// keys by item identity. If the virtualizer hands it a fresh
// [item, index] tuple for a row that is still visible, that row's DOM and
// component state are destroyed and rebuilt on every scroll frame — the
// UNSTABLE_LIST_IDENTITY diagnostic. Scroll must move the window, not
// resurrect rows that never left it.
mount();
const container = document.querySelector(".vlist-test")!;
const before = [...document.querySelectorAll(".row")].find(
(r) => r.textContent === "item-4",
)!;
expect(before).toBeTruthy();
Object.defineProperty(container, "scrollTop", { value: 40, configurable: true });
fireEvent.scroll(container);
await waitFor(() => expect(document.querySelector(".row")!.textContent).toBe("item-2"));
const after = [...document.querySelectorAll(".row")].find(
(r) => r.textContent === "item-4",
)!;
expect(after).toBe(before); // same node, not an equivalent copy
});
it("handles an empty list", () => {
render(() => (
{(item) => {item}
}
));
expect(document.querySelectorAll(".row")).toHaveLength(0);
const sizer = document.querySelector(".vlist-sizer") as HTMLElement;
expect(sizer.style.height).toBe("0px");
});
});
describe("VirtualList — where a reveal lands", () => {
const many = Array.from({ length: 1000 }, (_, i) => `item-${i}`);
const ITEM = 20;
const VIEW = 100; // five rows on screen
/**
* A list whose reveal the test can aim, on a scroller that remembers writes.
*
* jsdom's `scrollTop` is a placeholder that stores nothing, which is why the
* tests above fake it with a fixed value. A reveal needs the opposite: the
* component writes, and the test reads back what the window ended up at —
* including whether the write was a clamp rather than the number it asked for.
*/
function harness() {
const [target, setTarget] = createSignal<{
row: number;
n: number;
offset?: number;
keepInView?: boolean;
center?: boolean;
} | null>(null);
let nonce = 0;
let stored = 0;
render(() => (
{(item) => {item}
}
));
const scroller = document.querySelector(".vlist-test")!;
Object.defineProperty(scroller, "scrollTop", {
configurable: true,
get: () => stored,
set: (v: number) => {
stored = v;
},
});
const aim = (row: number, mode: "center" | "keepInView" | "plain", offset?: number) =>
setTarget({
row,
n: ++nonce,
offset,
...(mode === "center"
? { center: true }
: mode === "keepInView"
? { keepInView: true }
: {}),
});
return {
aim,
at: () => stored,
topRow: () => document.querySelector(".row")!.textContent,
};
}
it("puts a row that left the screen in the middle of the window", async () => {
const h = harness();
h.aim(100, "center");
// row top (2000) less half a window of room ((100 - 20) / 2) — as much list
// above it as below, which is the whole difference from an edge reveal.
await waitFor(() => expect(h.at()).toBe(100 * ITEM - (VIEW - ITEM) / 2));
// and the window follows the truth rather than the request
await waitFor(() => expect(h.topRow()).toBe("item-98"));
});
it("leaves a row that is still on screen exactly where it is", async () => {
// The half that stops this being a list that never stops moving: `n` answered
// by a jump is fine, `n` answered by a slide is not.
const h = harness();
h.aim(3, "center");
await new Promise((r) => setTimeout(r, 20));
expect(h.at()).toBe(0);
});
it("centres a row above the screen too, coming back up", async () => {
const h = harness();
h.aim(100, "center"); // leave the top of the list first
await waitFor(() => expect(h.at()).toBe(100 * ITEM - (VIEW - ITEM) / 2));
h.aim(2, "center");
await waitFor(() => expect(h.at()).toBe(2 * ITEM - (VIEW - ITEM) / 2));
// not below zero: the first rows cannot be pulled up to the middle
expect(h.at()).toBe(0);
});
it("never centres past the end of the list", async () => {
const h = harness();
h.aim(999, "center");
// asked for 19,940; the last screenful starts at total less the viewport
await waitFor(() => expect(h.at()).toBe(1000 * ITEM - VIEW));
});
it("still aligns a plain reveal to the top, offset and all", async () => {
// The centring is a new mode, not a replacement: `r`/`R`/`Alt+r` still ask for
// a row with a little list above it, and a grid cover still asks for its album.
const h = harness();
h.aim(50, "plain", -4);
await waitFor(() => expect(h.at()).toBe((50 - 4) * ITEM));
});
it("still refuses to move for keepInView, and still moves when it has to", async () => {
const h = harness();
h.aim(4, "keepInView"); // on screen: nothing happens
await new Promise((r) => setTimeout(r, 20));
expect(h.at()).toBe(0);
h.aim(60, "keepInView"); // off screen: to the top, not the middle
await waitFor(() => expect(h.at()).toBe(60 * ITEM));
});
});