/* 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 { sendMouse } from '@web/test-runner-commands' import { html } from 'lit' import '../src/data-grist.js' /** * A column that takes the room left over: `width: 'minmax(150px, 1fr)'`. * * A string width goes into grid-template-columns as it is, so fr was always accepted — and broke the * table. The header and the body are two CSS grids with the same template. A flexible track is sized * from what its cells hold, and the two grids hold different things: the body grew the column to its * longest text while the header kept it at its floor. Measured on the balance screen: 182px apart with * one fr column whose text was longer than the room, 251px with two fr columns. * * ``` * aligned every cell sits under its header, room or no room * room a flexible column takes the room on the right; the filler after the last column gets none * no room it stays at its floor and its text is cut ("…"), as a fixed column's is * ratio 1fr and 2fr share the room one to two * print the print template is a valid template (px became fr letter by letter: minmax(150fr, 1fr)) * resize dragging a flexible column's edge gives it a width in px from where it stood * ``` */ type Root = HTMLElement & { renderRoot: ShadowRoot; updateComplete: Promise } const settle = (ms = 150) => new Promise(resolve => setTimeout(resolve, ms)) const LONG = 'A very long account name that is far wider than any room the table has left for it' async function grid(widths: { name?: any; memo?: any }, width = 700, editable = false) { const columns: any[] = [ { type: 'string', name: 'id', hidden: true }, { type: 'string', name: 'name', header: 'name', width: widths.name ?? 150, record: { editable } }, { type: 'string', name: 'code', header: 'code', width: 100 }, { type: 'string', name: 'memo', header: 'memo', width: widths.memo ?? 120 } ] const records = Array.from({ length: 5 }, (_, r) => ({ id: String(r), name: r % 2 ? LONG : 'short', code: 'C-' + r, memo: 'note ' + r })) const el = (await fixture(html` `)) as Root await settle() await el.updateComplete const ox = el.renderRoot.querySelector('#grist') as Root await ox.updateComplete const scroller = ox.renderRoot.querySelector('div[grid]') as HTMLElement const header = ox.renderRoot.querySelector('ox-grid-header') as Root const body = ox.renderRoot.querySelector('ox-grid-body') as Root await body.updateComplete await settle(50) return { ox, scroller, header, body } } function heads(header: Root) { return Array.from(header.renderRoot.querySelectorAll('[role=columnheader]')) as HTMLElement[] } function width(element: Element) { return Math.round(element.getBoundingClientRect().width) } /** The largest distance, in left or in width, between a header cell and a cell of its column, over every row. */ function misalignment(header: Root, body: Root) { const hs = heads(header) let worst = 0 for (const row of Array.from(body.renderRoot.querySelectorAll('div[grid-row]'))) { const cells = Array.from(row.querySelectorAll(':scope > ox-grid-field')) as HTMLElement[] hs.forEach((h, i) => { const cell = cells[i] if (!cell) return const a = h.getBoundingClientRect() const b = cell.getBoundingClientRect() worst = Math.max(worst, Math.abs(a.left - b.left), Math.abs(a.width - b.width)) }) } return Math.round(worst) } /** Whether the text of a column's cell is cut in any row. */ function cut(body: Root, index: number) { return Array.from(body.renderRoot.querySelectorAll('div[grid-row]')).some(row => { const cell = row.querySelectorAll(':scope > ox-grid-field')[index] as Root | undefined return !!cell && [cell, ...Array.from(cell.renderRoot.querySelectorAll('*'))].some(e => e.scrollWidth > e.clientWidth + 1) }) } describe('a flexible column', () => { /* 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] }) }) it('with room, takes the room on the right and stays under its header', async () => { const { scroller, header, body } = await grid({ name: 'minmax(150px, 1fr)' }, 1200) const name = heads(header)[0] expect(width(name), 'the column takes the room').to.be.greaterThan(500) expect(scroller.scrollWidth, 'nothing overflows').to.be.at.most(scroller.clientWidth) expect(misalignment(header, body)).to.equal(0) }) it('without room, stays at its floor, cuts its text and stays under its header', async () => { const { header, body } = await grid({ name: 'minmax(150px, 1fr)', memo: 600 }, 700) expect(width(heads(header)[0])).to.equal(150) expect(cut(body, 0), 'the long name is cut').to.equal(true) expect(misalignment(header, body)).to.equal(0) }) it('two flexible columns share the room one to two and stay under their headers', async () => { const { header, body } = await grid({ name: 'minmax(100px, 1fr)', memo: 'minmax(100px, 2fr)' }, 900) const [name, , memo] = heads(header) /* fr shares the whole width of the tracks, not what is above their floors: 1fr and 2fr are x and 2x. */ const share = width(memo) / width(name) expect(share).to.be.within(1.9, 2.1) expect(misalignment(header, body)).to.equal(0) }) it('leaves a table of fixed columns as it was', async () => { const { body } = await grid({}, 700) const cell = body.renderRoot.querySelector('ox-grid-field') as HTMLElement expect(getComputedStyle(cell).contain).to.equal('none') }) it('prints with a valid template', async () => { const { ox } = await grid({ name: 'minmax(150px, 1fr)' }, 700) const print = ox.style.getPropertyValue('--grid-template-print-columns') expect(CSS.supports('grid-template-columns', print), print).to.equal(true) expect(print).to.contain('150fr') }) it('dragging its edge gives it a width in px from where it stood', async () => { const { header } = await grid({ name: 'minmax(150px, 1fr)' }, 1000, true) const name = heads(header)[0] const before = width(name) const splitter = name.querySelector('span[splitter]') as HTMLElement expect(!!splitter, 'an editable column has a handle').to.equal(true) const r = splitter.getBoundingClientRect() const x = Math.round(r.left + r.width / 2) const y = Math.round(r.top + r.height / 2) await sendMouse({ type: 'move', position: [x, y] }) await sendMouse({ type: 'down' }) await sendMouse({ type: 'move', position: [x - 40, y] }) await sendMouse({ type: 'up' }) await settle(300) expect(width(heads(header)[0])).to.be.within(before - 45, before - 35) }) })