@svgrid/grid 2.2.27 → 2.2.29

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