@svgrid/grid 1.1.0 → 1.1.1

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 (117) hide show
  1. package/dist/GridMenus.svelte +205 -127
  2. package/dist/SvGrid.controller.svelte.d.ts +44 -1
  3. package/dist/SvGrid.controller.svelte.js +223 -38
  4. package/dist/SvGrid.css +2012 -1709
  5. package/dist/SvGrid.helpers.test.d.ts +1 -0
  6. package/dist/SvGrid.helpers.test.js +298 -0
  7. package/dist/SvGrid.svelte +2346 -2043
  8. package/dist/SvGrid.types.d.ts +91 -4
  9. package/dist/aligned-grids.d.ts +6 -0
  10. package/dist/aligned-grids.js +84 -0
  11. package/dist/aligned-grids.test.d.ts +1 -0
  12. package/dist/aligned-grids.test.js +75 -0
  13. package/dist/build-api.coverage.test.d.ts +20 -0
  14. package/dist/build-api.coverage.test.js +505 -0
  15. package/dist/build-api.js +59 -29
  16. package/dist/cell-render.test.d.ts +1 -0
  17. package/dist/cell-render.test.js +338 -0
  18. package/dist/chart-export.test.d.ts +1 -0
  19. package/dist/chart-export.test.js +302 -0
  20. package/dist/chart.coverage.test.d.ts +1 -0
  21. package/dist/chart.coverage.test.js +748 -0
  22. package/dist/clipboard.js +88 -24
  23. package/dist/clipboard.test.d.ts +1 -0
  24. package/dist/clipboard.test.js +700 -0
  25. package/dist/collaboration.coverage.test.d.ts +1 -0
  26. package/dist/collaboration.coverage.test.js +200 -0
  27. package/dist/column-groups.d.ts +19 -0
  28. package/dist/column-groups.js +62 -0
  29. package/dist/column-groups.test.d.ts +1 -0
  30. package/dist/column-groups.test.js +56 -0
  31. package/dist/column-types.d.ts +10 -0
  32. package/dist/column-types.js +63 -0
  33. package/dist/column-types.test.d.ts +1 -0
  34. package/dist/column-types.test.js +62 -0
  35. package/dist/columns.test.d.ts +1 -0
  36. package/dist/columns.test.js +625 -0
  37. package/dist/core.d.ts +85 -0
  38. package/dist/editing.d.ts +7 -0
  39. package/dist/editing.js +191 -5
  40. package/dist/editing.test.d.ts +1 -0
  41. package/dist/editing.test.js +732 -0
  42. package/dist/editors/cell-editors.coverage.test.d.ts +1 -0
  43. package/dist/editors/cell-editors.coverage.test.js +139 -0
  44. package/dist/facet-buckets.test.d.ts +1 -0
  45. package/dist/facet-buckets.test.js +296 -0
  46. package/dist/filter-operators.test.d.ts +1 -0
  47. package/dist/filter-operators.test.js +135 -0
  48. package/dist/hyperformula-adapter.test.d.ts +1 -0
  49. package/dist/hyperformula-adapter.test.js +205 -0
  50. package/dist/index.d.ts +5 -2
  51. package/dist/index.js +4 -1
  52. package/dist/keyboard-handlers.coverage.test.d.ts +1 -0
  53. package/dist/keyboard-handlers.coverage.test.js +495 -0
  54. package/dist/keyboard-handlers.js +7 -2
  55. package/dist/menus.js +1 -0
  56. package/dist/menus.test.d.ts +1 -0
  57. package/dist/menus.test.js +560 -0
  58. package/dist/named-views.coverage.test.d.ts +1 -0
  59. package/dist/named-views.coverage.test.js +180 -0
  60. package/dist/row-drag.d.ts +49 -0
  61. package/dist/row-drag.js +221 -0
  62. package/dist/row-drag.test.d.ts +1 -0
  63. package/dist/row-drag.test.js +142 -0
  64. package/dist/row-resize.test.d.ts +1 -0
  65. package/dist/row-resize.test.js +329 -0
  66. package/dist/scroll-sync.js +3 -0
  67. package/dist/scroll-sync.test.d.ts +1 -0
  68. package/dist/scroll-sync.test.js +290 -0
  69. package/dist/selection.d.ts +7 -1
  70. package/dist/selection.js +76 -36
  71. package/dist/selection.multi-range.test.d.ts +1 -0
  72. package/dist/selection.multi-range.test.js +55 -0
  73. package/dist/selection.test.d.ts +1 -0
  74. package/dist/selection.test.js +647 -0
  75. package/dist/server-data-source.coverage.test.d.ts +1 -0
  76. package/dist/server-data-source.coverage.test.js +154 -0
  77. package/dist/spreadsheet.d.ts +30 -0
  78. package/dist/spreadsheet.js +48 -0
  79. package/dist/spreadsheet.test.d.ts +1 -0
  80. package/dist/spreadsheet.test.js +446 -0
  81. package/dist/sv-grid-scrollbar.js +13 -1
  82. package/dist/svgrid-wrapper.types.d.ts +19 -0
  83. package/dist/svgrid.behavior.test.js +20 -0
  84. package/dist/svgrid.interaction.test.js +31 -0
  85. package/dist/svgrid.new-features.wrapper.test.js +34 -2
  86. package/dist/test-setup.js +9 -3
  87. package/dist/virtualization/scroll-scaling.d.ts +17 -0
  88. package/dist/virtualization/scroll-scaling.js +35 -0
  89. package/dist/virtualization/scroll-scaling.test.js +42 -1
  90. package/package.json +2 -1
  91. package/src/GridMenus.svelte +205 -127
  92. package/src/SvGrid.controller.svelte.ts +2352 -2195
  93. package/src/SvGrid.css +2012 -1747
  94. package/src/SvGrid.svelte +2346 -2047
  95. package/src/SvGrid.types.ts +537 -456
  96. package/src/aligned-grids.test.ts +80 -0
  97. package/src/aligned-grids.ts +87 -0
  98. package/src/build-api.ts +683 -663
  99. package/src/clipboard.test.ts +49 -0
  100. package/src/clipboard.ts +51 -23
  101. package/src/column-groups.test.ts +59 -0
  102. package/src/column-groups.ts +80 -0
  103. package/src/column-types.test.ts +68 -0
  104. package/src/column-types.ts +82 -0
  105. package/src/core.ts +78 -0
  106. package/src/editing.ts +669 -513
  107. package/src/index.ts +12 -0
  108. package/src/menus.ts +1 -0
  109. package/src/row-drag.test.ts +168 -0
  110. package/src/row-drag.ts +255 -0
  111. package/src/scroll-sync.ts +2 -0
  112. package/src/selection.multi-range.test.ts +61 -0
  113. package/src/selection.ts +71 -37
  114. package/src/spreadsheet.test.ts +489 -445
  115. package/src/spreadsheet.ts +304 -246
  116. package/src/svgrid-wrapper.types.ts +19 -0
  117. package/src/svgrid.new-features.wrapper.test.ts +2 -2
@@ -0,0 +1,338 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { createCellRender } from './cell-render';
3
+ function makeRow(id, data) {
4
+ return {
5
+ id,
6
+ original: data,
7
+ getCellValueByColumnId: (colId) => data[colId],
8
+ };
9
+ }
10
+ function makeColumn(id, def = {}) {
11
+ return { id, columnDef: { ...def } };
12
+ }
13
+ function makeCtx(overrides = {}) {
14
+ return {
15
+ props: {},
16
+ grid: { id: 'grid' },
17
+ conditionalColumnStats: new Map(),
18
+ noteOverrides: {},
19
+ editorOptionsCache: {},
20
+ ...overrides,
21
+ };
22
+ }
23
+ describe('createCellRender / cellConditionalFormat', () => {
24
+ it('returns null when no conditionalFormats are configured', () => {
25
+ const cr = createCellRender(makeCtx());
26
+ const out = cr.cellConditionalFormat(makeRow('r1', { amt: 5 }), makeColumn('amt'), 5);
27
+ expect(out).toBeNull();
28
+ });
29
+ it('returns null when conditionalFormats is an empty array', () => {
30
+ const cr = createCellRender(makeCtx({ props: { conditionalFormats: [] } }));
31
+ expect(cr.cellConditionalFormat(makeRow('r', {}), makeColumn('amt'), 1)).toBeNull();
32
+ });
33
+ it('resolves a rule format against the value', () => {
34
+ const formats = [
35
+ {
36
+ type: 'rule',
37
+ columns: ['amt'],
38
+ when: ({ value }) => Number(value) > 10,
39
+ background: '#ff0000',
40
+ fontWeight: 700,
41
+ },
42
+ ];
43
+ const cr = createCellRender(makeCtx({ props: { conditionalFormats: formats } }));
44
+ const hit = cr.cellConditionalFormat(makeRow('r', { amt: 20 }), makeColumn('amt'), 20);
45
+ expect(hit?.background).toBe('#ff0000');
46
+ expect(hit?.fontWeight).toBe(700);
47
+ const miss = cr.cellConditionalFormat(makeRow('r', { amt: 1 }), makeColumn('amt'), 1);
48
+ expect(miss).toEqual({});
49
+ });
50
+ it('passes the precomputed column stat through to colorScale formats', () => {
51
+ const formats = [{ type: 'colorScale', min: '#fff', max: '#000' }];
52
+ const stats = new Map([['amt', { min: 0, max: 100 }]]);
53
+ const cr = createCellRender(makeCtx({ props: { conditionalFormats: formats }, conditionalColumnStats: stats }));
54
+ const out = cr.cellConditionalFormat(makeRow('r', { amt: 50 }), makeColumn('amt'), 50);
55
+ expect(out).not.toBeNull();
56
+ expect(out?.background).toBeDefined();
57
+ });
58
+ });
59
+ describe('createCellRender / computeRowClass', () => {
60
+ it('returns empty string when no rowClass configured', () => {
61
+ const cr = createCellRender(makeCtx());
62
+ expect(cr.computeRowClass(makeRow('r', {}), 0)).toBe('');
63
+ });
64
+ it('resolves a string rowClass', () => {
65
+ const rowClass = vi.fn(() => 'highlight');
66
+ const cr = createCellRender(makeCtx({ props: { rowClass } }));
67
+ const row = makeRow('r', { a: 1 });
68
+ expect(cr.computeRowClass(row, 3)).toBe('highlight');
69
+ expect(rowClass).toHaveBeenCalledWith({ row: row.original, rowIndex: 3 });
70
+ });
71
+ it('resolves an array / object rowClass', () => {
72
+ const cr = createCellRender(makeCtx({ props: { rowClass: () => ['a', '', 'b'] } }));
73
+ expect(cr.computeRowClass(makeRow('r', {}), 0)).toBe('a b');
74
+ const cr2 = createCellRender(makeCtx({ props: { rowClass: () => ({ on: true, off: false }) } }));
75
+ expect(cr2.computeRowClass(makeRow('r', {}), 0)).toBe('on');
76
+ });
77
+ });
78
+ describe('createCellRender / computeCellClass', () => {
79
+ it('returns empty string when cellClass is null/undefined', () => {
80
+ const cr = createCellRender(makeCtx());
81
+ expect(cr.computeCellClass(makeRow('r', {}), makeColumn('a'))).toBe('');
82
+ });
83
+ it('resolves a string cellClass', () => {
84
+ const cr = createCellRender(makeCtx());
85
+ expect(cr.computeCellClass(makeRow('r', {}), makeColumn('a', { cellClass: 'red' }))).toBe('red');
86
+ });
87
+ it('resolves an array cellClass', () => {
88
+ const cr = createCellRender(makeCtx());
89
+ expect(cr.computeCellClass(makeRow('r', {}), makeColumn('a', { cellClass: ['x', 'y'] }))).toBe('x y');
90
+ });
91
+ it('invokes a function cellClass with a minimal cell context', () => {
92
+ const cellClass = vi.fn((c) => (c.getValue() > 5 ? 'big' : 'small'));
93
+ const cr = createCellRender(makeCtx());
94
+ const row = makeRow('r', { score: 9 });
95
+ expect(cr.computeCellClass(row, makeColumn('score', { cellClass }))).toBe('big');
96
+ expect(cellClass).toHaveBeenCalledTimes(1);
97
+ const passed = cellClass.mock.calls[0][0];
98
+ expect(passed.row).toBe(row);
99
+ expect(passed.getValue()).toBe(9);
100
+ });
101
+ it('returns empty string for an unsupported cellClass type (number)', () => {
102
+ const cr = createCellRender(makeCtx());
103
+ expect(cr.computeCellClass(makeRow('r', {}), makeColumn('a', { cellClass: 123 }))).toBe('');
104
+ });
105
+ });
106
+ describe('createCellRender / computeCellTooltip', () => {
107
+ it('returns null when no tooltip configured', () => {
108
+ const cr = createCellRender(makeCtx());
109
+ expect(cr.computeCellTooltip(makeRow('r', {}), makeColumn('a'))).toBeNull();
110
+ });
111
+ it('returns a static string tooltip, but null for empty string', () => {
112
+ const cr = createCellRender(makeCtx());
113
+ expect(cr.computeCellTooltip(makeRow('r', {}), makeColumn('a', { tooltip: 'help' }))).toBe('help');
114
+ expect(cr.computeCellTooltip(makeRow('r', {}), makeColumn('a', { tooltip: '' }))).toBeNull();
115
+ });
116
+ it('invokes a function tooltip and stringifies the result', () => {
117
+ const tooltip = (c) => `val=${c.getValue()}`;
118
+ const cr = createCellRender(makeCtx());
119
+ const row = makeRow('r', { a: 42 });
120
+ expect(cr.computeCellTooltip(row, makeColumn('a', { tooltip }))).toBe('val=42');
121
+ });
122
+ it('returns null when the function tooltip returns a falsy value', () => {
123
+ const cr = createCellRender(makeCtx());
124
+ expect(cr.computeCellTooltip(makeRow('r', {}), makeColumn('a', { tooltip: () => '' }))).toBeNull();
125
+ });
126
+ it('returns null for an unsupported tooltip type', () => {
127
+ const cr = createCellRender(makeCtx());
128
+ expect(cr.computeCellTooltip(makeRow('r', {}), makeColumn('a', { tooltip: 5 }))).toBeNull();
129
+ });
130
+ });
131
+ describe('createCellRender / computeCellNote', () => {
132
+ it('returns null when neither overrides nor notes have an entry', () => {
133
+ const cr = createCellRender(makeCtx());
134
+ expect(cr.computeCellNote(makeRow('r', {}), makeColumn('a'))).toBeNull();
135
+ });
136
+ it('prefers the internal note override over props.notes', () => {
137
+ const cr = createCellRender(makeCtx({
138
+ noteOverrides: { r: { a: 'overlay' } },
139
+ props: { notes: { r: { a: 'stored' } } },
140
+ }));
141
+ expect(cr.computeCellNote(makeRow('r', {}), makeColumn('a'))).toBe('overlay');
142
+ });
143
+ it('treats a blank override as a removal (null)', () => {
144
+ const cr = createCellRender(makeCtx({
145
+ noteOverrides: { r: { a: ' ' } },
146
+ props: { notes: { r: { a: 'stored' } } },
147
+ }));
148
+ expect(cr.computeCellNote(makeRow('r', {}), makeColumn('a'))).toBeNull();
149
+ });
150
+ it('falls back to props.notes when no override exists', () => {
151
+ const cr = createCellRender(makeCtx({ props: { notes: { r: { a: 'stored note' } } } }));
152
+ expect(cr.computeCellNote(makeRow('r', {}), makeColumn('a'))).toBe('stored note');
153
+ });
154
+ it('returns null for a whitespace-only stored note', () => {
155
+ const cr = createCellRender(makeCtx({ props: { notes: { r: { a: ' ' } } } }));
156
+ expect(cr.computeCellNote(makeRow('r', {}), makeColumn('a'))).toBeNull();
157
+ });
158
+ it('returns null when the row or column is absent in the notes map', () => {
159
+ const cr = createCellRender(makeCtx({ props: { notes: { other: { a: 'x' } } } }));
160
+ expect(cr.computeCellNote(makeRow('r', {}), makeColumn('a'))).toBeNull();
161
+ const cr2 = createCellRender(makeCtx({ props: { notes: { r: { b: 'x' } } } }));
162
+ expect(cr2.computeCellNote(makeRow('r', {}), makeColumn('a'))).toBeNull();
163
+ });
164
+ });
165
+ describe('createCellRender / getColumnEditorOptions', () => {
166
+ it('returns [] for a function editorOptions when row has no original', () => {
167
+ const cr = createCellRender(makeCtx());
168
+ const col = makeColumn('a', { editorOptions: () => [{ value: 1, label: 'one' }] });
169
+ expect(cr.getColumnEditorOptions(col, null)).toEqual([]);
170
+ });
171
+ it('evaluates a function editorOptions per row', () => {
172
+ const cr = createCellRender(makeCtx());
173
+ const col = makeColumn('a', {
174
+ editorOptions: (data) => [{ value: data.code, label: data.name }],
175
+ });
176
+ const row = makeRow('r', { code: 'x', name: 'X' });
177
+ const opts = cr.getColumnEditorOptions(col, row);
178
+ expect(opts).toEqual([{ value: 'x', label: 'X' }]);
179
+ });
180
+ it('caches normalized static editorOptions and reuses them', () => {
181
+ const ctx = makeCtx();
182
+ const cr = createCellRender(ctx);
183
+ const src = ['a', 'b'];
184
+ const col = makeColumn('a', { editorOptions: src });
185
+ const first = cr.getColumnEditorOptions(col);
186
+ const second = cr.getColumnEditorOptions(col);
187
+ expect(first).toBe(second); // same cached array reference
188
+ expect(ctx.editorOptionsCache['a']).toBe(first);
189
+ });
190
+ it('re-normalizes when the editorOptions source changes', () => {
191
+ const ctx = makeCtx();
192
+ const cr = createCellRender(ctx);
193
+ const first = cr.getColumnEditorOptions(makeColumn('a', { editorOptions: ['a'] }));
194
+ const second = cr.getColumnEditorOptions(makeColumn('a', { editorOptions: ['b'] }));
195
+ expect(first).not.toBe(second);
196
+ });
197
+ });
198
+ describe('createCellRender / formatListCellValue', () => {
199
+ it('joins an array of values using the default separator', () => {
200
+ const cr = createCellRender(makeCtx());
201
+ const col = makeColumn('a', {
202
+ editorOptions: [
203
+ { value: 1, label: 'One' },
204
+ { value: 2, label: 'Two' },
205
+ ],
206
+ });
207
+ expect(cr.formatListCellValue(col, [1, 2])).toBe('One, Two');
208
+ });
209
+ it('honors a custom editorSeparator', () => {
210
+ const cr = createCellRender(makeCtx());
211
+ const col = makeColumn('a', {
212
+ editorSeparator: ' | ',
213
+ editorOptions: [
214
+ { value: 1, label: 'One' },
215
+ { value: 2, label: 'Two' },
216
+ ],
217
+ });
218
+ expect(cr.formatListCellValue(col, [1, 2])).toBe('One | Two');
219
+ });
220
+ it('returns empty string for null / empty scalar values', () => {
221
+ const cr = createCellRender(makeCtx());
222
+ const col = makeColumn('a', { editorOptions: [] });
223
+ expect(cr.formatListCellValue(col, null)).toBe('');
224
+ expect(cr.formatListCellValue(col, '')).toBe('');
225
+ });
226
+ it('maps a single scalar value to its label', () => {
227
+ const cr = createCellRender(makeCtx());
228
+ const col = makeColumn('a', {
229
+ editorOptions: [{ value: 'k', label: 'Known' }],
230
+ });
231
+ expect(cr.formatListCellValue(col, 'k')).toBe('Known');
232
+ // unknown value falls back to its string form
233
+ expect(cr.formatListCellValue(col, 'z')).toBe('z');
234
+ });
235
+ });
236
+ describe('createCellRender / formatCellValue', () => {
237
+ const row = makeRow('r', {});
238
+ it('uses a custom formatter callback when present', () => {
239
+ const cr = createCellRender(makeCtx());
240
+ const col = makeColumn('a', {
241
+ formatter: ({ value }) => `<<${value}>>`,
242
+ });
243
+ expect(cr.formatCellValue(col, 7, row)).toBe('<<7>>');
244
+ });
245
+ it('coerces a nullish formatter result to empty string', () => {
246
+ const cr = createCellRender(makeCtx());
247
+ const col = makeColumn('a', { formatter: () => null });
248
+ expect(cr.formatCellValue(col, 1, row)).toBe('');
249
+ });
250
+ it('masks password columns with bullets, capped at 12', () => {
251
+ const cr = createCellRender(makeCtx());
252
+ const col = makeColumn('a', { editorType: 'password' });
253
+ expect(cr.formatCellValue(col, 'abc', row)).toBe('•••');
254
+ expect(cr.formatCellValue(col, '', row)).toBe('');
255
+ expect(cr.formatCellValue(col, 'x'.repeat(50), row)).toBe('•'.repeat(12));
256
+ });
257
+ it('stringifies plain values when there is no format config', () => {
258
+ const cr = createCellRender(makeCtx());
259
+ const col = makeColumn('a');
260
+ expect(cr.formatCellValue(col, 'hello', row)).toBe('hello');
261
+ expect(cr.formatCellValue(col, null, row)).toBe('');
262
+ });
263
+ it('applies numeric / currency / percent format configs', () => {
264
+ const cr = createCellRender(makeCtx());
265
+ expect(cr.formatCellValue(makeColumn('a', { format: { type: 'number' } }), 1234.5, row)).toContain('1,234');
266
+ const cur = cr.formatCellValue(makeColumn('a', { format: { type: 'currency', currency: 'USD' } }), 1000, row);
267
+ expect(cur).toContain('$');
268
+ const pct = cr.formatCellValue(makeColumn('a', { format: { type: 'percent' } }), 0.25, row);
269
+ expect(pct).toContain('%');
270
+ });
271
+ it('formats date configs and falls back when the value is not a date', () => {
272
+ const cr = createCellRender(makeCtx());
273
+ const dateCol = makeColumn('a', { format: { type: 'date' } });
274
+ const out = cr.formatCellValue(dateCol, '2024-01-15T00:00:00Z', row);
275
+ expect(out).toMatch(/2024/);
276
+ // Unparseable -> returns String(value)
277
+ expect(cr.formatCellValue(dateCol, 'not-a-date', row)).toBe('not-a-date');
278
+ });
279
+ it('formats datetime configs with merged default options', () => {
280
+ const cr = createCellRender(makeCtx());
281
+ const col = makeColumn('a', { format: { type: 'datetime' } });
282
+ const out = cr.formatCellValue(col, '2024-03-10T13:45:00Z', row);
283
+ expect(out).toMatch(/2024/);
284
+ });
285
+ });
286
+ describe('createCellRender / formatPinnedValue', () => {
287
+ it('invokes a formatter with a null row', () => {
288
+ const cr = createCellRender(makeCtx());
289
+ const formatter = vi.fn(({ value, row }) => `${value}:${row}`);
290
+ const col = makeColumn('a', { formatter });
291
+ expect(cr.formatPinnedValue(col, 5)).toBe('5:null');
292
+ expect(formatter.mock.calls[0][0].row).toBeNull();
293
+ });
294
+ it('masks password pinned values', () => {
295
+ const cr = createCellRender(makeCtx());
296
+ expect(cr.formatPinnedValue(makeColumn('a', { editorType: 'password' }), 'pw')).toBe('••');
297
+ expect(cr.formatPinnedValue(makeColumn('a', { editorType: 'password' }), '')).toBe('');
298
+ });
299
+ it('stringifies when no format config', () => {
300
+ const cr = createCellRender(makeCtx());
301
+ expect(cr.formatPinnedValue(makeColumn('a'), 'total')).toBe('total');
302
+ expect(cr.formatPinnedValue(makeColumn('a'), null)).toBe('');
303
+ });
304
+ it('applies numeric and date format configs', () => {
305
+ const cr = createCellRender(makeCtx());
306
+ expect(cr.formatPinnedValue(makeColumn('a', { format: { type: 'number' } }), 9999)).toContain('9,999');
307
+ const d = cr.formatPinnedValue(makeColumn('a', { format: { type: 'date' } }), '2022-06-01T00:00:00Z');
308
+ expect(d).toMatch(/2022/);
309
+ // unparseable date falls through to String()
310
+ expect(cr.formatPinnedValue(makeColumn('a', { format: { type: 'date' } }), 'xyz')).toBe('xyz');
311
+ });
312
+ it('formats datetime pinned configs', () => {
313
+ const cr = createCellRender(makeCtx());
314
+ const out = cr.formatPinnedValue(makeColumn('a', { format: { type: 'datetime' } }), '2021-12-31T08:00:00Z');
315
+ expect(out).toMatch(/2021/);
316
+ });
317
+ });
318
+ describe('createCellRender / computePinnedCellClass', () => {
319
+ it('returns empty string when cellClass is not a function', () => {
320
+ const cr = createCellRender(makeCtx());
321
+ expect(cr.computePinnedCellClass({ a: 1 }, makeColumn('a', { cellClass: 'x' }))).toBe('');
322
+ });
323
+ it('invokes a function cellClass with a null row and the pinned value', () => {
324
+ const cr = createCellRender(makeCtx());
325
+ const cellClass = vi.fn((c) => (c.value > 0 ? 'pos' : 'neg'));
326
+ const col = makeColumn('a', {
327
+ cellClass,
328
+ field: 'a',
329
+ });
330
+ expect(cr.computePinnedCellClass({ a: 3 }, col)).toBe('pos');
331
+ expect(cellClass.mock.calls[0][0].row).toBeNull();
332
+ });
333
+ it('coerces a falsy cellClass result to empty string', () => {
334
+ const cr = createCellRender(makeCtx());
335
+ const col = makeColumn('a', { cellClass: () => undefined, field: 'a' });
336
+ expect(cr.computePinnedCellClass({ a: 1 }, col)).toBe('');
337
+ });
338
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,302 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import { chartToSvgString, chartToPngBlob, downloadChartSvg, downloadChartPng, } from './chart-export';
3
+ const SVG_NS = 'http://www.w3.org/2000/svg';
4
+ /**
5
+ * Build a small but realistic chart SVG matching the structure
6
+ * chart-export expects: `svg.sv-grid-chart-svg` with a viewBox, an
7
+ * interaction hit layer (`.sv-grid-chart-hit`) that the exporter must
8
+ * strip, plus a few themed nodes.
9
+ */
10
+ function makeChartSvg(opts = {}) {
11
+ const svg = document.createElementNS(SVG_NS, 'svg');
12
+ svg.setAttribute('class', 'sv-grid-chart-svg');
13
+ svg.setAttribute('viewBox', opts.viewBox ?? '0 0 400 250');
14
+ const axis = document.createElementNS(SVG_NS, 'text');
15
+ axis.setAttribute('class', 'sv-grid-chart-axis');
16
+ axis.textContent = 'A';
17
+ svg.appendChild(axis);
18
+ const hit = document.createElementNS(SVG_NS, 'rect');
19
+ hit.setAttribute('class', 'sv-grid-chart-hit');
20
+ svg.appendChild(hit);
21
+ const bar = document.createElementNS(SVG_NS, 'rect');
22
+ bar.setAttribute('class', 'sv-grid-chart-bar');
23
+ svg.appendChild(bar);
24
+ return svg;
25
+ }
26
+ describe('chartToSvgString', () => {
27
+ it('serializes the chart svg passed directly as an SVGSVGElement', () => {
28
+ const svg = makeChartSvg();
29
+ const out = chartToSvgString(svg);
30
+ expect(out).toContain('<svg');
31
+ expect(out).toContain('xmlns="http://www.w3.org/2000/svg"');
32
+ });
33
+ it('resolves the inner svg.sv-grid-chart-svg from a wrapping element', () => {
34
+ const wrap = document.createElement('div');
35
+ wrap.appendChild(makeChartSvg());
36
+ const out = chartToSvgString(wrap);
37
+ expect(out).toContain('<svg');
38
+ });
39
+ it('falls back to a plain inner <svg> when the class svg is absent', () => {
40
+ const wrap = document.createElement('div');
41
+ const svg = document.createElementNS(SVG_NS, 'svg');
42
+ svg.setAttribute('viewBox', '0 0 100 100');
43
+ wrap.appendChild(svg);
44
+ const out = chartToSvgString(wrap);
45
+ expect(out).toContain('<svg');
46
+ });
47
+ it('throws when no <svg> can be found', () => {
48
+ const div = document.createElement('div');
49
+ expect(() => chartToSvgString(div)).toThrow(/no <svg> found/);
50
+ });
51
+ it('inlines a <style> block with concrete theme colors', () => {
52
+ const out = chartToSvgString(makeChartSvg());
53
+ expect(out).toContain('<style');
54
+ expect(out).toContain('.sv-grid-chart-axis');
55
+ expect(out).toContain('.sv-grid-chart-datalabel');
56
+ expect(out).toContain('.sv-grid-chart-donut-total');
57
+ // default fallbacks used when no --sg-* vars are set in jsdom
58
+ expect(out).toContain('#0f172a'); // --sg-fg fallback
59
+ expect(out).toContain('#64748b'); // --sg-muted fallback
60
+ expect(out).toContain('#e2e8f0'); // --sg-border fallback
61
+ });
62
+ it('strips interaction-only hit layers from the export', () => {
63
+ const out = chartToSvgString(makeChartSvg());
64
+ expect(out).not.toContain('sv-grid-chart-hit');
65
+ // but keeps the real content
66
+ expect(out).toContain('sv-grid-chart-bar');
67
+ });
68
+ it('sets width/height from the viewBox and inserts a background rect', () => {
69
+ const out = chartToSvgString(makeChartSvg({ viewBox: '0 0 400 250' }));
70
+ expect(out).toContain('width="400"');
71
+ expect(out).toContain('height="250"');
72
+ // background rect spans the full size with the default --sg-bg fallback
73
+ expect(out).toMatch(/<rect[^>]*fill="#ffffff"/);
74
+ });
75
+ it('honors a custom background option over the theme default', () => {
76
+ const out = chartToSvgString(makeChartSvg(), { background: 'tomato' });
77
+ expect(out).toMatch(/<rect[^>]*fill="tomato"/);
78
+ expect(out).not.toContain('fill="#ffffff"');
79
+ });
80
+ it('falls back to client size / defaults when no usable viewBox', () => {
81
+ const svg = document.createElementNS(SVG_NS, 'svg');
82
+ svg.setAttribute('class', 'sv-grid-chart-svg');
83
+ // No viewBox attribute => baseVal width 0; jsdom clientWidth is 0 too,
84
+ // so the exporter uses its 520x300 hard fallback.
85
+ const out = chartToSvgString(svg);
86
+ expect(out).toContain('width="520"');
87
+ expect(out).toContain('height="300"');
88
+ });
89
+ it('reads --sg-* css variables when present on the element', () => {
90
+ const svg = makeChartSvg();
91
+ svg.style.setProperty('--sg-fg', '#112233');
92
+ svg.style.setProperty('--sg-muted', '#445566');
93
+ document.body.appendChild(svg);
94
+ try {
95
+ const out = chartToSvgString(svg);
96
+ expect(out).toContain('#112233');
97
+ expect(out).toContain('#445566');
98
+ }
99
+ finally {
100
+ svg.remove();
101
+ }
102
+ });
103
+ });
104
+ describe('downloadChartSvg', () => {
105
+ let createSpy;
106
+ let revokeSpy;
107
+ let clickSpy;
108
+ beforeEach(() => {
109
+ vi.useFakeTimers();
110
+ createSpy = vi
111
+ .spyOn(URL, 'createObjectURL')
112
+ .mockReturnValue('blob:fake-url');
113
+ revokeSpy = vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => { });
114
+ clickSpy = vi.fn();
115
+ // jsdom anchors don't navigate; stub click to observe the download.
116
+ vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(clickSpy);
117
+ });
118
+ afterEach(() => {
119
+ vi.useRealTimers();
120
+ vi.restoreAllMocks();
121
+ });
122
+ it('creates a blob url, triggers an anchor download, then revokes', () => {
123
+ downloadChartSvg(makeChartSvg(), 'my-chart.svg');
124
+ expect(createSpy).toHaveBeenCalledTimes(1);
125
+ const blob = createSpy.mock.calls[0][0];
126
+ expect(blob).toBeInstanceOf(Blob);
127
+ expect(blob.type).toBe('image/svg+xml');
128
+ expect(clickSpy).toHaveBeenCalledTimes(1);
129
+ // revoke is scheduled on a timer
130
+ expect(revokeSpy).not.toHaveBeenCalled();
131
+ vi.advanceTimersByTime(1000);
132
+ expect(revokeSpy).toHaveBeenCalledWith('blob:fake-url');
133
+ });
134
+ it('removes the temporary anchor from the DOM after clicking', () => {
135
+ const before = document.querySelectorAll('a').length;
136
+ downloadChartSvg(makeChartSvg());
137
+ expect(document.querySelectorAll('a').length).toBe(before);
138
+ });
139
+ it('uses the default filename when none is given', () => {
140
+ let captured;
141
+ vi.spyOn(HTMLAnchorElement.prototype, 'download', 'set').mockImplementation(function (v) {
142
+ captured = v;
143
+ });
144
+ downloadChartSvg(makeChartSvg());
145
+ expect(captured).toBe('chart.svg');
146
+ });
147
+ });
148
+ describe('chartToPngBlob', () => {
149
+ afterEach(() => {
150
+ vi.restoreAllMocks();
151
+ });
152
+ function stubImage(behavior) {
153
+ // jsdom's Image never fires load/error for a blob: src, so replace it
154
+ // with a controllable fake that triggers the chosen callback.
155
+ class FakeImage {
156
+ constructor() {
157
+ this.onload = null;
158
+ this.onerror = null;
159
+ this._src = '';
160
+ }
161
+ set src(_v) {
162
+ this._src = _v;
163
+ queueMicrotask(() => {
164
+ if (behavior === 'load')
165
+ this.onload?.();
166
+ else
167
+ this.onerror?.();
168
+ });
169
+ }
170
+ get src() {
171
+ return this._src;
172
+ }
173
+ }
174
+ vi.stubGlobal('Image', FakeImage);
175
+ }
176
+ it('rejects when the SVG image fails to rasterize', async () => {
177
+ stubImage('error');
178
+ vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:x');
179
+ const revoke = vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => { });
180
+ await expect(chartToPngBlob(makeChartSvg())).rejects.toThrow(/failed to rasterize/);
181
+ expect(revoke).toHaveBeenCalledWith('blob:x');
182
+ });
183
+ it('rejects when a 2d canvas context is unavailable', async () => {
184
+ stubImage('load');
185
+ vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:x');
186
+ vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => { });
187
+ vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue(null);
188
+ await expect(chartToPngBlob(makeChartSvg())).rejects.toThrow(/canvas 2d context unavailable/);
189
+ });
190
+ it('resolves a PNG blob once drawImage + toBlob succeed', async () => {
191
+ stubImage('load');
192
+ vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:x');
193
+ vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => { });
194
+ const drawImage = vi.fn();
195
+ vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({
196
+ drawImage,
197
+ });
198
+ vi.spyOn(HTMLCanvasElement.prototype, 'toBlob').mockImplementation(function (cb) {
199
+ cb(new Blob(['png-bytes'], { type: 'image/png' }));
200
+ });
201
+ const blob = await chartToPngBlob(makeChartSvg({ viewBox: '0 0 400 250' }), {
202
+ scale: 3,
203
+ });
204
+ expect(blob).toBeInstanceOf(Blob);
205
+ expect(blob.type).toBe('image/png');
206
+ expect(drawImage).toHaveBeenCalledTimes(1);
207
+ });
208
+ it('rejects when toBlob yields null', async () => {
209
+ stubImage('load');
210
+ vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:x');
211
+ vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => { });
212
+ vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({
213
+ drawImage: vi.fn(),
214
+ });
215
+ vi.spyOn(HTMLCanvasElement.prototype, 'toBlob').mockImplementation(function (cb) {
216
+ cb(null);
217
+ });
218
+ await expect(chartToPngBlob(makeChartSvg())).rejects.toThrow(/toBlob failed/);
219
+ });
220
+ it('scales the canvas by options.scale (default 2)', async () => {
221
+ stubImage('load');
222
+ vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:x');
223
+ vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => { });
224
+ vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({
225
+ drawImage: vi.fn(),
226
+ });
227
+ let canvasW = -1;
228
+ let canvasH = -1;
229
+ vi.spyOn(HTMLCanvasElement.prototype, 'toBlob').mockImplementation(function (cb) {
230
+ canvasW = this.width;
231
+ canvasH = this.height;
232
+ cb(new Blob([], { type: 'image/png' }));
233
+ });
234
+ // viewBox 400x250, default scale 2 => 800x500
235
+ await chartToPngBlob(makeChartSvg({ viewBox: '0 0 400 250' }));
236
+ expect(canvasW).toBe(800);
237
+ expect(canvasH).toBe(500);
238
+ });
239
+ it('falls back to the 520x300 default size when there is no viewBox', async () => {
240
+ stubImage('load');
241
+ vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:x');
242
+ vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => { });
243
+ vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({
244
+ drawImage: vi.fn(),
245
+ });
246
+ let canvasW = -1;
247
+ let canvasH = -1;
248
+ vi.spyOn(HTMLCanvasElement.prototype, 'toBlob').mockImplementation(function (cb) {
249
+ canvasW = this.width;
250
+ canvasH = this.height;
251
+ cb(new Blob([], { type: 'image/png' }));
252
+ });
253
+ // No viewBox + jsdom clientWidth 0 => 520x300 hard fallback, scale 2.
254
+ const noVb = document.createElementNS(SVG_NS, 'svg');
255
+ noVb.setAttribute('class', 'sv-grid-chart-svg');
256
+ await chartToPngBlob(noVb);
257
+ expect(canvasW).toBe(1040);
258
+ expect(canvasH).toBe(600);
259
+ });
260
+ });
261
+ describe('downloadChartPng', () => {
262
+ afterEach(() => {
263
+ vi.useRealTimers();
264
+ vi.restoreAllMocks();
265
+ });
266
+ it('rasterizes then downloads via an anchor and revokes the url', async () => {
267
+ vi.useFakeTimers();
268
+ // Make chartToPngBlob succeed.
269
+ class FakeImage {
270
+ constructor() {
271
+ this.onload = null;
272
+ this.onerror = null;
273
+ }
274
+ set src(_v) {
275
+ queueMicrotask(() => this.onload?.());
276
+ }
277
+ }
278
+ vi.stubGlobal('Image', FakeImage);
279
+ vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:png-url');
280
+ const revoke = vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => { });
281
+ vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({
282
+ drawImage: vi.fn(),
283
+ });
284
+ vi.spyOn(HTMLCanvasElement.prototype, 'toBlob').mockImplementation(function (cb) {
285
+ cb(new Blob(['p'], { type: 'image/png' }));
286
+ });
287
+ const click = vi.fn();
288
+ vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(click);
289
+ let filename;
290
+ vi.spyOn(HTMLAnchorElement.prototype, 'download', 'set').mockImplementation(function (v) {
291
+ filename = v;
292
+ });
293
+ const p = downloadChartPng(makeChartSvg(), 'pic.png');
294
+ // flush the microtask-driven image onload + promise chain
295
+ await vi.runAllTimersAsync();
296
+ await p;
297
+ expect(click).toHaveBeenCalledTimes(1);
298
+ expect(filename).toBe('pic.png');
299
+ vi.advanceTimersByTime(1000);
300
+ expect(revoke).toHaveBeenCalledWith('blob:png-url');
301
+ });
302
+ });
@@ -0,0 +1 @@
1
+ export {};