@svgrid/grid 2.2.27 → 2.2.28

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 (212) hide show
  1. package/README.md +199 -199
  2. package/dist/FlexRender.svelte +96 -96
  3. package/dist/GridFooter.svelte +179 -179
  4. package/dist/GridMenus.svelte +734 -734
  5. package/dist/SvAutoComplete.svelte +165 -165
  6. package/dist/SvCalendar.svelte +503 -503
  7. package/dist/SvCarousel.svelte +141 -141
  8. package/dist/SvCheckBox.svelte +101 -101
  9. package/dist/SvColorInput.svelte +181 -181
  10. package/dist/SvComboBox.svelte +275 -275
  11. package/dist/SvContextMenu.svelte +116 -116
  12. package/dist/SvCountryInput.svelte +160 -160
  13. package/dist/SvDrawer.svelte +254 -250
  14. package/dist/SvDropDownList.svelte +374 -374
  15. package/dist/SvDurationInput.svelte +126 -126
  16. package/dist/SvField.svelte +293 -293
  17. package/dist/SvForm.svelte +437 -437
  18. package/dist/SvGrid.css +2723 -2723
  19. package/dist/SvGrid.svelte +3333 -3333
  20. package/dist/SvGridChart.svelte +1724 -1724
  21. package/dist/SvGridChartPanel.svelte +485 -485
  22. package/dist/SvGridChartView.svelte +70 -70
  23. package/dist/SvGridDropdown.svelte +696 -696
  24. package/dist/SvGridSelect.svelte +259 -259
  25. package/dist/SvGroupCell.svelte +109 -109
  26. package/dist/SvListBox.svelte +287 -287
  27. package/dist/SvMaskedInput.svelte +122 -122
  28. package/dist/SvMenu.svelte +124 -124
  29. package/dist/SvMenuList.svelte +146 -146
  30. package/dist/SvMultiSelect.svelte +281 -281
  31. package/dist/SvNumberInput.svelte +168 -168
  32. package/dist/SvOtpInput.svelte +158 -158
  33. package/dist/SvPasswordInput.svelte +151 -151
  34. package/dist/SvPhoneInput.svelte +133 -131
  35. package/dist/SvPopover.svelte +197 -197
  36. package/dist/SvRadioGroup.svelte +107 -107
  37. package/dist/SvRowGroupPanel.svelte +170 -170
  38. package/dist/SvScrollArea.svelte +61 -61
  39. package/dist/SvSlider.svelte +200 -200
  40. package/dist/SvSwitchButton.svelte +107 -107
  41. package/dist/SvTagsInput.svelte +112 -112
  42. package/dist/SvTextInput.svelte +147 -147
  43. package/dist/SvTimePicker.svelte +245 -245
  44. package/dist/SvToaster.svelte +159 -159
  45. package/dist/SvToggleButton.svelte +85 -85
  46. package/dist/SvTooltip.svelte +161 -161
  47. package/dist/SvTour.svelte +204 -204
  48. package/dist/SvTree.svelte +437 -437
  49. package/dist/SvTreeSelect.svelte +233 -233
  50. package/dist/cdn/{GridMenus-BzxEYNtW.js → GridMenus-BbzEvTYO.js} +2 -2
  51. package/dist/cdn/{GridMenus-Dr-nGEXn.js → GridMenus-E220X2kM.js} +2 -2
  52. package/dist/cdn/{SvDateTimePicker-B1sDc9Pz.js → SvDateTimePicker-B8GopjhC.js} +2 -2
  53. package/dist/cdn/{SvDateTimePicker-Bh0FTs1E.js → SvDateTimePicker-DB0aIEk8.js} +2 -2
  54. package/dist/cdn/{SvGridChart-Bgk4hDs8.js → SvGridChart-BcLCYvw9.js} +1 -1
  55. package/dist/cdn/{SvGridChart-BKQsO5fZ.js → SvGridChart-BhnFdLd0.js} +1 -1
  56. package/dist/cdn/{SvGridChartPanel-DkCc2Z7b.js → SvGridChartPanel-Cmx9DHAS.js} +1 -1
  57. package/dist/cdn/{SvGridChartPanel-DSkHIQDd.js → SvGridChartPanel-DvjwxLvK.js} +1 -1
  58. package/dist/cdn/{SvGridChartView-BrbVaELm.js → SvGridChartView-B40DTJSX.js} +1 -1
  59. package/dist/cdn/{SvGridChartView-i9rWvSKg.js → SvGridChartView-PwqI4s02.js} +1 -1
  60. package/dist/cdn/{SvGridDropdown-D08sVmAy.js → SvGridDropdown-DjALc_4f.js} +1 -1
  61. package/dist/cdn/{SvGridDropdown-CGxxopQ4.js → SvGridDropdown-hpnHvOGt.js} +1 -1
  62. package/dist/cdn/{src-cgs1wcUS.js → src-BxNEslEo.js} +25 -25
  63. package/dist/cdn/{src-Cw8d9U8j.js → src-C4yKQZ5t.js} +26 -26
  64. package/dist/cdn/svgrid.js +5 -5
  65. package/dist/cdn/svgrid.svelte-external.js +5 -5
  66. package/dist/chart-export.js +8 -8
  67. package/package.json +10 -10
  68. package/src/FlexRender.svelte +96 -96
  69. package/src/GridFooter.svelte +179 -179
  70. package/src/GridMenus.svelte +734 -734
  71. package/src/SvAutoComplete.svelte +165 -165
  72. package/src/SvCalendar.svelte +503 -503
  73. package/src/SvCalendar.test.ts +226 -226
  74. package/src/SvCarousel.svelte +141 -141
  75. package/src/SvCheckBox.svelte +101 -101
  76. package/src/SvColorInput.svelte +181 -181
  77. package/src/SvComboBox.svelte +275 -275
  78. package/src/SvContextMenu.svelte +116 -116
  79. package/src/SvCountryInput.svelte +160 -160
  80. package/src/SvDrawer.svelte +254 -250
  81. package/src/SvDrawer.test.ts +18 -1
  82. package/src/SvDropDownList.svelte +374 -374
  83. package/src/SvDurationInput.svelte +126 -126
  84. package/src/SvField.svelte +293 -293
  85. package/src/SvForm.svelte +437 -437
  86. package/src/SvForm.test.ts +411 -411
  87. package/src/SvGrid.controller.svelte.ts +3239 -3239
  88. package/src/SvGrid.css +2723 -2723
  89. package/src/SvGrid.svelte +3333 -3333
  90. package/src/SvGrid.types.ts +1613 -1613
  91. package/src/SvGridChart.svelte +1724 -1724
  92. package/src/SvGridChartPanel.svelte +485 -485
  93. package/src/SvGridChartView.svelte +70 -70
  94. package/src/SvGridDropdown.svelte +696 -696
  95. package/src/SvGridSelect.svelte +259 -259
  96. package/src/SvGroupCell.svelte +109 -109
  97. package/src/SvListBox.svelte +287 -287
  98. package/src/SvMaskedInput.svelte +122 -122
  99. package/src/SvMenu.svelte +124 -124
  100. package/src/SvMenu.test.ts +97 -97
  101. package/src/SvMenuList.svelte +146 -146
  102. package/src/SvMultiSelect.svelte +281 -281
  103. package/src/SvNavPane.test.ts +15 -4
  104. package/src/SvNumberInput.svelte +168 -168
  105. package/src/SvOtpInput.svelte +158 -158
  106. package/src/SvPasswordInput.svelte +151 -151
  107. package/src/SvPhoneInput.svelte +133 -131
  108. package/src/SvPopover.svelte +197 -197
  109. package/src/SvRadioGroup.svelte +107 -107
  110. package/src/SvRowGroupPanel.svelte +170 -170
  111. package/src/SvScrollArea.svelte +61 -61
  112. package/src/SvSlider.svelte +200 -200
  113. package/src/SvSwitchButton.svelte +107 -107
  114. package/src/SvTagsInput.svelte +112 -112
  115. package/src/SvTextInput.svelte +147 -147
  116. package/src/SvTimePicker.svelte +245 -245
  117. package/src/SvToaster.svelte +159 -159
  118. package/src/SvToaster.test.ts +95 -95
  119. package/src/SvToggleButton.svelte +85 -85
  120. package/src/SvTooltip.svelte +161 -161
  121. package/src/SvTour.svelte +204 -204
  122. package/src/SvTree.svelte +437 -437
  123. package/src/SvTreeSelect.svelte +233 -233
  124. package/src/a11y/dismissable.test.ts +119 -119
  125. package/src/a11y/dismissable.ts +114 -114
  126. package/src/a11y.contract.test.ts +49 -49
  127. package/src/a11y.test.ts +59 -59
  128. package/src/a11y.ts +61 -61
  129. package/src/ai.test.ts +502 -502
  130. package/src/ai.ts +1391 -1391
  131. package/src/build-api.coverage.test.ts +633 -633
  132. package/src/build-api.ts +895 -895
  133. package/src/builtin-editors.grid.test.ts +85 -85
  134. package/src/cell-formatting.ts +169 -169
  135. package/src/cell-render.ts +469 -469
  136. package/src/chart-export.ts +201 -201
  137. package/src/chart-view.svelte.ts +36 -36
  138. package/src/chart.ts +2296 -2296
  139. package/src/collaboration.test.ts +104 -104
  140. package/src/collaboration.ts +167 -167
  141. package/src/core.performance.test.ts +30 -30
  142. package/src/core.ts +1111 -1111
  143. package/src/createAutocomplete.svelte.ts +132 -132
  144. package/src/createCombobox.svelte.ts +191 -191
  145. package/src/createCountryInput.svelte.ts +157 -157
  146. package/src/createDropdownList.svelte.ts +168 -168
  147. package/src/createForm.svelte.ts +386 -386
  148. package/src/createGrid.svelte.ts +42 -42
  149. package/src/createGrid.test.ts +10 -10
  150. package/src/createGridState.svelte.ts +17 -17
  151. package/src/createMenu.svelte.ts +225 -225
  152. package/src/createPopoverSelect.svelte.ts +213 -213
  153. package/src/createSlider.svelte.ts +191 -191
  154. package/src/createTooltip.svelte.ts +144 -144
  155. package/src/createTree.svelte.ts +319 -319
  156. package/src/editing.test.ts +859 -859
  157. package/src/editing.ts +675 -675
  158. package/src/editor-contract.ts +157 -157
  159. package/src/export-data-api.test.ts +126 -126
  160. package/src/export-format.test.ts +107 -107
  161. package/src/export-format.ts +598 -598
  162. package/src/filter-operators.ts +157 -157
  163. package/src/flex-render.ts +3 -3
  164. package/src/form-field.ts +127 -127
  165. package/src/index.ts +713 -713
  166. package/src/js-scroller.svelte.ts +173 -173
  167. package/src/keyboard.test.ts +59 -59
  168. package/src/keyboard.ts +97 -97
  169. package/src/list-nav.test.ts +49 -49
  170. package/src/list-nav.ts +29 -29
  171. package/src/list-option.test.ts +56 -56
  172. package/src/list-option.ts +172 -172
  173. package/src/menus.test.ts +700 -700
  174. package/src/menus.ts +652 -652
  175. package/src/merge-objects.ts +48 -48
  176. package/src/overlays.test.ts +90 -90
  177. package/src/render-component.ts +28 -28
  178. package/src/scheduler-model.test.ts +562 -562
  179. package/src/scheduler-model.ts +872 -872
  180. package/src/selection.test.ts +754 -754
  181. package/src/selection.ts +600 -600
  182. package/src/server-data-source.test.ts +289 -289
  183. package/src/server-data-source.ts +413 -413
  184. package/src/sparkline.test.ts +68 -68
  185. package/src/sparkline.ts +169 -169
  186. package/src/spreadsheet.test.ts +489 -489
  187. package/src/spreadsheet.ts +312 -312
  188. package/src/static-functions.ts +11 -11
  189. package/src/subscribe.ts +38 -38
  190. package/src/svgrid-wrapper.types.ts +524 -524
  191. package/src/svgrid.behavior.test.ts +706 -706
  192. package/src/svgrid.charting.test.ts +534 -534
  193. package/src/svgrid.features.test.ts +157 -157
  194. package/src/svgrid.new-features.wrapper.test.ts +251 -251
  195. package/src/svgrid.wrapper.test.ts +40 -40
  196. package/src/test-setup.ts +62 -62
  197. package/src/themes/index.ts +215 -215
  198. package/src/toast-store.svelte.ts +246 -246
  199. package/src/toast-store.test.ts +147 -147
  200. package/src/ui-buttons.test.ts +144 -144
  201. package/src/ui-inputs.test.ts +118 -118
  202. package/src/ui-range.test.ts +70 -70
  203. package/src/ui-selection.test.ts +155 -155
  204. package/src/ui-tier1.test.ts +142 -142
  205. package/src/virtual.test.ts +88 -88
  206. package/src/virtualization/column-virtualizer.test.ts +27 -27
  207. package/src/virtualization/column-virtualizer.ts +30 -30
  208. package/src/virtualization/svelte-virtualizer.svelte.ts +26 -26
  209. package/src/virtualization/types.ts +30 -30
  210. package/src/virtualization/virtualizer.test.ts +47 -47
  211. package/src/virtualization/virtualizer.ts +296 -296
  212. package/themes/ember.css +71 -71
package/src/menus.test.ts CHANGED
@@ -1,700 +1,700 @@
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('addFilterToken builds a newline-serialised in/notIn list, ignoring dupes/blanks', () => {
102
- const ctx = makeCtx()
103
- const m = createMenus(ctx)
104
- m.addFilterToken('symbol', 'TSM')
105
- m.addFilterToken('symbol', 'BP')
106
- m.addFilterToken('symbol', 'TSM') // dupe - ignored
107
- m.addFilterToken('symbol', ' ') // blank - ignored
108
- expect(ctx.filterRowValues.symbol).toBe('TSM\nBP')
109
- // mirrored into the menu model too, so both filter surfaces agree
110
- expect(ctx.filterMenuValues.symbol.value).toBe('TSM\nBP')
111
- })
112
-
113
- it('removeFilterToken drops a value from the list', () => {
114
- const ctx = makeCtx({ filterRowValues: { symbol: 'TSM\nBP\nBABA' } })
115
- const m = createMenus(ctx)
116
- m.removeFilterToken('symbol', 'BP')
117
- expect(ctx.filterRowValues.symbol).toBe('TSM\nBABA')
118
- })
119
-
120
- it('toggleFilterToken adds when absent and removes when present', () => {
121
- const ctx = makeCtx()
122
- const m = createMenus(ctx)
123
- m.toggleFilterToken('symbol', 'BP')
124
- expect(ctx.filterRowValues.symbol).toBe('BP')
125
- m.toggleFilterToken('symbol', 'BP')
126
- expect(ctx.filterRowValues.symbol).toBe('')
127
- })
128
-
129
- it('openInSuggest anchors the dropdown and targets the column; closeInSuggest clears it', () => {
130
- const ctx = makeCtx()
131
- const m = createMenus(ctx)
132
- const anchor = {
133
- getBoundingClientRect: () => ({ left: 40, bottom: 80 }),
134
- } as unknown as HTMLElement
135
- m.openInSuggest(anchor, 'symbol')
136
- expect(ctx.inSuggestFor).toBe('symbol')
137
- expect(ctx.inSuggestPos).toMatchObject({ y: 84 })
138
- m.closeInSuggest()
139
- expect(ctx.inSuggestFor).toBe(null)
140
- })
141
-
142
- it('closeMenus also dismisses the in-suggest dropdown', () => {
143
- const ctx = makeCtx({ inSuggestFor: 'symbol' })
144
- const m = createMenus(ctx)
145
- m.closeMenus()
146
- expect(ctx.inSuggestFor).toBe(null)
147
- })
148
-
149
- it('updateFilterMenuValueTo sets the upper bound for between', () => {
150
- const ctx = makeCtx({ filterMenuValues: { age: { operator: 'between', value: '5' } } })
151
- const m = createMenus(ctx)
152
- m.updateFilterMenuValueTo('age', '10')
153
- expect(ctx.filterMenuValues.age).toEqual({ operator: 'between', value: '5', valueTo: '10' })
154
- })
155
-
156
- it('updateFilterMenuValueTo defaults a missing entry to between', () => {
157
- const ctx = makeCtx()
158
- const m = createMenus(ctx)
159
- m.updateFilterMenuValueTo('age', '10')
160
- expect(ctx.filterMenuValues.age).toEqual({ operator: 'between', value: '', valueTo: '10' })
161
- })
162
- })
163
-
164
- describe('createMenus - toggleCheckboxWithKeyboard', () => {
165
- it('toggles on Enter and prevents default', () => {
166
- const ctx = makeCtx()
167
- const m = createMenus(ctx)
168
- const toggle = vi.fn()
169
- const ev = { key: 'Enter', preventDefault: vi.fn() } as any
170
- m.toggleCheckboxWithKeyboard(ev, toggle)
171
- expect(toggle).toHaveBeenCalled()
172
- expect(ev.preventDefault).toHaveBeenCalled()
173
- })
174
-
175
- it('toggles on Space', () => {
176
- const ctx = makeCtx()
177
- const m = createMenus(ctx)
178
- const toggle = vi.fn()
179
- m.toggleCheckboxWithKeyboard({ key: ' ', preventDefault: vi.fn() } as any, toggle)
180
- expect(toggle).toHaveBeenCalled()
181
- })
182
-
183
- it('ignores other keys', () => {
184
- const ctx = makeCtx()
185
- const m = createMenus(ctx)
186
- const toggle = vi.fn()
187
- m.toggleCheckboxWithKeyboard({ key: 'a', preventDefault: vi.fn() } as any, toggle)
188
- expect(toggle).not.toHaveBeenCalled()
189
- })
190
- })
191
-
192
- describe('createMenus - isColumnFiltered', () => {
193
- it('true when a value filter exists', () => {
194
- const ctx = makeCtx({ valueFilters: { team: new Set(['A']) } })
195
- const m = createMenus(ctx)
196
- expect(m.isColumnFiltered('team')).toBe(true)
197
- })
198
-
199
- it('true when a menu filter has a non-empty value', () => {
200
- const ctx = makeCtx({ filterMenuValues: { name: { operator: 'contains', value: 'x' } } })
201
- const m = createMenus(ctx)
202
- expect(m.isColumnFiltered('name')).toBe(true)
203
- })
204
-
205
- it('true for isBlank operator regardless of value', () => {
206
- const ctx = makeCtx({ filterMenuValues: { name: { operator: 'isBlank', value: '' } } })
207
- const m = createMenus(ctx)
208
- expect(m.isColumnFiltered('name')).toBe(true)
209
- })
210
-
211
- it('false for an empty/whitespace menu value', () => {
212
- const ctx = makeCtx({ filterMenuValues: { name: { operator: 'contains', value: ' ' } } })
213
- const m = createMenus(ctx)
214
- expect(m.isColumnFiltered('name')).toBe(false)
215
- expect(m.isColumnFiltered('other')).toBe(false)
216
- })
217
- })
218
-
219
- describe('createMenus - closeMenus and open* toggles', () => {
220
- it('closeMenus clears all popover state', () => {
221
- const ctx = makeCtx({
222
- columnMenuFor: 'a',
223
- filterMenuFor: 'b',
224
- operatorMenuFor: 'c',
225
- chooseColumnsPos: { x: 1, y: 1 },
226
- })
227
- const m = createMenus(ctx)
228
- m.closeMenus()
229
- expect(ctx.columnMenuFor).toBeNull()
230
- expect(ctx.filterMenuFor).toBeNull()
231
- expect(ctx.operatorMenuFor).toBeNull()
232
- expect(ctx.chooseColumnsPos).toBeNull()
233
- })
234
-
235
- function fakeMouseEvent() {
236
- const el = document.createElement('div')
237
- el.getBoundingClientRect = () =>
238
- ({ left: 100, right: 300, top: 50, bottom: 70, width: 200, height: 20 } as DOMRect)
239
- return {
240
- stopPropagation: vi.fn(),
241
- currentTarget: el,
242
- } as any
243
- }
244
-
245
- it('openColumnMenu opens, and a second call on the same column closes (toggle)', () => {
246
- const ctx = makeCtx()
247
- const m = createMenus(ctx)
248
- m.openColumnMenu(fakeMouseEvent(), 'name')
249
- expect(ctx.columnMenuFor).toBe('name')
250
- expect(ctx.columnMenuPos).toBeTruthy()
251
- // toggle off
252
- m.openColumnMenu(fakeMouseEvent(), 'name')
253
- expect(ctx.columnMenuFor).toBeNull()
254
- })
255
-
256
- it('openFilterMenu opens the funnel popover in menu mode', () => {
257
- const ctx = makeCtx({ showColumnFiltersEffective: true })
258
- const m = createMenus(ctx)
259
- m.openFilterMenu(fakeMouseEvent(), 'name')
260
- expect(ctx.filterMenuFor).toBe('name')
261
- expect(ctx.columnMenuSearch).toBe('')
262
- })
263
-
264
- it('openFilterMenu redirects focus to the inline input in row mode', () => {
265
- const container = document.createElement('div')
266
- const input = document.createElement('input')
267
- input.setAttribute('data-svgrid-filter-col', 'name')
268
- input.focus = vi.fn()
269
- input.select = vi.fn()
270
- container.appendChild(input)
271
- const ctx = makeCtx({
272
- showColumnFiltersEffective: false,
273
- showFilterRowEffective: true,
274
- scrollContainer: container,
275
- })
276
- const m = createMenus(ctx)
277
- m.openFilterMenu(fakeMouseEvent(), 'name')
278
- expect(input.focus).toHaveBeenCalled()
279
- expect(input.select).toHaveBeenCalled()
280
- expect(input.classList.contains('sv-grid-filter-value-pulse')).toBe(true)
281
- expect(ctx.filterMenuFor).toBeNull()
282
- })
283
-
284
- it('openOperatorMenu opens and toggles closed', () => {
285
- const ctx = makeCtx()
286
- const m = createMenus(ctx)
287
- m.openOperatorMenu(fakeMouseEvent(), 'age')
288
- expect(ctx.operatorMenuFor).toBe('age')
289
- m.openOperatorMenu(fakeMouseEvent(), 'age')
290
- expect(ctx.operatorMenuFor).toBeNull()
291
- })
292
-
293
- it('openChooseColumns positions the submenu panel', () => {
294
- const ctx = makeCtx()
295
- const m = createMenus(ctx)
296
- const el = document.createElement('div')
297
- el.getBoundingClientRect = () =>
298
- ({ left: 100, right: 300, top: 50, bottom: 70, width: 200, height: 20 } as DOMRect)
299
- m.openChooseColumns({ stopPropagation: vi.fn(), currentTarget: el } as any)
300
- expect(ctx.chooseColumnsPos).toBeTruthy()
301
- expect(typeof ctx.chooseColumnsPos.x).toBe('number')
302
- })
303
- })
304
-
305
- describe('createMenus - sorting and grouping from menu', () => {
306
- it('sortColumnFromMenu sets a single sort and closes menus', () => {
307
- const ctx = makeCtx({ columnMenuFor: 'name' })
308
- const m = createMenus(ctx)
309
- m.sortColumnFromMenu('name', true)
310
- expect(ctx.grid._peek().sorting).toEqual([{ id: 'name', desc: true }])
311
- expect(ctx.columnMenuFor).toBeNull()
312
- })
313
-
314
- it('clearColumnSort removes that column from sorting', () => {
315
- const ctx = makeCtx({ gridState: { sorting: [{ id: 'name', desc: false }, { id: 'age', desc: true }] } })
316
- const m = createMenus(ctx)
317
- m.clearColumnSort('name')
318
- expect(ctx.grid._peek().sorting).toEqual([{ id: 'age', desc: true }])
319
- })
320
-
321
- it('groupByColumnFromMenu adds a new grouping column', () => {
322
- const ctx = makeCtx({ grouping: [] })
323
- const m = createMenus(ctx)
324
- m.groupByColumnFromMenu('team')
325
- expect(ctx.grid.setGrouping).toHaveBeenCalledWith(['team'])
326
- })
327
-
328
- it('groupByColumnFromMenu is a no-op when already grouped', () => {
329
- const ctx = makeCtx({ grouping: ['team'] })
330
- const m = createMenus(ctx)
331
- m.groupByColumnFromMenu('team')
332
- expect(ctx.grid.setGrouping).not.toHaveBeenCalled()
333
- })
334
-
335
- it('clearGroupingFromMenu removes the column', () => {
336
- const ctx = makeCtx({ grouping: ['team', 'dept'] })
337
- const m = createMenus(ctx)
338
- m.clearGroupingFromMenu('team')
339
- expect(ctx.grid.setGrouping).toHaveBeenCalledWith(['dept'])
340
- })
341
- })
342
-
343
- describe('createMenus - facet checkboxes', () => {
344
- it('isFacetChecked: checked by default, unchecked when excluded', () => {
345
- const ctx = makeCtx({ valueFilters: { team: new Set(['A']) } })
346
- const m = createMenus(ctx)
347
- expect(m.isFacetChecked('team', 'A')).toBe(true)
348
- expect(m.isFacetChecked('team', 'B')).toBe(false)
349
- // no filter for column -> default checked
350
- expect(m.isFacetChecked('other', 'X')).toBe(true)
351
- })
352
-
353
- it('toggleFacetValue removes a value, building the set from all values', () => {
354
- const ctx = makeCtx({ columnMenuFacetValues: ['A', 'B', 'C'] })
355
- const m = createMenus(ctx)
356
- m.toggleFacetValue('team', 'A')
357
- expect([...ctx.valueFilters.team].sort()).toEqual(['B', 'C'])
358
- })
359
-
360
- it('toggleFacetValue re-adding the last value clears the filter (all selected)', () => {
361
- const ctx = makeCtx({
362
- columnMenuFacetValues: ['A', 'B'],
363
- valueFilters: { team: new Set(['A']) },
364
- })
365
- const m = createMenus(ctx)
366
- m.toggleFacetValue('team', 'B') // now {A,B} == all -> filter removed
367
- expect(ctx.valueFilters.team).toBeUndefined()
368
- })
369
-
370
- it('isAllFacetsChecked true with no filter, false with a subset', () => {
371
- const ctx = makeCtx({
372
- columnMenuFacetValues: ['A', 'B'],
373
- valueFilters: { team: new Set(['A']) },
374
- })
375
- const m = createMenus(ctx)
376
- expect(m.isAllFacetsChecked('team')).toBe(false)
377
- expect(m.isAllFacetsChecked('other')).toBe(true)
378
- })
379
-
380
- it('toggleAllFacets clears to empty set when all checked', () => {
381
- const ctx = makeCtx({ columnMenuFacetValues: ['A', 'B'] })
382
- const m = createMenus(ctx)
383
- m.toggleAllFacets('team') // was all checked -> empty set
384
- expect(ctx.valueFilters.team.size).toBe(0)
385
- })
386
-
387
- it('toggleAllFacets restores all when currently a subset', () => {
388
- const ctx = makeCtx({
389
- columnMenuFacetValues: ['A', 'B'],
390
- valueFilters: { team: new Set(['A']) },
391
- })
392
- const m = createMenus(ctx)
393
- m.toggleAllFacets('team') // was subset -> remove filter (all)
394
- expect(ctx.valueFilters.team).toBeUndefined()
395
- })
396
- })
397
-
398
- describe('createMenus - clearColumnFilter', () => {
399
- it('clears value, menu, and row filter state for the column', () => {
400
- const ctx = makeCtx({
401
- valueFilters: { team: new Set(['A']) },
402
- filterMenuValues: { team: { operator: 'contains', value: 'x' } },
403
- filterRowValues: { team: 'x' },
404
- })
405
- const m = createMenus(ctx)
406
- m.clearColumnFilter('team')
407
- expect(ctx.valueFilters.team).toBeUndefined()
408
- expect(ctx.filterMenuValues.team).toBeUndefined()
409
- expect(ctx.filterRowValues.team).toBeUndefined()
410
- })
411
-
412
- it('is a no-op when nothing is set for the column', () => {
413
- const ctx = makeCtx()
414
- const m = createMenus(ctx)
415
- expect(() => m.clearColumnFilter('team')).not.toThrow()
416
- })
417
- })
418
-
419
- describe('createMenus - pagination', () => {
420
- it('changePage applies the delta', () => {
421
- const ctx = makeCtx()
422
- const m = createMenus(ctx)
423
- m.changePage(1)
424
- const updater = ctx.grid.setPagination.mock.calls[0][0]
425
- expect(updater({ pageIndex: 2, pageSize: 10 })).toEqual({ pageIndex: 3, pageSize: 10 })
426
- })
427
-
428
- it('changePage clamps to zero on a negative delta', () => {
429
- const ctx = makeCtx()
430
- const m = createMenus(ctx)
431
- m.changePage(-1)
432
- const updater = ctx.grid.setPagination.mock.calls[0][0]
433
- expect(updater({ pageIndex: 0, pageSize: 10 })).toEqual({ pageIndex: 0, pageSize: 10 })
434
- expect(updater(undefined)).toEqual({ pageIndex: 0 }) // missing prev -> 0
435
- })
436
-
437
- it('goToPage clamps to zero', () => {
438
- const ctx = makeCtx()
439
- const m = createMenus(ctx)
440
- m.goToPage(-5)
441
- const updater = ctx.grid.setPagination.mock.calls[0][0]
442
- expect(updater({ pageIndex: 9 })).toEqual({ pageIndex: 0 })
443
- })
444
-
445
- it('setPageSize keeps the first visible row in view', () => {
446
- const ctx = makeCtx()
447
- const m = createMenus(ctx)
448
- m.setPageSize(20)
449
- const updater = ctx.grid.setPagination.mock.calls[0][0]
450
- // firstVisibleRow = 2*10 = 20; new index = floor(20/20) = 1
451
- expect(updater({ pageIndex: 2, pageSize: 10 })).toEqual({ pageIndex: 1, pageSize: 20 })
452
- })
453
- })
454
-
455
- describe('createMenus - context menu open/close', () => {
456
- function fakeContextEvent() {
457
- return { preventDefault: vi.fn(), clientX: 120, clientY: 200 } as any
458
- }
459
-
460
- it('openContextMenu is a no-op when contextMenu prop is unset', () => {
461
- const ctx = makeCtx({ props: {} })
462
- const m = createMenus(ctx)
463
- const ev = fakeContextEvent()
464
- m.openContextMenu(ev, 0, 0, 'name')
465
- expect(ev.preventDefault).not.toHaveBeenCalled()
466
- expect(ctx.contextMenuFor).toBeNull()
467
- })
468
-
469
- it('openContextMenu sets the target and position', () => {
470
- const ctx = makeCtx({
471
- props: { contextMenu: true },
472
- allRows: [{ id: 'r0', original: { id: 0, name: 'x' } }],
473
- })
474
- const m = createMenus(ctx)
475
- const ev = fakeContextEvent()
476
- m.openContextMenu(ev, 0, 1, 'name')
477
- expect(ev.preventDefault).toHaveBeenCalled()
478
- expect(ctx.contextMenuFor).toEqual({
479
- rowIndex: 0,
480
- colIndex: 1,
481
- columnId: 'name',
482
- rowId: 'r0',
483
- row: { id: 0, name: 'x' },
484
- })
485
- expect(ctx.contextMenuPos).toBeTruthy()
486
- })
487
-
488
- it('openContextMenu falls back to rowIndex when the row model is missing', () => {
489
- const ctx = makeCtx({ props: { contextMenu: true }, allRows: [] })
490
- const m = createMenus(ctx)
491
- m.openContextMenu(fakeContextEvent(), 3, 0, 'name')
492
- expect(ctx.contextMenuFor.rowId).toBe('3')
493
- expect(ctx.contextMenuFor.row).toBeNull()
494
- })
495
-
496
- it('closeContextMenu clears the target', () => {
497
- const ctx = makeCtx({ contextMenuFor: { rowIndex: 0 } })
498
- const m = createMenus(ctx)
499
- m.closeContextMenu()
500
- expect(ctx.contextMenuFor).toBeNull()
501
- })
502
- })
503
-
504
- describe('createMenus - contextMenuItems', () => {
505
- it('returns [] with no target', () => {
506
- const ctx = makeCtx({ contextMenuFor: null })
507
- const m = createMenus(ctx)
508
- expect(m.contextMenuItems()).toEqual([])
509
- })
510
-
511
- it('resolves the default built-in items and trims separators', () => {
512
- const ctx = makeCtx({
513
- props: { contextMenu: true },
514
- contextMenuFor: { rowIndex: 0, colIndex: 0, columnId: 'name', rowId: 'r0', row: { id: 0 } },
515
- })
516
- const m = createMenus(ctx)
517
- const items = m.contextMenuItems()
518
- const labels = items.filter((i) => !i.separator).map((i) => i.label)
519
- expect(labels).toContain('Copy')
520
- expect(labels).toContain('Paste')
521
- expect(labels).toContain('Remove row')
522
- expect(labels).toContain('Remove column')
523
- // no leading/trailing separator
524
- expect(items[0].separator).toBeFalsy()
525
- expect(items[items.length - 1].separator).toBeFalsy()
526
- })
527
-
528
- it('built-in run wiring invokes clipboard helpers', () => {
529
- const ctx = makeCtx({
530
- props: { contextMenu: ['copy', 'cut', 'paste', 'clear'] },
531
- contextMenuFor: { rowIndex: 0, colIndex: 0, columnId: 'name', rowId: 'r0', row: { id: 0 } },
532
- })
533
- const m = createMenus(ctx)
534
- const items = m.contextMenuItems()
535
- items.find((i) => i.key === 'copy')!.run!()
536
- items.find((i) => i.key === 'cut')!.run!()
537
- items.find((i) => i.key === 'paste')!.run!()
538
- items.find((i) => i.key === 'clear')!.run!()
539
- expect(ctx.copySelectionToClipboard).toHaveBeenCalled()
540
- expect(ctx.cutSelectionToClipboard).toHaveBeenCalled()
541
- expect(ctx.pasteFromClipboard).toHaveBeenCalled()
542
- expect(ctx.clearSelectedCells).toHaveBeenCalled()
543
- })
544
-
545
- it('row_above / row_below / remove_row mutate internalData', () => {
546
- const rowA = { id: 1 }
547
- const rowB = { id: 2 }
548
- const ctx = makeCtx({
549
- props: { contextMenu: ['row_above', 'row_below', 'remove_row'] },
550
- internalData: [rowA, rowB],
551
- contextMenuFor: { rowIndex: 1, colIndex: 0, columnId: 'name', rowId: 'r1', row: rowB },
552
- })
553
- const m = createMenus(ctx)
554
- const items = m.contextMenuItems()
555
- items.find((i) => i.key === 'row_above')!.run!()
556
- expect(ctx.internalData.length).toBe(3)
557
- expect(ctx.internalData.indexOf(rowB)).toBe(2) // inserted above rowB
558
- // remove
559
- const m2 = createMenus(makeCtx({
560
- props: { contextMenu: ['remove_row'] },
561
- internalData: [rowA, rowB],
562
- contextMenuFor: { rowIndex: 1, colIndex: 0, columnId: 'name', rowId: 'r1', row: rowB },
563
- }))
564
- const ctx2 = (m2 as any)
565
- const removeItem = m2.contextMenuItems().find((i) => i.key === 'remove_row')!
566
- removeItem.run!()
567
- })
568
-
569
- it('remove_col removes the matching column def', () => {
570
- const ctx = makeCtx({
571
- props: { contextMenu: ['remove_col'] },
572
- internalColumns: [{ id: 'name' }, { field: 'team' }],
573
- contextMenuFor: { rowIndex: 0, colIndex: 1, columnId: 'team', rowId: 'r0', row: { id: 0 } },
574
- })
575
- const m = createMenus(ctx)
576
- m.contextMenuItems().find((i) => i.key === 'remove_col')!.run!()
577
- expect(ctx.internalColumns).toEqual([{ id: 'name' }])
578
- })
579
-
580
- it('custom items honor hidden() and disabled() predicates', () => {
581
- const action = vi.fn()
582
- const ctx = makeCtx({
583
- props: {
584
- contextMenu: [
585
- { key: 'shown', label: 'Shown', action, disabled: () => true },
586
- { key: 'gone', label: 'Gone', action, hidden: () => true },
587
- ],
588
- },
589
- contextMenuFor: { rowIndex: 0, colIndex: 0, columnId: 'name', rowId: 'r0', row: { id: 0 } },
590
- })
591
- const m = createMenus(ctx)
592
- const items = m.contextMenuItems()
593
- expect(items.map((i) => i.key)).toEqual(['shown'])
594
- expect(items[0].disabled).toBe(true)
595
- items[0].run!()
596
- expect(action).toHaveBeenCalled()
597
- })
598
-
599
- it('appends comment item when editableComments enabled with default config', () => {
600
- const ctx = makeCtx({
601
- props: { contextMenu: true, editableComments: true },
602
- contextMenuFor: { rowIndex: 0, colIndex: 0, columnId: 'name', rowId: 'r0', row: { id: 0 } },
603
- })
604
- const m = createMenus(ctx)
605
- const labels = m.contextMenuItems().map((i) => i.label)
606
- expect(labels).toContain('Edit comment')
607
- })
608
- })
609
-
610
- describe('createMenus - comments', () => {
611
- it('comment item opens the editor seeded from existing note', () => {
612
- const ctx = makeCtx({
613
- props: { contextMenu: ['comment'], notes: { r0: { name: 'hello' } } },
614
- contextMenuPos: { x: 5, y: 9 },
615
- contextMenuFor: { rowIndex: 0, colIndex: 0, columnId: 'name', rowId: 'r0', row: { id: 0 } },
616
- })
617
- const m = createMenus(ctx)
618
- m.contextMenuItems().find((i) => i.key === 'comment')!.run!()
619
- expect(ctx.commentDraft).toBe('hello')
620
- expect(ctx.commentEditFor).toEqual({ rowId: 'r0', columnId: 'name', x: 5, y: 9 })
621
- expect(ctx.contextMenuFor).toBeNull()
622
- })
623
-
624
- it('saveComment writes the draft via onNoteChange and closes', () => {
625
- const onNoteChange = vi.fn()
626
- const ctx = makeCtx({
627
- props: { onNoteChange },
628
- commentEditFor: { rowId: 'r0', columnId: 'name', x: 0, y: 0 },
629
- commentDraft: 'note text',
630
- })
631
- const m = createMenus(ctx)
632
- m.saveComment()
633
- expect(ctx.noteOverrides.r0.name).toBe('note text')
634
- expect(onNoteChange).toHaveBeenCalledWith({ rowId: 'r0', columnId: 'name', note: 'note text' })
635
- expect(ctx.commentEditFor).toBeNull()
636
- })
637
-
638
- it('saveComment is a no-op without an active editor', () => {
639
- const ctx = makeCtx({ commentEditFor: null })
640
- const m = createMenus(ctx)
641
- m.saveComment()
642
- expect(ctx.commentEditFor).toBeNull()
643
- })
644
-
645
- it('removeComment writes an empty note and closes', () => {
646
- const ctx = makeCtx({
647
- commentEditFor: { rowId: 'r0', columnId: 'name', x: 0, y: 0 },
648
- noteOverrides: { r0: { name: 'old' } },
649
- })
650
- const m = createMenus(ctx)
651
- m.removeComment()
652
- expect(ctx.noteOverrides.r0.name).toBe('')
653
- expect(ctx.commentEditFor).toBeNull()
654
- })
655
-
656
- it('removeComment is a no-op without an active editor', () => {
657
- const ctx = makeCtx({ commentEditFor: null })
658
- const m = createMenus(ctx)
659
- expect(() => m.removeComment()).not.toThrow()
660
- })
661
-
662
- it('closeCommentEditor clears the editor', () => {
663
- const ctx = makeCtx({ commentEditFor: { rowId: 'r0', columnId: 'name', x: 0, y: 0 } })
664
- const m = createMenus(ctx)
665
- m.closeCommentEditor()
666
- expect(ctx.commentEditFor).toBeNull()
667
- })
668
- })
669
-
670
- describe('createMenus - chart context-menu item', () => {
671
- const target = { rowIndex: 0, colIndex: 0, columnId: 'a', rowId: '0', row: {} }
672
-
673
- it('appends "Chart selected range" to the default menu when charting is enabled', () => {
674
- const ctx = makeCtx({ props: { contextMenu: true }, chartingEnabled: true, contextMenuFor: target })
675
- const items = createMenus(ctx).contextMenuItems()
676
- expect(items.map((i) => i.label)).toContain('Chart selected range')
677
- })
678
-
679
- it('omits the chart item when charting is disabled', () => {
680
- const ctx = makeCtx({ props: { contextMenu: true }, chartingEnabled: false, contextMenuFor: target })
681
- const items = createMenus(ctx).contextMenuItems()
682
- expect(items.map((i) => i.label)).not.toContain('Chart selected range')
683
- })
684
-
685
- it('clicking the chart item opens the chart panel', () => {
686
- const ctx = makeCtx({ props: { contextMenu: true }, chartingEnabled: true, chartPanelOpen: false, contextMenuFor: target })
687
- const item = createMenus(ctx).contextMenuItems().find((i) => i.label === 'Chart selected range')
688
- item?.run?.()
689
- expect(ctx.chartPanelOpen).toBe(true)
690
- })
691
-
692
- it('localizes the chart item label via ctx.messages', () => {
693
- const ctx = makeCtx({
694
- props: { contextMenu: true }, chartingEnabled: true, contextMenuFor: target,
695
- messages: { chartRange: 'Graphique de la selection' },
696
- })
697
- const items = createMenus(ctx).contextMenuItems()
698
- expect(items.map((i) => i.label)).toContain('Graphique de la selection')
699
- })
700
- })
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('addFilterToken builds a newline-serialised in/notIn list, ignoring dupes/blanks', () => {
102
+ const ctx = makeCtx()
103
+ const m = createMenus(ctx)
104
+ m.addFilterToken('symbol', 'TSM')
105
+ m.addFilterToken('symbol', 'BP')
106
+ m.addFilterToken('symbol', 'TSM') // dupe - ignored
107
+ m.addFilterToken('symbol', ' ') // blank - ignored
108
+ expect(ctx.filterRowValues.symbol).toBe('TSM\nBP')
109
+ // mirrored into the menu model too, so both filter surfaces agree
110
+ expect(ctx.filterMenuValues.symbol.value).toBe('TSM\nBP')
111
+ })
112
+
113
+ it('removeFilterToken drops a value from the list', () => {
114
+ const ctx = makeCtx({ filterRowValues: { symbol: 'TSM\nBP\nBABA' } })
115
+ const m = createMenus(ctx)
116
+ m.removeFilterToken('symbol', 'BP')
117
+ expect(ctx.filterRowValues.symbol).toBe('TSM\nBABA')
118
+ })
119
+
120
+ it('toggleFilterToken adds when absent and removes when present', () => {
121
+ const ctx = makeCtx()
122
+ const m = createMenus(ctx)
123
+ m.toggleFilterToken('symbol', 'BP')
124
+ expect(ctx.filterRowValues.symbol).toBe('BP')
125
+ m.toggleFilterToken('symbol', 'BP')
126
+ expect(ctx.filterRowValues.symbol).toBe('')
127
+ })
128
+
129
+ it('openInSuggest anchors the dropdown and targets the column; closeInSuggest clears it', () => {
130
+ const ctx = makeCtx()
131
+ const m = createMenus(ctx)
132
+ const anchor = {
133
+ getBoundingClientRect: () => ({ left: 40, bottom: 80 }),
134
+ } as unknown as HTMLElement
135
+ m.openInSuggest(anchor, 'symbol')
136
+ expect(ctx.inSuggestFor).toBe('symbol')
137
+ expect(ctx.inSuggestPos).toMatchObject({ y: 84 })
138
+ m.closeInSuggest()
139
+ expect(ctx.inSuggestFor).toBe(null)
140
+ })
141
+
142
+ it('closeMenus also dismisses the in-suggest dropdown', () => {
143
+ const ctx = makeCtx({ inSuggestFor: 'symbol' })
144
+ const m = createMenus(ctx)
145
+ m.closeMenus()
146
+ expect(ctx.inSuggestFor).toBe(null)
147
+ })
148
+
149
+ it('updateFilterMenuValueTo sets the upper bound for between', () => {
150
+ const ctx = makeCtx({ filterMenuValues: { age: { operator: 'between', value: '5' } } })
151
+ const m = createMenus(ctx)
152
+ m.updateFilterMenuValueTo('age', '10')
153
+ expect(ctx.filterMenuValues.age).toEqual({ operator: 'between', value: '5', valueTo: '10' })
154
+ })
155
+
156
+ it('updateFilterMenuValueTo defaults a missing entry to between', () => {
157
+ const ctx = makeCtx()
158
+ const m = createMenus(ctx)
159
+ m.updateFilterMenuValueTo('age', '10')
160
+ expect(ctx.filterMenuValues.age).toEqual({ operator: 'between', value: '', valueTo: '10' })
161
+ })
162
+ })
163
+
164
+ describe('createMenus - toggleCheckboxWithKeyboard', () => {
165
+ it('toggles on Enter and prevents default', () => {
166
+ const ctx = makeCtx()
167
+ const m = createMenus(ctx)
168
+ const toggle = vi.fn()
169
+ const ev = { key: 'Enter', preventDefault: vi.fn() } as any
170
+ m.toggleCheckboxWithKeyboard(ev, toggle)
171
+ expect(toggle).toHaveBeenCalled()
172
+ expect(ev.preventDefault).toHaveBeenCalled()
173
+ })
174
+
175
+ it('toggles on Space', () => {
176
+ const ctx = makeCtx()
177
+ const m = createMenus(ctx)
178
+ const toggle = vi.fn()
179
+ m.toggleCheckboxWithKeyboard({ key: ' ', preventDefault: vi.fn() } as any, toggle)
180
+ expect(toggle).toHaveBeenCalled()
181
+ })
182
+
183
+ it('ignores other keys', () => {
184
+ const ctx = makeCtx()
185
+ const m = createMenus(ctx)
186
+ const toggle = vi.fn()
187
+ m.toggleCheckboxWithKeyboard({ key: 'a', preventDefault: vi.fn() } as any, toggle)
188
+ expect(toggle).not.toHaveBeenCalled()
189
+ })
190
+ })
191
+
192
+ describe('createMenus - isColumnFiltered', () => {
193
+ it('true when a value filter exists', () => {
194
+ const ctx = makeCtx({ valueFilters: { team: new Set(['A']) } })
195
+ const m = createMenus(ctx)
196
+ expect(m.isColumnFiltered('team')).toBe(true)
197
+ })
198
+
199
+ it('true when a menu filter has a non-empty value', () => {
200
+ const ctx = makeCtx({ filterMenuValues: { name: { operator: 'contains', value: 'x' } } })
201
+ const m = createMenus(ctx)
202
+ expect(m.isColumnFiltered('name')).toBe(true)
203
+ })
204
+
205
+ it('true for isBlank operator regardless of value', () => {
206
+ const ctx = makeCtx({ filterMenuValues: { name: { operator: 'isBlank', value: '' } } })
207
+ const m = createMenus(ctx)
208
+ expect(m.isColumnFiltered('name')).toBe(true)
209
+ })
210
+
211
+ it('false for an empty/whitespace menu value', () => {
212
+ const ctx = makeCtx({ filterMenuValues: { name: { operator: 'contains', value: ' ' } } })
213
+ const m = createMenus(ctx)
214
+ expect(m.isColumnFiltered('name')).toBe(false)
215
+ expect(m.isColumnFiltered('other')).toBe(false)
216
+ })
217
+ })
218
+
219
+ describe('createMenus - closeMenus and open* toggles', () => {
220
+ it('closeMenus clears all popover state', () => {
221
+ const ctx = makeCtx({
222
+ columnMenuFor: 'a',
223
+ filterMenuFor: 'b',
224
+ operatorMenuFor: 'c',
225
+ chooseColumnsPos: { x: 1, y: 1 },
226
+ })
227
+ const m = createMenus(ctx)
228
+ m.closeMenus()
229
+ expect(ctx.columnMenuFor).toBeNull()
230
+ expect(ctx.filterMenuFor).toBeNull()
231
+ expect(ctx.operatorMenuFor).toBeNull()
232
+ expect(ctx.chooseColumnsPos).toBeNull()
233
+ })
234
+
235
+ function fakeMouseEvent() {
236
+ const el = document.createElement('div')
237
+ el.getBoundingClientRect = () =>
238
+ ({ left: 100, right: 300, top: 50, bottom: 70, width: 200, height: 20 } as DOMRect)
239
+ return {
240
+ stopPropagation: vi.fn(),
241
+ currentTarget: el,
242
+ } as any
243
+ }
244
+
245
+ it('openColumnMenu opens, and a second call on the same column closes (toggle)', () => {
246
+ const ctx = makeCtx()
247
+ const m = createMenus(ctx)
248
+ m.openColumnMenu(fakeMouseEvent(), 'name')
249
+ expect(ctx.columnMenuFor).toBe('name')
250
+ expect(ctx.columnMenuPos).toBeTruthy()
251
+ // toggle off
252
+ m.openColumnMenu(fakeMouseEvent(), 'name')
253
+ expect(ctx.columnMenuFor).toBeNull()
254
+ })
255
+
256
+ it('openFilterMenu opens the funnel popover in menu mode', () => {
257
+ const ctx = makeCtx({ showColumnFiltersEffective: true })
258
+ const m = createMenus(ctx)
259
+ m.openFilterMenu(fakeMouseEvent(), 'name')
260
+ expect(ctx.filterMenuFor).toBe('name')
261
+ expect(ctx.columnMenuSearch).toBe('')
262
+ })
263
+
264
+ it('openFilterMenu redirects focus to the inline input in row mode', () => {
265
+ const container = document.createElement('div')
266
+ const input = document.createElement('input')
267
+ input.setAttribute('data-svgrid-filter-col', 'name')
268
+ input.focus = vi.fn()
269
+ input.select = vi.fn()
270
+ container.appendChild(input)
271
+ const ctx = makeCtx({
272
+ showColumnFiltersEffective: false,
273
+ showFilterRowEffective: true,
274
+ scrollContainer: container,
275
+ })
276
+ const m = createMenus(ctx)
277
+ m.openFilterMenu(fakeMouseEvent(), 'name')
278
+ expect(input.focus).toHaveBeenCalled()
279
+ expect(input.select).toHaveBeenCalled()
280
+ expect(input.classList.contains('sv-grid-filter-value-pulse')).toBe(true)
281
+ expect(ctx.filterMenuFor).toBeNull()
282
+ })
283
+
284
+ it('openOperatorMenu opens and toggles closed', () => {
285
+ const ctx = makeCtx()
286
+ const m = createMenus(ctx)
287
+ m.openOperatorMenu(fakeMouseEvent(), 'age')
288
+ expect(ctx.operatorMenuFor).toBe('age')
289
+ m.openOperatorMenu(fakeMouseEvent(), 'age')
290
+ expect(ctx.operatorMenuFor).toBeNull()
291
+ })
292
+
293
+ it('openChooseColumns positions the submenu panel', () => {
294
+ const ctx = makeCtx()
295
+ const m = createMenus(ctx)
296
+ const el = document.createElement('div')
297
+ el.getBoundingClientRect = () =>
298
+ ({ left: 100, right: 300, top: 50, bottom: 70, width: 200, height: 20 } as DOMRect)
299
+ m.openChooseColumns({ stopPropagation: vi.fn(), currentTarget: el } as any)
300
+ expect(ctx.chooseColumnsPos).toBeTruthy()
301
+ expect(typeof ctx.chooseColumnsPos.x).toBe('number')
302
+ })
303
+ })
304
+
305
+ describe('createMenus - sorting and grouping from menu', () => {
306
+ it('sortColumnFromMenu sets a single sort and closes menus', () => {
307
+ const ctx = makeCtx({ columnMenuFor: 'name' })
308
+ const m = createMenus(ctx)
309
+ m.sortColumnFromMenu('name', true)
310
+ expect(ctx.grid._peek().sorting).toEqual([{ id: 'name', desc: true }])
311
+ expect(ctx.columnMenuFor).toBeNull()
312
+ })
313
+
314
+ it('clearColumnSort removes that column from sorting', () => {
315
+ const ctx = makeCtx({ gridState: { sorting: [{ id: 'name', desc: false }, { id: 'age', desc: true }] } })
316
+ const m = createMenus(ctx)
317
+ m.clearColumnSort('name')
318
+ expect(ctx.grid._peek().sorting).toEqual([{ id: 'age', desc: true }])
319
+ })
320
+
321
+ it('groupByColumnFromMenu adds a new grouping column', () => {
322
+ const ctx = makeCtx({ grouping: [] })
323
+ const m = createMenus(ctx)
324
+ m.groupByColumnFromMenu('team')
325
+ expect(ctx.grid.setGrouping).toHaveBeenCalledWith(['team'])
326
+ })
327
+
328
+ it('groupByColumnFromMenu is a no-op when already grouped', () => {
329
+ const ctx = makeCtx({ grouping: ['team'] })
330
+ const m = createMenus(ctx)
331
+ m.groupByColumnFromMenu('team')
332
+ expect(ctx.grid.setGrouping).not.toHaveBeenCalled()
333
+ })
334
+
335
+ it('clearGroupingFromMenu removes the column', () => {
336
+ const ctx = makeCtx({ grouping: ['team', 'dept'] })
337
+ const m = createMenus(ctx)
338
+ m.clearGroupingFromMenu('team')
339
+ expect(ctx.grid.setGrouping).toHaveBeenCalledWith(['dept'])
340
+ })
341
+ })
342
+
343
+ describe('createMenus - facet checkboxes', () => {
344
+ it('isFacetChecked: checked by default, unchecked when excluded', () => {
345
+ const ctx = makeCtx({ valueFilters: { team: new Set(['A']) } })
346
+ const m = createMenus(ctx)
347
+ expect(m.isFacetChecked('team', 'A')).toBe(true)
348
+ expect(m.isFacetChecked('team', 'B')).toBe(false)
349
+ // no filter for column -> default checked
350
+ expect(m.isFacetChecked('other', 'X')).toBe(true)
351
+ })
352
+
353
+ it('toggleFacetValue removes a value, building the set from all values', () => {
354
+ const ctx = makeCtx({ columnMenuFacetValues: ['A', 'B', 'C'] })
355
+ const m = createMenus(ctx)
356
+ m.toggleFacetValue('team', 'A')
357
+ expect([...ctx.valueFilters.team].sort()).toEqual(['B', 'C'])
358
+ })
359
+
360
+ it('toggleFacetValue re-adding the last value clears the filter (all selected)', () => {
361
+ const ctx = makeCtx({
362
+ columnMenuFacetValues: ['A', 'B'],
363
+ valueFilters: { team: new Set(['A']) },
364
+ })
365
+ const m = createMenus(ctx)
366
+ m.toggleFacetValue('team', 'B') // now {A,B} == all -> filter removed
367
+ expect(ctx.valueFilters.team).toBeUndefined()
368
+ })
369
+
370
+ it('isAllFacetsChecked true with no filter, false with a subset', () => {
371
+ const ctx = makeCtx({
372
+ columnMenuFacetValues: ['A', 'B'],
373
+ valueFilters: { team: new Set(['A']) },
374
+ })
375
+ const m = createMenus(ctx)
376
+ expect(m.isAllFacetsChecked('team')).toBe(false)
377
+ expect(m.isAllFacetsChecked('other')).toBe(true)
378
+ })
379
+
380
+ it('toggleAllFacets clears to empty set when all checked', () => {
381
+ const ctx = makeCtx({ columnMenuFacetValues: ['A', 'B'] })
382
+ const m = createMenus(ctx)
383
+ m.toggleAllFacets('team') // was all checked -> empty set
384
+ expect(ctx.valueFilters.team.size).toBe(0)
385
+ })
386
+
387
+ it('toggleAllFacets restores all when currently a subset', () => {
388
+ const ctx = makeCtx({
389
+ columnMenuFacetValues: ['A', 'B'],
390
+ valueFilters: { team: new Set(['A']) },
391
+ })
392
+ const m = createMenus(ctx)
393
+ m.toggleAllFacets('team') // was subset -> remove filter (all)
394
+ expect(ctx.valueFilters.team).toBeUndefined()
395
+ })
396
+ })
397
+
398
+ describe('createMenus - clearColumnFilter', () => {
399
+ it('clears value, menu, and row filter state for the column', () => {
400
+ const ctx = makeCtx({
401
+ valueFilters: { team: new Set(['A']) },
402
+ filterMenuValues: { team: { operator: 'contains', value: 'x' } },
403
+ filterRowValues: { team: 'x' },
404
+ })
405
+ const m = createMenus(ctx)
406
+ m.clearColumnFilter('team')
407
+ expect(ctx.valueFilters.team).toBeUndefined()
408
+ expect(ctx.filterMenuValues.team).toBeUndefined()
409
+ expect(ctx.filterRowValues.team).toBeUndefined()
410
+ })
411
+
412
+ it('is a no-op when nothing is set for the column', () => {
413
+ const ctx = makeCtx()
414
+ const m = createMenus(ctx)
415
+ expect(() => m.clearColumnFilter('team')).not.toThrow()
416
+ })
417
+ })
418
+
419
+ describe('createMenus - pagination', () => {
420
+ it('changePage applies the delta', () => {
421
+ const ctx = makeCtx()
422
+ const m = createMenus(ctx)
423
+ m.changePage(1)
424
+ const updater = ctx.grid.setPagination.mock.calls[0][0]
425
+ expect(updater({ pageIndex: 2, pageSize: 10 })).toEqual({ pageIndex: 3, pageSize: 10 })
426
+ })
427
+
428
+ it('changePage clamps to zero on a negative delta', () => {
429
+ const ctx = makeCtx()
430
+ const m = createMenus(ctx)
431
+ m.changePage(-1)
432
+ const updater = ctx.grid.setPagination.mock.calls[0][0]
433
+ expect(updater({ pageIndex: 0, pageSize: 10 })).toEqual({ pageIndex: 0, pageSize: 10 })
434
+ expect(updater(undefined)).toEqual({ pageIndex: 0 }) // missing prev -> 0
435
+ })
436
+
437
+ it('goToPage clamps to zero', () => {
438
+ const ctx = makeCtx()
439
+ const m = createMenus(ctx)
440
+ m.goToPage(-5)
441
+ const updater = ctx.grid.setPagination.mock.calls[0][0]
442
+ expect(updater({ pageIndex: 9 })).toEqual({ pageIndex: 0 })
443
+ })
444
+
445
+ it('setPageSize keeps the first visible row in view', () => {
446
+ const ctx = makeCtx()
447
+ const m = createMenus(ctx)
448
+ m.setPageSize(20)
449
+ const updater = ctx.grid.setPagination.mock.calls[0][0]
450
+ // firstVisibleRow = 2*10 = 20; new index = floor(20/20) = 1
451
+ expect(updater({ pageIndex: 2, pageSize: 10 })).toEqual({ pageIndex: 1, pageSize: 20 })
452
+ })
453
+ })
454
+
455
+ describe('createMenus - context menu open/close', () => {
456
+ function fakeContextEvent() {
457
+ return { preventDefault: vi.fn(), clientX: 120, clientY: 200 } as any
458
+ }
459
+
460
+ it('openContextMenu is a no-op when contextMenu prop is unset', () => {
461
+ const ctx = makeCtx({ props: {} })
462
+ const m = createMenus(ctx)
463
+ const ev = fakeContextEvent()
464
+ m.openContextMenu(ev, 0, 0, 'name')
465
+ expect(ev.preventDefault).not.toHaveBeenCalled()
466
+ expect(ctx.contextMenuFor).toBeNull()
467
+ })
468
+
469
+ it('openContextMenu sets the target and position', () => {
470
+ const ctx = makeCtx({
471
+ props: { contextMenu: true },
472
+ allRows: [{ id: 'r0', original: { id: 0, name: 'x' } }],
473
+ })
474
+ const m = createMenus(ctx)
475
+ const ev = fakeContextEvent()
476
+ m.openContextMenu(ev, 0, 1, 'name')
477
+ expect(ev.preventDefault).toHaveBeenCalled()
478
+ expect(ctx.contextMenuFor).toEqual({
479
+ rowIndex: 0,
480
+ colIndex: 1,
481
+ columnId: 'name',
482
+ rowId: 'r0',
483
+ row: { id: 0, name: 'x' },
484
+ })
485
+ expect(ctx.contextMenuPos).toBeTruthy()
486
+ })
487
+
488
+ it('openContextMenu falls back to rowIndex when the row model is missing', () => {
489
+ const ctx = makeCtx({ props: { contextMenu: true }, allRows: [] })
490
+ const m = createMenus(ctx)
491
+ m.openContextMenu(fakeContextEvent(), 3, 0, 'name')
492
+ expect(ctx.contextMenuFor.rowId).toBe('3')
493
+ expect(ctx.contextMenuFor.row).toBeNull()
494
+ })
495
+
496
+ it('closeContextMenu clears the target', () => {
497
+ const ctx = makeCtx({ contextMenuFor: { rowIndex: 0 } })
498
+ const m = createMenus(ctx)
499
+ m.closeContextMenu()
500
+ expect(ctx.contextMenuFor).toBeNull()
501
+ })
502
+ })
503
+
504
+ describe('createMenus - contextMenuItems', () => {
505
+ it('returns [] with no target', () => {
506
+ const ctx = makeCtx({ contextMenuFor: null })
507
+ const m = createMenus(ctx)
508
+ expect(m.contextMenuItems()).toEqual([])
509
+ })
510
+
511
+ it('resolves the default built-in items and trims separators', () => {
512
+ const ctx = makeCtx({
513
+ props: { contextMenu: true },
514
+ contextMenuFor: { rowIndex: 0, colIndex: 0, columnId: 'name', rowId: 'r0', row: { id: 0 } },
515
+ })
516
+ const m = createMenus(ctx)
517
+ const items = m.contextMenuItems()
518
+ const labels = items.filter((i) => !i.separator).map((i) => i.label)
519
+ expect(labels).toContain('Copy')
520
+ expect(labels).toContain('Paste')
521
+ expect(labels).toContain('Remove row')
522
+ expect(labels).toContain('Remove column')
523
+ // no leading/trailing separator
524
+ expect(items[0].separator).toBeFalsy()
525
+ expect(items[items.length - 1].separator).toBeFalsy()
526
+ })
527
+
528
+ it('built-in run wiring invokes clipboard helpers', () => {
529
+ const ctx = makeCtx({
530
+ props: { contextMenu: ['copy', 'cut', 'paste', 'clear'] },
531
+ contextMenuFor: { rowIndex: 0, colIndex: 0, columnId: 'name', rowId: 'r0', row: { id: 0 } },
532
+ })
533
+ const m = createMenus(ctx)
534
+ const items = m.contextMenuItems()
535
+ items.find((i) => i.key === 'copy')!.run!()
536
+ items.find((i) => i.key === 'cut')!.run!()
537
+ items.find((i) => i.key === 'paste')!.run!()
538
+ items.find((i) => i.key === 'clear')!.run!()
539
+ expect(ctx.copySelectionToClipboard).toHaveBeenCalled()
540
+ expect(ctx.cutSelectionToClipboard).toHaveBeenCalled()
541
+ expect(ctx.pasteFromClipboard).toHaveBeenCalled()
542
+ expect(ctx.clearSelectedCells).toHaveBeenCalled()
543
+ })
544
+
545
+ it('row_above / row_below / remove_row mutate internalData', () => {
546
+ const rowA = { id: 1 }
547
+ const rowB = { id: 2 }
548
+ const ctx = makeCtx({
549
+ props: { contextMenu: ['row_above', 'row_below', 'remove_row'] },
550
+ internalData: [rowA, rowB],
551
+ contextMenuFor: { rowIndex: 1, colIndex: 0, columnId: 'name', rowId: 'r1', row: rowB },
552
+ })
553
+ const m = createMenus(ctx)
554
+ const items = m.contextMenuItems()
555
+ items.find((i) => i.key === 'row_above')!.run!()
556
+ expect(ctx.internalData.length).toBe(3)
557
+ expect(ctx.internalData.indexOf(rowB)).toBe(2) // inserted above rowB
558
+ // remove
559
+ const m2 = createMenus(makeCtx({
560
+ props: { contextMenu: ['remove_row'] },
561
+ internalData: [rowA, rowB],
562
+ contextMenuFor: { rowIndex: 1, colIndex: 0, columnId: 'name', rowId: 'r1', row: rowB },
563
+ }))
564
+ const ctx2 = (m2 as any)
565
+ const removeItem = m2.contextMenuItems().find((i) => i.key === 'remove_row')!
566
+ removeItem.run!()
567
+ })
568
+
569
+ it('remove_col removes the matching column def', () => {
570
+ const ctx = makeCtx({
571
+ props: { contextMenu: ['remove_col'] },
572
+ internalColumns: [{ id: 'name' }, { field: 'team' }],
573
+ contextMenuFor: { rowIndex: 0, colIndex: 1, columnId: 'team', rowId: 'r0', row: { id: 0 } },
574
+ })
575
+ const m = createMenus(ctx)
576
+ m.contextMenuItems().find((i) => i.key === 'remove_col')!.run!()
577
+ expect(ctx.internalColumns).toEqual([{ id: 'name' }])
578
+ })
579
+
580
+ it('custom items honor hidden() and disabled() predicates', () => {
581
+ const action = vi.fn()
582
+ const ctx = makeCtx({
583
+ props: {
584
+ contextMenu: [
585
+ { key: 'shown', label: 'Shown', action, disabled: () => true },
586
+ { key: 'gone', label: 'Gone', action, hidden: () => true },
587
+ ],
588
+ },
589
+ contextMenuFor: { rowIndex: 0, colIndex: 0, columnId: 'name', rowId: 'r0', row: { id: 0 } },
590
+ })
591
+ const m = createMenus(ctx)
592
+ const items = m.contextMenuItems()
593
+ expect(items.map((i) => i.key)).toEqual(['shown'])
594
+ expect(items[0].disabled).toBe(true)
595
+ items[0].run!()
596
+ expect(action).toHaveBeenCalled()
597
+ })
598
+
599
+ it('appends comment item when editableComments enabled with default config', () => {
600
+ const ctx = makeCtx({
601
+ props: { contextMenu: true, editableComments: true },
602
+ contextMenuFor: { rowIndex: 0, colIndex: 0, columnId: 'name', rowId: 'r0', row: { id: 0 } },
603
+ })
604
+ const m = createMenus(ctx)
605
+ const labels = m.contextMenuItems().map((i) => i.label)
606
+ expect(labels).toContain('Edit comment')
607
+ })
608
+ })
609
+
610
+ describe('createMenus - comments', () => {
611
+ it('comment item opens the editor seeded from existing note', () => {
612
+ const ctx = makeCtx({
613
+ props: { contextMenu: ['comment'], notes: { r0: { name: 'hello' } } },
614
+ contextMenuPos: { x: 5, y: 9 },
615
+ contextMenuFor: { rowIndex: 0, colIndex: 0, columnId: 'name', rowId: 'r0', row: { id: 0 } },
616
+ })
617
+ const m = createMenus(ctx)
618
+ m.contextMenuItems().find((i) => i.key === 'comment')!.run!()
619
+ expect(ctx.commentDraft).toBe('hello')
620
+ expect(ctx.commentEditFor).toEqual({ rowId: 'r0', columnId: 'name', x: 5, y: 9 })
621
+ expect(ctx.contextMenuFor).toBeNull()
622
+ })
623
+
624
+ it('saveComment writes the draft via onNoteChange and closes', () => {
625
+ const onNoteChange = vi.fn()
626
+ const ctx = makeCtx({
627
+ props: { onNoteChange },
628
+ commentEditFor: { rowId: 'r0', columnId: 'name', x: 0, y: 0 },
629
+ commentDraft: 'note text',
630
+ })
631
+ const m = createMenus(ctx)
632
+ m.saveComment()
633
+ expect(ctx.noteOverrides.r0.name).toBe('note text')
634
+ expect(onNoteChange).toHaveBeenCalledWith({ rowId: 'r0', columnId: 'name', note: 'note text' })
635
+ expect(ctx.commentEditFor).toBeNull()
636
+ })
637
+
638
+ it('saveComment is a no-op without an active editor', () => {
639
+ const ctx = makeCtx({ commentEditFor: null })
640
+ const m = createMenus(ctx)
641
+ m.saveComment()
642
+ expect(ctx.commentEditFor).toBeNull()
643
+ })
644
+
645
+ it('removeComment writes an empty note and closes', () => {
646
+ const ctx = makeCtx({
647
+ commentEditFor: { rowId: 'r0', columnId: 'name', x: 0, y: 0 },
648
+ noteOverrides: { r0: { name: 'old' } },
649
+ })
650
+ const m = createMenus(ctx)
651
+ m.removeComment()
652
+ expect(ctx.noteOverrides.r0.name).toBe('')
653
+ expect(ctx.commentEditFor).toBeNull()
654
+ })
655
+
656
+ it('removeComment is a no-op without an active editor', () => {
657
+ const ctx = makeCtx({ commentEditFor: null })
658
+ const m = createMenus(ctx)
659
+ expect(() => m.removeComment()).not.toThrow()
660
+ })
661
+
662
+ it('closeCommentEditor clears the editor', () => {
663
+ const ctx = makeCtx({ commentEditFor: { rowId: 'r0', columnId: 'name', x: 0, y: 0 } })
664
+ const m = createMenus(ctx)
665
+ m.closeCommentEditor()
666
+ expect(ctx.commentEditFor).toBeNull()
667
+ })
668
+ })
669
+
670
+ describe('createMenus - chart context-menu item', () => {
671
+ const target = { rowIndex: 0, colIndex: 0, columnId: 'a', rowId: '0', row: {} }
672
+
673
+ it('appends "Chart selected range" to the default menu when charting is enabled', () => {
674
+ const ctx = makeCtx({ props: { contextMenu: true }, chartingEnabled: true, contextMenuFor: target })
675
+ const items = createMenus(ctx).contextMenuItems()
676
+ expect(items.map((i) => i.label)).toContain('Chart selected range')
677
+ })
678
+
679
+ it('omits the chart item when charting is disabled', () => {
680
+ const ctx = makeCtx({ props: { contextMenu: true }, chartingEnabled: false, contextMenuFor: target })
681
+ const items = createMenus(ctx).contextMenuItems()
682
+ expect(items.map((i) => i.label)).not.toContain('Chart selected range')
683
+ })
684
+
685
+ it('clicking the chart item opens the chart panel', () => {
686
+ const ctx = makeCtx({ props: { contextMenu: true }, chartingEnabled: true, chartPanelOpen: false, contextMenuFor: target })
687
+ const item = createMenus(ctx).contextMenuItems().find((i) => i.label === 'Chart selected range')
688
+ item?.run?.()
689
+ expect(ctx.chartPanelOpen).toBe(true)
690
+ })
691
+
692
+ it('localizes the chart item label via ctx.messages', () => {
693
+ const ctx = makeCtx({
694
+ props: { contextMenu: true }, chartingEnabled: true, contextMenuFor: target,
695
+ messages: { chartRange: 'Graphique de la selection' },
696
+ })
697
+ const items = createMenus(ctx).contextMenuItems()
698
+ expect(items.map((i) => i.label)).toContain('Graphique de la selection')
699
+ })
700
+ })