@operato/data-grist 10.29.51 → 10.29.56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/src/data-card/data-card.js +5 -5
  3. package/dist/src/data-card/data-card.js.map +1 -1
  4. package/dist/src/data-grid/data-grid-body.d.ts +8 -0
  5. package/dist/src/data-grid/data-grid-body.js +44 -27
  6. package/dist/src/data-grid/data-grid-body.js.map +1 -1
  7. package/dist/src/data-grid/data-grid.d.ts +1 -1
  8. package/dist/src/data-grid/data-grid.js +3 -2
  9. package/dist/src/data-grid/data-grid.js.map +1 -1
  10. package/dist/src/data-grist.js +17 -17
  11. package/dist/src/data-grist.js.map +1 -1
  12. package/dist/src/data-list/data-list.js +5 -5
  13. package/dist/src/data-list/data-list.js.map +1 -1
  14. package/dist/src/data-manipulator.d.ts +16 -1
  15. package/dist/src/data-manipulator.js +57 -14
  16. package/dist/src/data-manipulator.js.map +1 -1
  17. package/dist/src/editors/ox-grist-editor-number.js +2 -7
  18. package/dist/src/editors/ox-grist-editor-number.js.map +1 -1
  19. package/dist/src/editors/ox-grist-editor-select.d.ts +14 -0
  20. package/dist/src/editors/ox-grist-editor-select.js +71 -12
  21. package/dist/src/editors/ox-grist-editor-select.js.map +1 -1
  22. package/dist/src/editors/ox-grist-editor.js +15 -13
  23. package/dist/src/editors/ox-grist-editor.js.map +1 -1
  24. package/dist/src/types.d.ts +5 -0
  25. package/dist/src/types.js.map +1 -1
  26. package/dist/src/utils/field-value.d.ts +11 -0
  27. package/dist/src/utils/field-value.js +38 -0
  28. package/dist/src/utils/field-value.js.map +1 -0
  29. package/dist/test/data-integrity.test.d.ts +1 -0
  30. package/dist/test/data-integrity.test.js +145 -0
  31. package/dist/test/data-integrity.test.js.map +1 -0
  32. package/dist/test/edit-lifecycle.test.d.ts +2 -0
  33. package/dist/test/edit-lifecycle.test.js +270 -0
  34. package/dist/test/edit-lifecycle.test.js.map +1 -0
  35. package/dist/test/list-records.test.d.ts +1 -0
  36. package/dist/test/list-records.test.js +46 -0
  37. package/dist/test/list-records.test.js.map +1 -0
  38. package/dist/test/page-errors.d.ts +1 -0
  39. package/dist/test/page-errors.js +44 -0
  40. package/dist/test/page-errors.js.map +1 -0
  41. package/dist/test/paste-integrity.test.d.ts +1 -0
  42. package/dist/test/paste-integrity.test.js +163 -0
  43. package/dist/test/paste-integrity.test.js.map +1 -0
  44. package/dist/test/searchable-select.test.d.ts +1 -0
  45. package/dist/test/searchable-select.test.js +65 -0
  46. package/dist/test/searchable-select.test.js.map +1 -0
  47. package/dist/tsconfig.tsbuildinfo +1 -1
  48. package/package.json +5 -5
  49. package/test/data-integrity.test.ts +150 -0
  50. package/test/edit-lifecycle.test.ts +302 -0
  51. package/test/list-records.test.ts +58 -0
  52. package/test/page-errors.ts +43 -0
  53. package/test/paste-integrity.test.ts +168 -0
  54. package/test/searchable-select.test.ts +77 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@operato/data-grist",
3
- "version": "10.29.51",
3
+ "version": "10.29.56",
4
4
  "description": "User interface for grid (desktop) and list (mobile)",
5
5
  "author": "heartyoh",
6
6
  "type": "module",
@@ -78,10 +78,10 @@
78
78
  "@material/web": "^2.0.0",
79
79
  "@operato/headroom": "^10.29.1",
80
80
  "@operato/i18n": "^10.29.27",
81
- "@operato/input": "^10.29.49",
81
+ "@operato/input": "^10.29.52",
82
82
  "@operato/list-state": "^10.29.49",
83
- "@operato/p13n": "^10.29.50",
84
- "@operato/popup": "^10.29.49",
83
+ "@operato/p13n": "^10.29.52",
84
+ "@operato/popup": "^10.29.52",
85
85
  "@operato/pull-to-refresh": "^10.29.23",
86
86
  "@operato/styles": "^10.29.30",
87
87
  "@operato/time-calculator": "^10.29.1",
@@ -124,5 +124,5 @@
124
124
  "prettier --write"
125
125
  ]
126
126
  },
127
- "gitHead": "dc2cbe82ecb8fc65d0723f3b380edc4396dac87e"
127
+ "gitHead": "d14bb995740e1f0499213645a05fab0eee0ab865"
128
128
  }
@@ -0,0 +1,150 @@
1
+ import { expect, fixture } from '@open-wc/testing'
2
+ import { html } from 'lit'
3
+ import { DataGrist } from '../src/data-grist.js'
4
+ import { DataManipulator } from '../src/data-manipulator.js'
5
+ import { GristRecord, ColumnConfig } from '../src/types.js'
6
+ import { failOnPageErrors } from './page-errors.js'
7
+
8
+ class IntegrityManipulator extends DataManipulator {}
9
+ customElements.define('integrity-manipulator', IntegrityManipulator)
10
+
11
+ const dirty = (records: GristRecord[]) =>
12
+ Object.getOwnPropertyDescriptor(DataGrist.prototype, 'dirtyRecords')!.get!.call({ dirtyData: { records } })
13
+ const owner = (records: GristRecord[]) => {
14
+ const grid = document.createElement('integrity-manipulator') as DataManipulator
15
+ grid.data = { records }
16
+ return grid
17
+ }
18
+ const deferred = () => {
19
+ let resolve!: (value: boolean) => void
20
+ const promise = new Promise<boolean>(done => { resolve = done })
21
+ return { promise, resolve }
22
+ }
23
+ const change = (grid: DataManipulator, record: GristRecord, after: any, validation?: () => Promise<boolean>) =>
24
+ grid.onFieldChange({ before: record.title, after, record, row: 0, column: { name: 'title', validation } as ColumnConfig })
25
+
26
+ describe('data integrity contracts', () => {
27
+ for (const value of [0, 10, false, true, new Date('2026-01-01')]) {
28
+ it(`keeps a new row containing ${String(value)}`, () => {
29
+ expect(dirty([{ __dirty__: '+', value }])).to.have.length(1)
30
+ })
31
+ }
32
+ it('drops only empty new rows, not cleared edits or deletions', () => {
33
+ const records: GristRecord[] = [
34
+ { __dirty__: '+', value: ' ' }, { __dirty__: '+', value: null },
35
+ { __dirty__: '+', value: {} }, { __dirty__: '+', value: [] },
36
+ { __dirty__: '+', value: { id: 0 } },
37
+ { __dirty__: 'M', value: '' }, { __dirty__: '-', value: '' }
38
+ ]
39
+ expect(dirty(records)).to.eql(records.slice(4))
40
+ })
41
+ it('validates blank mandatory strings but accepts zero and false and skips deletions', () => {
42
+ const records = [
43
+ { __dirty__: 'M', value: '' }, { __dirty__: 'M', value: ' ' },
44
+ { __dirty__: 'M', value: null }, { __dirty__: 'M', value: undefined },
45
+ { __dirty__: 'M', value: 0 }, { __dirty__: 'M', value: false },
46
+ { __dirty__: '-', value: null }
47
+ ]
48
+ const results = DataGrist.prototype.checkDirtyRecordsValidity.call({
49
+ dirtyRecords: records, compiledConfig: { columns: [{ name: 'value', record: { mandatory: true } }] }
50
+ } as any)
51
+ expect(results.map(result => result.record)).to.eql(records.slice(0, 4))
52
+ })
53
+ it('reports the original before value while preserving the edited row identity', () => {
54
+ const record = { title: 'before' }
55
+ const grid = owner([record])
56
+ let detail: any
57
+ grid.addEventListener('record-change', e => { detail = (e as CustomEvent).detail })
58
+ grid.onRecordChanged({ title: 'after' }, 0, null)
59
+ expect(detail.before.title).to.equal('before')
60
+ expect(detail.after.title).to.equal('after')
61
+ expect(grid.data.records[0] === record).to.equal(true)
62
+ })
63
+ it('does not apply a validated edit to a replacement row', async () => {
64
+ const record = { title: 'A' }
65
+ const grid = owner([record])
66
+ const pending = deferred()
67
+ const editing = change(grid, record, 'edited A', () => pending.promise)
68
+ grid.data = { records: [{ title: 'B' }] }
69
+ pending.resolve(true)
70
+ await editing
71
+ expect(grid.data.records[0].title).to.equal('B')
72
+ })
73
+ it('does not apply an older validation after a later edit', async () => {
74
+ const record = { title: 'A' }
75
+ const grid = owner([record])
76
+ const pending = deferred()
77
+ const editing = change(grid, record, 'old edit', () => pending.promise)
78
+ await change(grid, record, 'latest edit')
79
+ pending.resolve(true)
80
+ await editing
81
+ expect(record.title).to.equal('latest edit')
82
+ })
83
+ it('returning to the current value invalidates a pending edit', async () => {
84
+ const record = { title: 'A' }
85
+ const grid = owner([record])
86
+ const pending = deferred()
87
+ const editing = change(grid, record, 'old edit', () => pending.promise)
88
+ await change(grid, record, 'A')
89
+ pending.resolve(true)
90
+ await editing
91
+ expect(record.title).to.equal('A')
92
+ })
93
+ it('follows the same record after an in-place reorder', async () => {
94
+ const record = { title: 'A' }, other = { title: 'B' }
95
+ const grid = owner([record, other])
96
+ const pending = deferred()
97
+ const editing = change(grid, record, 'edited A', () => pending.promise)
98
+ grid.data.records.reverse()
99
+ pending.resolve(true)
100
+ await editing
101
+ expect(other.title).to.equal('B')
102
+ expect(record.title).to.equal('edited A')
103
+ })
104
+ it('rejects an invalid edit and accepts a new append-row edit', async () => {
105
+ const record = { title: 'A' }
106
+ const grid = owner([record])
107
+ await change(grid, record, 'invalid', async () => false)
108
+ expect(record.title).to.equal('A')
109
+ const empty = owner([])
110
+ await change(empty, { __dirty__: '+' }, 'new')
111
+ expect(empty.data.records[0]).to.include({ title: 'new', __dirty__: '+' })
112
+ })
113
+ it('does not add back a deleted last row whose edit arrives late', async () => {
114
+ const record = { id: 'b', title: 'B' }
115
+ const grid = owner([{ id: 'a', title: 'A' }, record])
116
+ grid.data.records.splice(1, 1)
117
+ await grid.onFieldChange({ before: 'B', after: 'late', record, row: 1, column: { name: 'title' } as ColumnConfig })
118
+ expect(grid.data.records).to.have.length(1)
119
+ expect(record.title).to.equal('B')
120
+ })
121
+ it('drops a new row still being validated when the owner replaces the rows', async () => {
122
+ const grid = owner([])
123
+ const pending = deferred()
124
+ const editing = change(grid, grid.appendRecord, 'waits', () => pending.promise)
125
+ grid.discardPendingEdits()
126
+ pending.resolve(true)
127
+ await editing
128
+ expect(grid.data.records).to.have.length(0)
129
+ })
130
+ })
131
+
132
+ describe('rendered grid save patches', () => {
133
+ failOnPageErrors()
134
+ it('includes zero in a new row patch and removes it on reset', async () => {
135
+ const el = await fixture<DataGrist>(html`<ox-grist style="height:300px"
136
+ .config=${{ columns: [{ name: 'amount', type: 'number', header: 'amount', record: { editable: true } }], rows: { appendable: true } }}
137
+ .data=${{ records: [] }}></ox-grist>`)
138
+ await el.updateComplete
139
+ await el.grist.updateComplete
140
+ const column = el.compiledConfig.columns.find(c => c.name === 'amount')!
141
+ el.grist.dispatchEvent(new CustomEvent('field-change', { bubbles: true, composed: true,
142
+ detail: { before: undefined, after: 0, column, record: el.grist.appendRecord, row: 0 } }))
143
+ await el.updateComplete
144
+ expect(el.dirtyRecords).to.have.length(1)
145
+ expect(el.exportPatchList({})).to.eql([{ patchFlag: '+', amount: 0 }])
146
+ el.reset()
147
+ await el.updateComplete
148
+ expect(el.exportPatchList({})).to.eql([])
149
+ })
150
+ })
@@ -0,0 +1,302 @@
1
+ /* Elements are compared by presence, not by value: a failing element comparison makes chai render the DOM and hang the runner. */
2
+ import { expect, fixture } from '@open-wc/testing'
3
+ import { html } from 'lit'
4
+
5
+ import '../src/data-grist.js'
6
+ import '../src/record-view/record-view.js'
7
+ import { DataGrist } from '../src/data-grist.js'
8
+ import { OxGristEditor } from '../src/editors/ox-grist-editor.js'
9
+ import { RecordDelete } from '../src/handlers/record-delete.js'
10
+ import { failOnPageErrors } from './page-errors.js'
11
+
12
+ /*
13
+ * An edit waits while its column's validation runs (a server check, say). What happens to the rows meanwhile decides
14
+ * whether it still applies: selecting, refreshing and adding rows keep the row it was typed into, so it applies;
15
+ * deleting that row, undo and a reset replace it, so it does not. Typing the shown value again supersedes it. The
16
+ * edits here go through the real editors - typed into the input and committed the way leaving the cell commits.
17
+ */
18
+
19
+ const deferred = () => {
20
+ let resolve!: (value: boolean) => void
21
+ const promise = new Promise<boolean>(done => (resolve = done))
22
+ return { promise, resolve }
23
+ }
24
+ const settle = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms))
25
+
26
+ /* The title column waits for the validation the test hands it; `other` has none. */
27
+ let validating: Promise<boolean> | undefined
28
+ const columns = [
29
+ { type: 'gutter', gutterName: 'row-selector', multiple: true },
30
+ {
31
+ type: 'string',
32
+ name: 'title',
33
+ header: 'title',
34
+ record: { editable: true },
35
+ validation: () => validating ?? true
36
+ },
37
+ { type: 'string', name: 'other', header: 'other', record: { editable: true } }
38
+ ]
39
+
40
+ const mount = async (mode: 'GRID' | 'LIST' | 'CARD', records: any[]) => {
41
+ const el = await fixture<DataGrist>(html`<ox-grist
42
+ style="height:300px"
43
+ .mode=${mode}
44
+ .config=${{ columns, rows: { appendable: true, selectable: { multiple: true } }, list: { fields: ['title'] } }}
45
+ .data=${{ records }}
46
+ ></ox-grist>`)
47
+ await idle(el)
48
+ return el
49
+ }
50
+
51
+ const rows = (el: DataGrist) => el.grist.data.records
52
+
53
+ /* `updateComplete` resolves false while an update set another one going (a list takes its records in `updated`). */
54
+ const idle = async (el: DataGrist) => {
55
+ while (!((await el.updateComplete) && (await el.grist.updateComplete)));
56
+ }
57
+
58
+ /* Type into the editor of a field and commit it as leaving the cell does. */
59
+ const typeInto = async (editor: OxGristEditor, text: string) => {
60
+ await editor.updateComplete
61
+ const input = editor.renderRoot.querySelector('input') as HTMLInputElement
62
+ input.value = text
63
+ input.dispatchEvent(new Event('input', { bubbles: true, composed: true }))
64
+ input.dispatchEvent(new Event('change', { bubbles: true, composed: true }))
65
+ editor.flush()
66
+ }
67
+
68
+ /* A grid cell: open its editor the way a double-click does, type, commit, close. */
69
+ const typeInGrid = async (el: DataGrist, row: number, name: string, text: string) => {
70
+ await idle(el)
71
+ const body = el.grist.renderRoot.querySelector('ox-grid-body') as any
72
+ const column = body.columns.filter((column: any) => !column.hidden).findIndex((column: any) => column.name === name)
73
+ body.startEditTarget(row, column)
74
+ await body.updateComplete
75
+ const field = body.renderRoot.querySelector(`#cell-${row}-${column}`) as any
76
+ await field.updateComplete
77
+ await typeInto(field.renderRoot.querySelector('*') as OxGristEditor, text)
78
+ body.resetEdit()
79
+ await body.updateComplete
80
+ }
81
+
82
+ /*
83
+ * A list item or card: its record view, as `RecordViewHandler` opens it for that item and hands its changes to the
84
+ * item. Built here in place of the popup; it is the item's own child, so its changes reach the list the same way.
85
+ */
86
+ const typeInItem = async (el: DataGrist, index: number, name: string, text: string) => {
87
+ /* Anything may draw the list again before the next edit (a page attached on scroll, a selection). */
88
+ el.grist.requestUpdate()
89
+ await idle(el)
90
+ const items = el.grist.renderRoot.querySelectorAll('ox-record-partial, ox-record-card')
91
+ const item = items[index] as any
92
+ const view = document.createElement('ox-record-view') as any
93
+ Object.assign(view, { columns: el.compiledConfig.columns, record: item.record, rowIndex: item.rowIndex })
94
+ item.appendChild(view)
95
+ await view.updateComplete
96
+ const body = view.renderRoot.querySelector('ox-record-view-body') as any
97
+ await body.updateComplete
98
+ const field = Array.from(body.renderRoot.querySelectorAll('ox-grid-field') as NodeListOf<any>).find(
99
+ one => one.column?.name === name
100
+ )
101
+ field.editing = true
102
+ await field.updateComplete
103
+ await typeInto(field.renderRoot.querySelector('*') as OxGristEditor, text)
104
+ view.remove()
105
+ }
106
+
107
+ describe('an edit waiting for validation', () => {
108
+ failOnPageErrors()
109
+
110
+ afterEach(() => {
111
+ validating = undefined
112
+ })
113
+
114
+ it('applies after another field of the row was edited meanwhile', async () => {
115
+ const el = await mount('GRID', [{ id: '1', title: 'A', other: 'x' }])
116
+ const pending = deferred()
117
+ validating = pending.promise
118
+ await typeInGrid(el, 0, 'title', 'checked')
119
+ validating = undefined
120
+ await typeInGrid(el, 0, 'other', 'y')
121
+ pending.resolve(true)
122
+ await settle()
123
+ expect(rows(el)[0]).to.include({ title: 'checked', other: 'y' })
124
+ })
125
+
126
+ it('applies after rows were selected, which rebuilds the rows around the same records', async () => {
127
+ const el = await mount('GRID', [
128
+ { id: '1', title: 'A' },
129
+ { id: '2', title: 'B' }
130
+ ])
131
+ const pending = deferred()
132
+ validating = pending.promise
133
+ await typeInGrid(el, 0, 'title', 'checked')
134
+ const before = rows(el)
135
+ el.select(record => record.id === '2')
136
+ await idle(el)
137
+ expect(rows(el) !== before).to.equal(true)
138
+ pending.resolve(true)
139
+ await settle()
140
+ expect(rows(el)[0].title).to.equal('checked')
141
+ expect(rows(el)[1].title).to.equal('B')
142
+ })
143
+
144
+ it('applies to its row after a row was added on top', async () => {
145
+ const el = await mount('GRID', [{ id: '1', title: 'A' }])
146
+ const pending = deferred()
147
+ validating = pending.promise
148
+ await typeInGrid(el, 0, 'title', 'checked')
149
+ el.addRecordToTop({ title: 'top' })
150
+ await idle(el)
151
+ pending.resolve(true)
152
+ await settle()
153
+ expect(rows(el).map(record => record.title)).to.eql(['top', 'checked'])
154
+ })
155
+
156
+ it('is superseded by typing the shown value again', async () => {
157
+ const el = await mount('GRID', [{ id: '1', title: 'A' }])
158
+ const pending = deferred()
159
+ validating = pending.promise
160
+ await typeInGrid(el, 0, 'title', 'mistake')
161
+ validating = undefined
162
+ await typeInGrid(el, 0, 'title', 'A')
163
+ pending.resolve(true)
164
+ await settle()
165
+ expect(rows(el)[0].title).to.equal('A')
166
+ expect(el.dirtyRecords).to.have.length(0)
167
+ })
168
+
169
+ it('does not apply to a row deleted meanwhile', async () => {
170
+ const el = await mount('GRID', [
171
+ { id: '1', title: 'A' },
172
+ { id: '2', title: 'B' }
173
+ ])
174
+ const pending = deferred()
175
+ validating = pending.promise
176
+ await typeInGrid(el, 1, 'title', 'checked')
177
+ const record = rows(el)[1]
178
+ const field = (el.grist.renderRoot.querySelector('ox-grid-body') as any).renderRoot.querySelector('#cell-1-1')
179
+ RecordDelete(el.compiledConfig.columns, el.grist.data, field.column, record, 1, field)
180
+ pending.resolve(true)
181
+ await settle()
182
+ expect(rows(el).map(record => record.title)).to.eql(['A'])
183
+ expect(record.title).to.equal('B')
184
+ })
185
+
186
+ it('does not apply after undo, and an edit after undo does', async () => {
187
+ const el = await mount('GRID', [{ id: '1', title: 'A' }])
188
+ /* Snapshots for undo are taken at most once a second, by one timer for every grid on the page. */
189
+ await settle(1100)
190
+ await typeInGrid(el, 0, 'other', 'first')
191
+ const pending = deferred()
192
+ validating = pending.promise
193
+ await typeInGrid(el, 0, 'title', 'checked')
194
+ el.undo()
195
+ await idle(el)
196
+ pending.resolve(true)
197
+ await settle()
198
+ expect(rows(el)[0]).to.include({ title: 'A' })
199
+ expect(rows(el)[0].other).to.equal(undefined)
200
+ validating = undefined
201
+ await typeInGrid(el, 0, 'title', 'after undo')
202
+ expect(rows(el)[0].title).to.equal('after undo')
203
+ }).timeout(5000)
204
+
205
+ it('does not apply after a reset', async () => {
206
+ const el = await mount('GRID', [{ id: '1', title: 'A' }])
207
+ const pending = deferred()
208
+ validating = pending.promise
209
+ await typeInGrid(el, 1, 'title', 'new row')
210
+ el.reset()
211
+ await idle(el)
212
+ pending.resolve(true)
213
+ await settle()
214
+ expect(rows(el).map(record => record.title)).to.eql(['A'])
215
+ })
216
+ })
217
+
218
+ /* The owner replacing the rows: a new row still being validated was typed into rows that are gone. */
219
+ describe('a new row waiting for validation', () => {
220
+ failOnPageErrors()
221
+
222
+ afterEach(() => {
223
+ validating = undefined
224
+ })
225
+
226
+ /* [name, what comes before the edit, the replacement] */
227
+ const replacements: [string, (el: DataGrist) => void, (el: DataGrist) => void][] = [
228
+ ['undo', () => {}, el => el.undo()],
229
+ ['redo', el => el.undo(), el => el.redo()],
230
+ ['a restored draft', () => {}, el => el.restoreUnsavedDraft({ records: [{ id: '1', title: 'draft' }] }, null)]
231
+ ]
232
+
233
+ for (const [name, before, replace] of replacements) {
234
+ it(`is not added after ${name}`, async () => {
235
+ const el = await mount('GRID', [{ id: '1', title: 'A' }])
236
+ /* Snapshots for undo are taken at most once a second, by one timer for every grid on the page. */
237
+ await settle(1100)
238
+ await typeInGrid(el, 0, 'other', 'first')
239
+ before(el)
240
+ await idle(el)
241
+ const pending = deferred()
242
+ validating = pending.promise
243
+ await typeInGrid(el, 1, 'title', 'new row')
244
+ replace(el)
245
+ await idle(el)
246
+ pending.resolve(true)
247
+ await settle()
248
+ expect(rows(el)).to.have.length(1)
249
+ }).timeout(5000)
250
+ }
251
+ })
252
+
253
+ for (const mode of ['GRID', 'LIST', 'CARD'] as const) {
254
+ describe(`the append row of a ${mode}`, () => {
255
+ failOnPageErrors()
256
+
257
+ afterEach(() => {
258
+ validating = undefined
259
+ })
260
+
261
+ const type = (el: DataGrist, row: number, name: string, text: string) =>
262
+ mode === 'GRID' ? typeInGrid(el, row, name, text) : typeInItem(el, row, name, text)
263
+
264
+ it('takes a field still being validated and one typed meanwhile into one new row', async () => {
265
+ const el = await mount(mode, [{ id: '1', title: 'A' }])
266
+ const pending = deferred()
267
+ validating = pending.promise
268
+ await type(el, 1, 'title', 'new')
269
+ validating = undefined
270
+ await type(el, 1, 'other', 'meanwhile')
271
+ pending.resolve(true)
272
+ await settle()
273
+ await idle(el)
274
+ expect(rows(el)).to.have.length(2)
275
+ expect(rows(el)[1]).to.include({ title: 'new', other: 'meanwhile', __dirty__: '+' })
276
+ expect(el.exportPatchList({})).to.eql([{ patchFlag: '+', title: 'new', other: 'meanwhile' }])
277
+ })
278
+
279
+ it('offers a fresh empty row once its row was added', async () => {
280
+ const el = await mount(mode, [])
281
+ await type(el, 0, 'title', 'first')
282
+ await idle(el)
283
+ await type(el, 1, 'title', 'second')
284
+ expect(rows(el).map(record => record.title)).to.eql(['first', 'second'])
285
+ })
286
+
287
+ it('is not added again when its new row was deleted while a field waited', async () => {
288
+ const el = await mount(mode, [])
289
+ const pending = deferred()
290
+ validating = pending.promise
291
+ await type(el, 0, 'title', 'waits')
292
+ validating = undefined
293
+ await type(el, 0, 'other', 'adds the row')
294
+ await idle(el)
295
+ /* Resetting a new row removes it (`record-reset` of a row that has no origin). */
296
+ el.grist.onRecordChanged(undefined as any, 0, null)
297
+ pending.resolve(true)
298
+ await settle()
299
+ expect(rows(el)).to.have.length(0)
300
+ })
301
+ })
302
+ }
@@ -0,0 +1,58 @@
1
+ /* Elements are compared by presence, not by value: a failing element comparison makes chai render the DOM and hang the runner. */
2
+ import { expect, fixture } from '@open-wc/testing'
3
+ import { html } from 'lit'
4
+
5
+ import '../src/data-grist.js'
6
+ import { DataGrist } from '../src/data-grist.js'
7
+ import { failOnPageErrors } from './page-errors.js'
8
+
9
+ /*
10
+ * A list or card shows the records of its data, whenever its config arrives. The grist hands the data over at once
11
+ * and the compiled config a render later - and a personal config loads later still - so a list that emptied itself
12
+ * on a config change showed nothing until the next fetch (2026-10-26).
13
+ */
14
+
15
+ const records = [
16
+ { id: '1', title: 'one' },
17
+ { id: '2', title: 'two' }
18
+ ]
19
+ const config = (header: string) => ({
20
+ columns: [{ type: 'string', name: 'title', header, record: { editable: true } }],
21
+ rows: { appendable: false },
22
+ list: { fields: ['title'] }
23
+ })
24
+
25
+ /* `updateComplete` resolves false while an update set another one going. */
26
+ const idle = async (el: DataGrist) => {
27
+ while (!((await el.updateComplete) && (await el.grist.updateComplete)));
28
+ }
29
+
30
+ for (const [mode, tag] of [
31
+ ['LIST', 'ox-record-partial'],
32
+ ['CARD', 'ox-record-card']
33
+ ] as const) {
34
+ describe(`a ${mode} and its config`, () => {
35
+ failOnPageErrors()
36
+
37
+ const shown = (el: DataGrist) =>
38
+ Array.from(el.grist.renderRoot.querySelectorAll(tag) as NodeListOf<any>).map(item => item.record.title)
39
+
40
+ it('shows the records it was given with its config', async () => {
41
+ const el = await fixture<DataGrist>(
42
+ html`<ox-grist style="height:300px" .mode=${mode} .config=${config('title')} .data=${{ records }}></ox-grist>`
43
+ )
44
+ await idle(el)
45
+ expect(shown(el)).to.eql(['one', 'two'])
46
+ })
47
+
48
+ it('keeps its records when the config changes after them', async () => {
49
+ const el = await fixture<DataGrist>(
50
+ html`<ox-grist style="height:300px" .mode=${mode} .config=${config('title')} .data=${{ records }}></ox-grist>`
51
+ )
52
+ await idle(el)
53
+ el.config = config('renamed')
54
+ await idle(el)
55
+ expect(shown(el)).to.eql(['one', 'two'])
56
+ })
57
+ })
58
+ }
@@ -0,0 +1,43 @@
1
+ /*
2
+ * Fail a test when the page throws while it runs - a render exception or a rejected promise nobody awaited. The
3
+ * runner only logs those as 「thrown outside a test」 and the test still passes. The browser's ResizeObserver loop
4
+ * notice is not an error of the page.
5
+ */
6
+ export function failOnPageErrors() {
7
+ const errors: string[] = []
8
+ const benign = (message: string) => message.includes('ResizeObserver loop')
9
+
10
+ const onError = (e: ErrorEvent) => {
11
+ if (benign(String(e.message))) {
12
+ e.stopImmediatePropagation()
13
+ e.preventDefault()
14
+ return
15
+ }
16
+ errors.push(String(e.error?.stack || e.message))
17
+ }
18
+ const onRejection = (e: PromiseRejectionEvent) => {
19
+ errors.push(String(e.reason?.stack || e.reason))
20
+ }
21
+ let previousOnError: OnErrorEventHandler = null
22
+
23
+ before(() => {
24
+ window.addEventListener('error', onError, true)
25
+ window.addEventListener('unhandledrejection', onRejection)
26
+ previousOnError = window.onerror
27
+ window.onerror = (message, ...rest) =>
28
+ benign(String(message)) ? true : previousOnError?.call(window, message, ...rest)
29
+ })
30
+ after(() => {
31
+ window.removeEventListener('error', onError, true)
32
+ window.removeEventListener('unhandledrejection', onRejection)
33
+ window.onerror = previousOnError
34
+ })
35
+ beforeEach(() => {
36
+ errors.length = 0
37
+ })
38
+ afterEach(async () => {
39
+ /* Let renders the test started finish, so their errors land in this test. */
40
+ await new Promise(resolve => setTimeout(resolve, 50))
41
+ if (errors.length) throw new Error(`the page threw while the test ran:\n${errors.join('\n')}`)
42
+ })
43
+ }