/* 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 '../src/record-view/record-view.js' import { DataGrist } from '../src/data-grist.js' import { OxGristEditor } from '../src/editors/ox-grist-editor.js' import { RecordDelete } from '../src/handlers/record-delete.js' import { failOnPageErrors } from './page-errors.js' /* * An edit waits while its column's validation runs (a server check, say). What happens to the rows meanwhile decides * whether it still applies: selecting, refreshing and adding rows keep the row it was typed into, so it applies; * deleting that row, undo and a reset replace it, so it does not. Typing the shown value again supersedes it. The * edits here go through the real editors - typed into the input and committed the way leaving the cell commits. */ const deferred = () => { let resolve!: (value: boolean) => void const promise = new Promise(done => (resolve = done)) return { promise, resolve } } const settle = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms)) /* The title column waits for the validation the test hands it; `other` has none. */ let validating: Promise | undefined const columns = [ { type: 'gutter', gutterName: 'row-selector', multiple: true }, { type: 'string', name: 'title', header: 'title', record: { editable: true }, validation: () => validating ?? true }, { type: 'string', name: 'other', header: 'other', record: { editable: true } } ] const mount = async (mode: 'GRID' | 'LIST' | 'CARD', records: any[]) => { const el = await fixture(html``) await idle(el) return el } const rows = (el: DataGrist) => el.grist.data.records /* `updateComplete` resolves false while an update set another one going (a list takes its records in `updated`). */ const idle = async (el: DataGrist) => { while (!((await el.updateComplete) && (await el.grist.updateComplete))); } /* Type into the editor of a field and commit it as leaving the cell does. */ const typeInto = async (editor: OxGristEditor, text: string) => { await editor.updateComplete const input = editor.renderRoot.querySelector('input') as HTMLInputElement input.value = text input.dispatchEvent(new Event('input', { bubbles: true, composed: true })) input.dispatchEvent(new Event('change', { bubbles: true, composed: true })) editor.flush() } /* A grid cell: open its editor the way a double-click does, type, commit, close. */ const typeInGrid = async (el: DataGrist, row: number, name: string, text: string) => { await idle(el) const body = el.grist.renderRoot.querySelector('ox-grid-body') as any const column = body.columns.filter((column: any) => !column.hidden).findIndex((column: any) => column.name === name) body.startEditTarget(row, column) await body.updateComplete const field = body.renderRoot.querySelector(`#cell-${row}-${column}`) as any await field.updateComplete await typeInto(field.renderRoot.querySelector('*') as OxGristEditor, text) body.resetEdit() await body.updateComplete } /* * A list item or card: its record view, as `RecordViewHandler` opens it for that item and hands its changes to the * item. Built here in place of the popup; it is the item's own child, so its changes reach the list the same way. */ const typeInItem = async (el: DataGrist, index: number, name: string, text: string) => { /* Anything may draw the list again before the next edit (a page attached on scroll, a selection). */ el.grist.requestUpdate() await idle(el) const items = el.grist.renderRoot.querySelectorAll('ox-record-partial, ox-record-card') const item = items[index] as any const view = document.createElement('ox-record-view') as any Object.assign(view, { columns: el.compiledConfig.columns, record: item.record, rowIndex: item.rowIndex }) item.appendChild(view) await view.updateComplete const body = view.renderRoot.querySelector('ox-record-view-body') as any await body.updateComplete const field = Array.from(body.renderRoot.querySelectorAll('ox-grid-field') as NodeListOf).find( one => one.column?.name === name ) field.editing = true await field.updateComplete await typeInto(field.renderRoot.querySelector('*') as OxGristEditor, text) view.remove() } describe('an edit waiting for validation', () => { failOnPageErrors() afterEach(() => { validating = undefined }) it('applies after another field of the row was edited meanwhile', async () => { const el = await mount('GRID', [{ id: '1', title: 'A', other: 'x' }]) const pending = deferred() validating = pending.promise await typeInGrid(el, 0, 'title', 'checked') validating = undefined await typeInGrid(el, 0, 'other', 'y') pending.resolve(true) await settle() expect(rows(el)[0]).to.include({ title: 'checked', other: 'y' }) }) it('applies after rows were selected, which rebuilds the rows around the same records', async () => { const el = await mount('GRID', [ { id: '1', title: 'A' }, { id: '2', title: 'B' } ]) const pending = deferred() validating = pending.promise await typeInGrid(el, 0, 'title', 'checked') const before = rows(el) el.select(record => record.id === '2') await idle(el) expect(rows(el) !== before).to.equal(true) pending.resolve(true) await settle() expect(rows(el)[0].title).to.equal('checked') expect(rows(el)[1].title).to.equal('B') }) it('applies to its row after a row was added on top', async () => { const el = await mount('GRID', [{ id: '1', title: 'A' }]) const pending = deferred() validating = pending.promise await typeInGrid(el, 0, 'title', 'checked') el.addRecordToTop({ title: 'top' }) await idle(el) pending.resolve(true) await settle() expect(rows(el).map(record => record.title)).to.eql(['top', 'checked']) }) it('is superseded by typing the shown value again', async () => { const el = await mount('GRID', [{ id: '1', title: 'A' }]) const pending = deferred() validating = pending.promise await typeInGrid(el, 0, 'title', 'mistake') validating = undefined await typeInGrid(el, 0, 'title', 'A') pending.resolve(true) await settle() expect(rows(el)[0].title).to.equal('A') expect(el.dirtyRecords).to.have.length(0) }) it('does not apply to a row deleted meanwhile', async () => { const el = await mount('GRID', [ { id: '1', title: 'A' }, { id: '2', title: 'B' } ]) const pending = deferred() validating = pending.promise await typeInGrid(el, 1, 'title', 'checked') const record = rows(el)[1] const field = (el.grist.renderRoot.querySelector('ox-grid-body') as any).renderRoot.querySelector('#cell-1-1') RecordDelete(el.compiledConfig.columns, el.grist.data, field.column, record, 1, field) pending.resolve(true) await settle() expect(rows(el).map(record => record.title)).to.eql(['A']) expect(record.title).to.equal('B') }) it('does not apply after undo, and an edit after undo does', async () => { const el = await mount('GRID', [{ id: '1', title: 'A' }]) /* Snapshots for undo are taken at most once a second, by one timer for every grid on the page. */ await settle(1100) await typeInGrid(el, 0, 'other', 'first') const pending = deferred() validating = pending.promise await typeInGrid(el, 0, 'title', 'checked') el.undo() await idle(el) pending.resolve(true) await settle() expect(rows(el)[0]).to.include({ title: 'A' }) expect(rows(el)[0].other).to.equal(undefined) validating = undefined await typeInGrid(el, 0, 'title', 'after undo') expect(rows(el)[0].title).to.equal('after undo') }).timeout(5000) it('does not apply after a reset', async () => { const el = await mount('GRID', [{ id: '1', title: 'A' }]) const pending = deferred() validating = pending.promise await typeInGrid(el, 1, 'title', 'new row') el.reset() await idle(el) pending.resolve(true) await settle() expect(rows(el).map(record => record.title)).to.eql(['A']) }) }) /* The owner replacing the rows: a new row still being validated was typed into rows that are gone. */ describe('a new row waiting for validation', () => { failOnPageErrors() afterEach(() => { validating = undefined }) /* [name, what comes before the edit, the replacement] */ const replacements: [string, (el: DataGrist) => void, (el: DataGrist) => void][] = [ ['undo', () => {}, el => el.undo()], ['redo', el => el.undo(), el => el.redo()], ['a restored draft', () => {}, el => el.restoreUnsavedDraft({ records: [{ id: '1', title: 'draft' }] }, null)] ] for (const [name, before, replace] of replacements) { it(`is not added after ${name}`, async () => { const el = await mount('GRID', [{ id: '1', title: 'A' }]) /* Snapshots for undo are taken at most once a second, by one timer for every grid on the page. */ await settle(1100) await typeInGrid(el, 0, 'other', 'first') before(el) await idle(el) const pending = deferred() validating = pending.promise await typeInGrid(el, 1, 'title', 'new row') replace(el) await idle(el) pending.resolve(true) await settle() expect(rows(el)).to.have.length(1) }).timeout(5000) } }) for (const mode of ['GRID', 'LIST', 'CARD'] as const) { describe(`the append row of a ${mode}`, () => { failOnPageErrors() afterEach(() => { validating = undefined }) const type = (el: DataGrist, row: number, name: string, text: string) => mode === 'GRID' ? typeInGrid(el, row, name, text) : typeInItem(el, row, name, text) it('takes a field still being validated and one typed meanwhile into one new row', async () => { const el = await mount(mode, [{ id: '1', title: 'A' }]) const pending = deferred() validating = pending.promise await type(el, 1, 'title', 'new') validating = undefined await type(el, 1, 'other', 'meanwhile') pending.resolve(true) await settle() await idle(el) expect(rows(el)).to.have.length(2) expect(rows(el)[1]).to.include({ title: 'new', other: 'meanwhile', __dirty__: '+' }) expect(el.exportPatchList({})).to.eql([{ patchFlag: '+', title: 'new', other: 'meanwhile' }]) }) it('offers a fresh empty row once its row was added', async () => { const el = await mount(mode, []) await type(el, 0, 'title', 'first') await idle(el) await type(el, 1, 'title', 'second') expect(rows(el).map(record => record.title)).to.eql(['first', 'second']) }) it('is not added again when its new row was deleted while a field waited', async () => { const el = await mount(mode, []) const pending = deferred() validating = pending.promise await type(el, 0, 'title', 'waits') validating = undefined await type(el, 0, 'other', 'adds the row') await idle(el) /* Resetting a new row removes it (`record-reset` of a row that has no origin). */ el.grist.onRecordChanged(undefined as any, 0, null) pending.resolve(true) await settle() expect(rows(el)).to.have.length(0) }) }) }