/* 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' import { DataGrist } from '../src/data-grist.js' import { failOnPageErrors } from './page-errors.js' /* * A report read on a phone (2026-11-06). The grid's totals row is drawn as a totals card after the cards, and a * grist with a narrow mode draws it while its own width is under narrowWidth - a table on a wide screen, cards on a * phone, from one config. */ const records = [ { id: '1', name: 'one', shares: 15250, since: '2020-01-01' }, { id: '2', name: 'two', shares: 5600, since: '2026-03-17' } ] const config = (accumulator: boolean) => ({ columns: [ { type: 'string', name: 'name', header: 'Name' }, { type: 'integer', name: 'shares', header: 'Shares', accumulator: 'sum' } ], rows: { appendable: false, editable: false, selectable: false, accumulator }, list: { fields: ['name'], details: ['shares'] } }) const idle = async (el: DataGrist) => { while (!((await el.updateComplete) && (await el.grist.updateComplete))); } const frame = (width: number) => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve))) describe('a card list and its totals', () => { failOnPageErrors() it('draws the totals card after the cards, each total naming its column', async () => { const el = await fixture( html`` ) await idle(el) const totals = el.grist.renderRoot.querySelector('[totals]') as HTMLElement expect(totals, 'no totals card').to.exist const shares = totals.querySelector('[total="shares"]') as HTMLElement expect(shares.querySelector('label')!.textContent).to.equal('Shares') expect(shares.textContent).to.contain('20850') expect(totals.querySelector('[total="name"]'), 'a column with no accumulator is totalled').to.equal(null) const cards = el.grist.renderRoot.querySelectorAll('ox-record-card') expect(totals.getBoundingClientRect().top, 'the totals stand above a card').to.be.at.least(cards[cards.length - 1].getBoundingClientRect().bottom) }) it('draws no totals card when the rows do not accumulate', async () => { const el = await fixture( html`` ) await idle(el) expect(el.grist.renderRoot.querySelector('[totals]')).to.equal(null) }) }) describe('a grist with a narrow mode', () => { failOnPageErrors() it('draws its mode when wide and its narrow mode when narrow, by its own width', async () => { const el = await fixture( html`` ) await frame(0) await idle(el) expect(el.shownMode).to.equal('GRID') expect(el.renderRoot.querySelector('ox-grid'), 'not a grid while wide').to.exist el.style.width = '340px' await frame(0) await idle(el) expect(el.shownMode).to.equal('CARD') expect(el.renderRoot.querySelector('ox-card'), 'not cards while narrow').to.exist expect(el.mode, 'the requested mode was overwritten').to.equal('GRID') el.style.width = '900px' await frame(0) await idle(el) expect(el.renderRoot.querySelector('ox-grid'), 'not a grid again once wide').to.exist }) it('keeps its mode at every width when no narrow mode is set', async () => { const el = await fixture( html`` ) await frame(0) await idle(el) expect(el.renderRoot.querySelector('ox-grid')).to.exist }) }) describe('a card detail with a label', () => { failOnPageErrors() it('keeps the label whole when details stand in a row', async () => { const labelled = { ...config(false), columns: [ { type: 'string', name: 'name', header: 'Name' }, { type: 'string', name: 'since', header: 'Since', label: { renderer: () => '취득일' } } ], list: { fields: ['name'], details: ['since'] } } const el = await fixture( html`` ) await idle(el) const card = el.grist.renderRoot.querySelector('ox-record-card') as any const field = card.renderRoot.querySelector('[detail] ox-card-field') as HTMLElement & { renderRoot: ShadowRoot } const label = field.renderRoot.querySelector('label') as HTMLElement expect(label.scrollWidth, 'the label is cut').to.be.at.most(label.clientWidth) const range = document.createRange() range.setStartAfter(label) range.setEnd(field.renderRoot, field.renderRoot.childNodes.length) const words = document.createRange() words.selectNodeContents(label) expect(range.getBoundingClientRect().left - words.getBoundingClientRect().right, 'the label touches its value').to.be.at.least(4) const value = Array.from(field.renderRoot.children).find(child => child !== label && child.tagName !== 'STYLE') as HTMLElement const text = document.createRange() text.selectNodeContents(value) expect(value.getBoundingClientRect().width, 'the value is cut').to.be.at.least(text.getBoundingClientRect().width - 0.01) }) })