@svgrid/grid 1.0.2 → 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 (216) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +137 -39
  3. package/dist/GridFooter.svelte +164 -0
  4. package/dist/GridFooter.svelte.d.ts +29 -0
  5. package/dist/GridMenus.svelte +648 -0
  6. package/dist/GridMenus.svelte.d.ts +31 -0
  7. package/dist/SvGrid.controller.svelte.d.ts +472 -0
  8. package/dist/SvGrid.controller.svelte.js +1917 -0
  9. package/dist/SvGrid.css +2012 -0
  10. package/dist/SvGrid.helpers.d.ts +35 -0
  11. package/dist/SvGrid.helpers.js +160 -0
  12. package/dist/SvGrid.helpers.test.d.ts +1 -0
  13. package/dist/SvGrid.helpers.test.js +298 -0
  14. package/dist/SvGrid.svelte +2346 -8742
  15. package/dist/SvGrid.svelte.d.ts +4 -357
  16. package/dist/SvGrid.types.d.ts +523 -0
  17. package/dist/SvGrid.types.js +1 -0
  18. package/dist/SvGridChart.svelte +1060 -23
  19. package/dist/SvGridChart.svelte.d.ts +17 -0
  20. package/dist/aligned-grids.d.ts +6 -0
  21. package/dist/aligned-grids.js +84 -0
  22. package/dist/aligned-grids.test.d.ts +1 -0
  23. package/dist/aligned-grids.test.js +75 -0
  24. package/dist/build-api.coverage.test.d.ts +20 -0
  25. package/dist/build-api.coverage.test.js +505 -0
  26. package/dist/build-api.d.ts +5 -0
  27. package/dist/build-api.js +557 -0
  28. package/dist/cell-render.d.ts +15 -0
  29. package/dist/cell-render.js +246 -0
  30. package/dist/cell-render.test.d.ts +1 -0
  31. package/dist/cell-render.test.js +338 -0
  32. package/dist/cell-values.d.ts +28 -0
  33. package/dist/cell-values.js +89 -0
  34. package/dist/chart-export.test.d.ts +1 -0
  35. package/dist/chart-export.test.js +302 -0
  36. package/dist/chart.coverage.test.d.ts +1 -0
  37. package/dist/chart.coverage.test.js +748 -0
  38. package/dist/chart.d.ts +370 -3
  39. package/dist/chart.js +1135 -42
  40. package/dist/clipboard.d.ts +15 -0
  41. package/dist/clipboard.js +420 -0
  42. package/dist/clipboard.test.d.ts +1 -0
  43. package/dist/clipboard.test.js +700 -0
  44. package/dist/collaboration.coverage.test.d.ts +1 -0
  45. package/dist/collaboration.coverage.test.js +200 -0
  46. package/dist/column-groups.d.ts +19 -0
  47. package/dist/column-groups.js +62 -0
  48. package/dist/column-groups.test.d.ts +1 -0
  49. package/dist/column-groups.test.js +56 -0
  50. package/dist/column-types.d.ts +10 -0
  51. package/dist/column-types.js +63 -0
  52. package/dist/column-types.test.d.ts +1 -0
  53. package/dist/column-types.test.js +62 -0
  54. package/dist/columns.d.ts +30 -0
  55. package/dist/columns.js +277 -0
  56. package/dist/columns.test.d.ts +1 -0
  57. package/dist/columns.test.js +625 -0
  58. package/dist/core.d.ts +86 -1
  59. package/dist/css.d.ts +3 -0
  60. package/dist/editing.d.ts +31 -0
  61. package/dist/editing.js +529 -0
  62. package/dist/editing.test.d.ts +1 -0
  63. package/dist/editing.test.js +732 -0
  64. package/dist/editors/cell-editors.coverage.test.d.ts +1 -0
  65. package/dist/editors/cell-editors.coverage.test.js +139 -0
  66. package/dist/editors/cell-editors.d.ts +1 -1
  67. package/dist/facet-buckets.d.ts +13 -0
  68. package/dist/facet-buckets.js +54 -0
  69. package/dist/facet-buckets.test.d.ts +1 -0
  70. package/dist/facet-buckets.test.js +296 -0
  71. package/dist/features.d.ts +5 -0
  72. package/dist/features.js +30 -0
  73. package/dist/filter-operators.d.ts +16 -0
  74. package/dist/filter-operators.js +69 -0
  75. package/dist/filter-operators.test.d.ts +1 -0
  76. package/dist/filter-operators.test.js +135 -0
  77. package/dist/hyperformula-adapter.d.ts +82 -0
  78. package/dist/hyperformula-adapter.js +73 -0
  79. package/dist/hyperformula-adapter.test.d.ts +1 -0
  80. package/dist/hyperformula-adapter.test.js +205 -0
  81. package/dist/index.d.ts +9 -3
  82. package/dist/index.js +8 -2
  83. package/dist/keyboard-handlers.coverage.test.d.ts +1 -0
  84. package/dist/keyboard-handlers.coverage.test.js +495 -0
  85. package/dist/keyboard-handlers.d.ts +7 -0
  86. package/dist/keyboard-handlers.js +202 -0
  87. package/dist/menus.d.ts +40 -0
  88. package/dist/menus.js +390 -0
  89. package/dist/menus.test.d.ts +1 -0
  90. package/dist/menus.test.js +560 -0
  91. package/dist/named-views.coverage.test.d.ts +1 -0
  92. package/dist/named-views.coverage.test.js +180 -0
  93. package/dist/named-views.d.ts +27 -0
  94. package/dist/named-views.js +39 -0
  95. package/dist/row-drag.d.ts +49 -0
  96. package/dist/row-drag.js +221 -0
  97. package/dist/row-drag.test.d.ts +1 -0
  98. package/dist/row-drag.test.js +142 -0
  99. package/dist/row-resize.d.ts +43 -0
  100. package/dist/row-resize.js +158 -0
  101. package/dist/row-resize.test.d.ts +1 -0
  102. package/dist/row-resize.test.js +329 -0
  103. package/dist/scroll-sync.d.ts +9 -0
  104. package/dist/scroll-sync.js +89 -0
  105. package/dist/scroll-sync.test.d.ts +1 -0
  106. package/dist/scroll-sync.test.js +290 -0
  107. package/dist/selection.d.ts +32 -0
  108. package/dist/selection.js +427 -0
  109. package/dist/selection.multi-range.test.d.ts +1 -0
  110. package/dist/selection.multi-range.test.js +55 -0
  111. package/dist/selection.test.d.ts +1 -0
  112. package/dist/selection.test.js +647 -0
  113. package/dist/server-data-source.coverage.test.d.ts +1 -0
  114. package/dist/server-data-source.coverage.test.js +154 -0
  115. package/dist/spreadsheet.d.ts +110 -0
  116. package/dist/spreadsheet.js +242 -0
  117. package/dist/spreadsheet.test.d.ts +1 -0
  118. package/dist/spreadsheet.test.js +446 -0
  119. package/dist/summaries.d.ts +12 -0
  120. package/dist/summaries.js +65 -0
  121. package/dist/sv-grid-scrollbar.js +13 -1
  122. package/dist/svgrid-wrapper.types.d.ts +31 -1
  123. package/dist/svgrid.behavior.test.js +20 -0
  124. package/dist/svgrid.comments-autocomplete.test.d.ts +1 -0
  125. package/dist/svgrid.comments-autocomplete.test.js +96 -0
  126. package/dist/svgrid.context-menu.test.d.ts +1 -0
  127. package/dist/svgrid.context-menu.test.js +102 -0
  128. package/dist/svgrid.interaction.test.js +31 -0
  129. package/dist/svgrid.new-features.wrapper.test.js +64 -6
  130. package/dist/svgrid.wrapper.test.js +27 -1
  131. package/dist/test-setup.js +9 -3
  132. package/dist/virtualization/column-virtualizer.d.ts +2 -0
  133. package/dist/virtualization/scroll-scaling.d.ts +45 -0
  134. package/dist/virtualization/scroll-scaling.js +99 -0
  135. package/dist/virtualization/scroll-scaling.test.d.ts +1 -0
  136. package/dist/virtualization/scroll-scaling.test.js +127 -0
  137. package/dist/virtualization/svelte-virtualizer.svelte.d.ts +2 -0
  138. package/dist/virtualization/svelte-virtualizer.svelte.js +2 -0
  139. package/dist/virtualization/virtualizer.d.ts +7 -0
  140. package/dist/virtualization/virtualizer.js +30 -0
  141. package/package.json +2 -1
  142. package/src/GridFooter.svelte +164 -0
  143. package/src/GridMenus.svelte +648 -0
  144. package/src/SvGrid.controller.svelte.ts +2352 -0
  145. package/src/SvGrid.css +2012 -0
  146. package/src/SvGrid.helpers.test.ts +415 -0
  147. package/src/SvGrid.helpers.ts +185 -0
  148. package/src/SvGrid.svelte +2346 -8742
  149. package/src/SvGrid.types.ts +537 -0
  150. package/src/SvGridChart.svelte +1060 -23
  151. package/src/aligned-grids.test.ts +80 -0
  152. package/src/aligned-grids.ts +87 -0
  153. package/src/build-api.coverage.test.ts +532 -0
  154. package/src/build-api.ts +683 -0
  155. package/src/cell-render.test.ts +451 -0
  156. package/src/cell-render.ts +426 -0
  157. package/src/cell-values.ts +114 -0
  158. package/src/chart-export.test.ts +370 -0
  159. package/src/chart.coverage.test.ts +814 -0
  160. package/src/chart.ts +1352 -47
  161. package/src/clipboard.test.ts +780 -0
  162. package/src/clipboard.ts +552 -0
  163. package/src/collaboration.coverage.test.ts +220 -0
  164. package/src/column-groups.test.ts +59 -0
  165. package/src/column-groups.ts +80 -0
  166. package/src/column-types.test.ts +68 -0
  167. package/src/column-types.ts +82 -0
  168. package/src/columns.test.ts +702 -0
  169. package/src/columns.ts +419 -0
  170. package/src/core.ts +86 -0
  171. package/src/css.d.ts +3 -0
  172. package/src/editing.test.ts +837 -0
  173. package/src/editing.ts +669 -0
  174. package/src/editors/cell-editors.coverage.test.ts +156 -0
  175. package/src/editors/cell-editors.ts +1 -0
  176. package/src/facet-buckets.test.ts +353 -0
  177. package/src/facet-buckets.ts +67 -0
  178. package/src/features.ts +128 -0
  179. package/src/filter-operators.test.ts +174 -0
  180. package/src/filter-operators.ts +87 -0
  181. package/src/hyperformula-adapter.test.ts +256 -0
  182. package/src/hyperformula-adapter.ts +124 -0
  183. package/src/index.ts +49 -0
  184. package/src/keyboard-handlers.coverage.test.ts +560 -0
  185. package/src/keyboard-handlers.ts +353 -0
  186. package/src/keyboard.ts +97 -97
  187. package/src/menus.test.ts +620 -0
  188. package/src/menus.ts +555 -0
  189. package/src/named-views.coverage.test.ts +210 -0
  190. package/src/named-views.ts +48 -0
  191. package/src/row-drag.test.ts +168 -0
  192. package/src/row-drag.ts +255 -0
  193. package/src/row-resize.test.ts +369 -0
  194. package/src/row-resize.ts +171 -0
  195. package/src/scroll-sync.test.ts +330 -0
  196. package/src/scroll-sync.ts +218 -0
  197. package/src/selection.multi-range.test.ts +61 -0
  198. package/src/selection.test.ts +722 -0
  199. package/src/selection.ts +579 -0
  200. package/src/server-data-source.coverage.test.ts +180 -0
  201. package/src/spreadsheet.test.ts +489 -0
  202. package/src/spreadsheet.ts +304 -0
  203. package/src/summaries.ts +204 -0
  204. package/src/sv-grid-scrollbar.ts +13 -1
  205. package/src/svgrid-wrapper.types.ts +31 -1
  206. package/src/svgrid.behavior.test.ts +22 -0
  207. package/src/svgrid.comments-autocomplete.test.ts +112 -0
  208. package/src/svgrid.context-menu.test.ts +126 -0
  209. package/src/svgrid.interaction.test.ts +30 -0
  210. package/src/svgrid.new-features.wrapper.test.ts +67 -6
  211. package/src/svgrid.wrapper.test.ts +27 -1
  212. package/src/test-setup.ts +9 -6
  213. package/src/virtualization/scroll-scaling.test.ts +148 -0
  214. package/src/virtualization/scroll-scaling.ts +121 -0
  215. package/src/virtualization/svelte-virtualizer.svelte.ts +2 -0
  216. package/src/virtualization/virtualizer.ts +26 -0
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Unit tests for aligned grids: two members sharing a group mirror horizontal
3
+ * scroll and column-resize widths to each other. Driven with minimal fake ctxs.
4
+ */
5
+ import { describe, expect, it } from 'vitest'
6
+ import { createAlignedGrids } from './aligned-grids'
7
+
8
+ function makeCtx(group: string | undefined) {
9
+ return {
10
+ props: { alignedGridGroup: group },
11
+ scrollContainer: { scrollLeft: 0 } as { scrollLeft: number },
12
+ columnWidths: {} as Record<string, number>,
13
+ } as Record<string, any>
14
+ }
15
+
16
+ describe('aligned grids', () => {
17
+ it('mirrors horizontal scroll to peers in the same group', () => {
18
+ const a = makeCtx('g')
19
+ const b = makeCtx('g')
20
+ const ha = createAlignedGrids(a)
21
+ const hb = createAlignedGrids(b)
22
+ const un1 = ha.register()
23
+ const un2 = hb.register()
24
+
25
+ ha.broadcastScroll(240)
26
+ expect(b.scrollContainer.scrollLeft).toBe(240)
27
+ // and the other way
28
+ hb.broadcastScroll(80)
29
+ expect(a.scrollContainer.scrollLeft).toBe(80)
30
+ un1(); un2()
31
+ })
32
+
33
+ it('mirrors column-resize widths to peers (matched by column id)', () => {
34
+ const a = makeCtx('g')
35
+ const b = makeCtx('g')
36
+ const ha = createAlignedGrids(a)
37
+ const hb = createAlignedGrids(b)
38
+ ha.register(); hb.register()
39
+
40
+ a.columnWidths = { Feb: 180 }
41
+ ha.broadcastWidths()
42
+ expect(b.columnWidths.Feb).toBe(180)
43
+ })
44
+
45
+ it('does NOT mirror across different groups', () => {
46
+ const a = makeCtx('one')
47
+ const b = makeCtx('two')
48
+ const ha = createAlignedGrids(a)
49
+ const hb = createAlignedGrids(b)
50
+ ha.register(); hb.register()
51
+
52
+ ha.broadcastScroll(200)
53
+ expect(b.scrollContainer.scrollLeft).toBe(0)
54
+ a.columnWidths = { Feb: 999 }
55
+ ha.broadcastWidths()
56
+ expect(b.columnWidths.Feb).toBeUndefined()
57
+ })
58
+
59
+ it('is inert with no group set, and after unregister', () => {
60
+ const a = makeCtx(undefined)
61
+ const b = makeCtx(undefined)
62
+ const ha = createAlignedGrids(a)
63
+ const hb = createAlignedGrids(b)
64
+ ha.register(); hb.register()
65
+ ha.broadcastScroll(120)
66
+ expect(b.scrollContainer.scrollLeft).toBe(0)
67
+
68
+ // Grouped, then unregistered -> no longer receives updates.
69
+ const c = makeCtx('gg')
70
+ const d = makeCtx('gg')
71
+ const hc = createAlignedGrids(c)
72
+ const hd = createAlignedGrids(d)
73
+ const unc = hc.register()
74
+ const und = hd.register()
75
+ und() // d leaves the group
76
+ hc.broadcastScroll(55)
77
+ expect(d.scrollContainer.scrollLeft).toBe(0)
78
+ unc()
79
+ })
80
+ })
@@ -0,0 +1,87 @@
1
+ // Aligned grids - keep two or more independent grids in lockstep on horizontal
2
+ // scroll and column widths. Grids that share the same `alignedGridGroup` string
3
+ // register with a module-level registry (same pattern as managed row dragging);
4
+ // when one scrolls horizontally or a column is resized, the others follow.
5
+ //
6
+ // Loop-safety without flags: scroll sync no-ops when the target already has the
7
+ // value (so the echo scroll event never fires); width sync compares a serialized
8
+ // snapshot and pre-seeds it on apply, so the reactive effect that fires after
9
+ // applying a peer's widths sees "no change" and doesn't echo.
10
+
11
+ type Member = {
12
+ id: number;
13
+ applyScroll: (left: number) => void;
14
+ applyWidths: (widths: Record<string, number>) => void;
15
+ };
16
+
17
+ // Shared across every grid in the bundle.
18
+ const groups = new Map<string, Map<number, Member>>();
19
+ let seq = 0;
20
+
21
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
+ export function createAlignedGrids<TFeatures, TData>(ctx: any) {
23
+ const id = ++seq;
24
+ let lastWidthsSerial = "";
25
+
26
+ const group = (): string | null => ctx.props.alignedGridGroup ?? null;
27
+
28
+ function peers(): Member[] {
29
+ const g = group();
30
+ if (!g) return [];
31
+ const m = groups.get(g);
32
+ if (!m) return [];
33
+ const out: Member[] = [];
34
+ for (const member of m.values()) if (member.id !== id) out.push(member);
35
+ return out;
36
+ }
37
+
38
+ const self: Member = {
39
+ id,
40
+ applyScroll(left: number) {
41
+ const el = ctx.scrollContainer as HTMLElement | null;
42
+ // Only set when it differs, so no echo scroll event fires -> no loop.
43
+ if (el && el.scrollLeft !== left) el.scrollLeft = left;
44
+ },
45
+ applyWidths(widths: Record<string, number>) {
46
+ const merged = { ...(ctx.columnWidths as Record<string, number>), ...widths };
47
+ // Pre-seed the serial so the columnWidths effect that fires after this
48
+ // set sees an unchanged snapshot and skips its own broadcast.
49
+ lastWidthsSerial = JSON.stringify(merged);
50
+ ctx.columnWidths = merged;
51
+ },
52
+ };
53
+
54
+ // Register on mount; returns the unregister for the effect's cleanup.
55
+ function register(): () => void {
56
+ const g = group();
57
+ if (!g) return () => {};
58
+ let m = groups.get(g);
59
+ if (!m) {
60
+ m = new Map();
61
+ groups.set(g, m);
62
+ }
63
+ m.set(id, self);
64
+ return () => {
65
+ const mm = groups.get(g);
66
+ if (!mm) return;
67
+ mm.delete(id);
68
+ if (!mm.size) groups.delete(g);
69
+ };
70
+ }
71
+
72
+ function broadcastScroll(left: number) {
73
+ const p = peers();
74
+ for (const member of p) member.applyScroll(left);
75
+ }
76
+
77
+ function broadcastWidths() {
78
+ if (!group()) return;
79
+ const serial = JSON.stringify((ctx.columnWidths as Record<string, number>) ?? {});
80
+ if (serial === lastWidthsSerial) return; // nothing new (or echo of an apply)
81
+ lastWidthsSerial = serial;
82
+ const w = ctx.columnWidths as Record<string, number>;
83
+ for (const member of peers()) member.applyWidths(w);
84
+ }
85
+
86
+ return { register, broadcastScroll, broadcastWidths, alignedGridId: id };
87
+ }
@@ -0,0 +1,532 @@
1
+ /**
2
+ * Coverage-focused behavioral tests for the SvGridApi assembled in
3
+ * `build-api.ts`. The existing `svgrid.api.test.ts` / `svgrid.api-extensions
4
+ * .test.ts` cover filters, row selection, pagination, navigation, and
5
+ * getState/setState. This file exercises the remaining surface:
6
+ * - getCellValue / setCellValue
7
+ * - selectCells / getSelected
8
+ * - addRow(s) / removeRow(s) / applyTransaction
9
+ * - addColumn(s) / removeColumn / setColumnVisible / getColumns
10
+ * - setColumnWidth / setColumnPinning / setColumnOrder
11
+ * - setFacetFilter
12
+ * - undo / redo / canUndo / canRedo / clearHistory
13
+ * - find (open/close/query/hits)
14
+ * - grouping (setGroupBy / expandAllGroups / collapseAllGroups)
15
+ * - autosizeColumn / refresh
16
+ *
17
+ * All tests mount the real <SvGrid /> in jsdom and drive the api handed back
18
+ * from onApiReady.
19
+ */
20
+
21
+ import { describe, expect, it } from "vitest";
22
+ import { mount, unmount } from "svelte";
23
+ import SvGrid from "./SvGrid.svelte";
24
+ import {
25
+ columnFilteringFeature,
26
+ columnGroupingFeature,
27
+ rowExpandingFeature,
28
+ rowSelectionFeature,
29
+ rowSortingFeature,
30
+ tableFeatures,
31
+ } from "./index";
32
+ import type { ColumnDef, SvGridApi } from "./index";
33
+
34
+ type Row = { id: number; name: string; team: string; salary: number };
35
+
36
+ const baseRows: Row[] = [
37
+ { id: 1, name: "Ada Lovelace", team: "Research", salary: 142_000 },
38
+ { id: 2, name: "Grace Hopper", team: "Compilers", salary: 158_000 },
39
+ { id: 3, name: "Alan Turing", team: "Research", salary: 138_000 },
40
+ { id: 4, name: "Margaret Hamilton", team: "Apollo", salary: 165_000 },
41
+ { id: 5, name: "Linus Torvalds", team: "Kernel", salary: 175_000 },
42
+ ];
43
+
44
+ const baseColumns: ColumnDef<any, Row>[] = [
45
+ { field: "name", header: "Name", width: 200 },
46
+ { field: "team", header: "Team", width: 160 },
47
+ { field: "salary", header: "Salary", width: 140 },
48
+ ];
49
+
50
+ const flush = () => Promise.resolve();
51
+
52
+ function mountGrid(
53
+ extraProps: Record<string, unknown> = {},
54
+ features: any = tableFeatures({
55
+ columnFilteringFeature,
56
+ rowSortingFeature,
57
+ rowSelectionFeature,
58
+ columnGroupingFeature,
59
+ rowExpandingFeature,
60
+ }),
61
+ ): Promise<{ api: SvGridApi<any, Row>; target: HTMLElement; destroy: () => void }> {
62
+ return new Promise((resolveApi, rejectApi) => {
63
+ const target = document.createElement("div");
64
+ document.body.appendChild(target);
65
+ let captured: SvGridApi<any, Row> | null = null;
66
+ const app = mount(SvGrid, {
67
+ target,
68
+ props: {
69
+ data: baseRows.map((r) => ({ ...r })),
70
+ columns: baseColumns.map((c) => ({ ...c })),
71
+ features,
72
+ getRowId: (r: Row) => String(r.id),
73
+ rowHeight: 36,
74
+ containerHeight: 480,
75
+ virtualization: false,
76
+ enableCellSelection: true,
77
+ onApiReady(api: SvGridApi<any, Row>) {
78
+ captured = api;
79
+ resolveApi({
80
+ api,
81
+ target,
82
+ destroy: () => {
83
+ unmount(app);
84
+ target.remove();
85
+ },
86
+ });
87
+ },
88
+ ...extraProps,
89
+ } as any,
90
+ });
91
+ queueMicrotask(() => {
92
+ if (!captured) rejectApi(new Error("onApiReady never fired"));
93
+ });
94
+ });
95
+ }
96
+
97
+ describe("SvGridApi - cell read / write", () => {
98
+ it("getCellValue reads through the column field", async () => {
99
+ const { api, destroy } = await mountGrid();
100
+ try {
101
+ expect(api.getCellValue(0, "name")).toBe("Ada Lovelace");
102
+ expect(api.getCellValue(3, "team")).toBe("Apollo");
103
+ } finally {
104
+ destroy();
105
+ }
106
+ });
107
+
108
+ it("getCellValue returns undefined for out-of-range row or unknown column", async () => {
109
+ const { api, destroy } = await mountGrid();
110
+ try {
111
+ expect(api.getCellValue(999, "name")).toBeUndefined();
112
+ expect(api.getCellValue(0, "nope")).toBeUndefined();
113
+ } finally {
114
+ destroy();
115
+ }
116
+ });
117
+
118
+ it("setCellValue writes a new immutable row and reads back", async () => {
119
+ const { api, destroy } = await mountGrid();
120
+ try {
121
+ api.setCellValue(0, "name", "Ada L.");
122
+ await flush();
123
+ expect(api.getCellValue(0, "name")).toBe("Ada L.");
124
+ // other rows untouched
125
+ expect(api.getCellValue(1, "name")).toBe("Grace Hopper");
126
+ } finally {
127
+ destroy();
128
+ }
129
+ });
130
+
131
+ it("setCellValue is a no-op for an invalid target", async () => {
132
+ const { api, destroy } = await mountGrid();
133
+ try {
134
+ expect(() => api.setCellValue(999, "name", "x")).not.toThrow();
135
+ expect(() => api.setCellValue(0, "unknown", "x")).not.toThrow();
136
+ } finally {
137
+ destroy();
138
+ }
139
+ });
140
+ });
141
+
142
+ describe("SvGridApi - cell selection", () => {
143
+ it("selectCells sets a rectangle; getSelected reads it back normalized", async () => {
144
+ const { api, destroy } = await mountGrid();
145
+ try {
146
+ api.selectCells([[2, 1, 0, 0]]); // unsorted corners
147
+ await flush();
148
+ expect(api.getSelected()).toEqual([[0, 0, 2, 1]]);
149
+ } finally {
150
+ destroy();
151
+ }
152
+ });
153
+
154
+ it("selectCells clamps open-ended (Infinity) coordinates to grid bounds", async () => {
155
+ const { api, destroy } = await mountGrid();
156
+ try {
157
+ api.selectCells([[0, 0, Infinity, Infinity]]);
158
+ await flush();
159
+ // 5 rows (0..4), 3 cols (0..2)
160
+ expect(api.getSelected()).toEqual([[0, 0, 4, 2]]);
161
+ } finally {
162
+ destroy();
163
+ }
164
+ });
165
+
166
+ it("empty selectCells clears the range; getSelected returns []", async () => {
167
+ const { api, destroy } = await mountGrid();
168
+ try {
169
+ api.selectCells([[0, 0, 1, 1]]);
170
+ await flush();
171
+ api.selectCells([]);
172
+ await flush();
173
+ expect(api.getSelected()).toEqual([]);
174
+ } finally {
175
+ destroy();
176
+ }
177
+ });
178
+ });
179
+
180
+ describe("SvGridApi - row mutations", () => {
181
+ it("addRow appends to the bottom by default", async () => {
182
+ const { api, destroy } = await mountGrid();
183
+ try {
184
+ api.addRow({ id: 6, name: "New", team: "QA", salary: 1 } as Row);
185
+ await flush();
186
+ const data = api.getData() as Row[];
187
+ expect(data.length).toBe(6);
188
+ expect(data[5]!.id).toBe(6);
189
+ } finally {
190
+ destroy();
191
+ }
192
+ });
193
+
194
+ it("addRows at top / numeric index insert in place", async () => {
195
+ const { api, destroy } = await mountGrid();
196
+ try {
197
+ api.addRows([{ id: 7, name: "Top", team: "T", salary: 1 } as Row], "top");
198
+ await flush();
199
+ expect((api.getData() as Row[])[0]!.id).toBe(7);
200
+
201
+ api.addRows([{ id: 8, name: "Mid", team: "M", salary: 1 } as Row], 2);
202
+ await flush();
203
+ expect((api.getData() as Row[])[2]!.id).toBe(8);
204
+ } finally {
205
+ destroy();
206
+ }
207
+ });
208
+
209
+ it("removeRow / removeRows drop by index", async () => {
210
+ const { api, destroy } = await mountGrid();
211
+ try {
212
+ api.removeRow(0);
213
+ await flush();
214
+ expect((api.getData() as Row[]).map((r) => r.id)).toEqual([2, 3, 4, 5]);
215
+
216
+ api.removeRows([0, 2]); // drops ids 2 and 4
217
+ await flush();
218
+ expect((api.getData() as Row[]).map((r) => r.id)).toEqual([3, 5]);
219
+ } finally {
220
+ destroy();
221
+ }
222
+ });
223
+
224
+ it("applyTransaction adds, updates (by id), and removes; returns counts", async () => {
225
+ const { api, destroy } = await mountGrid();
226
+ try {
227
+ const result = api.applyTransaction({
228
+ add: [{ id: 9, name: "Added", team: "X", salary: 1 } as Row],
229
+ update: [
230
+ { id: 2, name: "Grace H.", team: "Compilers", salary: 999 } as Row,
231
+ ],
232
+ remove: ["3"], // by getRowId string
233
+ });
234
+ await flush();
235
+ expect(result).toEqual({ added: 1, updated: 1, removed: 1 });
236
+ const data = api.getData() as Row[];
237
+ expect(data.find((r) => r.id === 3)).toBeUndefined();
238
+ expect(data.find((r) => r.id === 2)!.salary).toBe(999);
239
+ expect(data.find((r) => r.id === 9)).toBeTruthy();
240
+ } finally {
241
+ destroy();
242
+ }
243
+ });
244
+
245
+ it("applyTransaction removes by object reference too", async () => {
246
+ const { api, destroy } = await mountGrid();
247
+ try {
248
+ const target = (api.getData() as Row[])[0]!;
249
+ const result = api.applyTransaction({ remove: [target] });
250
+ await flush();
251
+ expect(result.removed).toBe(1);
252
+ expect((api.getData() as Row[]).find((r) => r === target)).toBeUndefined();
253
+ } finally {
254
+ destroy();
255
+ }
256
+ });
257
+ });
258
+
259
+ describe("SvGridApi - column mutations", () => {
260
+ it("addColumn left / right / numeric position, then getColumns reflects it", async () => {
261
+ const { api, destroy } = await mountGrid();
262
+ try {
263
+ api.addColumn({ field: "id", header: "ID" } as any, "left");
264
+ await flush();
265
+ expect(api.getColumns()[0]!.id).toBe("id");
266
+
267
+ api.addColumns([{ field: "extra", header: "Extra" } as any], "right");
268
+ await flush();
269
+ const cols = api.getColumns();
270
+ expect(cols[cols.length - 1]!.id).toBe("extra");
271
+ } finally {
272
+ destroy();
273
+ }
274
+ });
275
+
276
+ it("removeColumn drops a column from the visible set", async () => {
277
+ const { api, destroy } = await mountGrid();
278
+ try {
279
+ api.removeColumn("team");
280
+ await flush();
281
+ expect(api.getColumns().find((c) => c.id === "team")).toBeUndefined();
282
+ } finally {
283
+ destroy();
284
+ }
285
+ });
286
+
287
+ it("setColumnVisible toggles visibility; getColumns lists hidden as visible:false", async () => {
288
+ const { api, destroy } = await mountGrid();
289
+ try {
290
+ api.setColumnVisible("team", false);
291
+ await flush();
292
+ expect(api.isColumnVisible("team")).toBe(false);
293
+ const hidden = api.getColumns().find((c) => c.id === "team");
294
+ expect(hidden!.visible).toBe(false);
295
+
296
+ api.setColumnVisible("team", true);
297
+ await flush();
298
+ expect(api.isColumnVisible("team")).toBe(true);
299
+ } finally {
300
+ destroy();
301
+ }
302
+ });
303
+
304
+ it("setColumnWidth clamps + getColumnWidths round-trips", async () => {
305
+ const { api, destroy } = await mountGrid();
306
+ try {
307
+ api.setColumnWidth("name", 333.7);
308
+ await flush();
309
+ expect(api.getColumnWidths().name).toBe(333);
310
+ } finally {
311
+ destroy();
312
+ }
313
+ });
314
+
315
+ it("setColumnPinning dedupes; getColumnPinning returns copies", async () => {
316
+ const { api, destroy } = await mountGrid();
317
+ try {
318
+ api.setColumnPinning({ left: ["name", "name"], right: ["salary"] });
319
+ await flush();
320
+ const pin = api.getColumnPinning();
321
+ expect(pin.left).toEqual(["name"]);
322
+ expect(pin.right).toEqual(["salary"]);
323
+ // mutating the returned copy doesn't affect internal state
324
+ pin.left.push("hacked");
325
+ expect(api.getColumnPinning().left).toEqual(["name"]);
326
+ } finally {
327
+ destroy();
328
+ }
329
+ });
330
+
331
+ it("setColumnOrder + getColumnOrder round-trip", async () => {
332
+ const { api, destroy } = await mountGrid();
333
+ try {
334
+ api.setColumnOrder(["salary", "name", "team"]);
335
+ await flush();
336
+ expect(api.getColumnOrder().slice(0, 3)).toEqual([
337
+ "salary",
338
+ "name",
339
+ "team",
340
+ ]);
341
+ } finally {
342
+ destroy();
343
+ }
344
+ });
345
+
346
+ // Note: autosizeColumn / autosizeAllColumns are intentionally not asserted
347
+ // here - they measure text via real DOM layout (canvas/escape), which jsdom
348
+ // does not provide, so they throw in this environment. They are exercised by
349
+ // the demo gallery / Playwright suite per the coverage config notes.
350
+ });
351
+
352
+ describe("SvGridApi - facet filter", () => {
353
+ it("setFacetFilter narrows displayed rows; null clears it", async () => {
354
+ const { api, destroy } = await mountGrid();
355
+ try {
356
+ api.setFacetFilter("team", ["Research"]);
357
+ await flush();
358
+ const displayed = api.getDisplayedRows() as Row[];
359
+ expect(displayed.every((r) => r.team === "Research")).toBe(true);
360
+ expect(displayed.length).toBe(2);
361
+
362
+ api.setFacetFilter("team", null);
363
+ await flush();
364
+ expect((api.getDisplayedRows() as Row[]).length).toBe(5);
365
+ } finally {
366
+ destroy();
367
+ }
368
+ });
369
+
370
+ it("setFacetFilter with an empty array also clears", async () => {
371
+ const { api, destroy } = await mountGrid();
372
+ try {
373
+ api.setFacetFilter("team", ["Apollo"]);
374
+ await flush();
375
+ api.setFacetFilter("team", []);
376
+ await flush();
377
+ expect((api.getDisplayedRows() as Row[]).length).toBe(5);
378
+ } finally {
379
+ destroy();
380
+ }
381
+ });
382
+ });
383
+
384
+ describe("SvGridApi - find", () => {
385
+ it("openFind / setFindQuery / getFindHits / closeFind", async () => {
386
+ const { api, destroy } = await mountGrid();
387
+ try {
388
+ api.openFind();
389
+ api.setFindQuery("Research");
390
+ await flush();
391
+ const hits = api.getFindHits();
392
+ expect(Array.isArray(hits)).toBe(true);
393
+ // two cells contain "Research"
394
+ expect(hits.length).toBeGreaterThanOrEqual(2);
395
+
396
+ api.closeFind();
397
+ await flush();
398
+ expect(api.getFindHits()).toEqual([]);
399
+ } finally {
400
+ destroy();
401
+ }
402
+ });
403
+ });
404
+
405
+ describe("SvGridApi - undo / redo / history", () => {
406
+ it("canUndo is false before any history; undo/redo are no-ops then", async () => {
407
+ const { api, destroy } = await mountGrid();
408
+ try {
409
+ expect(api.canUndo()).toBe(false);
410
+ expect(api.canRedo()).toBe(false);
411
+ expect(api.undo()).toBe(false);
412
+ expect(api.redo()).toBe(false);
413
+ } finally {
414
+ destroy();
415
+ }
416
+ });
417
+
418
+ it("clearHistory resets the stacks without throwing", async () => {
419
+ const { api, destroy } = await mountGrid();
420
+ try {
421
+ expect(() => api.clearHistory()).not.toThrow();
422
+ expect(api.canUndo()).toBe(false);
423
+ expect(api.canRedo()).toBe(false);
424
+ } finally {
425
+ destroy();
426
+ }
427
+ });
428
+ });
429
+
430
+ describe("SvGridApi - grouping / expansion", () => {
431
+ it("setGroupBy groups rows; the underlying data is unchanged", async () => {
432
+ const { api, destroy } = await mountGrid();
433
+ try {
434
+ api.setGroupBy(["team"]);
435
+ await flush();
436
+ // Grouping reshapes the display pipeline; the source data is intact.
437
+ expect((api.getData() as Row[]).length).toBe(5);
438
+ expect(api.getState().grouping).toContain("team");
439
+ } finally {
440
+ destroy();
441
+ }
442
+ });
443
+
444
+ it("expandAllGroups / collapseAllGroups run without error", async () => {
445
+ const { api, destroy } = await mountGrid();
446
+ try {
447
+ api.setGroupBy(["team"]);
448
+ await flush();
449
+ expect(() => api.expandAllGroups()).not.toThrow();
450
+ await flush();
451
+ expect(() => api.collapseAllGroups()).not.toThrow();
452
+ await flush();
453
+ // un-group
454
+ api.setGroupBy([]);
455
+ await flush();
456
+ expect((api.getData() as Row[]).length).toBe(5);
457
+ } finally {
458
+ destroy();
459
+ }
460
+ });
461
+
462
+ it("setRowExpanded toggles a row's expanded flag", async () => {
463
+ const { api, destroy } = await mountGrid();
464
+ try {
465
+ expect(() => api.setRowExpanded("1", true)).not.toThrow();
466
+ await flush();
467
+ expect(() => api.setRowExpanded("1", false)).not.toThrow();
468
+ } finally {
469
+ destroy();
470
+ }
471
+ });
472
+ });
473
+
474
+ describe("SvGridApi - sort opt-out + refresh", () => {
475
+ it("setSort with a direction sets a single active clause", async () => {
476
+ const { api, destroy } = await mountGrid();
477
+ try {
478
+ api.setSort("salary", "asc");
479
+ await flush();
480
+ expect(api.getState().sorting).toEqual([{ id: "salary", desc: false }]);
481
+
482
+ api.setSort("name", "desc");
483
+ await flush();
484
+ // Setting a direction replaces the active sort clause.
485
+ expect(api.getState().sorting).toEqual([{ id: "name", desc: true }]);
486
+ } finally {
487
+ destroy();
488
+ }
489
+ });
490
+
491
+ it("setSort with a falsy direction removes that column's clause", async () => {
492
+ const { api, destroy } = await mountGrid();
493
+ try {
494
+ api.setSort("salary", "asc");
495
+ await flush();
496
+ api.setSort("salary", null as any);
497
+ await flush();
498
+ expect(
499
+ api.getState().sorting.find((s) => s.id === "salary"),
500
+ ).toBeUndefined();
501
+ } finally {
502
+ destroy();
503
+ }
504
+ });
505
+
506
+ it("setSort is ignored for a non-sortable column", async () => {
507
+ const { api, destroy } = await mountGrid({
508
+ columns: [
509
+ { field: "name", header: "Name", width: 200, sortable: false },
510
+ { field: "team", header: "Team", width: 160 },
511
+ ],
512
+ });
513
+ try {
514
+ api.setSort("name", "asc");
515
+ await flush();
516
+ expect(api.getState().sorting.find((s) => s.id === "name")).toBeUndefined();
517
+ } finally {
518
+ destroy();
519
+ }
520
+ });
521
+
522
+ it("refresh re-runs the pipeline without throwing or losing data", async () => {
523
+ const { api, destroy } = await mountGrid();
524
+ try {
525
+ expect(() => api.refresh()).not.toThrow();
526
+ await flush();
527
+ expect((api.getData() as Row[]).length).toBe(5);
528
+ } finally {
529
+ destroy();
530
+ }
531
+ });
532
+ });