/* 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/editors/ox-grist-editor-select.js'
import '../src/editors/ox-grist-editor-text.js'
import { buildColumn } from '../src/configure/column-builder.js'
import { OxGristEditor } from '../src/editors/ox-grist-editor.js'
/*
* An editor commits, on blur or flush, only a value the user entered into its control since it opened. Leaving a
* page flushes every mounted editor: a popup editor's
was read as `undefined` and cleared its cell, and an
* untouched select re-parsed '1' for 1 (or took its first option) and marked the row changed (2026-10-19).
*/
/* A popup editor as most of them are: the cell shows the value in a
, the popup's confirmation commits. */
class ProbeDivEditor extends OxGristEditor {
get editorTemplate() {
return html`
${this.value}
`
}
}
customElements.define('probe-div-editor', ProbeDivEditor)
const mount = async
(tag: string, column: any, record: any) => {
const editor = (await fixture(
html`${Object.assign(document.createElement(tag), { column: buildColumn(column), record, row: 0 })}`
)) as T
await editor.updateComplete
const changes: any[] = []
editor.addEventListener('field-change', event => changes.push((event as CustomEvent).detail))
return { editor, changes }
}
describe('what an editor commits', () => {
it('a popup editor nobody typed into commits nothing on flush', async () => {
const { editor, changes } = await mount(
'probe-div-editor',
{ type: 'string', name: 'crontab', record: { editable: true } },
{ crontab: '0 * * * *' }
)
editor.flush()
expect(changes.length).to.equal(0)
})
it('an untouched select with number values commits nothing on flush', async () => {
const { editor, changes } = await mount(
'ox-grist-editor-select',
{ type: 'select', name: 'grade', record: { editable: true, options: [{ display: 'one', value: 1 }, { display: 'two', value: 2 }] } },
{ grade: 1 }
)
editor.flush()
expect(changes.length).to.equal(0)
})
it('an untouched select whose value is not among its options commits nothing on flush', async () => {
const { editor, changes } = await mount(
'ox-grist-editor-select',
{ type: 'select', name: 'grade', record: { editable: true, options: ['a', 'b'] } },
{ grade: 'retired' }
)
editor.flush()
expect(changes.length).to.equal(0)
})
it('leaving an untouched select commits nothing', async () => {
const { editor, changes } = await mount(
'ox-grist-editor-select',
{ type: 'select', name: 'grade', record: { editable: true, options: [{ display: 'one', value: 1 }, { display: 'two', value: 2 }] } },
{ grade: 1 }
)
editor._onfocusout()
expect(changes.length).to.equal(0)
})
it('text typed and not yet committed is committed on flush', async () => {
const { editor, changes } = await mount(
'ox-grist-editor-text',
{ type: 'string', name: 'title', record: { editable: true } },
{ title: 'before' }
)
const input = editor.renderRoot.querySelector('input') as HTMLInputElement
input.value = 'after'
input.dispatchEvent(new Event('input', { bubbles: true, composed: true }))
editor.flush()
expect(changes.length).to.equal(1)
expect(changes[0].after).to.equal('after')
})
it('a select the user changed is committed on flush', async () => {
const { editor, changes } = await mount(
'ox-grist-editor-select',
{ type: 'select', name: 'grade', record: { editable: true, options: ['a', 'b'] } },
{ grade: 'a' }
)
const select = editor.renderRoot.querySelector('select') as HTMLSelectElement
select.value = 'b'
select.dispatchEvent(new Event('input', { bubbles: true, composed: true }))
select.dispatchEvent(new Event('change', { bubbles: true, composed: true }))
editor.flush()
expect(changes.length).to.equal(1)
expect(changes[0].after).to.equal('b')
})
})