import { expect, fixture } from '@open-wc/testing' import { html } from 'lit' import '../src/data-grist.js' import { DataGrist } from '../src/data-grist.js' /** * FOOTER-BUTTON-REVIEW G04 G05 G07 G03: showing the empty row, adding rows by a paste and starting a row are apart; a * row the grid adds starts from the screen's draft; a paste that cannot add its rows says so; a fixed column stays first. */ async function withClipboard(text: string, run: () => Promise): Promise { const previous = Object.getOwnPropertyDescriptor(navigator, 'clipboard') Object.defineProperty(navigator, 'clipboard', { configurable: true, value: { read: async () => [{ getType: async (mime: string) => { if (mime !== 'text/plain') 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 } } const until = async (check: () => boolean, ms = 2000) => { const end = Date.now() + ms while (!check()) { if (Date.now() > end) throw Error('timed out') await new Promise(resolve => setTimeout(resolve, 20)) } } async function gristOf(rows: Record, records = [{ id: 'a', name: 'A', kind: 'X' }]) { const grist = await fixture(html``) await until(() => !!body(grist)) await grist.updateComplete return grist } const body = (grist: DataGrist) => (grist.renderRoot.querySelector('ox-grid') as any)?.renderRoot?.querySelector('ox-grid-body') as any const shownRows = (grist: DataGrist) => body(grist).renderRoot.querySelectorAll('ox-grid-field[data-column="name"], ox-grid-field').length async function pasteAt(grist: DataGrist, row: number, text: string) { const grid = body(grist) grid.focused = { row, column: 1 } await withClipboard(text, () => grid.paste()) await new Promise(resolve => setTimeout(resolve, 50)) await grist.updateComplete } describe('행 추가·붙여넣기 정책 (G04 G05 G07)', () => { it('빈 행을 보이지 않아도 붙여넣기는 행을 늘린다 — 늘어난 행은 화면의 초안 값으로 시작한다', async () => { const grist = await gristOf({ appendable: false, pasteAppendable: true, newRecord: () => ({ kind: 'DRAFT' }) }) expect(body(grist).config.rows.appendable).to.equal(false) await pasteAt(grist, 0, 'one\ntwo\nthree') const records = grist.dirtyData.records expect(records.map((one: any) => one.name)).to.eql(['one', 'two', 'three']) expect(records.slice(1).map((one: any) => [one.kind, one.__dirty__])).to.eql([['DRAFT', '+'], ['DRAFT', '+']]) }) it('행을 늘릴 수 없으면 잘린 행 수를 알린다 — 조용히 버리지 않는다', async () => { const grist = await gristOf({ appendable: false, pasteAppendable: false }) let detail: any grist.addEventListener('paste-rows-dropped', (e: Event) => (detail = (e as CustomEvent).detail)) await pasteAt(grist, 0, 'one\ntwo\nthree') expect(grist.dirtyData.records.map((one: any) => one.name)).to.eql(['one']) expect(detail).to.eql({ rows: 3, applied: 1, dropped: 2 }) }) it('pasteAppendable 을 말하지 않은 화면은 예전처럼 appendable 하나로 정한다', async () => { const grist = await gristOf({ appendable: true }) expect(grist.compiledConfig.rows.pasteAppendable).to.equal(true) await pasteAt(grist, 0, 'one\ntwo') expect(grist.dirtyData.records.length).to.equal(2) }) it('행 추가는 맨 위에 초안 값으로 만들고 첫 입력 칸에 초점을 둔다', async () => { const grist = await gristOf({ appendable: false, newRecord: () => ({ kind: 'DRAFT' }) }) const added = grist.addNewRecord({ name: '' }) await new Promise(resolve => setTimeout(resolve, 50)) await grist.updateComplete expect(grist.dirtyData.records[0]).to.equal(added) expect([added.kind, added.__dirty__]).to.eql(['DRAFT', '+']) const focused = body(grist).focused expect(focused.row).to.equal(0) expect(grist.compiledConfig.columns[focused.column].name).to.equal('name') }) it('저장 전 새 행은 표시를 눌러 제거한다', async () => { const grist = await gristOf({ appendable: false }) const added = grist.addNewRecord({ name: 'temp' }) await new Promise(resolve => setTimeout(resolve, 50)) const grid = body(grist) grid.dispatchEvent(new CustomEvent('remove-new-record', { bubbles: true, composed: true, detail: { record: added, row: 0 } })) await grist.updateComplete expect(grist.dirtyData.records.map((one: any) => one.id)).to.eql(['a']) /* A saved row is never removed this way. */ const saved = grist.dirtyData.records[0] grid.dispatchEvent(new CustomEvent('remove-new-record', { bubbles: true, composed: true, detail: { record: saved, row: 0 } })) expect(grist.dirtyData.records.length).to.equal(1) expect(shownRows(grist)).to.be.greaterThan(0) }) }) describe('개인 컬럼 설정과 고정 열 (G03)', () => { const columns = () => [ { type: 'gutter', gutterName: 'sequence', fixed: true }, { type: 'string', name: 'name', header: 'name', fixed: true, width: 100 }, { type: 'string', name: 'amount', header: 'amount', width: 100 }, { type: 'string', name: 'memo', header: 'memo', width: 100 } ] async function withPersonal(config: Record, personal: Record) { const grist = await fixture(html``) grist.personalConfig = personal await grist.updateComplete await new Promise(resolve => setTimeout(resolve, 20)) return grist.compiledConfig.columns.map(one => `${one.name ?? one.gutterName}${one.fixed ? '*' : ''}${one.hidden ? '(h)' : ''}:${one.width}`) } it('같은 배치에서는 개인 순서를 따르되 화면이 고정한 열은 앞에 남는다', async () => { const shown = await withPersonal( { columns: columns(), columnsVersion: 'v2' }, { columnsVersion: 'v2', columns: [{ name: 'memo', width: 150 }, { name: 'amount' }, { name: 'name' }] } ) expect(shown.slice(1)).to.eql(['name*:100', 'memo:150', 'amount:100']) expect(shown[0].startsWith('__seq__*')).to.equal(true) }) it('배치가 바뀐 화면에서는 폭과 숨김만 이어받고 옛 순서는 버린다', async () => { const shown = await withPersonal( { columns: columns(), columnsVersion: 'v2' }, { columns: [{ name: 'memo', width: 150, hidden: true }, { name: 'amount' }, { name: 'name' }] } ) expect(shown.slice(1)).to.eql(['name*:100', 'amount:100', 'memo(h):150']) }) }) describe('편집을 지키며 다시 읽기 — 이어 읽는 목록 (G06)', () => { it('무한 스크롤 목록은 처음부터 다시 읽어 행이 겹치지 않고, 편집은 다시 읽은 행에 남는다', async () => { let reads = 0 const grist = await fixture(html` { reads++ return { records: [{ id: 'a', name: 'A' }, { id: 'b', name: 'B' }], total: 2 } }} >`) await until(() => grist.dirtyData.records?.length === 2) const record = grist.dirtyData.records[0] const column = grist.compiledConfig.columns.find(one => one.name === 'name')! body(grist).dispatchEvent(new CustomEvent('field-change', { bubbles: true, composed: true, detail: { before: 'A', after: 'A2', column, record, row: 0 } })) await until(() => grist.dirtyRecords.length === 1) const { editsOf } = await import('../src/edit-rebase.js') const unplaced = await grist.fetchKeepingEdits(editsOf(grist.dirtyRecords)) expect(unplaced).to.eql([]) expect(grist.dirtyData.records.map((one: any) => one.id)).to.eql(['a', 'b']) expect(grist.dirtyData.records[0].name).to.equal('A2') expect(grist.dirtyRecords.length).to.equal(1) expect(reads).to.be.greaterThan(1) }) })