import { expect, fixture } from '@open-wc/testing' import { html } from 'lit' import '../src/data-grist.js' import { DataGrist } from '../src/data-grist.js' import { DataGridBody } from '../src/data-grid/data-grid-body.js' import { OxGristEditorNumber } from '../src/editors/ox-grist-editor-number.js' import { failOnPageErrors } from './page-errors.js' /* The clipboard holds `text` as `mime` while `run` reads it. */ async function withClipboard(mime: string, text: string, run: () => Promise): Promise { const previous = Object.getOwnPropertyDescriptor(navigator, 'clipboard') Object.defineProperty(navigator, 'clipboard', { configurable: true, value: { read: async () => [{ getType: async (requested: string) => { if (requested !== mime) throw Error('no type') return new Blob([text], { type: mime }) } }] } }) try { return await run() } finally { if (previous) Object.defineProperty(navigator, 'clipboard', previous) else delete (navigator as any).clipboard } } /* What a paste sends, read off a body that only records its field changes. */ async function paste(mime: string, text: string, type = 'number', options: any = {}) { const changes: any[] = [] const body: any = { focused: { row: options.row ?? 0, column: 0 }, data: { records: options.records || [{ id: 'a', value: 1 }] }, columns: [{ name: 'value', type, record: { editable: options.editable ?? true } }], config: { rows: { appendable: options.appendable ?? false } }, appendRecord: { __dirty__: '+' }, pasteTargets: (DataGridBody.prototype as any).pasteTargets, resetEdit() {}, focus() {}, dispatchEvent(e: CustomEvent) { changes.push(e.detail) } } await withClipboard(mime, text, () => DataGridBody.prototype.paste.call(body)) return { body, changes } } const source = (mime: string, values: string[]) => mime === 'text/html' ? `${values.map(value => ``).join('')}
${value}
` : values.join('\n') describe('paste and input data contracts', () => { for (const mime of ['text/html', 'text/plain']) { for (const [type, input, expected] of [ ['number', 'abc', 'abc'], ['number', '1,200', '1,200'], ['number', ' ', null], ['number', 'Infinity', 'Infinity'], ['number', '12.5', 12.5], ['integer', '12.5', 12], ['integer', '-1.5', -2], ['boolean', 'false', false], ['checkbox', 'true', true], ['number', '0', 0] ] as const) { it(`${mime} parses ${type} ${input}`, async () => { const { changes } = await paste(mime, source(mime, [input]), type) expect(changes[0].after).to.equal(expected) }) } /* A blank cell clears an existing row's field and leaves a new row's to its default; id 0 is an existing row. */ for (const [type, input, records, row, expected] of [ ['number', ' ', [{ id: 0, value: 1 }], 0, null], ['string', ' ', [{ id: 'a', value: 'x' }], 1, undefined], ['number', ' ', [{ id: 'a', value: 1 }], 1, undefined], ['select', ' ', [{ id: 'a', value: 1 }], 1, undefined] ] as const) { it(`${mime} pastes a blank ${type} into ${row ? 'a new' : 'an existing'} row as ${expected}`, async () => { const { changes } = await paste(mime, source(mime, [input]), type, { records, row, appendable: true }) expect(changes).to.have.length(1) expect(changes[0].after).to.equal(expected) }) } it(`${mime} passes the destination row to editable`, async () => { const seen: number[] = [] await paste(mime, source(mime, ['1', '2']), 'number', { records: [{ id: 'a' }, { id: 'b' }], editable: (_v: any, _c: any, _r: any, row: number) => { seen.push(row); return true } }) expect(seen).to.eql([0, 1]) }) } it('integer IME input uses the same conversion as the number editor', () => { let after: any const column: any = { name: 'value', type: 'integer', record: { editable: true } } const body: any = { _imeBuffer: { value: '12.5' }, _resetIMEBuffer() {}, columns: [column], data: { records: [{ value: 1 }] }, dispatchEvent(e: CustomEvent) { after = e.detail.after } } DataGridBody.prototype._commitIMEBuffer.call(body, 0, 0) const expected = OxGristEditorNumber.prototype.formatFromEditor.call({ column } as any, '12.5') expect(after).to.equal(expected) }) }) /* Pasting into a rendered grid: the rows that end up in the grid, through its own field-change handling. */ describe('paste into a grid', () => { failOnPageErrors() const mount = async (options: { appendable: boolean; editable?: boolean; validation?: () => Promise }) => { const el = await fixture(html``) await el.updateComplete await el.grist.updateComplete return el } const bodyOf = (el: DataGrist) => el.grist.renderRoot.querySelector('ox-grid-body') as any const pasteAt = async (el: DataGrist, row: number, mime: string, text: string, column = 0) => { const body = bodyOf(el) body.focused = { row, column } await withClipboard(mime, text, () => body.paste()) await el.updateComplete } const table = (mime: string, rows: string[][]) => mime === 'text/html' ? `${rows.map(cells => `${cells.map(cell => ``).join('')}`).join('')}
${cell}
` : rows.map(cells => cells.join('\t')).join('\n') const rows = (el: DataGrist) => el.grist.data.records.map(({ amount, memo }) => ({ amount, memo })) for (const mime of ['text/html', 'text/plain']) { it(`${mime} stops at the last row when rows cannot be appended`, async () => { const el = await mount({ appendable: false }) await pasteAt(el, 0, mime, table(mime, [['2', 'x'], ['3', 'y']])) expect(rows(el)).to.eql([{ amount: 2, memo: 'x' }]) }) it(`${mime} appends new rows in order`, async () => { const el = await mount({ appendable: true }) await pasteAt(el, 1, mime, table(mime, [['2', 'x'], ['3', 'y']])) expect(rows(el)).to.eql([{ amount: 1, memo: 'kept' }, { amount: 2, memo: 'x' }, { amount: 3, memo: 'y' }]) }) it(`${mime} adds no row for read-only cells`, async () => { const el = await mount({ appendable: true, editable: false }) await pasteAt(el, 1, mime, table(mime, [['2', 'x']])) expect(el.grist.data.records).to.have.length(1) }) it(`${mime} adds no row for a blank new row`, async () => { const el = await mount({ appendable: true }) await pasteAt(el, 1, mime, table(mime, [[' ', ' '], ['4', 'z']])) expect(rows(el)).to.eql([{ amount: 1, memo: 'kept' }, { amount: 4, memo: 'z' }]) expect(el.dirtyRecords).to.have.length(1) }) it(`${mime} keeps a new row's cells together while one is being validated`, async () => { let resolve!: (valid: boolean) => void const el = await mount({ appendable: true, validation: () => new Promise(done => (resolve = done)) }) await pasteAt(el, 1, mime, table(mime, [['5', 'w']])) resolve(true) await new Promise(done => setTimeout(done)) expect(rows(el)).to.eql([{ amount: 1, memo: 'kept' }, { amount: 5, memo: 'w' }]) }) it(`${mime} into the append row and a key typed into it while the paste is validated make one row`, async () => { let resolve!: (valid: boolean) => void const el = await mount({ appendable: true, validation: () => new Promise(done => (resolve = done)) }) await pasteAt(el, 1, mime, table(mime, [['5']])) const body = bodyOf(el) body._imeBuffer.value = 'typed' body._commitIMEBuffer(1, 1) resolve(true) await new Promise(done => setTimeout(done)) expect(rows(el)).to.eql([{ amount: 1, memo: 'kept' }, { amount: 5, memo: 'typed' }]) }) } })