|
| 1 | +// @vitest-environment jsdom |
| 2 | +import "@testing-library/jest-dom/vitest"; |
| 3 | +import { act, cleanup, render, waitFor } from "@testing-library/react"; |
| 4 | +import * as React from "react"; |
| 5 | +import { afterEach, describe, expect, test } from "vitest"; |
| 6 | + |
| 7 | +import { createColumnHelper } from "@pretable/core"; |
| 8 | + |
| 9 | +import { PretableSurface } from "../pretable-surface"; |
| 10 | +import { measureRenderedRowHeight } from "../row-height"; |
| 11 | + |
| 12 | +/** |
| 13 | + * A row's height has a floor, and until now that floor was the literal 44 — |
| 14 | + * written in April, before the token contract existed, and never revisited. |
| 15 | + * Every shipped theme states a row height per density tier, and three of the |
| 16 | + * nine tiers are shorter than 44: Excel is 20 / 24 / 32, and both pretable and |
| 17 | + * Material are 40 at compact. Under those the floor won every time, so the |
| 18 | + * theme's stated height was unreachable and Excel's rows rendered at more than |
| 19 | + * twice their intended size. |
| 20 | + * |
| 21 | + * jsdom has no layout engine — every element measures zero — so the floor is |
| 22 | + * the ONLY thing deciding a row's height here. That makes it exactly the right |
| 23 | + * environment to pin the floor, and useless for anything about content. |
| 24 | + */ |
| 25 | + |
| 26 | +type Row = { id: number; name: string }; |
| 27 | + |
| 28 | +const column = createColumnHelper<Row>(); |
| 29 | +const columns = [column.accessor("name", { type: "text" })] as const; |
| 30 | +const rows: readonly Row[] = [ |
| 31 | + { id: 1, name: "a" }, |
| 32 | + { id: 2, name: "b" }, |
| 33 | +]; |
| 34 | + |
| 35 | +function setThemeRowHeight(value: string | null) { |
| 36 | + if (value === null) { |
| 37 | + document.documentElement.style.removeProperty("--pretable-row-height"); |
| 38 | + return; |
| 39 | + } |
| 40 | + document.documentElement.style.setProperty("--pretable-row-height", value); |
| 41 | +} |
| 42 | + |
| 43 | +function renderedRowHeights(container: HTMLElement): number[] { |
| 44 | + return [ |
| 45 | + ...container.querySelectorAll<HTMLElement>("[data-pretable-row]"), |
| 46 | + ].map((row) => Number(row.getAttribute("data-pretable-row-height"))); |
| 47 | +} |
| 48 | + |
| 49 | +afterEach(() => { |
| 50 | + cleanup(); |
| 51 | + setThemeRowHeight(null); |
| 52 | +}); |
| 53 | + |
| 54 | +describe("row height floor", () => { |
| 55 | + test("measureRenderedRowHeight floors at the height it is given", () => { |
| 56 | + const row = document.createElement("div"); |
| 57 | + const cell = document.createElement("div"); |
| 58 | + cell.setAttribute("data-pretable-cell", ""); |
| 59 | + row.appendChild(cell); |
| 60 | + document.body.appendChild(row); |
| 61 | + |
| 62 | + // Same empty row, two themes. A single hard-coded floor cannot produce |
| 63 | + // both of these numbers, which is the whole point. |
| 64 | + expect(measureRenderedRowHeight(row, 20)).toBe(20); |
| 65 | + expect(measureRenderedRowHeight(row, 56)).toBe(56); |
| 66 | + }); |
| 67 | + |
| 68 | + test("content taller than the floor still wins", () => { |
| 69 | + // The floor is a floor, not a height. Regressing it into an assignment |
| 70 | + // would clip every wrapped row, and jsdom cannot see clipping — so assert |
| 71 | + // it here, where scrollHeight is the one dimension jsdom does report. |
| 72 | + const row = document.createElement("div"); |
| 73 | + const cell = document.createElement("div"); |
| 74 | + cell.setAttribute("data-pretable-cell", ""); |
| 75 | + Object.defineProperty(cell, "scrollHeight", { value: 90 }); |
| 76 | + row.appendChild(cell); |
| 77 | + document.body.appendChild(row); |
| 78 | + |
| 79 | + expect(measureRenderedRowHeight(row, 20)).toBe(90); |
| 80 | + }); |
| 81 | + |
| 82 | + test("a surface renders its rows at the theme's row height", async () => { |
| 83 | + setThemeRowHeight("20px"); |
| 84 | + const { container } = render( |
| 85 | + <PretableSurface |
| 86 | + ariaLabel="Excel-density grid" |
| 87 | + columns={columns} |
| 88 | + getRowId={(row) => row.id} |
| 89 | + overscan={0} |
| 90 | + rows={rows} |
| 91 | + viewportHeight={200} |
| 92 | + />, |
| 93 | + ); |
| 94 | + |
| 95 | + await waitFor(() => { |
| 96 | + expect(renderedRowHeights(container).length).toBeGreaterThan(0); |
| 97 | + }); |
| 98 | + expect(renderedRowHeights(container)).toEqual( |
| 99 | + renderedRowHeights(container).map(() => 20), |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + test("with no theme, rows keep the historical 44px default", async () => { |
| 104 | + // The fallback is unchanged on purpose: an app that never imports a theme |
| 105 | + // file — and every jsdom test in this package — must render exactly as it |
| 106 | + // did before. This is the check that keeps the change surgical. |
| 107 | + const { container } = render( |
| 108 | + <PretableSurface |
| 109 | + ariaLabel="Unthemed grid" |
| 110 | + columns={columns} |
| 111 | + getRowId={(row) => row.id} |
| 112 | + overscan={0} |
| 113 | + rows={rows} |
| 114 | + viewportHeight={200} |
| 115 | + />, |
| 116 | + ); |
| 117 | + |
| 118 | + await waitFor(() => { |
| 119 | + expect(renderedRowHeights(container).length).toBeGreaterThan(0); |
| 120 | + }); |
| 121 | + expect(renderedRowHeights(container)).toEqual( |
| 122 | + renderedRowHeights(container).map(() => 44), |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + test("rows nobody has measured yet are estimated at the theme's height", async () => { |
| 127 | + // The floor governs rows the DOM has rendered. Everything below the |
| 128 | + // viewport is a guess, and the guess has its own constant — renderer-dom's |
| 129 | + // defaultRowHeight, also 44. Fixing only the floor would leave a themed |
| 130 | + // grid estimating its scroll extent at one height and measuring at |
| 131 | + // another: under Excel, a 500-row grid would claim 22000px of scroll for |
| 132 | + // 10000px of rows, and the scrollbar would visibly shrink as you scrolled. |
| 133 | + setThemeRowHeight("20px"); |
| 134 | + const many: readonly Row[] = Array.from({ length: 500 }, (_, i) => ({ |
| 135 | + id: i, |
| 136 | + name: `row ${i}`, |
| 137 | + })); |
| 138 | + const { container } = render( |
| 139 | + <PretableSurface |
| 140 | + ariaLabel="Estimated grid" |
| 141 | + columns={columns} |
| 142 | + getRowId={(row) => row.id} |
| 143 | + overscan={0} |
| 144 | + rows={many} |
| 145 | + viewportHeight={200} |
| 146 | + />, |
| 147 | + ); |
| 148 | + |
| 149 | + await waitFor(() => { |
| 150 | + expect(renderedRowHeights(container).length).toBeGreaterThan(0); |
| 151 | + }); |
| 152 | + const content = container.querySelector<HTMLElement>( |
| 153 | + "[data-pretable-scroll-content]", |
| 154 | + ); |
| 155 | + expect(content).not.toBeNull(); |
| 156 | + expect(content!.style.height).toBe(`${500 * 20}px`); |
| 157 | + }); |
| 158 | + |
| 159 | + test("a density flip re-measures rows that did not otherwise change", async () => { |
| 160 | + // The subtle one. Rows are re-measured only when their measurement key |
| 161 | + // changes, and that key is built from the row's own markup — class, style, |
| 162 | + // state attributes, cell text. A density switch changes NONE of those in |
| 163 | + // jsdom: same rows, same text, same classes. If the floor is not part of |
| 164 | + // the key, every already-measured row keeps its old height and only rows |
| 165 | + // scrolled into view afterwards pick up the new density. |
| 166 | + setThemeRowHeight("20px"); |
| 167 | + const { container } = render( |
| 168 | + <PretableSurface |
| 169 | + ariaLabel="Density-switching grid" |
| 170 | + columns={columns} |
| 171 | + getRowId={(row) => row.id} |
| 172 | + overscan={0} |
| 173 | + rows={rows} |
| 174 | + viewportHeight={200} |
| 175 | + />, |
| 176 | + ); |
| 177 | + |
| 178 | + await waitFor(() => { |
| 179 | + expect(renderedRowHeights(container)[0]).toBe(20); |
| 180 | + }); |
| 181 | + |
| 182 | + await act(async () => { |
| 183 | + setThemeRowHeight("56px"); |
| 184 | + }); |
| 185 | + |
| 186 | + await waitFor(() => { |
| 187 | + expect(renderedRowHeights(container)).toEqual( |
| 188 | + renderedRowHeights(container).map(() => 56), |
| 189 | + ); |
| 190 | + }); |
| 191 | + }); |
| 192 | +}); |
0 commit comments