/* 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 failed fetch is not an empty list. * * The provider wrapped the fetch in try/finally with no catch, so a thrown fetch hid the spinner and * nothing else: with no rows the grid said "nothing to show", with rows from before it kept them as if * they were the answer. Both are a confident wrong statement about data the grid never saw. */ type Rec = { id: string; name: string } const config = { columns: [ { type: 'string', name: 'id', hidden: true }, { type: 'string', name: 'name', header: 'name' } ], pagination: { pages: [20] } } const settle = (ms = 150) => new Promise(resolve => setTimeout(resolve, ms)) async function grist(mode: 'GRID' | 'LIST' | 'CARD', handler: () => Promise) { const el = (await fixture(html` `)) as HTMLElement & { fetch(): Promise; updateComplete: Promise; renderRoot: ShadowRoot } await el.fetch().catch(() => {}) await settle() await el.updateComplete return el } function emptyNote(el: HTMLElement & { renderRoot: ShadowRoot }): HTMLElement | null { const body = el.renderRoot.querySelector('#grist') as (HTMLElement & { renderRoot: ShadowRoot }) | null /* Every mode draws the note in its own shadow root - GRID on the grid host, not in its body. */ return (body?.renderRoot.querySelector('ox-empty-note') as HTMLElement | null) ?? null } describe('ox-grist when the fetch fails', () => { /* * Resizing the grid body inside a fixture trips Chrome's benign "ResizeObserver loop completed" * error, which mocha reports as a failure. It says nothing about fetch failures; keep it out. */ 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 }) for (const mode of ['GRID', 'LIST', 'CARD'] as const) { it(`${mode}: says it could not load, not that there is nothing, and retries`, async () => { let calls = 0 const el = await grist(mode, async () => { calls++ throw new Error('server down') }) const note = emptyNote(el) expect(note, 'a note is drawn').to.exist expect(note!.getAttribute('role')).to.equal('alert') expect(note!.getAttribute('title')).to.equal('could not load the list') const retry = note!.querySelector('md-text-button') as HTMLElement expect(retry, 'retry').to.exist const before = calls retry.click() await settle() expect(calls).to.equal(before + 1) }) } it('an empty answer still says empty, without an alert', async () => { const el = await grist('GRID', async () => ({ total: 0, records: [] as Rec[] })) const note = emptyNote(el) expect(note, 'a note is drawn').to.exist expect(note!.hasAttribute('role')).to.equal(false) expect(note!.getAttribute('title')).to.equal('nothing here yet.') }) it('GRID: the note stays in the visible grid when the columns are wider than it', async () => { const wide = { columns: [ { type: 'string', name: 'id', hidden: true }, { type: 'string', name: 'a', header: 'a', width: 600 }, { type: 'string', name: 'b', header: 'b', width: 600 } ], pagination: { pages: [20] } } const el = (await fixture(html` ({ total: 0, records: [] as Rec[] })}> `)) as HTMLElement & { fetch(): Promise; updateComplete: Promise; renderRoot: ShadowRoot } await el.fetch() await settle() await el.updateComplete const note = emptyNote(el) expect(note, 'a note is drawn').to.exist const box = el.getBoundingClientRect() const at = note!.getBoundingClientRect() /* Inside the 500px the person sees, not centred in the 1200px of columns. */ expect(at.left >= box.left && at.right <= box.right, `note ${at.left}-${at.right} within grid ${box.left}-${box.right}`).to.equal(true) }) it('rows from before stay, with a band saying they are not a fresh result; a good fetch clears it', async () => { let fail = false const el = await grist('GRID', async () => { if (fail) throw new Error('server down') return { total: 1, records: [{ id: '1', name: 'first' }] } }) expect(el.renderRoot.querySelector('ox-list-state') === null, 'absent').to.equal(true) fail = true await el.fetch().catch(() => {}) await settle() await el.updateComplete const state = el.renderRoot.querySelector('ox-list-state')! expect(state, 'failure band').to.exist await state.updateComplete expect(state.renderRoot.querySelector('[role="alert"]')?.textContent).to.contain('could not refresh') fail = false await el.fetch() await settle() await el.updateComplete expect(el.renderRoot.querySelector('ox-list-state') === null, 'absent').to.equal(true) }) it('the error still reaches whoever awaited the fetch', async () => { const el = (await fixture(html` { throw new Error('server down') }}> `)) as HTMLElement & { fetch(): Promise } let caught: unknown await el.fetch().catch(e => (caught = e)) expect((caught as Error)?.message).to.equal('server down') }) })