/* 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 { buildColumn } from '../src/configure/column-builder.js' import { OxGristEditor } from '../src/editors/ox-grist-editor.js' /* * An empty cell of a select column (the new row at the bottom of an appendable grid) must take its first option. The * browser tells a change only when the picked option differs from the shown one, so the editor must not open showing * the first option as picked (2026-11-08, 물품 대장 「종류」: 「처음에 "노트북.PC"를 선택해도 변화 이벤트가 발생하지 않아서」). */ const KINDS = [ { display: '노트북.PC', value: 'COMPUTER' }, { display: '모니터', value: 'MONITOR' } ] const mount = async (record: any, mandatory = true) => { const editor = (await fixture( html`${Object.assign(document.createElement('ox-grist-editor-select'), { column: buildColumn({ type: 'select', name: 'kind', record: { editable: true, mandatory, options: KINDS } }), record, row: 0 })}` )) as OxGristEditor await editor.updateComplete const changes: any[] = [] editor.addEventListener('field-change', event => changes.push((event as CustomEvent).detail)) return { editor, changes, select: editor.renderRoot.querySelector('select') as HTMLSelectElement } } /* What the browser does on a pick: it tells input and change only when the picked option is not the shown one. */ const pick = (select: HTMLSelectElement, value: string) => { if (select.value === value) return select.value = value select.dispatchEvent(new Event('input', { bubbles: true, composed: true })) select.dispatchEvent(new Event('change', { bubbles: true, composed: true })) } describe('a select cell with no value', () => { for (const record of [{}, { kind: null }, { kind: '' }]) { it(`takes the first option when it is picked (${JSON.stringify(record)})`, async () => { const { editor, changes, select } = await mount(record) pick(select, 'COMPUTER') editor.flush() expect(changes.length).to.equal(1) expect(changes[0].after).to.equal('COMPUTER') }) } it('lists only the real options to pick from', async () => { const { select } = await mount({}) const pickable = Array.from(select.options).filter(option => !option.hidden) expect(pickable.map(option => option.value)).to.deep.equal(['COMPUTER', 'MONITOR']) }) it('an untouched empty cell commits nothing', async () => { const { editor, changes } = await mount({}) editor.flush() editor._onfocusout() expect(changes.length).to.equal(0) }) it('a cell with a value opens on that value, with no blank option', async () => { const { select } = await mount({ kind: 'MONITOR' }) expect(select.value).to.equal('MONITOR') expect(select.options.length).to.equal(2) }) })