import { vi } from 'vitest'; // jsdom doesn't ship IntersectionObserver, so a list that loads as you scroll // (InfiniteScrollSentinel, ListContinuation) never fires in a test. This // installs a stand-in that records each observer, so a test can play // "the reader scrolled to the bottom" with scrollToEnd(). type Observer = { cb: IntersectionObserverCallback; self: IntersectionObserver; connected: boolean }; export function installIntersectionObserverMock() { const observers: Observer[] = []; class MockIntersectionObserver { observe = vi.fn(); unobserve = vi.fn(); disconnect = vi.fn(() => { const o = observers.find((x) => x.self === (this as unknown as IntersectionObserver)); if (o) o.connected = false; }); takeRecords = vi.fn(() => []); constructor(cb: IntersectionObserverCallback) { observers.push({ cb, self: this as unknown as IntersectionObserver, connected: true }); } } vi.stubGlobal('IntersectionObserver', MockIntersectionObserver); return { /** Reports every connected observer's target as on screen. */ scrollToEnd() { for (const o of observers) { if (!o.connected) continue; o.cb([{ isIntersecting: true } as IntersectionObserverEntry], o.self); } }, /** How many observers are currently watching. */ connectedCount() { return observers.filter((o) => o.connected).length; } }; }