/* 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' /* * Group headings (rows.groups): the management check reads 「자금 지연」 above its late money, in a table on a wide * screen and over the cards on a phone (2026-11-06). A heading stands wherever the group value changes, written by * the group column's renderer. */ const NAMES: Record = { funds: 'Late money', books: 'Books' } const records = [ { id: '1', group: 'funds', what: 'Overdue collections', count: 23 }, { id: '2', group: 'funds', what: 'Unpaid VAT', count: 1 }, { id: '3', group: 'books', what: 'Lines without an account', count: 4 } ] const config = (groups: any[]) => ({ columns: [ { type: 'string', name: 'group', header: 'Group', hidden: true, record: { renderer: (value: string) => NAMES[value] } }, { type: 'string', name: 'what', header: 'What' }, { type: 'integer', name: 'count', header: 'Count' } ], rows: { appendable: false, editable: false, selectable: false, groups }, list: { fields: ['what'], details: ['count'] } }) const idle = async (el: DataGrist) => { while (!((await el.updateComplete) && (await el.grist.updateComplete))); } const mount = async (mode: 'GRID' | 'CARD', groups: any[]) => { const el = await fixture( html`` ) await idle(el) /* The grid draws its rows in ox-grid-body. */ const root = (mode == 'GRID' ? (el.grist.renderRoot.querySelector('ox-grid-body') as any).renderRoot : el.grist.renderRoot) as ShadowRoot return root } describe('group headings', () => { failOnPageErrors() for (const mode of ['GRID', 'CARD'] as const) { it(`stand where the group changes, in the group column's words (${mode})`, async () => { const root = await mount(mode, [{ column: 'group' }]) const headings = Array.from(root.querySelectorAll('[group-heading]')) as HTMLElement[] expect(headings.map(one => one.textContent!.trim())).to.deep.equal(['Late money', 'Books']) /* Each heading stands above the first row of its group and spans the whole row. */ const rows = Array.from(root.querySelectorAll(mode == 'GRID' ? 'ox-grid-field[role="gridcell"]' : 'ox-record-card')) as HTMLElement[] const firstOf = (text: string) => rows.find((one: any) => one.record?.what === text && (mode == 'CARD' || one.column?.name === 'what'))!.getBoundingClientRect() const box = (one: HTMLElement) => (mode == 'GRID' ? (one.firstElementChild as HTMLElement) : one).getBoundingClientRect() expect(box(headings[0]).bottom).to.be.at.most(firstOf('Overdue collections').top + 0.5) expect(box(headings[1]).top).to.be.at.least(firstOf('Unpaid VAT').bottom - 0.5) expect(box(headings[1]).bottom).to.be.at.most(firstOf('Lines without an account').top + 0.5) expect(box(headings[1]).width, 'the heading does not span the row').to.be.above(600) }) it(`are not drawn when the rows are not grouped (${mode})`, async () => { const root = await mount(mode, []) expect(root.querySelector('[group-heading]')).to.equal(null) }) } }) describe('a link a renderer returns', () => { failOnPageErrors() for (const mode of ['GRID', 'CARD'] as const) { it(`reads as the text around it, not the browser's blue underline (${mode})`, async () => { const linked = { ...config([]), columns: [ { type: 'string', name: 'what', header: 'What', record: { renderer: (value: string) => html`${value}` } }, { type: 'integer', name: 'count', header: 'Count' } ] } const el = await fixture( html`` ) await idle(el) const root = (mode == 'GRID' ? (el.grist.renderRoot.querySelector('ox-grid-body') as any).renderRoot : el.grist.renderRoot) as ShadowRoot const fields = Array.from(root.querySelectorAll(mode == 'GRID' ? 'ox-grid-field' : 'ox-record-card')) as any[] const holder = mode == 'GRID' ? fields.find(one => one.column?.name === 'what') : fields[0].renderRoot.querySelector('ox-card-field') const link = holder.renderRoot.querySelector('a') as HTMLElement const style = getComputedStyle(link) expect(style.textDecorationLine).to.equal('none') expect(style.color).to.equal(getComputedStyle(link.parentElement ?? holder).color) }) } }) describe('a card field with nothing to show', () => { failOnPageErrors() it('takes no place - no label stands alone', async () => { const labelled = { ...config([]), columns: [ { type: 'string', name: 'what', header: 'What' }, { type: 'integer', name: 'count', header: 'Count', label: { renderer: () => 'Count' }, record: { renderer: (value: any) => value ?? '' } } ] } const el = await fixture( html`` ) await idle(el) const cards = Array.from(el.grist.renderRoot.querySelectorAll('ox-record-card')) as any[] const countOf = (card: any) => card.renderRoot.querySelector('[detail] ox-card-field') as HTMLElement expect(countOf(cards[0]).getBoundingClientRect().width, 'an empty field stands').to.equal(0) expect(countOf(cards[1]).getBoundingClientRect().width, 'a field with a value is gone').to.be.above(0) }) }) describe('the rows under a row (tree.childrenProperty)', () => { failOnPageErrors() const tree = [ { id: '1', group: 'funds', what: 'Planned money not seen', count: 3, children: [{ id: '1a', what: 'Azure', count: 7 }, { id: '1b', what: 'Office 365' }] }, { id: '2', group: 'funds', what: 'Unpaid VAT', count: 1 }, { id: '3', group: 'books', what: 'Lines without an account', count: 4 } ] const treeConfig = { ...config([{ column: 'group' }]), tree: { childrenProperty: 'children', expanded: () => true } } const mount = async (mode: 'GRID' | 'CARD') => { const el = await fixture( html`` ) await idle(el) return el } it('stand inside the parent card on a card list, not as cards of their own', async () => { const el = await mount('CARD') const cards = Array.from(el.grist.renderRoot.querySelectorAll('ox-record-card')) as any[] expect(cards.map(card => card.record.what)).to.deep.equal(['Planned money not seen', 'Unpaid VAT', 'Lines without an account']) const children = Array.from(cards[0].renderRoot.querySelectorAll('[child]')) as HTMLElement[] expect(children.map(child => (child.querySelector('ox-card-field') as any).record.what)).to.deep.equal(['Azure', 'Office 365']) expect(cards[1].renderRoot.querySelector('[children]'), 'a row with no children shows a children block').to.equal(null) /* A child's title takes its own line; its details stand under it. */ const [title, detail] = Array.from(children[0].querySelectorAll('ox-card-field')) as HTMLElement[] const titleBox = title.getBoundingClientRect() expect(detail.getBoundingClientRect().top, 'the details stand beside the title').to.be.above(titleBox.top + titleBox.height / 2) /* The room between two children is wider than the room between a child's two lines. */ const between = children[1].getBoundingClientRect().top - children[0].getBoundingClientRect().bottom const within = detail.getBoundingClientRect().top - titleBox.bottom expect(between, 'the children run together').to.be.at.least(within + 8) const headings = Array.from(el.grist.renderRoot.querySelectorAll('[group-heading]')) as HTMLElement[] expect(headings.map(one => one.textContent!.trim())).to.deep.equal(['Late money', 'Books']) }) it('stand as rows under the parent in the grid, and do not open a group again', async () => { const el = await mount('GRID') const body = (el.grist.renderRoot.querySelector('ox-grid-body') as any).renderRoot as ShadowRoot const whats = (Array.from(body.querySelectorAll('ox-grid-field[role="gridcell"]')) as any[]).filter(one => one.column?.name === 'what').map(one => one.record.what) expect(whats).to.deep.equal(['Planned money not seen', 'Azure', 'Office 365', 'Unpaid VAT', 'Lines without an account']) /* A child steps in under its parent at its first column, and only there. */ const cell = (what: string, name: string) => (Array.from(body.querySelectorAll('ox-grid-field[role="gridcell"]')) as any[]).find(one => one.record.what === what && one.column?.name === name) const pad = (one: HTMLElement) => parseFloat(getComputedStyle(one).paddingLeft) expect(pad(cell('Azure', 'what')), 'a child does not step in').to.be.above(pad(cell('Planned money not seen', 'what')) + 8) expect(pad(cell('Azure', 'count'))).to.equal(pad(cell('Planned money not seen', 'count'))) const headings = Array.from(body.querySelectorAll('[group-heading]')) as HTMLElement[] expect(headings.map(one => one.textContent!.trim())).to.deep.equal(['Late money', 'Books']) }) }) describe('a page button a renderer returns', () => { failOnPageErrors() for (const mode of ['GRID', 'CARD'] as const) { it(`takes the page's quiet look, not the filled gutter button (${mode})`, async () => { const acting = { ...config([]), columns: [ { type: 'string', name: 'what', header: 'What' }, { type: 'string', name: 'act', header: '', record: { renderer: () => html`` } } ], list: { fields: ['what'], details: ['act'] } } const el = await fixture( html`` ) await idle(el) const root = (mode == 'GRID' ? (el.grist.renderRoot.querySelector('ox-grid-body') as any).renderRoot : el.grist.renderRoot) as ShadowRoot const fields = Array.from(root.querySelectorAll(mode == 'GRID' ? 'ox-grid-field' : 'ox-record-card')) as any[] const holder = mode == 'GRID' ? fields.find(one => one.column?.name === 'act') : fields[0].renderRoot.querySelector('[detail] ox-card-field') const button = holder.renderRoot.querySelector('button') as HTMLElement expect(getComputedStyle(button).backgroundColor, 'a quiet button is filled').to.be.oneOf(['rgba(0, 0, 0, 0)', 'transparent']) }) } }) describe('an empty text detail on a card', () => { failOnPageErrors() it('takes no place with the default text renderer', async () => { const texts = { ...config([]), columns: [ { type: 'string', name: 'what', header: 'What' }, { type: 'string', name: 'summary', header: 'Summary', label: { renderer: () => 'Summary' } } ], list: { fields: ['what'], details: ['summary'] } } const el = await fixture( html`` ) await idle(el) const cards = Array.from(el.grist.renderRoot.querySelectorAll('ox-record-card')) as any[] const summaryOf = (card: any) => card.renderRoot.querySelector('[detail] ox-card-field') as HTMLElement expect(summaryOf(cards[0]).getBoundingClientRect().width, 'an empty summary stands').to.equal(0) expect(summaryOf(cards[1]).getBoundingClientRect().width).to.be.above(0) }) }) describe('a gutter button with its title', () => { failOnPageErrors() it("keeps the gutter's small filled look - the page button look is for a renderer's button", async () => { const acting = { ...config([]), columns: [ { type: 'gutter', gutterName: 'button', icon: 'play_arrow', title: 'Start', iconOnly: false, width: 72 }, { type: 'string', name: 'what', header: 'What' } ] } const el = await fixture(html``) await idle(el) const root = (el.grist.renderRoot.querySelector('ox-grid-body') as any).renderRoot as ShadowRoot const field = (Array.from(root.querySelectorAll('ox-grid-field')) as any[]).find(one => one.column?.gutterName === 'button') const button = field.renderRoot.querySelector('button') as HTMLElement const look = getComputedStyle(button) expect(look.fontWeight, 'not the page button weight').to.not.equal('600') expect(look.fontSize, "the gutter's 0.8rem").to.equal(`${0.8 * parseFloat(getComputedStyle(document.documentElement).fontSize)}px`) expect(look.borderTopWidth, 'no page button border').to.equal('0px') }) })