/* 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' /** * A column's edge can be dragged and double-clicked on every grid. * * The handle on a column's edge also draws the grid's vertical line, and a grid with nothing to edit * hides its lines (--grid-header-splitter-display: none) - which took the handle with them. A read * list then had no way to widen a column whose text was cut, though reading is what it is for. * * ``` * read-only the handle stays and draws no line until the pointer is on it or drags it; the header * keeps the look it had without one (its right padding, its name not pushed) * drag dragging the edge sets the width, on a read-only grid as on an editable one * fit double-clicking the edge widens the column until no cell in it is cut, header included * click a click that does not drag changes nothing (an fr column stays flexible) * ``` */ type Root = HTMLElement & { renderRoot: ShadowRoot; updateComplete: Promise } const settle = (ms = 150) => new Promise(resolve => setTimeout(resolve, ms)) const LONG = 'A long counterparty name that does not fit its column' async function grid({ editable = false, nameWidth = 120 as number | string } = {}) { const columns: any[] = [ { type: 'string', name: 'id', hidden: true }, { type: 'string', name: 'name', header: 'name', width: nameWidth, record: { editable } }, { type: 'string', name: 'code', header: 'code', width: 100, record: { editable } }, /* Text and a mark side by side, as the cash list's counterparty and its 「미등록」: long text pushes the mark out. */ { type: 'string', name: 'memo', header: 'memo', width: 120, record: { editable, renderer: (value: string) => html`${value} unregistered` } } ] const records = Array.from({ length: 5 }, (_, r) => ({ id: String(r), name: r === 3 ? LONG : 'short', code: 'C-' + r, memo: r === 2 ? LONG : 'note' })) const el = (await fixture(html` `)) as Root await settle() await el.updateComplete const ox = el.renderRoot.querySelector('#grist') as Root await ox.updateComplete 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, header, body } } const heads = (header: Root) => Array.from(header.renderRoot.querySelectorAll('[role=columnheader]')) as HTMLElement[] const width = (element: Element) => Math.round(element.getBoundingClientRect().width) const handle = (head: HTMLElement) => head.querySelector('span[splitter]') as HTMLElement | null function centre(element: Element): [number, number] { const r = element.getBoundingClientRect() return [Math.round(r.left + r.width / 2), Math.round(r.top + r.height / 2)] } /** Whether the text of any cell of the column under this header is cut - the cell itself, or text it ends with "…". */ function cut(head: HTMLElement, body: Root) { const left = head.getBoundingClientRect().left return (Array.from(body.renderRoot.querySelectorAll('ox-grid-field')) as Root[]) .filter(cell => Math.abs(cell.getBoundingClientRect().left - left) < 1) .some(cell => [cell, ...Array.from(cell.renderRoot.querySelectorAll('*'))] .filter(e => e === cell || getComputedStyle(e).textOverflow === 'ellipsis') .some(e => e.scrollWidth > e.clientWidth + 1) ) } async function drag(from: [number, number], by: number) { await sendMouse({ type: 'move', position: from }) await sendMouse({ type: 'down' }) await sendMouse({ type: 'move', position: [from[0] + by, from[1]] }) await sendMouse({ type: 'up' }) await settle(300) } async function doubleClick([x, y]: [number, number]) { await executeServerCommand('send-double-click', { x, y }) await settle(300) } describe('a column edge', () => { /* 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) }) afterEach(async () => { await sendMouse({ type: 'move', position: [0, 0] }) }) after(() => { window.removeEventListener('error', ignoreResizeLoop, true) window.onerror = previousOnError }) describe('on a read-only grid', () => { it('has a handle that draws no line and leaves the header as it was', async () => { const { header } = await grid() const name = heads(header)[0] const splitter = handle(name) expect(!!splitter, 'the handle is there').to.equal(true) expect(getComputedStyle(splitter!).borderRightColor, 'no line').to.equal('rgba(0, 0, 0, 0)') expect(parseFloat(getComputedStyle(name).paddingRight), 'the right padding stays').to.be.greaterThan(0) expect(width(splitter!), 'the handle is narrow').to.be.at.most(8) }) it('draws its line while the pointer is on it', async () => { const { header } = await grid() const splitter = handle(heads(header)[0])! await sendMouse({ type: 'move', position: centre(splitter) }) await settle(100) expect(getComputedStyle(splitter).borderRightColor).not.to.equal('rgba(0, 0, 0, 0)') }) it('can be dragged to widen the column', async () => { const { header } = await grid() const name = heads(header)[0] const before = width(name) await drag(centre(handle(name)!), 40) expect(width(heads(header)[0])).to.be.within(before + 35, before + 45) }) }) for (const editable of [false, true]) { describe(`on ${editable ? 'an editable' : 'a read-only'} grid, a double-click`, () => { it('widens the column until no cell in it is cut', async () => { const { header, body } = await grid({ editable }) const name = heads(header)[0] expect(cut(name, body), 'the long name is cut to begin with').to.equal(true) const before = width(name) await doubleClick(centre(handle(name)!)) const after = heads(header)[0] expect(width(after)).to.be.greaterThan(before) expect(cut(after, body)).to.equal(false) }) it('leaves a column whose cells all fit as it is', async () => { const { header } = await grid({ editable }) const code = heads(header)[1] const before = width(code) await doubleClick(centre(handle(code)!)) expect(width(heads(header)[1])).to.equal(before) }) it('widens a column of text and a mark until the mark is back', async () => { const { header, body } = await grid({ editable }) const memo = heads(header)[2] expect(cut(memo, body), 'the long text pushes the mark out to begin with').to.equal(true) await doubleClick(centre(handle(memo)!)) expect(cut(heads(header)[2], body)).to.equal(false) }) }) } it('does nothing on a click that does not drag (an fr column stays flexible)', async () => { const { ox, header } = await grid({ editable: true, nameWidth: 'minmax(120px, 1fr)' }) const before = ox.style.getPropertyValue('--grid-template-columns') await sendMouse({ type: 'click', position: centre(handle(heads(header)[0])!) }) await settle(300) expect(ox.style.getPropertyValue('--grid-template-columns')).to.equal(before) }) })