import { describe, expect, it } from "vitest"; import { readSources } from "./rawSources.testkit"; // Every infinite list's read-ahead waits while the list fetches ANYTHING and runs again when the fetch // settles. A read-ahead that only checks `isFetchingNextPage` calls during a background // refetch, joins it through NEXT_PAGE and loads no page: the grid stalls at its loaded end until the // reader scrolls the range. The jsdom virtualizer measures nothing, so the wiring is pinned in source. const files = readSources(__dirname, ["../**/*.tsx", "!../**/*.test.tsx", "!../design-lab/**"]); const source = (name: string) => { const hit = Object.entries(files).find(([path]) => path.endsWith(`/${name}`)); if (!hit) throw new Error(`no ${name}`); return hit[1] as string; }; describe("an infinite list's read-ahead", () => { it.each(["ui/VirtualGrid.tsx", "PlexBrowse.tsx"])( "%s gates on mayReadAhead and re-runs on isFetching", (file) => { const src = source(file); expect(src).toMatch(/mayReadAhead\(hasNextPage, isFetching\)/); expect(src).toMatch(/\[[^\]]*\bisFetching\b[^\]]*fetchNextPage\]\);/); expect(src).not.toMatch(/hasNextPage && !isFetchingNextPage/); }, ); it("the feed hands the grid the query's isFetching", () => { expect(source("Feed.tsx")).toMatch(/isFetching=\{query\.isFetching\}/); }); // Retry is off, so a failed page settles at once; a read-ahead that re-runs on the settle would // request it again once a round-trip while the end is in view. it("stands down at a failed next page", () => { expect(source("Feed.tsx")).toMatch( /hasNextPage=\{!!hasNextPage && !query\.isFetchNextPageError\}/, ); expect(source("PlexBrowse.tsx")).toMatch( /const hasNextPage = browseQ\.hasNextPage && !isFetchNextPageError;/, ); }); }); // A click joins an in-flight next page but cancels a background refetch (nextPageOnClick): through // NEXT_PAGE it would join the refetch and load nothing. NEXT_PAGE is only for the read-aheads, so a // click site that goes back to it fails here (the YouTube "Load more" was once missed). describe("a click that loads the next page", () => { const READ_AHEADS = new Set(["components/ui/VirtualGrid.tsx", "modules/plex/PlexBrowse.tsx"]); it("uses nextPageOnClick; NEXT_PAGE only in the read-aheads", () => { const users = Object.entries(files) .filter(([, src]) => /fetchNextPage\(NEXT_PAGE\)/.test(src as string)) .map(([path]) => path.replace(/^\.\.\//, "")); expect(users.sort()).toEqual([...READ_AHEADS].sort()); for (const [file, sites] of [ ["AdminLogs.tsx", 2], ["Feed.tsx", 2], ["PlexBrowse.tsx", 1], ] as const) expect(source(file).match(/fetchNextPage\(nextPageOnClick\(/g)?.length, file).toBe(sites); }); });