/* 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 { executeServerCommand, sendMouse } from '@web/test-runner-commands' import { html } from 'lit' import '../src/data-grist.js' import '../src/data-report.js' /** * One wheel turn moves one box. * * A grid inside a page has two scrollers above each other: its own and the page's. Whatever a turn * does, the worst outcome is both moving at once — the eye loses the row it was on and the place on * the page together. So: * * ``` * rows a turn over the rows moves the rows; once they are at their end, the next turn moves * the page (the browser hands it on — a turn that reaches the end stops there) * sideways a sideways turn never leaves the grid (at the end of the columns it would otherwise * become the browser's back gesture) * header a vertical turn over the header or footer moves the columns (down = right); when the * columns are already at that end the turn is left to the browser untouched * ``` * * These turns are real input through the browser (`send-wheel` in web-test-runner.config.mjs). A * WheelEvent dispatched from the page runs the listeners but scrolls nothing. */ type Root = HTMLElement & { renderRoot: ShadowRoot; updateComplete: Promise } const settle = (ms = 400) => new Promise(resolve => setTimeout(resolve, ms)) /** The step a vertical turn over the header moves the columns by. */ const STEP = 30 async function page() { 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 outer = (await fixture(html`
({ total: records.length, records })} >
`)) as HTMLElement const el = outer.querySelector('ox-grist') as Root & { fetch(): Promise } await el.fetch() await settle(150) await el.updateComplete const grist = el.renderRoot.querySelector('#grist') as Root 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 /* * The ends as the browser lets the grid reach them. scrollWidth - clientWidth overshoots by the * vertical scrollbar's gutter here (885 measured, 880 reachable), so it is not trusted. */ scroller.scrollTop = scroller.scrollLeft = 1e6 const maxTop = Math.round(scroller.scrollTop) const maxLeft = Math.round(scroller.scrollLeft) scroller.scrollTop = scroller.scrollLeft = 0 expect(maxTop, 'the rows must overflow, or this measures nothing').to.be.greaterThan(200) expect(maxLeft, 'the columns must overflow, or this measures nothing').to.be.greaterThan(200) return { outer, scroller, header, body, maxTop, maxLeft } } /** Where every box stands: the page, and the grid both ways. */ function where(outer: HTMLElement, scroller: HTMLElement) { return { page: Math.round(outer.scrollTop), top: Math.round(scroller.scrollTop), left: Math.round(scroller.scrollLeft) } } /* * Where to put the pointer, in the viewport. Header and body are as wide as all the columns and the body * as tall as all the rows, so their own boxes reach off screen once the grid scrolls — the point is taken * from the grid's visible box instead. */ function overRows(scroller: HTMLElement, header: HTMLElement) { const r = scroller.getBoundingClientRect() return { x: Math.round(r.left + r.width / 2), y: Math.round(header.getBoundingClientRect().bottom + 100) } } function overHeader(scroller: HTMLElement, header: HTMLElement) { const r = scroller.getBoundingClientRect() const h = header.getBoundingClientRect() return { x: Math.round(r.left + r.width / 2), y: Math.round(h.top + h.height / 2) } } async function turn(at: { x: number; y: number }, deltaY: number, deltaX = 0) { await executeServerCommand('send-wheel', { ...at, deltaX, deltaY }) await settle() } describe('one wheel turn moves one box', () => { /* 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(async () => { window.removeEventListener('error', ignoreResizeLoop, true) window.onerror = previousOnError await sendMouse({ type: 'move', position: [0, 0] }) }) describe('over the rows', () => { it('a turn moves the rows, nothing else', async () => { const { outer, scroller, header } = await page() await turn(overRows(scroller, header), 100) const now = where(outer, scroller) expect(now.top).to.be.greaterThan(0) expect(now.page).to.equal(0) expect(now.left).to.equal(0) }) it('the turn that reaches the last row stops there', async () => { const { outer, scroller, header, maxTop } = await page() scroller.scrollTop = maxTop - 10 await settle(100) await turn(overRows(scroller, header), 100) const now = where(outer, scroller) expect(now.top).to.be.within(maxTop - 1, maxTop) expect(now.page).to.equal(0) }) it('with the rows at their end, the next turn moves the page', async () => { const { outer, scroller, header, maxTop } = await page() scroller.scrollTop = maxTop scroller.scrollLeft = 100 await settle(100) await turn(overRows(scroller, header), 100) const now = where(outer, scroller) expect(now.page).to.be.greaterThan(0) expect(now.top).to.be.within(maxTop - 1, maxTop) expect(now.left, 'the turn does not become a sideways move').to.equal(100) }) it('with the rows at their start, a turn up moves the page', async () => { const { outer, scroller, header } = await page() outer.scrollTop = 40 await settle(100) await turn(overRows(scroller, header), -100) const now = where(outer, scroller) expect(now.page).to.be.lessThan(40) expect(now.top).to.equal(0) }) it('a sideways turn at the last column stays in the grid', async () => { const { scroller } = await page() const style = getComputedStyle(scroller) expect(style.overscrollBehaviorX).to.equal('contain') expect(style.overscrollBehaviorY).to.equal('auto') }) }) describe('over the header', () => { it('a turn down moves the columns right, nothing else', async () => { const { outer, scroller, header } = await page() scroller.scrollLeft = 100 await settle(100) await turn(overHeader(scroller, header), 100) expect(where(outer, scroller)).to.deep.equal({ page: 0, top: 0, left: 100 + STEP }) }) it('a turn up moves the columns left, nothing else', async () => { const { outer, scroller, header } = await page() scroller.scrollLeft = 100 await settle(100) await turn(overHeader(scroller, header), -100) expect(where(outer, scroller)).to.deep.equal({ page: 0, top: 0, left: 100 - STEP }) }) it('the turn that brings the columns to their last one stops there', async () => { const { outer, scroller, header, maxLeft } = await page() scroller.scrollLeft = maxLeft - 10 await settle(100) await turn(overHeader(scroller, header), 100) const now = where(outer, scroller) expect(now.left).to.be.within(maxLeft - 1, maxLeft) expect(now.top, 'the rows stay').to.equal(0) expect(now.page, 'the page stays').to.equal(0) }) it('the turn that brings the columns to their first one stops there', async () => { const { outer, scroller, header } = await page() scroller.scrollTop = 100 scroller.scrollLeft = 10 await settle(100) await turn(overHeader(scroller, header), -100) expect(where(outer, scroller)).to.deep.equal({ page: 0, top: 100, left: 0 }) }) it('with the columns at their last one, a turn down is left to the browser', async () => { const { outer, scroller, header, maxLeft } = await page() scroller.scrollLeft = maxLeft await settle(100) await turn(overHeader(scroller, header), 100) const now = where(outer, scroller) expect(now.left).to.be.within(maxLeft - 1, maxLeft) expect(now.top, 'the rows take the turn').to.be.greaterThan(0) expect(now.page).to.equal(0) }) }) /* * The report's header is a box of its own beside the body (they copy each other's scrollLeft), so * a turn the header leaves alone goes to the boxes around it. The same rule holds: the columns, or * the rest — never both. */ describe('over the report header', () => { async function report() { const columns: any[] = [ { type: 'string', name: 'id', hidden: true }, { type: 'string', name: 'kind', header: 'kind', width: 110 } ] 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), kind: 'K' + (r % 4) } for (let i = 0; i < 12; i++) record['c' + i] = `v${r}-${i}` return record }) const outer = (await fixture(html`
({ total: records.length, records })} >
`)) as HTMLElement const el = outer.querySelector('ox-report') as Root & { fetch(): Promise } await el.fetch() await settle(150) await el.updateComplete const component = el.renderRoot.querySelector('#report') as Root await component.updateComplete const header = component.renderRoot.querySelector('ox-report-header') as HTMLElement const body = component.renderRoot.querySelector('ox-report-body') as HTMLElement header.scrollLeft = 1e6 const maxLeft = Math.round(header.scrollLeft) header.scrollLeft = 0 await settle(100) expect(maxLeft, 'the columns must overflow, or this measures nothing').to.be.greaterThan(200) const r = header.getBoundingClientRect() const at = { x: Math.round(r.left + r.width / 2), y: Math.round(r.top + r.height / 2) } /* Everything a turn could move. */ const boxes = () => ({ page: Math.round(outer.scrollTop), left: Math.round(header.scrollLeft), rows: Math.round(body.scrollTop), wrap: Math.round((el.renderRoot.querySelector('#wrap') as HTMLElement).scrollTop) }) return { header, body, maxLeft, at, boxes } } it('a turn down moves the columns right, nothing else', async () => { const { header, at, boxes } = await report() header.scrollLeft = 100 await settle(100) await turn(at, 100) expect(boxes()).to.deep.equal({ page: 0, left: 140, rows: 0, wrap: 0 }) }) it('the turn that brings the columns to their last one stops there', async () => { const { header, maxLeft, at, boxes } = await report() header.scrollLeft = maxLeft - 10 await settle(100) await turn(at, 100) expect(boxes()).to.deep.equal({ page: 0, left: maxLeft, rows: 0, wrap: 0 }) }) it('the turn that brings the columns to their first one stops there', async () => { const { header, at, boxes } = await report() header.scrollLeft = 10 await settle(100) await turn(at, -100) expect(boxes()).to.deep.equal({ page: 0, left: 0, rows: 0, wrap: 0 }) }) it('with the columns at their last one, a turn down is left to the browser', async () => { const { header, maxLeft, at, boxes } = await report() header.scrollLeft = maxLeft await settle(100) await turn(at, 100) const now = boxes() expect(now.left).to.equal(maxLeft) expect(now.page + now.wrap, 'a box around the header takes the turn').to.be.greaterThan(0) }) }) })