/* 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' /** * A cell keeps what its renderer draws inside its column. * * `FieldRenderer` may return a plain string, and the cell rendered it as a bare text node. Only * `span` and `pre` were clipped, so a long string — or an `` — spilled past its track. The body's * scrollWidth grew past the header's (the header is just the sum of the column widths): at the far * right the header stopped while the body kept scrolling, and stray text showed where there were no * columns. Header and body now share one scroller, so the overflow shows as the grid scrolling past * its last column instead. * * [measured] operato-office contract list — body 5503 / header 3830 (2026-10-04) * * The long cells sit in the last column, because only content that runs past the last track widens * the body; in a middle column the next cell paints over it and the defect hides. */ const LONG = '긴 글자가 열 너비를 넘는다 '.repeat(40) const settle = (ms = 150) => new Promise(resolve => setTimeout(resolve, ms)) type Root = HTMLElement & { renderRoot: ShadowRoot; updateComplete: Promise } async function grid(renderer: (value: unknown) => unknown) { const config = { columns: [ { type: 'string', name: 'id', hidden: true }, { type: 'string', name: 'name', header: 'name', width: 120 }, { type: 'string', name: 'note', header: 'note', width: 160, record: { renderer } } ], pagination: { pages: [20] } } const el = (await fixture(html` ({ total: 2, records: [ { id: '1', name: 'a', note: LONG }, { id: '2', name: 'b', note: LONG } ] })} > `)) as Root & { fetch(): Promise } await el.fetch() await settle() 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 const notes = Array.from(body.renderRoot.querySelectorAll('ox-grid-field')).filter( f => (f as any).column?.name === 'note' ) as Root[] return { scroller, header, body, notes } } describe('a grid cell keeps its content inside its 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(() => { window.removeEventListener('error', ignoreResizeLoop, true) window.onerror = previousOnError }) const renderers: [string, (value: unknown) => unknown][] = [ ['a plain string', value => String(value)], ['an ', value => html`${value}`] ] for (const [kind, renderer] of renderers) { it(`${kind}: the grid scrolls exactly as far as its columns reach`, async () => { const { scroller, header, body, notes } = await grid(renderer) expect(notes.length, 'the long cells must be drawn, or this measures nothing').to.be.at.least(2) expect(body.offsetWidth, 'header and body span the same width').to.equal(header.offsetWidth) expect(scroller.scrollWidth, 'content past the last column scrolls on into empty space').to.equal( header.offsetWidth ) }) it(`${kind}: the cell is as wide as its column and clips what does not fit`, async () => { const { notes } = await grid(renderer) for (const note of notes) { expect(Math.round(note.getBoundingClientRect().width)).to.equal(160) expect(getComputedStyle(note).overflowX).to.equal('hidden') } }) } it('a plain string still ends in an ellipsis', async () => { const { notes } = await grid(value => String(value)) const text = notes[0].renderRoot.querySelector('span') as HTMLElement | null expect(!!text, 'the string sits in a box that can take text-overflow').to.equal(true) expect(getComputedStyle(text!).textOverflow).to.equal('ellipsis') expect(text!.clientWidth).to.be.lessThan(160) }) })