@svgrid/grid 1.1.2 → 1.2.3

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 (143) hide show
  1. package/dist/SvGrid.css +294 -16
  2. package/dist/SvGrid.svelte +4 -1
  3. package/dist/SvGridDropdown.svelte +7 -3
  4. package/dist/cdn/svgrid.js +13506 -0
  5. package/dist/cdn/svgrid.svelte-external.js +11190 -0
  6. package/package.json +12 -2
  7. package/src/SvGrid.css +294 -16
  8. package/src/SvGrid.svelte +4 -1
  9. package/src/SvGridDropdown.svelte +7 -3
  10. package/dist/SvGrid.helpers.test.d.ts +0 -1
  11. package/dist/SvGrid.helpers.test.js +0 -298
  12. package/dist/SvGridChart.test.d.ts +0 -1
  13. package/dist/SvGridChart.test.js +0 -62
  14. package/dist/a11y.contract.test.d.ts +0 -1
  15. package/dist/a11y.contract.test.js +0 -39
  16. package/dist/a11y.test.d.ts +0 -1
  17. package/dist/a11y.test.js +0 -44
  18. package/dist/aligned-grids.test.d.ts +0 -1
  19. package/dist/aligned-grids.test.js +0 -75
  20. package/dist/build-api.coverage.test.d.ts +0 -20
  21. package/dist/build-api.coverage.test.js +0 -505
  22. package/dist/cell-formatting.test.d.ts +0 -1
  23. package/dist/cell-formatting.test.js +0 -234
  24. package/dist/cell-render.test.d.ts +0 -1
  25. package/dist/cell-render.test.js +0 -338
  26. package/dist/chart-export.test.d.ts +0 -1
  27. package/dist/chart-export.test.js +0 -302
  28. package/dist/chart.coverage.test.d.ts +0 -1
  29. package/dist/chart.coverage.test.js +0 -748
  30. package/dist/chart.test.d.ts +0 -1
  31. package/dist/chart.test.js +0 -289
  32. package/dist/clipboard.test.d.ts +0 -1
  33. package/dist/clipboard.test.js +0 -700
  34. package/dist/collaboration.coverage.test.d.ts +0 -1
  35. package/dist/collaboration.coverage.test.js +0 -200
  36. package/dist/collaboration.test.d.ts +0 -1
  37. package/dist/collaboration.test.js +0 -65
  38. package/dist/column-groups.test.d.ts +0 -1
  39. package/dist/column-groups.test.js +0 -56
  40. package/dist/column-types.test.d.ts +0 -1
  41. package/dist/column-types.test.js +0 -62
  42. package/dist/columns.test.d.ts +0 -1
  43. package/dist/columns.test.js +0 -625
  44. package/dist/conditional-formatting.test.d.ts +0 -1
  45. package/dist/conditional-formatting.test.js +0 -87
  46. package/dist/core.coverage.test.d.ts +0 -1
  47. package/dist/core.coverage.test.js +0 -186
  48. package/dist/core.performance.test.d.ts +0 -1
  49. package/dist/core.performance.test.js +0 -29
  50. package/dist/createGrid.test.d.ts +0 -1
  51. package/dist/createGrid.test.js +0 -9
  52. package/dist/editing.test.d.ts +0 -1
  53. package/dist/editing.test.js +0 -732
  54. package/dist/editors/cell-editors.coverage.test.d.ts +0 -1
  55. package/dist/editors/cell-editors.coverage.test.js +0 -139
  56. package/dist/editors/cell-editors.test.d.ts +0 -1
  57. package/dist/editors/cell-editors.test.js +0 -75
  58. package/dist/facet-buckets.test.d.ts +0 -1
  59. package/dist/facet-buckets.test.js +0 -296
  60. package/dist/fill-patterns.test.d.ts +0 -1
  61. package/dist/fill-patterns.test.js +0 -81
  62. package/dist/filter-operators.test.d.ts +0 -1
  63. package/dist/filter-operators.test.js +0 -135
  64. package/dist/filtering/excel-filters.test.d.ts +0 -1
  65. package/dist/filtering/excel-filters.test.js +0 -116
  66. package/dist/filtering/locale-filter.test.d.ts +0 -13
  67. package/dist/filtering/locale-filter.test.js +0 -189
  68. package/dist/flex-render.test.d.ts +0 -1
  69. package/dist/flex-render.test.js +0 -142
  70. package/dist/group-aggregate.test.d.ts +0 -1
  71. package/dist/group-aggregate.test.js +0 -32
  72. package/dist/hyperformula-adapter.test.d.ts +0 -1
  73. package/dist/hyperformula-adapter.test.js +0 -205
  74. package/dist/keyboard-handlers.coverage.test.d.ts +0 -1
  75. package/dist/keyboard-handlers.coverage.test.js +0 -495
  76. package/dist/keyboard.test.d.ts +0 -1
  77. package/dist/keyboard.test.js +0 -27
  78. package/dist/menus.test.d.ts +0 -1
  79. package/dist/menus.test.js +0 -560
  80. package/dist/named-views.coverage.test.d.ts +0 -1
  81. package/dist/named-views.coverage.test.js +0 -180
  82. package/dist/named-views.test.d.ts +0 -1
  83. package/dist/named-views.test.js +0 -57
  84. package/dist/new-features.test.d.ts +0 -1
  85. package/dist/new-features.test.js +0 -183
  86. package/dist/render-component.test.d.ts +0 -1
  87. package/dist/render-component.test.js +0 -41
  88. package/dist/row-drag.test.d.ts +0 -1
  89. package/dist/row-drag.test.js +0 -142
  90. package/dist/row-resize.test.d.ts +0 -1
  91. package/dist/row-resize.test.js +0 -329
  92. package/dist/scroll-sync.test.d.ts +0 -1
  93. package/dist/scroll-sync.test.js +0 -290
  94. package/dist/selection.multi-range.test.d.ts +0 -1
  95. package/dist/selection.multi-range.test.js +0 -55
  96. package/dist/selection.test.d.ts +0 -1
  97. package/dist/selection.test.js +0 -647
  98. package/dist/server-data-source.coverage.test.d.ts +0 -1
  99. package/dist/server-data-source.coverage.test.js +0 -154
  100. package/dist/server-data-source.test.d.ts +0 -1
  101. package/dist/server-data-source.test.js +0 -81
  102. package/dist/sparkline.test.d.ts +0 -1
  103. package/dist/sparkline.test.js +0 -52
  104. package/dist/spreadsheet.test.d.ts +0 -1
  105. package/dist/spreadsheet.test.js +0 -446
  106. package/dist/subscribe.test.d.ts +0 -1
  107. package/dist/subscribe.test.js +0 -93
  108. package/dist/svgrid.api-extensions.test.d.ts +0 -14
  109. package/dist/svgrid.api-extensions.test.js +0 -334
  110. package/dist/svgrid.api.test.d.ts +0 -15
  111. package/dist/svgrid.api.test.js +0 -169
  112. package/dist/svgrid.behavior.test.d.ts +0 -15
  113. package/dist/svgrid.behavior.test.js +0 -625
  114. package/dist/svgrid.column-reorder.test.d.ts +0 -15
  115. package/dist/svgrid.column-reorder.test.js +0 -224
  116. package/dist/svgrid.comments-autocomplete.test.d.ts +0 -1
  117. package/dist/svgrid.comments-autocomplete.test.js +0 -96
  118. package/dist/svgrid.context-menu.test.d.ts +0 -1
  119. package/dist/svgrid.context-menu.test.js +0 -102
  120. package/dist/svgrid.features.test.d.ts +0 -1
  121. package/dist/svgrid.features.test.js +0 -129
  122. package/dist/svgrid.interaction.test.d.ts +0 -9
  123. package/dist/svgrid.interaction.test.js +0 -349
  124. package/dist/svgrid.locale-filtering.test.d.ts +0 -15
  125. package/dist/svgrid.locale-filtering.test.js +0 -261
  126. package/dist/svgrid.new-features.wrapper.test.d.ts +0 -1
  127. package/dist/svgrid.new-features.wrapper.test.js +0 -194
  128. package/dist/svgrid.row-pinning.test.d.ts +0 -14
  129. package/dist/svgrid.row-pinning.test.js +0 -250
  130. package/dist/svgrid.set-filter.test.d.ts +0 -17
  131. package/dist/svgrid.set-filter.test.js +0 -268
  132. package/dist/svgrid.wrapper.test.d.ts +0 -1
  133. package/dist/svgrid.wrapper.test.js +0 -59
  134. package/dist/test-setup.d.ts +0 -1
  135. package/dist/test-setup.js +0 -35
  136. package/dist/transaction.test.d.ts +0 -1
  137. package/dist/transaction.test.js +0 -89
  138. package/dist/virtualization/column-virtualizer.test.d.ts +0 -1
  139. package/dist/virtualization/column-virtualizer.test.js +0 -25
  140. package/dist/virtualization/scroll-scaling.test.d.ts +0 -1
  141. package/dist/virtualization/scroll-scaling.test.js +0 -127
  142. package/dist/virtualization/virtualizer.test.d.ts +0 -1
  143. package/dist/virtualization/virtualizer.test.js +0 -41
@@ -1,732 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from 'vitest';
2
- import { createEditing } from './editing';
3
- function makeCtx(opts = {}) {
4
- const columns = opts.columns ?? [
5
- { id: 'a', field: 'a', editable: true, editorType: 'text' },
6
- { id: 'b', field: 'b', editable: true, editorType: 'number' },
7
- ];
8
- const data = (opts.data ?? [
9
- { a: 'a0', b: 0 },
10
- { a: 'a1', b: 1 },
11
- ]).map((d) => ({ ...d }));
12
- const allColumns = columns.map((c) => ({
13
- id: c.id,
14
- columnDef: {
15
- field: c.field,
16
- editable: c.editable,
17
- editorType: c.editorType,
18
- editorMultiple: c.editorMultiple,
19
- },
20
- }));
21
- let activeCell = null;
22
- const buildRows = (rows) => rows.map((row, index) => ({
23
- id: String(index),
24
- original: row,
25
- getCanExpand: () => opts.groupRows?.has(index) ?? false,
26
- getCellValueByColumnId: (colId) => row[colId],
27
- }));
28
- let backing = data;
29
- const ctx = {
30
- allColumns,
31
- _rows: buildRows(data),
32
- get allRows() {
33
- return ctx._rows;
34
- },
35
- editingEnabled: opts.editingEnabled ?? true,
36
- editingCell: null,
37
- editorSelectAll: false,
38
- editedCellValues: {},
39
- selectionRange: { anchor: null, focus: null },
40
- gridRootEl: null,
41
- props: {},
42
- history: [],
43
- historyPtr: -1,
44
- historyVersion: 0,
45
- UNDO_LIMIT: 100,
46
- activeCalls: [],
47
- selectionCalls: [],
48
- setActiveCell: (r, c) => {
49
- activeCell = { rowIndex: r, colIndex: c };
50
- ctx.activeCalls.push([r, c]);
51
- },
52
- setSelection: (r, c) => {
53
- ctx.selectionCalls.push([r, c]);
54
- },
55
- grid: {
56
- getState: () => ({ activeCell }),
57
- store: { setState: vi.fn() },
58
- },
59
- };
60
- Object.defineProperty(ctx, 'internalData', {
61
- get: () => backing,
62
- set: (next) => {
63
- backing = next;
64
- ctx._rows = buildRows(next);
65
- },
66
- });
67
- ctx.internalData = data;
68
- return ctx;
69
- }
70
- const editingFor = (opts) => {
71
- const ctx = makeCtx(opts);
72
- return { ctx, ed: createEditing(ctx) };
73
- };
74
- describe('isCellEditable', () => {
75
- it('treats undefined editable as editable', () => {
76
- const { ctx, ed } = editingFor({
77
- columns: [{ id: 'a', field: 'a' }],
78
- data: [{ a: 1 }],
79
- });
80
- expect(ed.isCellEditable(ctx.allColumns[0], ctx.allRows[0])).toBe(true);
81
- });
82
- it('treats editable:false as locked (fast path, no row needed)', () => {
83
- const { ctx, ed } = editingFor({
84
- columns: [{ id: 'a', field: 'a', editable: false }],
85
- });
86
- expect(ed.isCellEditable(ctx.allColumns[0])).toBe(false);
87
- });
88
- it('honours a cell-level predicate returning false', () => {
89
- const { ctx, ed } = editingFor({
90
- columns: [{ id: 'a', field: 'a', editable: () => false }],
91
- data: [{ a: 1 }],
92
- });
93
- expect(ed.isCellEditable(ctx.allColumns[0], ctx.allRows[0])).toBe(false);
94
- });
95
- it('honours a cell-level predicate returning true', () => {
96
- const { ctx, ed } = editingFor({
97
- columns: [{ id: 'a', field: 'a', editable: (cc) => cc.row.id === '0' }],
98
- data: [{ a: 1 }],
99
- });
100
- expect(ed.isCellEditable(ctx.allColumns[0], ctx.allRows[0])).toBe(true);
101
- });
102
- it('passes a usable CellContext to the predicate', () => {
103
- const spy = vi.fn().mockReturnValue(true);
104
- const { ctx, ed } = editingFor({
105
- columns: [{ id: 'a', field: 'a', editable: spy }],
106
- data: [{ a: 'hello' }],
107
- });
108
- ed.isCellEditable(ctx.allColumns[0], ctx.allRows[0]);
109
- const cc = spy.mock.calls[0][0];
110
- expect(cc.getValue()).toBe('hello');
111
- expect(cc.cell.getValue()).toBe('hello');
112
- expect(cc.cell.getContext()).toBe(cc);
113
- expect(cc.column).toBe(ctx.allColumns[0]);
114
- });
115
- it('returns editable when predicate exists but no row supplied', () => {
116
- const { ctx, ed } = editingFor({
117
- columns: [{ id: 'a', field: 'a', editable: () => false }],
118
- });
119
- // No row -> cannot evaluate predicate -> defaults to editable.
120
- expect(ed.isCellEditable(ctx.allColumns[0])).toBe(true);
121
- });
122
- it('biases to not-editable when the predicate throws', () => {
123
- const { ctx, ed } = editingFor({
124
- columns: [
125
- {
126
- id: 'a',
127
- field: 'a',
128
- editable: () => {
129
- throw new Error('boom');
130
- },
131
- },
132
- ],
133
- data: [{ a: 1 }],
134
- });
135
- expect(ed.isCellEditable(ctx.allColumns[0], ctx.allRows[0])).toBe(false);
136
- });
137
- });
138
- describe('isCellEditableAt', () => {
139
- it('resolves row/column by index then defers to isCellEditable', () => {
140
- const { ed } = editingFor({
141
- columns: [
142
- { id: 'a', field: 'a', editable: true },
143
- { id: 'b', field: 'b', editable: false },
144
- ],
145
- data: [{ a: 1, b: 2 }],
146
- });
147
- expect(ed.isCellEditableAt(0, 0)).toBe(true);
148
- expect(ed.isCellEditableAt(0, 1)).toBe(false);
149
- });
150
- it('returns false when the column index is out of range', () => {
151
- const { ed } = editingFor();
152
- expect(ed.isCellEditableAt(0, 99)).toBe(false);
153
- });
154
- });
155
- describe('getRowColumnValue', () => {
156
- it('reads the base value when the column id is known', () => {
157
- const { ctx, ed } = editingFor({
158
- columns: [{ id: 'a', field: 'a' }],
159
- data: [{ a: 'x' }],
160
- });
161
- expect(ed.getRowColumnValue(ctx.allRows[0], 'a')).toBe('x');
162
- });
163
- it('falls back to row.getCellValueByColumnId for an unknown column', () => {
164
- const { ctx, ed } = editingFor({
165
- columns: [{ id: 'a', field: 'a' }],
166
- data: [{ a: 'x', ghost: 'g' }],
167
- });
168
- expect(ed.getRowColumnValue(ctx.allRows[0], 'ghost')).toBe('g');
169
- });
170
- });
171
- describe('getCellDisplayValue', () => {
172
- it('returns the base value when there is no pending edit', () => {
173
- const { ed } = editingFor();
174
- expect(ed.getCellDisplayValue('0', 'a', 'base')).toBe('base');
175
- });
176
- it('prefers an edited override keyed by rowId:colId', () => {
177
- const { ctx, ed } = editingFor();
178
- ctx.editedCellValues['0:a'] = 'edited';
179
- expect(ed.getCellDisplayValue('0', 'a', 'base')).toBe('edited');
180
- });
181
- it('treats an explicit undefined override as present', () => {
182
- const { ctx, ed } = editingFor();
183
- ctx.editedCellValues['0:a'] = undefined;
184
- expect(ed.getCellDisplayValue('0', 'a', 'base')).toBeUndefined();
185
- });
186
- });
187
- describe('startEditingWithChar', () => {
188
- it('seeds the editor with the typed character and sets active/selection', () => {
189
- const { ctx, ed } = editingFor({
190
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
191
- data: [{ a: 'x' }],
192
- });
193
- expect(ed.startEditingWithChar(0, 0, 'Z')).toBe(true);
194
- expect(ctx.editingCell).toEqual({
195
- rowId: '0',
196
- columnId: 'a',
197
- editorType: 'text',
198
- value: 'Z',
199
- });
200
- expect(ctx.editorSelectAll).toBe(false);
201
- expect(ctx.activeCalls).toEqual([[0, 0]]);
202
- expect(ctx.selectionCalls).toEqual([[0, 0]]);
203
- });
204
- it('defaults the editor type to text when none is declared', () => {
205
- const { ctx, ed } = editingFor({
206
- columns: [{ id: 'a', field: 'a' }],
207
- data: [{ a: 'x' }],
208
- });
209
- ed.startEditingWithChar(0, 0, 'k');
210
- expect(ctx.editingCell.editorType).toBe('text');
211
- });
212
- it('returns false when editing is globally disabled', () => {
213
- const { ctx, ed } = editingFor({ editingEnabled: false });
214
- expect(ed.startEditingWithChar(0, 0, 'a')).toBe(false);
215
- expect(ctx.editingCell).toBeNull();
216
- });
217
- it('returns false for a missing row or column', () => {
218
- const { ed } = editingFor();
219
- expect(ed.startEditingWithChar(99, 0, 'a')).toBe(false);
220
- expect(ed.startEditingWithChar(0, 99, 'a')).toBe(false);
221
- });
222
- it('returns false for a group row', () => {
223
- const { ed } = editingFor({
224
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
225
- data: [{ a: 'x' }],
226
- groupRows: new Set([0]),
227
- });
228
- expect(ed.startEditingWithChar(0, 0, 'a')).toBe(false);
229
- });
230
- it('returns false for a non-editable cell', () => {
231
- const { ed } = editingFor({
232
- columns: [{ id: 'a', field: 'a', editable: false, editorType: 'text' }],
233
- data: [{ a: 'x' }],
234
- });
235
- expect(ed.startEditingWithChar(0, 0, 'a')).toBe(false);
236
- });
237
- it('rejects type-to-edit for checkbox/date/datetime/list/chips editors', () => {
238
- for (const editorType of ['checkbox', 'date', 'datetime', 'list', 'chips']) {
239
- const { ed } = editingFor({
240
- columns: [{ id: 'a', field: 'a', editorType }],
241
- data: [{ a: 'x' }],
242
- });
243
- expect(ed.startEditingWithChar(0, 0, 'a')).toBe(false);
244
- }
245
- });
246
- it('rejects a non-numeric character on a number editor', () => {
247
- const { ed } = editingFor({
248
- columns: [{ id: 'a', field: 'a', editorType: 'number' }],
249
- data: [{ a: 1 }],
250
- });
251
- expect(ed.startEditingWithChar(0, 0, 'x')).toBe(false);
252
- });
253
- it('accepts a numeric character on a number editor', () => {
254
- const { ctx, ed } = editingFor({
255
- columns: [{ id: 'a', field: 'a', editorType: 'number' }],
256
- data: [{ a: 1 }],
257
- });
258
- expect(ed.startEditingWithChar(0, 0, '7')).toBe(true);
259
- expect(ctx.editingCell.value).toBe('7');
260
- });
261
- });
262
- describe('saveEditingCell', () => {
263
- it('does nothing when there is no editing cell', () => {
264
- const { ctx, ed } = editingFor();
265
- ed.saveEditingCell();
266
- expect(ctx.grid.store.setState).not.toHaveBeenCalled();
267
- });
268
- it('parses, writes original, records edited value, and clears editing', () => {
269
- const { ctx, ed } = editingFor({
270
- columns: [{ id: 'b', field: 'b', editorType: 'number' }],
271
- data: [{ b: 0 }],
272
- });
273
- ctx.editingCell = { rowId: '0', columnId: 'b', editorType: 'number', value: '42' };
274
- ed.saveEditingCell();
275
- expect(ctx.internalData[0].b).toBe(42); // parsed to number, written to original
276
- expect(ctx.editedCellValues['0:b']).toBe(42);
277
- expect(ctx.editingCell).toBeNull();
278
- expect(ctx.grid.store.setState).toHaveBeenCalled();
279
- });
280
- it('coerces an unparseable number to null', () => {
281
- const { ctx, ed } = editingFor({
282
- columns: [{ id: 'b', field: 'b', editorType: 'number' }],
283
- data: [{ b: 5 }],
284
- });
285
- ctx.editingCell = { rowId: '0', columnId: 'b', editorType: 'number', value: 'abc' };
286
- ed.saveEditingCell();
287
- expect(ctx.internalData[0].b).toBeNull();
288
- });
289
- it('respects editorMultiple when parsing list editors', () => {
290
- const { ctx, ed } = editingFor({
291
- columns: [
292
- { id: 'a', field: 'a', editorType: 'list', editorMultiple: true },
293
- ],
294
- data: [{ a: [] }],
295
- });
296
- ctx.editingCell = {
297
- rowId: '0',
298
- columnId: 'a',
299
- editorType: 'list',
300
- value: ['x', 'y'],
301
- };
302
- ed.saveEditingCell();
303
- expect(ctx.internalData[0].a).toEqual(['x', 'y']);
304
- });
305
- it('pushes a history step and bumps the version on a real change', () => {
306
- const { ctx, ed } = editingFor({
307
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
308
- data: [{ a: 'old' }],
309
- });
310
- ctx.editingCell = { rowId: '0', columnId: 'a', editorType: 'text', value: 'new' };
311
- ed.saveEditingCell();
312
- expect(ctx.history).toHaveLength(1);
313
- expect(ctx.history[0]).toMatchObject({
314
- rowId: '0',
315
- columnId: 'a',
316
- field: 'a',
317
- before: 'old',
318
- after: 'new',
319
- });
320
- expect(ctx.historyPtr).toBe(0);
321
- expect(ctx.historyVersion).toBe(1);
322
- });
323
- it('does not push history when the value is unchanged', () => {
324
- const { ctx, ed } = editingFor({
325
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
326
- data: [{ a: 'same' }],
327
- });
328
- ctx.editingCell = { rowId: '0', columnId: 'a', editorType: 'text', value: 'same' };
329
- ed.saveEditingCell();
330
- expect(ctx.history).toHaveLength(0);
331
- expect(ctx.historyVersion).toBe(0);
332
- });
333
- it('truncates forward (redo) history when editing after an undo', () => {
334
- const { ctx, ed } = editingFor({
335
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
336
- data: [{ a: 'v0' }],
337
- });
338
- // Pretend there is forward history the user could have redone.
339
- ctx.history = [
340
- { rowId: '0', columnId: 'a', field: 'a', before: 'x', after: 'y' },
341
- { rowId: '0', columnId: 'a', field: 'a', before: 'y', after: 'z' },
342
- ];
343
- ctx.historyPtr = 0; // pointer is behind the tail
344
- ctx.editingCell = { rowId: '0', columnId: 'a', editorType: 'text', value: 'v1' };
345
- ed.saveEditingCell();
346
- // Forward step (index 1) dropped, new step appended at index 1.
347
- expect(ctx.history).toHaveLength(2);
348
- expect(ctx.history[1].after).toBe('v1');
349
- expect(ctx.historyPtr).toBe(1);
350
- });
351
- it('caps the history at UNDO_LIMIT dropping the oldest entries', () => {
352
- const { ctx, ed } = editingFor({
353
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
354
- data: [{ a: 'start' }],
355
- });
356
- ctx.UNDO_LIMIT = 3;
357
- ctx.history = [
358
- { rowId: '0', columnId: 'a', field: 'a', before: '0', after: '1' },
359
- { rowId: '0', columnId: 'a', field: 'a', before: '1', after: '2' },
360
- { rowId: '0', columnId: 'a', field: 'a', before: '2', after: '3' },
361
- ];
362
- ctx.historyPtr = 2;
363
- ctx.editingCell = { rowId: '0', columnId: 'a', editorType: 'text', value: 'final' };
364
- ed.saveEditingCell();
365
- expect(ctx.history).toHaveLength(3); // capped
366
- expect(ctx.history[2].after).toBe('final'); // newest kept
367
- expect(ctx.history[0].after).toBe('2'); // oldest dropped
368
- expect(ctx.historyPtr).toBe(2);
369
- });
370
- it('fires onCellValueChange with the post-write payload', () => {
371
- const onCellValueChange = vi.fn();
372
- const { ctx, ed } = editingFor({
373
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
374
- data: [{ a: 'before' }],
375
- });
376
- ctx.props.onCellValueChange = onCellValueChange;
377
- ctx.editingCell = { rowId: '0', columnId: 'a', editorType: 'text', value: 'after' };
378
- ed.saveEditingCell();
379
- expect(onCellValueChange).toHaveBeenCalledWith(expect.objectContaining({
380
- rowIndex: 0,
381
- columnId: 'a',
382
- oldValue: 'before',
383
- newValue: 'after',
384
- }));
385
- });
386
- it('clears editing even when row/column cannot be resolved', () => {
387
- const { ctx, ed } = editingFor();
388
- ctx.editingCell = {
389
- rowId: 'missing',
390
- columnId: 'missing',
391
- editorType: 'text',
392
- value: 'x',
393
- };
394
- ed.saveEditingCell();
395
- expect(ctx.editingCell).toBeNull();
396
- expect(ctx.history).toHaveLength(0);
397
- });
398
- });
399
- describe('applyHistoryStep', () => {
400
- const step = {
401
- rowId: '0',
402
- columnId: 'a',
403
- field: 'a',
404
- before: 'B',
405
- after: 'A',
406
- };
407
- it('applies the before value on undo', () => {
408
- const { ctx, ed } = editingFor({
409
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
410
- data: [{ a: 'A' }],
411
- });
412
- ed.applyHistoryStep(step, 'undo');
413
- expect(ctx.internalData[0].a).toBe('B');
414
- expect(ctx.editedCellValues['0:a']).toBe('B');
415
- expect(ctx.grid.store.setState).toHaveBeenCalled();
416
- });
417
- it('applies the after value on redo', () => {
418
- const { ctx, ed } = editingFor({
419
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
420
- data: [{ a: 'B' }],
421
- });
422
- ed.applyHistoryStep(step, 'redo');
423
- expect(ctx.internalData[0].a).toBe('A');
424
- });
425
- it('fires onCellValueChange with swapped old/new on undo', () => {
426
- const onCellValueChange = vi.fn();
427
- const { ctx, ed } = editingFor({
428
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
429
- data: [{ a: 'A' }],
430
- });
431
- ctx.props.onCellValueChange = onCellValueChange;
432
- ed.applyHistoryStep(step, 'undo');
433
- expect(onCellValueChange).toHaveBeenCalledWith(expect.objectContaining({ oldValue: 'A', newValue: 'B' }));
434
- });
435
- it('is a no-op when the row or column is missing', () => {
436
- const { ctx, ed } = editingFor();
437
- ed.applyHistoryStep({ rowId: 'nope', columnId: 'a', field: 'a', before: 1, after: 2 }, 'undo');
438
- expect(ctx.grid.store.setState).not.toHaveBeenCalled();
439
- });
440
- });
441
- describe('updateEditingCellValue', () => {
442
- it('updates the value while preserving the rest of the editing cell', () => {
443
- const { ctx, ed } = editingFor();
444
- ctx.editingCell = { rowId: '0', columnId: 'a', editorType: 'text', value: 'old' };
445
- ed.updateEditingCellValue('new');
446
- expect(ctx.editingCell).toEqual({
447
- rowId: '0',
448
- columnId: 'a',
449
- editorType: 'text',
450
- value: 'new',
451
- });
452
- });
453
- it('leaves a null editing cell untouched', () => {
454
- const { ctx, ed } = editingFor();
455
- ed.updateEditingCellValue('x');
456
- expect(ctx.editingCell).toBeNull();
457
- });
458
- });
459
- describe('onEditorKeyDown', () => {
460
- function fakeEvent(key) {
461
- return {
462
- key,
463
- stopPropagation: vi.fn(),
464
- preventDefault: vi.fn(),
465
- };
466
- }
467
- it('Enter saves the cell and refocuses the grid root', () => {
468
- const { ctx, ed } = editingFor({
469
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
470
- data: [{ a: 'old' }],
471
- });
472
- const focus = vi.fn();
473
- ctx.gridRootEl = { focus };
474
- ctx.editingCell = { rowId: '0', columnId: 'a', editorType: 'text', value: 'new' };
475
- const ev = fakeEvent('Enter');
476
- ed.onEditorKeyDown(ev);
477
- expect(ev.stopPropagation).toHaveBeenCalled();
478
- expect(ev.preventDefault).toHaveBeenCalled();
479
- expect(ctx.internalData[0].a).toBe('new');
480
- expect(ctx.editingCell).toBeNull();
481
- expect(focus).toHaveBeenCalledWith({ preventScroll: true });
482
- });
483
- it('Escape cancels without saving and refocuses the grid root', () => {
484
- const { ctx, ed } = editingFor({
485
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
486
- data: [{ a: 'old' }],
487
- });
488
- const focus = vi.fn();
489
- ctx.gridRootEl = { focus };
490
- ctx.editingCell = { rowId: '0', columnId: 'a', editorType: 'text', value: 'discard' };
491
- const ev = fakeEvent('Escape');
492
- ed.onEditorKeyDown(ev);
493
- expect(ctx.internalData[0].a).toBe('old'); // unchanged
494
- expect(ctx.editingCell).toBeNull();
495
- expect(focus).toHaveBeenCalled();
496
- });
497
- it('stops propagation for other keys without saving or cancelling', () => {
498
- const { ctx, ed } = editingFor();
499
- ctx.editingCell = { rowId: '0', columnId: 'a', editorType: 'text', value: 'x' };
500
- const ev = fakeEvent('a');
501
- ed.onEditorKeyDown(ev);
502
- expect(ev.stopPropagation).toHaveBeenCalled();
503
- expect(ev.preventDefault).not.toHaveBeenCalled();
504
- expect(ctx.editingCell).not.toBeNull();
505
- });
506
- it('tolerates a null gridRootEl on Escape', () => {
507
- const { ctx, ed } = editingFor();
508
- ctx.gridRootEl = null;
509
- ctx.editingCell = { rowId: '0', columnId: 'a', editorType: 'text', value: 'x' };
510
- expect(() => ed.onEditorKeyDown(fakeEvent('Escape'))).not.toThrow();
511
- expect(ctx.editingCell).toBeNull();
512
- });
513
- });
514
- describe('focusOnMount', () => {
515
- let rafCb = null;
516
- beforeEach(() => {
517
- rafCb = null;
518
- vi.stubGlobal('requestAnimationFrame', (cb) => {
519
- rafCb = cb;
520
- return 1;
521
- });
522
- });
523
- function fakeInput(value) {
524
- return {
525
- value,
526
- focus: vi.fn(),
527
- select: vi.fn(),
528
- setSelectionRange: vi.fn(),
529
- };
530
- }
531
- it('selects all when editorSelectAll is set', () => {
532
- const { ctx, ed } = editingFor();
533
- ctx.editorSelectAll = true;
534
- const node = fakeInput('hello');
535
- ed.focusOnMount(node);
536
- rafCb?.(0);
537
- expect(node.focus).toHaveBeenCalledWith({ preventScroll: true });
538
- expect(node.select).toHaveBeenCalled();
539
- expect(node.setSelectionRange).not.toHaveBeenCalled();
540
- });
541
- it('places the caret at the end when not selecting all', () => {
542
- const { ctx, ed } = editingFor();
543
- ctx.editorSelectAll = false;
544
- const node = fakeInput('hello');
545
- ed.focusOnMount(node);
546
- rafCb?.(0);
547
- expect(node.setSelectionRange).toHaveBeenCalledWith(5, 5);
548
- expect(node.select).not.toHaveBeenCalled();
549
- });
550
- it('swallows errors from inputs that reject selection ranges', () => {
551
- const { ctx, ed } = editingFor();
552
- ctx.editorSelectAll = false;
553
- const node = {
554
- value: '2020-01-01',
555
- focus: vi.fn(),
556
- select: vi.fn(),
557
- setSelectionRange: () => {
558
- throw new Error('unsupported');
559
- },
560
- };
561
- ed.focusOnMount(node);
562
- expect(() => rafCb?.(0)).not.toThrow();
563
- expect(node.focus).toHaveBeenCalled();
564
- });
565
- });
566
- describe('onCellDoubleClick', () => {
567
- it('enters edit mode seeded with the current display value', () => {
568
- const { ctx, ed } = editingFor({
569
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
570
- data: [{ a: 'val' }],
571
- });
572
- ed.onCellDoubleClick(0, 0);
573
- expect(ctx.editorSelectAll).toBe(true);
574
- expect(ctx.editingCell).toEqual({
575
- rowId: '0',
576
- columnId: 'a',
577
- editorType: 'text',
578
- value: 'val',
579
- });
580
- expect(ctx.activeCalls).toEqual([[0, 0]]);
581
- expect(ctx.selectionCalls).toEqual([[0, 0]]);
582
- });
583
- it('prefers an edited override as the seed value', () => {
584
- const { ctx, ed } = editingFor({
585
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
586
- data: [{ a: 'base' }],
587
- });
588
- ctx.editedCellValues['0:a'] = 'override';
589
- ed.onCellDoubleClick(0, 0);
590
- expect(ctx.editingCell.value).toBe('override');
591
- });
592
- it('returns early when editing is disabled', () => {
593
- const { ctx, ed } = editingFor({ editingEnabled: false });
594
- ed.onCellDoubleClick(0, 0);
595
- expect(ctx.editingCell).toBeNull();
596
- });
597
- it('returns early for missing row/column or group rows', () => {
598
- const { ctx, ed } = editingFor({
599
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
600
- data: [{ a: 'x' }],
601
- groupRows: new Set([0]),
602
- });
603
- ed.onCellDoubleClick(99, 0);
604
- ed.onCellDoubleClick(0, 99);
605
- ed.onCellDoubleClick(0, 0); // group row
606
- expect(ctx.editingCell).toBeNull();
607
- });
608
- it('returns early for a non-editable cell', () => {
609
- const { ctx, ed } = editingFor({
610
- columns: [{ id: 'a', field: 'a', editable: false, editorType: 'text' }],
611
- data: [{ a: 'x' }],
612
- });
613
- ed.onCellDoubleClick(0, 0);
614
- expect(ctx.editingCell).toBeNull();
615
- });
616
- it('is a no-op when already editing the same cell', () => {
617
- const { ctx, ed } = editingFor({
618
- columns: [{ id: 'a', field: 'a', editorType: 'text' }],
619
- data: [{ a: 'x' }],
620
- });
621
- ctx.editingCell = { rowId: '0', columnId: 'a', editorType: 'text', value: 'mid-edit' };
622
- ed.onCellDoubleClick(0, 0);
623
- expect(ctx.editingCell.value).toBe('mid-edit'); // untouched
624
- expect(ctx.activeCalls).toEqual([]); // setActiveCell not called
625
- });
626
- it('seeds a multi-select list editor with an array', () => {
627
- const { ctx, ed } = editingFor({
628
- columns: [
629
- { id: 'a', field: 'a', editorType: 'list', editorMultiple: true },
630
- ],
631
- data: [{ a: 'one' }],
632
- });
633
- ed.onCellDoubleClick(0, 0);
634
- expect(Array.isArray(ctx.editingCell.value)).toBe(true);
635
- expect(ctx.editingCell.value).toEqual(['one']);
636
- });
637
- it('unwraps an array seed for a single-select list editor', () => {
638
- const { ctx, ed } = editingFor({
639
- columns: [{ id: 'a', field: 'a', editorType: 'list' }],
640
- data: [{ a: ['first', 'second'] }],
641
- });
642
- ed.onCellDoubleClick(0, 0);
643
- expect(ctx.editingCell.value).toBe('first');
644
- });
645
- it('uses empty string for a single-select chips editor seeded from an empty array', () => {
646
- const { ctx, ed } = editingFor({
647
- columns: [{ id: 'a', field: 'a', editorType: 'chips' }],
648
- data: [{ a: [] }],
649
- });
650
- ed.onCellDoubleClick(0, 0);
651
- expect(ctx.editingCell.value).toBe('');
652
- });
653
- });
654
- describe('clipboard fallbacks (non-paste branches only)', () => {
655
- // onGridPaste and pasteFromClipboard guard on navigator.clipboard.readText.
656
- // The actual paste transformation is covered by clipboard.test.ts; here we
657
- // only assert the early-return guards so we do not duplicate that coverage.
658
- it('pasteFromClipboard returns early when the async API is unavailable', async () => {
659
- Object.defineProperty(navigator, 'clipboard', {
660
- value: undefined,
661
- configurable: true,
662
- });
663
- const { ctx, ed } = editingFor();
664
- await ed.pasteFromClipboard();
665
- expect(ctx.grid.store.setState).not.toHaveBeenCalled();
666
- });
667
- it('pasteFromClipboard swallows a rejected readText', async () => {
668
- Object.defineProperty(navigator, 'clipboard', {
669
- value: { readText: vi.fn().mockRejectedValue(new Error('denied')) },
670
- configurable: true,
671
- });
672
- const { ctx, ed } = editingFor();
673
- await expect(ed.pasteFromClipboard()).resolves.toBeUndefined();
674
- expect(ctx.grid.store.setState).not.toHaveBeenCalled();
675
- });
676
- it('onGridPaste ignores the native event when the async API is present', () => {
677
- Object.defineProperty(navigator, 'clipboard', {
678
- value: { readText: vi.fn() },
679
- configurable: true,
680
- });
681
- const { ctx, ed } = editingFor();
682
- const ev = {
683
- clipboardData: { getData: () => 'a\tb' },
684
- preventDefault: vi.fn(),
685
- };
686
- ed.onGridPaste(ev);
687
- expect(ev.preventDefault).not.toHaveBeenCalled();
688
- expect(ctx.grid.store.setState).not.toHaveBeenCalled();
689
- });
690
- it('onGridPaste returns early on empty clipboard text in the fallback path', () => {
691
- Object.defineProperty(navigator, 'clipboard', {
692
- value: undefined,
693
- configurable: true,
694
- });
695
- const { ctx, ed } = editingFor();
696
- const ev = {
697
- clipboardData: { getData: () => '' },
698
- preventDefault: vi.fn(),
699
- };
700
- ed.onGridPaste(ev);
701
- expect(ev.preventDefault).not.toHaveBeenCalled();
702
- expect(ctx.grid.store.setState).not.toHaveBeenCalled();
703
- });
704
- it('onGridPaste applies pasted text via the fallback path', () => {
705
- Object.defineProperty(navigator, 'clipboard', {
706
- value: undefined,
707
- configurable: true,
708
- });
709
- const { ctx, ed } = editingFor({
710
- columns: [
711
- { id: 'a', field: 'a', editorType: 'text', editable: true },
712
- { id: 'b', field: 'b', editorType: 'text', editable: true },
713
- ],
714
- data: [
715
- { a: '', b: '' },
716
- { a: '', b: '' },
717
- ],
718
- });
719
- ctx.selectionRange = {
720
- anchor: { rowIndex: 0, colIndex: 0 },
721
- focus: { rowIndex: 0, colIndex: 0 },
722
- };
723
- const ev = {
724
- clipboardData: { getData: () => 'X\tY\nZ\tW' },
725
- preventDefault: vi.fn(),
726
- };
727
- ed.onGridPaste(ev);
728
- expect(ev.preventDefault).toHaveBeenCalled();
729
- expect(ctx.internalData[0]).toMatchObject({ a: 'X', b: 'Y' });
730
- expect(ctx.internalData[1]).toMatchObject({ a: 'Z', b: 'W' });
731
- });
732
- });