import { expect, fixture } from '@open-wc/testing' import { html } from 'lit' import '../src/data-grist.js' import { DataGrist } from '../src/data-grist.js' import { editsOf, editsSince, layEdits, mergeHeld, settleSent } from '../src/edit-rebase.js' import { GristRecord } from '../src/types.js' const edited = (id: string, before: Record, after: Record): GristRecord => ({ id, ...before, ...after, __dirty__: 'M', __dirtyfields__: Object.fromEntries(Object.keys(after).map(name => [name, { before: before[name], after: after[name] }])) }) describe('edits kept across a read (edit-rebase)', () => { it('keeps what was edited after a save was sent, and nothing that was sent', () => { const a = edited('a', { amount: 1 }, { amount: 2 }) const sent = editsOf([a]) /* While the save is out: A changed again in another field, B edited, a new row typed. */ a.memo = 'later' a.__dirtyfields__!.memo = { before: undefined, after: 'later' } const b = edited('b', { amount: 5 }, { amount: 6 }) const added: GristRecord = { title: 'new', __dirty__: '+' } const { edits, lost } = editsSince(editsOf([a, b, added]), sent) expect(lost).to.eql([]) expect(edits.map(one => [one.id, one.flag, one.values])).to.eql([ ['a', 'M', { memo: 'later' }], ['b', 'M', { amount: 6 }], [undefined, '+', { title: 'new' }] ]) }) it('drops a row whose sent edit was not touched again', () => { const a = edited('a', { amount: 1 }, { amount: 2 }) expect(editsSince(editsOf([a]), editsOf([a])).edits).to.eql([]) }) it('turns later edits of a saved new row into edits of the row the save made', () => { const added: GristRecord = { title: 'new', __dirty__: '+' } const sent = editsOf([added]) added.amount = 9 const { edits } = editsSince(editsOf([added]), sent, new Map([[added, 'n1']])) expect(edits).to.eql([{ id: 'n1', flag: 'M', values: { amount: 9 } }]) }) it('says a saved new row it cannot find again instead of adding it twice', () => { const added: GristRecord = { title: 'new', __dirty__: '+' } const sent = editsOf([added]) added.amount = 9 const { edits, lost } = editsSince(editsOf([added]), sent) expect(edits).to.eql([]) expect(lost.map(one => one.values)).to.eql([{ amount: 9 }]) }) it('lays edits on the rows read and returns the ones whose row is gone', () => { const records: GristRecord[] = [{ id: 'a', amount: 2 }] const unplaced = layEdits(records, [ { id: 'a', flag: 'M', values: { memo: 'later' } }, { id: 'gone', flag: 'M', values: { amount: 1 } }, { flag: '+', values: { title: 'new' } } ]) expect(records).to.eql([ { id: 'a', amount: 2, memo: 'later', __dirty__: 'M' }, { title: 'new', __dirty__: '+' } ]) expect(unplaced.map(one => one.id)).to.eql(['gone']) }) it('a grid read after a slow save keeps the edits typed meanwhile as edits', async () => { const server = [ { id: 'a', amount: 1 }, { id: 'b', amount: 5 } ] const grist = await fixture( html` ({ records: server.map(one => ({ ...one })), total: server.length })} >` ) await grist.fetch() await grist.updateComplete const rows = () => grist.dirtyData.records as GristRecord[] const set = (id: string, amount: number) => { const record = rows().find(one => one.id === id)! record.amount = amount record.__dirty__ = 'M' grist.checkDirties() } set('a', 2) const sent = editsOf(grist.dirtyRecords) set('b', 6) // typed while the save of A is out server[0].amount = 2 // the save of A lands const { edits } = editsSince(editsOf(grist.dirtyRecords), sent) await grist.fetchKeepingEdits(edits) expect(grist.dirtyRecords.map(one => [one.id, one.amount])).to.eql([['b', 6]]) expect(rows().find(one => one.id === 'a')!.amount).to.equal(2) expect(rows().find(one => one.id === 'a')!.__dirty__).to.equal(undefined) }) }) describe('edits kept across a read and the browser draft (ADR-0098)', () => { it('settles the draft of the edits before the save, and keeps the ones laid back', async () => { const actions: string[] = [] const grist = await fixture( html` ({ records: [{ id: 'a', amount: 2 }, { id: 'b', amount: 5 }], total: 2 })} @list-draft=${(event: CustomEvent) => actions.push(event.detail.action)} >` ) await grist.fetch() await grist.updateComplete actions.length = 0 await grist.fetchKeepingEdits([{ id: 'b', flag: 'M', values: { amount: 6 } }]) await grist.updateComplete expect(actions[0]).to.equal('discard') expect(actions.indexOf('loaded')).to.be.greaterThan(0) expect(actions.lastIndexOf('edit')).to.be.greaterThan(actions.indexOf('loaded')) }) }) describe('a read that fails after a save', () => { it('leaves the edits on the screen, keeps them in the browser again and says the failure', async () => { const actions: string[] = [] let fail = false const grist = await fixture( html` { if (fail) throw new Error('offline') return { records: [{ id: 'a', amount: 2 }], total: 1 } }} @list-draft=${(event: CustomEvent) => actions.push(event.detail.action)} >` ) await grist.fetch() await grist.updateComplete const record = (grist.dirtyData.records as GristRecord[])[0] record.amount = 7 record.__dirty__ = 'M' grist.checkDirties() actions.length = 0 fail = true let said: unknown await grist.fetchKeepingEdits(editsOf(grist.dirtyRecords)).catch(error => (said = error)) expect(String(said)).to.contain('offline') expect(grist.dirtyRecords.map(one => [one.id, one.amount])).to.eql([['a', 7]]) expect(actions[0]).to.equal('discard') expect(actions[actions.length - 1]).to.equal('edit') }) }) describe('a save that landed when the list cannot be read again (settleSent)', () => { it('marks what was sent as saved, keeps what was typed after it, and gives a saved new row its id', async () => { const grist = await fixture( html` ({ records: [{ id: 'a', amount: 1, memo: '' }], total: 1 })} >` ) await grist.fetch() await grist.updateComplete const [a] = grist.dirtyData.records as GristRecord[] a.amount = 2 a.__dirty__ = 'M' const added = { amount: 9, __dirty__: '+' } as GristRecord ;(grist.dirtyData.records as GristRecord[]).push(added) grist.checkDirties() const sent = editsOf(grist.dirtyRecords) a.memo = 'later' // typed while the save was out grist.checkDirties() settleSent(sent, new Map([[added, 'n1']])) grist.checkDirties() expect(grist.dirtyRecords.map(one => [one.id, Object.keys(one.__dirtyfields__ ?? {})])).to.eql([['a', ['memo']]]) expect(added.id).to.equal('n1') }) }) describe('held entries joined with the edits made since (mergeHeld)', () => { it('never lays an older held value over a newer input; the held value stays held', () => { const current = [{ id: 'a', flag: 'M' as const, values: { memo: 'newer input' } }] const held = [ { id: 'a', flag: 'M' as const, values: { memo: 'older held input', amount: 5 } }, { id: 'b', flag: 'M' as const, values: { memo: 'only held' } } ] const { edits, kept } = mergeHeld(current, held) const records: GristRecord[] = [{ id: 'a', memo: '', amount: 1 }, { id: 'b', memo: '' }] expect(layEdits(records, edits)).to.eql([]) expect(records.map(one => [one.id, one.memo, one.amount])).to.eql([['a', 'newer input', 5], ['b', 'only held', undefined]]) expect(kept).to.eql([{ id: 'a', flag: 'M', values: { memo: 'older held input' } }]) expect(current[0].values).to.eql({ memo: 'newer input' }, 'the edits passed in are not changed') }) it('takes a held value equal to the current one as the same entry', () => { const { kept } = mergeHeld([{ id: 'a', flag: 'M', values: { memo: 'x' } }], [{ id: 'a', flag: 'M', values: { memo: 'x' } }]) expect(kept).to.eql([]) }) })