/* Elements are compared by presence, not by value: a failing element comparison makes chai render the DOM and hang the runner. */ import { expect, fixture } from '@open-wc/testing' import { html } from 'lit' import '../src/data-grist.js' /** * Header and body move as one. * * The body used to be its own scroller, and the header a second scroller that copied the body's * scrollLeft from its scroll event (and the other way round). Three things went wrong: * * ``` * gap the compositor moves the body at once; the header only moves after the main thread * handles the event, so on a busy screen it trails by several frames * shake the sync ran both ways, so a late header event put the body back one frame * the end when the two scroll widths differ, the header stops while the body goes on * ``` * * Now the role=grid box is the only scroller. The header sticks to its top and the body does not * scroll. Nothing copies a position, so nothing can trail or pull back. */ type Root = HTMLElement & { renderRoot: ShadowRoot; updateComplete: Promise } const settle = (ms = 150) => new Promise(resolve => setTimeout(resolve, ms)) async function grid() { const columns: any[] = [ { type: 'gutter', gutterName: 'sequence' }, { type: 'string', name: 'id', hidden: true }, { type: 'string', name: 'code', header: 'code', width: 120, fixed: true } ] for (let i = 0; i < 12; i++) { columns.push({ type: 'string', name: 'c' + i, header: 'col ' + i, width: 110 }) } const records = Array.from({ length: 60 }, (_, r) => { const record: any = { id: String(r), code: 'C-' + r } for (let i = 0; i < 12; i++) record['c' + i] = `v${r}-${i}` return record }) const el = (await fixture(html` ({ total: records.length, records })} > `)) as Root & { fetch(): Promise } await el.fetch() await settle() await el.updateComplete const grist = el.renderRoot.querySelector('#grist') as Root & { focused?: { row: number; column: number } } const scroller = grist.renderRoot.querySelector('div[grid]') as HTMLElement const header = grist.renderRoot.querySelector('ox-grid-header') as Root const body = grist.renderRoot.querySelector('ox-grid-body') as Root await body.updateComplete return { grist, scroller, header, body } } /** The largest horizontal distance between a column's header cell and its cell in the first row. */ function misalignment(header: Root, body: Root) { const heads = Array.from(header.renderRoot.querySelectorAll('[role=columnheader]')) as HTMLElement[] const row = body.renderRoot.querySelector('div[grid-row]') as HTMLElement const cells = Array.from(row.querySelectorAll('ox-grid-field')) as HTMLElement[] const n = Math.min(heads.length, cells.length) let worst = 0 for (let i = 0; i < n; i++) { worst = Math.max(worst, Math.abs(heads[i].getBoundingClientRect().left - cells[i].getBoundingClientRect().left)) } return { n, worst } } describe('grid header and body share one scroller', () => { /* Resizing the grid body inside a fixture trips Chrome's benign "ResizeObserver loop" error. */ const ignoreResizeLoop = (e: ErrorEvent) => { if (String(e.message).includes('ResizeObserver loop')) { e.stopImmediatePropagation() e.preventDefault() } } let previousOnError: OnErrorEventHandler = null before(() => { window.addEventListener('error', ignoreResizeLoop, true) previousOnError = window.onerror window.onerror = (message, ...rest) => String(message).includes('ResizeObserver loop') ? true : previousOnError?.call(window, message, ...rest) }) after(() => { window.removeEventListener('error', ignoreResizeLoop, true) window.onerror = previousOnError }) it('neither the header nor the body scrolls on its own', async () => { const { scroller, header, body } = await grid() expect(scroller.scrollWidth, 'the grid must overflow, or this measures nothing').to.be.greaterThan( scroller.clientWidth ) expect(getComputedStyle(body).overflowX).to.equal('visible') expect(getComputedStyle(header).overflowX).to.equal('visible') expect(header.offsetWidth, 'header and body span the same width').to.equal(body.offsetWidth) }) for (const where of ['start', 'middle', 'end'] as const) { it(`every column lines up with its header at the ${where}`, async () => { const { scroller, header, body } = await grid() const max = scroller.scrollWidth - scroller.clientWidth scroller.scrollLeft = where === 'start' ? 0 : where === 'middle' ? Math.round(max / 2) : max await settle(50) const { n, worst } = misalignment(header, body) expect(n, 'columns must be drawn, or this measures nothing').to.be.greaterThan(10) expect(worst).to.equal(0) }) } it('the header stays on top when the rows scroll', async () => { const { scroller, header } = await grid() scroller.scrollTop = 600 await settle(50) expect(scroller.scrollTop, 'the rows must scroll, or this measures nothing').to.be.greaterThan(0) expect(header.getBoundingClientRect().top).to.equal(scroller.getBoundingClientRect().top) }) it('a focused row that was under the header is brought out below it', async () => { const { grist, scroller, header, body } = await grid() scroller.scrollTop = 600 await settle(50) grist.focused = { row: 2, column: 3 } await grist.updateComplete await body.updateComplete await settle(50) const focused = body.renderRoot.querySelector('ox-grid-field[focused]') as HTMLElement expect(!!focused, 'a cell must take the focus').to.equal(true) expect(focused.getBoundingClientRect().top).to.be.at.least(header.getBoundingClientRect().bottom - 1) }) })