@svgrid/grid 1.0.2 → 1.1.0

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/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 +570 -0
  6. package/dist/GridMenus.svelte.d.ts +31 -0
  7. package/dist/SvGrid.controller.svelte.d.ts +429 -0
  8. package/dist/SvGrid.controller.svelte.js +1732 -0
  9. package/dist/SvGrid.css +1709 -0
  10. package/dist/SvGrid.helpers.d.ts +35 -0
  11. package/dist/SvGrid.helpers.js +160 -0
  12. package/dist/SvGrid.svelte +344 -7043
  13. package/dist/SvGrid.svelte.d.ts +4 -357
  14. package/dist/SvGrid.types.d.ts +436 -0
  15. package/dist/SvGrid.types.js +1 -0
  16. package/dist/SvGridChart.svelte +1060 -23
  17. package/dist/SvGridChart.svelte.d.ts +17 -0
  18. package/dist/build-api.d.ts +5 -0
  19. package/dist/build-api.js +527 -0
  20. package/dist/cell-render.d.ts +15 -0
  21. package/dist/cell-render.js +246 -0
  22. package/dist/cell-values.d.ts +28 -0
  23. package/dist/cell-values.js +89 -0
  24. package/dist/chart.d.ts +370 -3
  25. package/dist/chart.js +1135 -42
  26. package/dist/clipboard.d.ts +15 -0
  27. package/dist/clipboard.js +356 -0
  28. package/dist/columns.d.ts +30 -0
  29. package/dist/columns.js +277 -0
  30. package/dist/core.d.ts +1 -1
  31. package/dist/css.d.ts +3 -0
  32. package/dist/editing.d.ts +24 -0
  33. package/dist/editing.js +343 -0
  34. package/dist/editors/cell-editors.d.ts +1 -1
  35. package/dist/facet-buckets.d.ts +13 -0
  36. package/dist/facet-buckets.js +54 -0
  37. package/dist/features.d.ts +5 -0
  38. package/dist/features.js +30 -0
  39. package/dist/filter-operators.d.ts +16 -0
  40. package/dist/filter-operators.js +69 -0
  41. package/dist/hyperformula-adapter.d.ts +82 -0
  42. package/dist/hyperformula-adapter.js +73 -0
  43. package/dist/index.d.ts +5 -2
  44. package/dist/index.js +5 -2
  45. package/dist/keyboard-handlers.d.ts +7 -0
  46. package/dist/keyboard-handlers.js +197 -0
  47. package/dist/menus.d.ts +40 -0
  48. package/dist/menus.js +389 -0
  49. package/dist/named-views.d.ts +27 -0
  50. package/dist/named-views.js +39 -0
  51. package/dist/row-resize.d.ts +43 -0
  52. package/dist/row-resize.js +158 -0
  53. package/dist/scroll-sync.d.ts +9 -0
  54. package/dist/scroll-sync.js +86 -0
  55. package/dist/selection.d.ts +26 -0
  56. package/dist/selection.js +387 -0
  57. package/dist/spreadsheet.d.ts +80 -0
  58. package/dist/spreadsheet.js +194 -0
  59. package/dist/summaries.d.ts +12 -0
  60. package/dist/summaries.js +65 -0
  61. package/dist/svgrid-wrapper.types.d.ts +12 -1
  62. package/dist/svgrid.comments-autocomplete.test.d.ts +1 -0
  63. package/dist/svgrid.comments-autocomplete.test.js +96 -0
  64. package/dist/svgrid.context-menu.test.d.ts +1 -0
  65. package/dist/svgrid.context-menu.test.js +102 -0
  66. package/dist/svgrid.new-features.wrapper.test.js +30 -4
  67. package/dist/svgrid.wrapper.test.js +27 -1
  68. package/dist/virtualization/column-virtualizer.d.ts +2 -0
  69. package/dist/virtualization/scroll-scaling.d.ts +28 -0
  70. package/dist/virtualization/scroll-scaling.js +64 -0
  71. package/dist/virtualization/scroll-scaling.test.d.ts +1 -0
  72. package/dist/virtualization/scroll-scaling.test.js +86 -0
  73. package/dist/virtualization/svelte-virtualizer.svelte.d.ts +2 -0
  74. package/dist/virtualization/svelte-virtualizer.svelte.js +2 -0
  75. package/dist/virtualization/virtualizer.d.ts +7 -0
  76. package/dist/virtualization/virtualizer.js +30 -0
  77. package/package.json +1 -1
  78. package/src/GridFooter.svelte +164 -0
  79. package/src/GridMenus.svelte +570 -0
  80. package/src/SvGrid.controller.svelte.ts +2195 -0
  81. package/src/SvGrid.css +1747 -0
  82. package/src/SvGrid.helpers.test.ts +415 -0
  83. package/src/SvGrid.helpers.ts +185 -0
  84. package/src/SvGrid.svelte +348 -7043
  85. package/src/SvGrid.types.ts +456 -0
  86. package/src/SvGridChart.svelte +1060 -23
  87. package/src/build-api.coverage.test.ts +532 -0
  88. package/src/build-api.ts +663 -0
  89. package/src/cell-render.test.ts +451 -0
  90. package/src/cell-render.ts +426 -0
  91. package/src/cell-values.ts +114 -0
  92. package/src/chart-export.test.ts +370 -0
  93. package/src/chart.coverage.test.ts +814 -0
  94. package/src/chart.ts +1352 -47
  95. package/src/clipboard.test.ts +731 -0
  96. package/src/clipboard.ts +524 -0
  97. package/src/collaboration.coverage.test.ts +220 -0
  98. package/src/columns.test.ts +702 -0
  99. package/src/columns.ts +419 -0
  100. package/src/core.ts +8 -0
  101. package/src/css.d.ts +3 -0
  102. package/src/editing.test.ts +837 -0
  103. package/src/editing.ts +513 -0
  104. package/src/editors/cell-editors.coverage.test.ts +156 -0
  105. package/src/editors/cell-editors.ts +1 -0
  106. package/src/facet-buckets.test.ts +353 -0
  107. package/src/facet-buckets.ts +67 -0
  108. package/src/features.ts +128 -0
  109. package/src/filter-operators.test.ts +174 -0
  110. package/src/filter-operators.ts +87 -0
  111. package/src/hyperformula-adapter.test.ts +256 -0
  112. package/src/hyperformula-adapter.ts +124 -0
  113. package/src/index.ts +37 -0
  114. package/src/keyboard-handlers.coverage.test.ts +560 -0
  115. package/src/keyboard-handlers.ts +353 -0
  116. package/src/keyboard.ts +97 -97
  117. package/src/menus.test.ts +620 -0
  118. package/src/menus.ts +554 -0
  119. package/src/named-views.coverage.test.ts +210 -0
  120. package/src/named-views.ts +48 -0
  121. package/src/row-resize.test.ts +369 -0
  122. package/src/row-resize.ts +171 -0
  123. package/src/scroll-sync.test.ts +330 -0
  124. package/src/scroll-sync.ts +216 -0
  125. package/src/selection.test.ts +722 -0
  126. package/src/selection.ts +545 -0
  127. package/src/server-data-source.coverage.test.ts +180 -0
  128. package/src/spreadsheet.test.ts +445 -0
  129. package/src/spreadsheet.ts +246 -0
  130. package/src/summaries.ts +204 -0
  131. package/src/sv-grid-scrollbar.ts +13 -1
  132. package/src/svgrid-wrapper.types.ts +12 -1
  133. package/src/svgrid.behavior.test.ts +22 -0
  134. package/src/svgrid.comments-autocomplete.test.ts +112 -0
  135. package/src/svgrid.context-menu.test.ts +126 -0
  136. package/src/svgrid.interaction.test.ts +30 -0
  137. package/src/svgrid.new-features.wrapper.test.ts +65 -4
  138. package/src/svgrid.wrapper.test.ts +27 -1
  139. package/src/test-setup.ts +9 -6
  140. package/src/virtualization/scroll-scaling.test.ts +148 -0
  141. package/src/virtualization/scroll-scaling.ts +121 -0
  142. package/src/virtualization/svelte-virtualizer.svelte.ts +2 -0
  143. package/src/virtualization/virtualizer.ts +26 -0
@@ -0,0 +1,620 @@
1
+ import { describe, expect, it, vi } from 'vitest'
2
+ import { createMenus } from './menus'
3
+
4
+ // ---------------------------------------------------------------------------
5
+ // Fake controller (`ctx`) for the menus factory. The factory reads/writes
6
+ // plain menu/filter/pagination state and calls a few `ctx.grid.*` methods and
7
+ // clipboard/data helpers. We build the minimal surface each path touches.
8
+ // ---------------------------------------------------------------------------
9
+
10
+ function makeCtx(overrides: any = {}) {
11
+ let gridState: any = {
12
+ grouping: overrides.grouping ?? [],
13
+ ...overrides.gridState,
14
+ }
15
+ const ctx: any = {
16
+ filterRowValues: {},
17
+ filterMenuValues: {},
18
+ valueFilters: {},
19
+ columnMenuFor: null,
20
+ filterMenuFor: null,
21
+ operatorMenuFor: null,
22
+ chooseColumnsPos: null,
23
+ columnMenuPos: null,
24
+ filterMenuPos: null,
25
+ operatorMenuPos: null,
26
+ columnMenuSearch: '',
27
+ contextMenuFor: null,
28
+ contextMenuPos: { x: 0, y: 0 },
29
+ commentEditFor: null,
30
+ commentDraft: '',
31
+ noteOverrides: {},
32
+ columnMenuFacetValues: [],
33
+ showColumnFiltersEffective: true,
34
+ showFilterRowEffective: false,
35
+ scrollContainer: null,
36
+ internalData: [],
37
+ internalColumns: [],
38
+ props: {},
39
+ copySelectionToClipboard: vi.fn(),
40
+ cutSelectionToClipboard: vi.fn(),
41
+ pasteFromClipboard: vi.fn(),
42
+ clearSelectedCells: vi.fn(),
43
+ allRows: [],
44
+ grid: {
45
+ getState: () => gridState,
46
+ setGrouping: vi.fn((g: any) => {
47
+ gridState = { ...gridState, grouping: g }
48
+ }),
49
+ setPagination: vi.fn(),
50
+ store: {
51
+ setState: vi.fn((updater: any) => {
52
+ gridState =
53
+ typeof updater === 'function' ? updater(gridState) : { ...gridState, ...updater }
54
+ }),
55
+ },
56
+ _peek: () => gridState,
57
+ },
58
+ ...overrides,
59
+ }
60
+ return ctx
61
+ }
62
+
63
+ describe('createMenus - filter row/menu value updates', () => {
64
+ it('updateFilterRow seeds both filterRowValues and filterMenuValues', () => {
65
+ const ctx = makeCtx()
66
+ const m = createMenus(ctx)
67
+ m.updateFilterRow('name', 'abc')
68
+ expect(ctx.filterRowValues.name).toBe('abc')
69
+ expect(ctx.filterMenuValues.name).toEqual({ operator: 'contains', value: 'abc' })
70
+ })
71
+
72
+ it('updateFilterRow preserves an existing operator', () => {
73
+ const ctx = makeCtx({ filterMenuValues: { name: { operator: 'equals', value: '' } } })
74
+ const m = createMenus(ctx)
75
+ m.updateFilterRow('name', 'x')
76
+ expect(ctx.filterMenuValues.name).toEqual({ operator: 'equals', value: 'x' })
77
+ })
78
+
79
+ it('updateFilterOperator changes operator, defaulting value', () => {
80
+ const ctx = makeCtx()
81
+ const m = createMenus(ctx)
82
+ m.updateFilterOperator('age', 'greaterThan' as any)
83
+ expect(ctx.filterMenuValues.age).toEqual({ operator: 'greaterThan', value: '' })
84
+ })
85
+
86
+ it('updateFilterOperator keeps an existing value', () => {
87
+ const ctx = makeCtx({ filterMenuValues: { age: { operator: 'contains', value: '5' } } })
88
+ const m = createMenus(ctx)
89
+ m.updateFilterOperator('age', 'lessThan' as any)
90
+ expect(ctx.filterMenuValues.age).toEqual({ operator: 'lessThan', value: '5' })
91
+ })
92
+
93
+ it('updateFilterMenuValue mirrors value into the filter row', () => {
94
+ const ctx = makeCtx()
95
+ const m = createMenus(ctx)
96
+ m.updateFilterMenuValue('name', 'foo')
97
+ expect(ctx.filterMenuValues.name).toEqual({ operator: 'contains', value: 'foo' })
98
+ expect(ctx.filterRowValues.name).toBe('foo')
99
+ })
100
+
101
+ it('updateFilterMenuValueTo sets the upper bound for between', () => {
102
+ const ctx = makeCtx({ filterMenuValues: { age: { operator: 'between', value: '5' } } })
103
+ const m = createMenus(ctx)
104
+ m.updateFilterMenuValueTo('age', '10')
105
+ expect(ctx.filterMenuValues.age).toEqual({ operator: 'between', value: '5', valueTo: '10' })
106
+ })
107
+
108
+ it('updateFilterMenuValueTo defaults a missing entry to between', () => {
109
+ const ctx = makeCtx()
110
+ const m = createMenus(ctx)
111
+ m.updateFilterMenuValueTo('age', '10')
112
+ expect(ctx.filterMenuValues.age).toEqual({ operator: 'between', value: '', valueTo: '10' })
113
+ })
114
+ })
115
+
116
+ describe('createMenus - toggleCheckboxWithKeyboard', () => {
117
+ it('toggles on Enter and prevents default', () => {
118
+ const ctx = makeCtx()
119
+ const m = createMenus(ctx)
120
+ const toggle = vi.fn()
121
+ const ev = { key: 'Enter', preventDefault: vi.fn() } as any
122
+ m.toggleCheckboxWithKeyboard(ev, toggle)
123
+ expect(toggle).toHaveBeenCalled()
124
+ expect(ev.preventDefault).toHaveBeenCalled()
125
+ })
126
+
127
+ it('toggles on Space', () => {
128
+ const ctx = makeCtx()
129
+ const m = createMenus(ctx)
130
+ const toggle = vi.fn()
131
+ m.toggleCheckboxWithKeyboard({ key: ' ', preventDefault: vi.fn() } as any, toggle)
132
+ expect(toggle).toHaveBeenCalled()
133
+ })
134
+
135
+ it('ignores other keys', () => {
136
+ const ctx = makeCtx()
137
+ const m = createMenus(ctx)
138
+ const toggle = vi.fn()
139
+ m.toggleCheckboxWithKeyboard({ key: 'a', preventDefault: vi.fn() } as any, toggle)
140
+ expect(toggle).not.toHaveBeenCalled()
141
+ })
142
+ })
143
+
144
+ describe('createMenus - isColumnFiltered', () => {
145
+ it('true when a value filter exists', () => {
146
+ const ctx = makeCtx({ valueFilters: { team: new Set(['A']) } })
147
+ const m = createMenus(ctx)
148
+ expect(m.isColumnFiltered('team')).toBe(true)
149
+ })
150
+
151
+ it('true when a menu filter has a non-empty value', () => {
152
+ const ctx = makeCtx({ filterMenuValues: { name: { operator: 'contains', value: 'x' } } })
153
+ const m = createMenus(ctx)
154
+ expect(m.isColumnFiltered('name')).toBe(true)
155
+ })
156
+
157
+ it('true for isBlank operator regardless of value', () => {
158
+ const ctx = makeCtx({ filterMenuValues: { name: { operator: 'isBlank', value: '' } } })
159
+ const m = createMenus(ctx)
160
+ expect(m.isColumnFiltered('name')).toBe(true)
161
+ })
162
+
163
+ it('false for an empty/whitespace menu value', () => {
164
+ const ctx = makeCtx({ filterMenuValues: { name: { operator: 'contains', value: ' ' } } })
165
+ const m = createMenus(ctx)
166
+ expect(m.isColumnFiltered('name')).toBe(false)
167
+ expect(m.isColumnFiltered('other')).toBe(false)
168
+ })
169
+ })
170
+
171
+ describe('createMenus - closeMenus and open* toggles', () => {
172
+ it('closeMenus clears all popover state', () => {
173
+ const ctx = makeCtx({
174
+ columnMenuFor: 'a',
175
+ filterMenuFor: 'b',
176
+ operatorMenuFor: 'c',
177
+ chooseColumnsPos: { x: 1, y: 1 },
178
+ })
179
+ const m = createMenus(ctx)
180
+ m.closeMenus()
181
+ expect(ctx.columnMenuFor).toBeNull()
182
+ expect(ctx.filterMenuFor).toBeNull()
183
+ expect(ctx.operatorMenuFor).toBeNull()
184
+ expect(ctx.chooseColumnsPos).toBeNull()
185
+ })
186
+
187
+ function fakeMouseEvent() {
188
+ const el = document.createElement('div')
189
+ el.getBoundingClientRect = () =>
190
+ ({ left: 100, right: 300, top: 50, bottom: 70, width: 200, height: 20 } as DOMRect)
191
+ return {
192
+ stopPropagation: vi.fn(),
193
+ currentTarget: el,
194
+ } as any
195
+ }
196
+
197
+ it('openColumnMenu opens, and a second call on the same column closes (toggle)', () => {
198
+ const ctx = makeCtx()
199
+ const m = createMenus(ctx)
200
+ m.openColumnMenu(fakeMouseEvent(), 'name')
201
+ expect(ctx.columnMenuFor).toBe('name')
202
+ expect(ctx.columnMenuPos).toBeTruthy()
203
+ // toggle off
204
+ m.openColumnMenu(fakeMouseEvent(), 'name')
205
+ expect(ctx.columnMenuFor).toBeNull()
206
+ })
207
+
208
+ it('openFilterMenu opens the funnel popover in menu mode', () => {
209
+ const ctx = makeCtx({ showColumnFiltersEffective: true })
210
+ const m = createMenus(ctx)
211
+ m.openFilterMenu(fakeMouseEvent(), 'name')
212
+ expect(ctx.filterMenuFor).toBe('name')
213
+ expect(ctx.columnMenuSearch).toBe('')
214
+ })
215
+
216
+ it('openFilterMenu redirects focus to the inline input in row mode', () => {
217
+ const container = document.createElement('div')
218
+ const input = document.createElement('input')
219
+ input.setAttribute('data-svgrid-filter-col', 'name')
220
+ input.focus = vi.fn()
221
+ input.select = vi.fn()
222
+ container.appendChild(input)
223
+ const ctx = makeCtx({
224
+ showColumnFiltersEffective: false,
225
+ showFilterRowEffective: true,
226
+ scrollContainer: container,
227
+ })
228
+ const m = createMenus(ctx)
229
+ m.openFilterMenu(fakeMouseEvent(), 'name')
230
+ expect(input.focus).toHaveBeenCalled()
231
+ expect(input.select).toHaveBeenCalled()
232
+ expect(input.classList.contains('sv-grid-filter-value-pulse')).toBe(true)
233
+ expect(ctx.filterMenuFor).toBeNull()
234
+ })
235
+
236
+ it('openOperatorMenu opens and toggles closed', () => {
237
+ const ctx = makeCtx()
238
+ const m = createMenus(ctx)
239
+ m.openOperatorMenu(fakeMouseEvent(), 'age')
240
+ expect(ctx.operatorMenuFor).toBe('age')
241
+ m.openOperatorMenu(fakeMouseEvent(), 'age')
242
+ expect(ctx.operatorMenuFor).toBeNull()
243
+ })
244
+
245
+ it('openChooseColumns positions the submenu panel', () => {
246
+ const ctx = makeCtx()
247
+ const m = createMenus(ctx)
248
+ const el = document.createElement('div')
249
+ el.getBoundingClientRect = () =>
250
+ ({ left: 100, right: 300, top: 50, bottom: 70, width: 200, height: 20 } as DOMRect)
251
+ m.openChooseColumns({ stopPropagation: vi.fn(), currentTarget: el } as any)
252
+ expect(ctx.chooseColumnsPos).toBeTruthy()
253
+ expect(typeof ctx.chooseColumnsPos.x).toBe('number')
254
+ })
255
+ })
256
+
257
+ describe('createMenus - sorting and grouping from menu', () => {
258
+ it('sortColumnFromMenu sets a single sort and closes menus', () => {
259
+ const ctx = makeCtx({ columnMenuFor: 'name' })
260
+ const m = createMenus(ctx)
261
+ m.sortColumnFromMenu('name', true)
262
+ expect(ctx.grid._peek().sorting).toEqual([{ id: 'name', desc: true }])
263
+ expect(ctx.columnMenuFor).toBeNull()
264
+ })
265
+
266
+ it('clearColumnSort removes that column from sorting', () => {
267
+ const ctx = makeCtx({ gridState: { sorting: [{ id: 'name', desc: false }, { id: 'age', desc: true }] } })
268
+ const m = createMenus(ctx)
269
+ m.clearColumnSort('name')
270
+ expect(ctx.grid._peek().sorting).toEqual([{ id: 'age', desc: true }])
271
+ })
272
+
273
+ it('groupByColumnFromMenu adds a new grouping column', () => {
274
+ const ctx = makeCtx({ grouping: [] })
275
+ const m = createMenus(ctx)
276
+ m.groupByColumnFromMenu('team')
277
+ expect(ctx.grid.setGrouping).toHaveBeenCalledWith(['team'])
278
+ })
279
+
280
+ it('groupByColumnFromMenu is a no-op when already grouped', () => {
281
+ const ctx = makeCtx({ grouping: ['team'] })
282
+ const m = createMenus(ctx)
283
+ m.groupByColumnFromMenu('team')
284
+ expect(ctx.grid.setGrouping).not.toHaveBeenCalled()
285
+ })
286
+
287
+ it('clearGroupingFromMenu removes the column', () => {
288
+ const ctx = makeCtx({ grouping: ['team', 'dept'] })
289
+ const m = createMenus(ctx)
290
+ m.clearGroupingFromMenu('team')
291
+ expect(ctx.grid.setGrouping).toHaveBeenCalledWith(['dept'])
292
+ })
293
+ })
294
+
295
+ describe('createMenus - facet checkboxes', () => {
296
+ it('isFacetChecked: checked by default, unchecked when excluded', () => {
297
+ const ctx = makeCtx({ valueFilters: { team: new Set(['A']) } })
298
+ const m = createMenus(ctx)
299
+ expect(m.isFacetChecked('team', 'A')).toBe(true)
300
+ expect(m.isFacetChecked('team', 'B')).toBe(false)
301
+ // no filter for column -> default checked
302
+ expect(m.isFacetChecked('other', 'X')).toBe(true)
303
+ })
304
+
305
+ it('toggleFacetValue removes a value, building the set from all values', () => {
306
+ const ctx = makeCtx({ columnMenuFacetValues: ['A', 'B', 'C'] })
307
+ const m = createMenus(ctx)
308
+ m.toggleFacetValue('team', 'A')
309
+ expect([...ctx.valueFilters.team].sort()).toEqual(['B', 'C'])
310
+ })
311
+
312
+ it('toggleFacetValue re-adding the last value clears the filter (all selected)', () => {
313
+ const ctx = makeCtx({
314
+ columnMenuFacetValues: ['A', 'B'],
315
+ valueFilters: { team: new Set(['A']) },
316
+ })
317
+ const m = createMenus(ctx)
318
+ m.toggleFacetValue('team', 'B') // now {A,B} == all -> filter removed
319
+ expect(ctx.valueFilters.team).toBeUndefined()
320
+ })
321
+
322
+ it('isAllFacetsChecked true with no filter, false with a subset', () => {
323
+ const ctx = makeCtx({
324
+ columnMenuFacetValues: ['A', 'B'],
325
+ valueFilters: { team: new Set(['A']) },
326
+ })
327
+ const m = createMenus(ctx)
328
+ expect(m.isAllFacetsChecked('team')).toBe(false)
329
+ expect(m.isAllFacetsChecked('other')).toBe(true)
330
+ })
331
+
332
+ it('toggleAllFacets clears to empty set when all checked', () => {
333
+ const ctx = makeCtx({ columnMenuFacetValues: ['A', 'B'] })
334
+ const m = createMenus(ctx)
335
+ m.toggleAllFacets('team') // was all checked -> empty set
336
+ expect(ctx.valueFilters.team.size).toBe(0)
337
+ })
338
+
339
+ it('toggleAllFacets restores all when currently a subset', () => {
340
+ const ctx = makeCtx({
341
+ columnMenuFacetValues: ['A', 'B'],
342
+ valueFilters: { team: new Set(['A']) },
343
+ })
344
+ const m = createMenus(ctx)
345
+ m.toggleAllFacets('team') // was subset -> remove filter (all)
346
+ expect(ctx.valueFilters.team).toBeUndefined()
347
+ })
348
+ })
349
+
350
+ describe('createMenus - clearColumnFilter', () => {
351
+ it('clears value, menu, and row filter state for the column', () => {
352
+ const ctx = makeCtx({
353
+ valueFilters: { team: new Set(['A']) },
354
+ filterMenuValues: { team: { operator: 'contains', value: 'x' } },
355
+ filterRowValues: { team: 'x' },
356
+ })
357
+ const m = createMenus(ctx)
358
+ m.clearColumnFilter('team')
359
+ expect(ctx.valueFilters.team).toBeUndefined()
360
+ expect(ctx.filterMenuValues.team).toBeUndefined()
361
+ expect(ctx.filterRowValues.team).toBeUndefined()
362
+ })
363
+
364
+ it('is a no-op when nothing is set for the column', () => {
365
+ const ctx = makeCtx()
366
+ const m = createMenus(ctx)
367
+ expect(() => m.clearColumnFilter('team')).not.toThrow()
368
+ })
369
+ })
370
+
371
+ describe('createMenus - pagination', () => {
372
+ it('changePage applies the delta', () => {
373
+ const ctx = makeCtx()
374
+ const m = createMenus(ctx)
375
+ m.changePage(1)
376
+ const updater = ctx.grid.setPagination.mock.calls[0][0]
377
+ expect(updater({ pageIndex: 2, pageSize: 10 })).toEqual({ pageIndex: 3, pageSize: 10 })
378
+ })
379
+
380
+ it('changePage clamps to zero on a negative delta', () => {
381
+ const ctx = makeCtx()
382
+ const m = createMenus(ctx)
383
+ m.changePage(-1)
384
+ const updater = ctx.grid.setPagination.mock.calls[0][0]
385
+ expect(updater({ pageIndex: 0, pageSize: 10 })).toEqual({ pageIndex: 0, pageSize: 10 })
386
+ expect(updater(undefined)).toEqual({ pageIndex: 0 }) // missing prev -> 0
387
+ })
388
+
389
+ it('goToPage clamps to zero', () => {
390
+ const ctx = makeCtx()
391
+ const m = createMenus(ctx)
392
+ m.goToPage(-5)
393
+ const updater = ctx.grid.setPagination.mock.calls[0][0]
394
+ expect(updater({ pageIndex: 9 })).toEqual({ pageIndex: 0 })
395
+ })
396
+
397
+ it('setPageSize keeps the first visible row in view', () => {
398
+ const ctx = makeCtx()
399
+ const m = createMenus(ctx)
400
+ m.setPageSize(20)
401
+ const updater = ctx.grid.setPagination.mock.calls[0][0]
402
+ // firstVisibleRow = 2*10 = 20; new index = floor(20/20) = 1
403
+ expect(updater({ pageIndex: 2, pageSize: 10 })).toEqual({ pageIndex: 1, pageSize: 20 })
404
+ })
405
+ })
406
+
407
+ describe('createMenus - context menu open/close', () => {
408
+ function fakeContextEvent() {
409
+ return { preventDefault: vi.fn(), clientX: 120, clientY: 200 } as any
410
+ }
411
+
412
+ it('openContextMenu is a no-op when contextMenu prop is unset', () => {
413
+ const ctx = makeCtx({ props: {} })
414
+ const m = createMenus(ctx)
415
+ const ev = fakeContextEvent()
416
+ m.openContextMenu(ev, 0, 0, 'name')
417
+ expect(ev.preventDefault).not.toHaveBeenCalled()
418
+ expect(ctx.contextMenuFor).toBeNull()
419
+ })
420
+
421
+ it('openContextMenu sets the target and position', () => {
422
+ const ctx = makeCtx({
423
+ props: { contextMenu: true },
424
+ allRows: [{ id: 'r0', original: { id: 0, name: 'x' } }],
425
+ })
426
+ const m = createMenus(ctx)
427
+ const ev = fakeContextEvent()
428
+ m.openContextMenu(ev, 0, 1, 'name')
429
+ expect(ev.preventDefault).toHaveBeenCalled()
430
+ expect(ctx.contextMenuFor).toEqual({
431
+ rowIndex: 0,
432
+ colIndex: 1,
433
+ columnId: 'name',
434
+ rowId: 'r0',
435
+ row: { id: 0, name: 'x' },
436
+ })
437
+ expect(ctx.contextMenuPos).toBeTruthy()
438
+ })
439
+
440
+ it('openContextMenu falls back to rowIndex when the row model is missing', () => {
441
+ const ctx = makeCtx({ props: { contextMenu: true }, allRows: [] })
442
+ const m = createMenus(ctx)
443
+ m.openContextMenu(fakeContextEvent(), 3, 0, 'name')
444
+ expect(ctx.contextMenuFor.rowId).toBe('3')
445
+ expect(ctx.contextMenuFor.row).toBeNull()
446
+ })
447
+
448
+ it('closeContextMenu clears the target', () => {
449
+ const ctx = makeCtx({ contextMenuFor: { rowIndex: 0 } })
450
+ const m = createMenus(ctx)
451
+ m.closeContextMenu()
452
+ expect(ctx.contextMenuFor).toBeNull()
453
+ })
454
+ })
455
+
456
+ describe('createMenus - contextMenuItems', () => {
457
+ it('returns [] with no target', () => {
458
+ const ctx = makeCtx({ contextMenuFor: null })
459
+ const m = createMenus(ctx)
460
+ expect(m.contextMenuItems()).toEqual([])
461
+ })
462
+
463
+ it('resolves the default built-in items and trims separators', () => {
464
+ const ctx = makeCtx({
465
+ props: { contextMenu: true },
466
+ contextMenuFor: { rowIndex: 0, colIndex: 0, columnId: 'name', rowId: 'r0', row: { id: 0 } },
467
+ })
468
+ const m = createMenus(ctx)
469
+ const items = m.contextMenuItems()
470
+ const labels = items.filter((i) => !i.separator).map((i) => i.label)
471
+ expect(labels).toContain('Copy')
472
+ expect(labels).toContain('Paste')
473
+ expect(labels).toContain('Remove row')
474
+ expect(labels).toContain('Remove column')
475
+ // no leading/trailing separator
476
+ expect(items[0].separator).toBeFalsy()
477
+ expect(items[items.length - 1].separator).toBeFalsy()
478
+ })
479
+
480
+ it('built-in run wiring invokes clipboard helpers', () => {
481
+ const ctx = makeCtx({
482
+ props: { contextMenu: ['copy', 'cut', 'paste', 'clear'] },
483
+ contextMenuFor: { rowIndex: 0, colIndex: 0, columnId: 'name', rowId: 'r0', row: { id: 0 } },
484
+ })
485
+ const m = createMenus(ctx)
486
+ const items = m.contextMenuItems()
487
+ items.find((i) => i.key === 'copy')!.run!()
488
+ items.find((i) => i.key === 'cut')!.run!()
489
+ items.find((i) => i.key === 'paste')!.run!()
490
+ items.find((i) => i.key === 'clear')!.run!()
491
+ expect(ctx.copySelectionToClipboard).toHaveBeenCalled()
492
+ expect(ctx.cutSelectionToClipboard).toHaveBeenCalled()
493
+ expect(ctx.pasteFromClipboard).toHaveBeenCalled()
494
+ expect(ctx.clearSelectedCells).toHaveBeenCalled()
495
+ })
496
+
497
+ it('row_above / row_below / remove_row mutate internalData', () => {
498
+ const rowA = { id: 1 }
499
+ const rowB = { id: 2 }
500
+ const ctx = makeCtx({
501
+ props: { contextMenu: ['row_above', 'row_below', 'remove_row'] },
502
+ internalData: [rowA, rowB],
503
+ contextMenuFor: { rowIndex: 1, colIndex: 0, columnId: 'name', rowId: 'r1', row: rowB },
504
+ })
505
+ const m = createMenus(ctx)
506
+ const items = m.contextMenuItems()
507
+ items.find((i) => i.key === 'row_above')!.run!()
508
+ expect(ctx.internalData.length).toBe(3)
509
+ expect(ctx.internalData.indexOf(rowB)).toBe(2) // inserted above rowB
510
+ // remove
511
+ const m2 = createMenus(makeCtx({
512
+ props: { contextMenu: ['remove_row'] },
513
+ internalData: [rowA, rowB],
514
+ contextMenuFor: { rowIndex: 1, colIndex: 0, columnId: 'name', rowId: 'r1', row: rowB },
515
+ }))
516
+ const ctx2 = (m2 as any)
517
+ const removeItem = m2.contextMenuItems().find((i) => i.key === 'remove_row')!
518
+ removeItem.run!()
519
+ })
520
+
521
+ it('remove_col removes the matching column def', () => {
522
+ const ctx = makeCtx({
523
+ props: { contextMenu: ['remove_col'] },
524
+ internalColumns: [{ id: 'name' }, { field: 'team' }],
525
+ contextMenuFor: { rowIndex: 0, colIndex: 1, columnId: 'team', rowId: 'r0', row: { id: 0 } },
526
+ })
527
+ const m = createMenus(ctx)
528
+ m.contextMenuItems().find((i) => i.key === 'remove_col')!.run!()
529
+ expect(ctx.internalColumns).toEqual([{ id: 'name' }])
530
+ })
531
+
532
+ it('custom items honor hidden() and disabled() predicates', () => {
533
+ const action = vi.fn()
534
+ const ctx = makeCtx({
535
+ props: {
536
+ contextMenu: [
537
+ { key: 'shown', label: 'Shown', action, disabled: () => true },
538
+ { key: 'gone', label: 'Gone', action, hidden: () => true },
539
+ ],
540
+ },
541
+ contextMenuFor: { rowIndex: 0, colIndex: 0, columnId: 'name', rowId: 'r0', row: { id: 0 } },
542
+ })
543
+ const m = createMenus(ctx)
544
+ const items = m.contextMenuItems()
545
+ expect(items.map((i) => i.key)).toEqual(['shown'])
546
+ expect(items[0].disabled).toBe(true)
547
+ items[0].run!()
548
+ expect(action).toHaveBeenCalled()
549
+ })
550
+
551
+ it('appends comment item when editableComments enabled with default config', () => {
552
+ const ctx = makeCtx({
553
+ props: { contextMenu: true, editableComments: true },
554
+ contextMenuFor: { rowIndex: 0, colIndex: 0, columnId: 'name', rowId: 'r0', row: { id: 0 } },
555
+ })
556
+ const m = createMenus(ctx)
557
+ const labels = m.contextMenuItems().map((i) => i.label)
558
+ expect(labels).toContain('Edit comment')
559
+ })
560
+ })
561
+
562
+ describe('createMenus - comments', () => {
563
+ it('comment item opens the editor seeded from existing note', () => {
564
+ const ctx = makeCtx({
565
+ props: { contextMenu: ['comment'], notes: { r0: { name: 'hello' } } },
566
+ contextMenuPos: { x: 5, y: 9 },
567
+ contextMenuFor: { rowIndex: 0, colIndex: 0, columnId: 'name', rowId: 'r0', row: { id: 0 } },
568
+ })
569
+ const m = createMenus(ctx)
570
+ m.contextMenuItems().find((i) => i.key === 'comment')!.run!()
571
+ expect(ctx.commentDraft).toBe('hello')
572
+ expect(ctx.commentEditFor).toEqual({ rowId: 'r0', columnId: 'name', x: 5, y: 9 })
573
+ expect(ctx.contextMenuFor).toBeNull()
574
+ })
575
+
576
+ it('saveComment writes the draft via onNoteChange and closes', () => {
577
+ const onNoteChange = vi.fn()
578
+ const ctx = makeCtx({
579
+ props: { onNoteChange },
580
+ commentEditFor: { rowId: 'r0', columnId: 'name', x: 0, y: 0 },
581
+ commentDraft: 'note text',
582
+ })
583
+ const m = createMenus(ctx)
584
+ m.saveComment()
585
+ expect(ctx.noteOverrides.r0.name).toBe('note text')
586
+ expect(onNoteChange).toHaveBeenCalledWith({ rowId: 'r0', columnId: 'name', note: 'note text' })
587
+ expect(ctx.commentEditFor).toBeNull()
588
+ })
589
+
590
+ it('saveComment is a no-op without an active editor', () => {
591
+ const ctx = makeCtx({ commentEditFor: null })
592
+ const m = createMenus(ctx)
593
+ m.saveComment()
594
+ expect(ctx.commentEditFor).toBeNull()
595
+ })
596
+
597
+ it('removeComment writes an empty note and closes', () => {
598
+ const ctx = makeCtx({
599
+ commentEditFor: { rowId: 'r0', columnId: 'name', x: 0, y: 0 },
600
+ noteOverrides: { r0: { name: 'old' } },
601
+ })
602
+ const m = createMenus(ctx)
603
+ m.removeComment()
604
+ expect(ctx.noteOverrides.r0.name).toBe('')
605
+ expect(ctx.commentEditFor).toBeNull()
606
+ })
607
+
608
+ it('removeComment is a no-op without an active editor', () => {
609
+ const ctx = makeCtx({ commentEditFor: null })
610
+ const m = createMenus(ctx)
611
+ expect(() => m.removeComment()).not.toThrow()
612
+ })
613
+
614
+ it('closeCommentEditor clears the editor', () => {
615
+ const ctx = makeCtx({ commentEditFor: { rowId: 'r0', columnId: 'name', x: 0, y: 0 } })
616
+ const m = createMenus(ctx)
617
+ m.closeCommentEditor()
618
+ expect(ctx.commentEditFor).toBeNull()
619
+ })
620
+ })