import { expect, fixture } from '@open-wc/testing' import { html } from 'lit' import '../src/data-grist.js' /** * The last fetch asked is the one the grid shows. * * A ledger opened from a dashboard card asked twice in a row - once with no conditions as it read its address, * then with the month the card named. The grid kept whichever answer arrived first - the read with no conditions - * and showed every line under a headroom that said October (2026-10-16). */ const config = { columns: [ { type: 'string', name: 'id', hidden: true }, { type: 'string', name: 'name', header: 'name' } ], pagination: { pages: [20] } } const answer = (total: number) => ({ total, records: Array.from({ length: Math.min(total, 20) }, (_, i) => ({ id: `${total}-${i}`, name: `row ${i}` })) }) const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)) type Grist = HTMLElement & { fetch(): Promise; data: { total: number }; updateComplete: Promise } describe('ox-grist with two fetches out at once', () => { /* Resizing inside a fixture trips Chrome's benign ResizeObserver loop error; it says nothing about fetching. */ 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 }) /* * The answer depends on the conditions, as on the ledger: the read with none, asked first, comes back first; the * month's comes back after it. The grid fetches once by itself when attached; the test starts after that settled. */ const MONTH = [{ name: 'month', operator: 'eq', value: 10 }] const byConditions = (failUnfiltered = false) => async ({ filters }: { filters?: unknown[] }) => { if (!filters?.length) { await wait(20) if (failUnfiltered) throw new Error('the unfiltered read failed') return answer(2908) } await wait(300) return answer(4) } async function attached(handler: any) { const el = (await fixture(html``)) as Grist & { filters: unknown[] renderRoot: ShadowRoot } await wait(400) return el } it('shows the answer to the later fetch, though the earlier one came back first', async () => { const el = await attached(byConditions()) el.filters = [] const unfiltered = el.fetch() el.filters = MONTH const month = el.fetch() await Promise.all([unfiltered, month]) await el.updateComplete expect(el.data.total).to.equal(4) }) it('does not report the failure of a fetch a later one replaced', async () => { const el = await attached(byConditions(true)) el.filters = [] const unfiltered = el.fetch().then( () => 'kept quiet', () => 'reported' ) el.filters = MONTH await el.fetch() expect(await unfiltered).to.equal('kept quiet') await el.updateComplete expect(el.data.total).to.equal(4) /* The grid draws "could not load" from fetchFailed - a replaced read must not make it say so. */ const body = el.renderRoot.querySelector('#grist') as (HTMLElement & { renderRoot: ShadowRoot }) | null expect(!!body?.renderRoot.querySelector('ox-empty-note')).to.equal(false) }) it('still shows a single fetch as before', async () => { const el = (await fixture(html` answer(7)}>`)) as Grist await el.fetch() await el.updateComplete expect(el.data.total).to.equal(7) }) })