|
| 1 | +import { expect, test } from '@playwright/test' |
| 2 | + |
| 3 | +// Records the data-index and viewport-relative top of a rendered item that |
| 4 | +// sits clearly inside the viewport (so it is already measured) with rows above |
| 5 | +// it inside the viewport and room to stay visible after a moderate upward |
| 6 | +// scroll moves it down. The window is wider than a row (rows are 90px) so it |
| 7 | +// always contains at least one candidate; we pick the one nearest the target. |
| 8 | +const pickAnchor = () => { |
| 9 | + const container = document.querySelector('#scroll-container') |
| 10 | + if (!container) throw new Error('Container not found') |
| 11 | + const containerTop = container.getBoundingClientRect().top |
| 12 | + |
| 13 | + const TARGET = 100 |
| 14 | + const candidates = Array.from(container.querySelectorAll('[data-index]')) |
| 15 | + .map((el) => ({ |
| 16 | + index: Number(el.getAttribute('data-index')), |
| 17 | + top: el.getBoundingClientRect().top - containerTop, |
| 18 | + })) |
| 19 | + // Away from both edges: rows above it, and room to move down ~200px. |
| 20 | + .filter((c) => c.top > 30 && c.top < 170) |
| 21 | + .sort((a, b) => Math.abs(a.top - TARGET) - Math.abs(b.top - TARGET)) |
| 22 | + |
| 23 | + if (candidates.length === 0) throw new Error('No anchor candidate found') |
| 24 | + return candidates[0] |
| 25 | +} |
| 26 | + |
| 27 | +const topOfIndex = (index: number) => { |
| 28 | + const container = document.querySelector('#scroll-container') |
| 29 | + if (!container) throw new Error('Container not found') |
| 30 | + const el = container.querySelector(`[data-index="${index}"]`) |
| 31 | + if (!el) return null |
| 32 | + return el.getBoundingClientRect().top - container.getBoundingClientRect().top |
| 33 | +} |
| 34 | + |
| 35 | +// Largest gap between consecutive rendered rows. Before measurement, rows are |
| 36 | +// positioned from the 50px estimate while their real height is 90px, so they |
| 37 | +// overlap (gap ~40px); once measured, positions are contiguous (gap ~0). This |
| 38 | +// is a direct, pollable signal that measurement has settled. Returns a large |
| 39 | +// finite sentinel (not Infinity — page.evaluate serializes that to null) when |
| 40 | +// the DOM isn't ready yet so the poll keeps waiting. |
| 41 | +const NOT_READY = 1e9 |
| 42 | +const maxRowGap = () => { |
| 43 | + const container = document.querySelector('#scroll-container') |
| 44 | + if (!container) return NOT_READY |
| 45 | + const containerTop = container.getBoundingClientRect().top |
| 46 | + const rows = Array.from(container.querySelectorAll('[data-index]')) |
| 47 | + .map((el) => { |
| 48 | + const rect = el.getBoundingClientRect() |
| 49 | + return { |
| 50 | + top: rect.top - containerTop, |
| 51 | + bottom: rect.bottom - containerTop, |
| 52 | + } |
| 53 | + }) |
| 54 | + .sort((a, b) => a.top - b.top) |
| 55 | + if (rows.length < 2) return NOT_READY |
| 56 | + let gap = 0 |
| 57 | + for (let i = 1; i < rows.length; i++) { |
| 58 | + gap = Math.max(gap, Math.abs(rows[i].top - rows[i - 1].bottom)) |
| 59 | + } |
| 60 | + return gap |
| 61 | +} |
| 62 | + |
| 63 | +// Regression test for the "items jump while scrolling up" bug. |
| 64 | +// |
| 65 | +// When scrolling backward into never-measured items, their estimate→actual |
| 66 | +// size delta lives above the viewport, so scrollTop MUST be compensated or the |
| 67 | +// anchored content visibly jumps. The fix adjusts on first measurement even |
| 68 | +// during backward scroll (it only skips compensation for RE-measurements while |
| 69 | +// scrolling up). This test anchors on a visible item and asserts it moves down |
| 70 | +// by exactly the scroll delta — no extra jump from uncompensated measurement. |
| 71 | +test('anchored item stays stable when scrolling up into unmeasured items', async ({ |
| 72 | + page, |
| 73 | +}) => { |
| 74 | + await page.goto('/scroll-anchor/?initialOffset=10000') |
| 75 | + |
| 76 | + // Wait for the initial measurement to settle (rows become contiguous). |
| 77 | + await expect.poll(() => page.evaluate(maxRowGap)).toBeLessThan(1) |
| 78 | + |
| 79 | + const anchor = await page.evaluate(pickAnchor) |
| 80 | + |
| 81 | + const SCROLL_UP = 200 |
| 82 | + await page.evaluate((delta) => { |
| 83 | + const container = document.querySelector('#scroll-container') |
| 84 | + if (!container) throw new Error('Container not found') |
| 85 | + container.scrollTop -= delta |
| 86 | + }, SCROLL_UP) |
| 87 | + |
| 88 | + // Poll until the anchor settles at its compensated position: it should have |
| 89 | + // moved down by exactly the scroll delta. With the fix this converges; any |
| 90 | + // extra shift is the uncompensated estimate→actual delta of the items |
| 91 | + // measured above it (~40px per item) — the regression — and times out here. |
| 92 | + await expect |
| 93 | + .poll(async () => { |
| 94 | + const top = await page.evaluate(topOfIndex, anchor.index) |
| 95 | + return top === null ? Infinity : Math.abs(top - (anchor.top + SCROLL_UP)) |
| 96 | + }) |
| 97 | + .toBeLessThan(8) |
| 98 | +}) |
0 commit comments