@svgrid/grid 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/dist/GridMenus.svelte +205 -127
  2. package/dist/SvGrid.controller.svelte.d.ts +44 -1
  3. package/dist/SvGrid.controller.svelte.js +223 -38
  4. package/dist/SvGrid.css +2012 -1709
  5. package/dist/SvGrid.helpers.test.d.ts +1 -0
  6. package/dist/SvGrid.helpers.test.js +298 -0
  7. package/dist/SvGrid.svelte +2346 -2043
  8. package/dist/SvGrid.types.d.ts +91 -4
  9. package/dist/aligned-grids.d.ts +6 -0
  10. package/dist/aligned-grids.js +84 -0
  11. package/dist/aligned-grids.test.d.ts +1 -0
  12. package/dist/aligned-grids.test.js +75 -0
  13. package/dist/build-api.coverage.test.d.ts +20 -0
  14. package/dist/build-api.coverage.test.js +505 -0
  15. package/dist/build-api.js +59 -29
  16. package/dist/cell-render.test.d.ts +1 -0
  17. package/dist/cell-render.test.js +338 -0
  18. package/dist/chart-export.test.d.ts +1 -0
  19. package/dist/chart-export.test.js +302 -0
  20. package/dist/chart.coverage.test.d.ts +1 -0
  21. package/dist/chart.coverage.test.js +748 -0
  22. package/dist/clipboard.js +88 -24
  23. package/dist/clipboard.test.d.ts +1 -0
  24. package/dist/clipboard.test.js +700 -0
  25. package/dist/collaboration.coverage.test.d.ts +1 -0
  26. package/dist/collaboration.coverage.test.js +200 -0
  27. package/dist/column-groups.d.ts +19 -0
  28. package/dist/column-groups.js +62 -0
  29. package/dist/column-groups.test.d.ts +1 -0
  30. package/dist/column-groups.test.js +56 -0
  31. package/dist/column-types.d.ts +10 -0
  32. package/dist/column-types.js +63 -0
  33. package/dist/column-types.test.d.ts +1 -0
  34. package/dist/column-types.test.js +62 -0
  35. package/dist/columns.test.d.ts +1 -0
  36. package/dist/columns.test.js +625 -0
  37. package/dist/core.d.ts +85 -0
  38. package/dist/editing.d.ts +7 -0
  39. package/dist/editing.js +191 -5
  40. package/dist/editing.test.d.ts +1 -0
  41. package/dist/editing.test.js +732 -0
  42. package/dist/editors/cell-editors.coverage.test.d.ts +1 -0
  43. package/dist/editors/cell-editors.coverage.test.js +139 -0
  44. package/dist/facet-buckets.test.d.ts +1 -0
  45. package/dist/facet-buckets.test.js +296 -0
  46. package/dist/filter-operators.test.d.ts +1 -0
  47. package/dist/filter-operators.test.js +135 -0
  48. package/dist/hyperformula-adapter.test.d.ts +1 -0
  49. package/dist/hyperformula-adapter.test.js +205 -0
  50. package/dist/index.d.ts +5 -2
  51. package/dist/index.js +4 -1
  52. package/dist/keyboard-handlers.coverage.test.d.ts +1 -0
  53. package/dist/keyboard-handlers.coverage.test.js +495 -0
  54. package/dist/keyboard-handlers.js +7 -2
  55. package/dist/menus.js +1 -0
  56. package/dist/menus.test.d.ts +1 -0
  57. package/dist/menus.test.js +560 -0
  58. package/dist/named-views.coverage.test.d.ts +1 -0
  59. package/dist/named-views.coverage.test.js +180 -0
  60. package/dist/row-drag.d.ts +49 -0
  61. package/dist/row-drag.js +221 -0
  62. package/dist/row-drag.test.d.ts +1 -0
  63. package/dist/row-drag.test.js +142 -0
  64. package/dist/row-resize.test.d.ts +1 -0
  65. package/dist/row-resize.test.js +329 -0
  66. package/dist/scroll-sync.js +3 -0
  67. package/dist/scroll-sync.test.d.ts +1 -0
  68. package/dist/scroll-sync.test.js +290 -0
  69. package/dist/selection.d.ts +7 -1
  70. package/dist/selection.js +76 -36
  71. package/dist/selection.multi-range.test.d.ts +1 -0
  72. package/dist/selection.multi-range.test.js +55 -0
  73. package/dist/selection.test.d.ts +1 -0
  74. package/dist/selection.test.js +647 -0
  75. package/dist/server-data-source.coverage.test.d.ts +1 -0
  76. package/dist/server-data-source.coverage.test.js +154 -0
  77. package/dist/spreadsheet.d.ts +30 -0
  78. package/dist/spreadsheet.js +48 -0
  79. package/dist/spreadsheet.test.d.ts +1 -0
  80. package/dist/spreadsheet.test.js +446 -0
  81. package/dist/sv-grid-scrollbar.js +13 -1
  82. package/dist/svgrid-wrapper.types.d.ts +19 -0
  83. package/dist/svgrid.behavior.test.js +20 -0
  84. package/dist/svgrid.interaction.test.js +31 -0
  85. package/dist/svgrid.new-features.wrapper.test.js +34 -2
  86. package/dist/test-setup.js +9 -3
  87. package/dist/virtualization/scroll-scaling.d.ts +17 -0
  88. package/dist/virtualization/scroll-scaling.js +35 -0
  89. package/dist/virtualization/scroll-scaling.test.js +42 -1
  90. package/package.json +2 -1
  91. package/src/GridMenus.svelte +205 -127
  92. package/src/SvGrid.controller.svelte.ts +2352 -2195
  93. package/src/SvGrid.css +2012 -1747
  94. package/src/SvGrid.svelte +2346 -2047
  95. package/src/SvGrid.types.ts +537 -456
  96. package/src/aligned-grids.test.ts +80 -0
  97. package/src/aligned-grids.ts +87 -0
  98. package/src/build-api.ts +683 -663
  99. package/src/clipboard.test.ts +49 -0
  100. package/src/clipboard.ts +51 -23
  101. package/src/column-groups.test.ts +59 -0
  102. package/src/column-groups.ts +80 -0
  103. package/src/column-types.test.ts +68 -0
  104. package/src/column-types.ts +82 -0
  105. package/src/core.ts +78 -0
  106. package/src/editing.ts +669 -513
  107. package/src/index.ts +12 -0
  108. package/src/menus.ts +1 -0
  109. package/src/row-drag.test.ts +168 -0
  110. package/src/row-drag.ts +255 -0
  111. package/src/scroll-sync.ts +2 -0
  112. package/src/selection.multi-range.test.ts +61 -0
  113. package/src/selection.ts +71 -37
  114. package/src/spreadsheet.test.ts +489 -445
  115. package/src/spreadsheet.ts +304 -246
  116. package/src/svgrid-wrapper.types.ts +19 -0
  117. package/src/svgrid.new-features.wrapper.test.ts +2 -2
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,139 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { normalizeEditorOptions, parseEditorValue } from './cell-editors';
3
+ describe('normalizeEditorOptions', () => {
4
+ it('returns [] for undefined', () => {
5
+ expect(normalizeEditorOptions(undefined)).toEqual([]);
6
+ });
7
+ it('treats a function (mistakenly passed) as empty', () => {
8
+ // The type says functions are resolved earlier, but the guard handles it.
9
+ const fn = (() => []);
10
+ expect(normalizeEditorOptions(fn)).toEqual([]);
11
+ });
12
+ it('maps bare strings to { value, label }', () => {
13
+ expect(normalizeEditorOptions(['a', 'b'])).toEqual([
14
+ { value: 'a', label: 'a' },
15
+ { value: 'b', label: 'b' },
16
+ ]);
17
+ });
18
+ it('maps bare numbers to stringified labels', () => {
19
+ expect(normalizeEditorOptions([1, 2])).toEqual([
20
+ { value: 1, label: '1' },
21
+ { value: 2, label: '2' },
22
+ ]);
23
+ });
24
+ it('keeps an explicit label and color', () => {
25
+ expect(normalizeEditorOptions([{ value: 'x', label: 'Ex', color: '#f00' }])).toEqual([{ value: 'x', label: 'Ex', color: '#f00' }]);
26
+ });
27
+ it('defaults a missing label to String(value)', () => {
28
+ expect(normalizeEditorOptions([{ value: 7 }])).toEqual([
29
+ { value: 7, label: '7', color: undefined },
30
+ ]);
31
+ });
32
+ });
33
+ describe('parseEditorValue - rating', () => {
34
+ it('rounds and clamps into 0..5', () => {
35
+ expect(parseEditorValue('rating', 3.4)).toBe(3);
36
+ expect(parseEditorValue('rating', 4.6)).toBe(5);
37
+ expect(parseEditorValue('rating', 9)).toBe(5);
38
+ expect(parseEditorValue('rating', -2)).toBe(0);
39
+ });
40
+ it('returns 0 for non-finite input', () => {
41
+ expect(parseEditorValue('rating', 'nope')).toBe(0);
42
+ expect(parseEditorValue('rating', Infinity)).toBe(0);
43
+ });
44
+ });
45
+ describe('parseEditorValue - color', () => {
46
+ it('returns the trimmed color string', () => {
47
+ expect(parseEditorValue('color', ' #abcdef ')).toBe('#abcdef');
48
+ });
49
+ it('passes non-hex color strings through', () => {
50
+ expect(parseEditorValue('color', 'rebeccapurple')).toBe('rebeccapurple');
51
+ });
52
+ it('falls back to #000000 for empty/nullish', () => {
53
+ expect(parseEditorValue('color', '')).toBe('#000000');
54
+ expect(parseEditorValue('color', null)).toBe('#000000');
55
+ expect(parseEditorValue('color', undefined)).toBe('#000000');
56
+ });
57
+ });
58
+ describe('parseEditorValue - time', () => {
59
+ it('returns null for empty input', () => {
60
+ expect(parseEditorValue('time', '')).toBe(null);
61
+ expect(parseEditorValue('time', null)).toBe(null);
62
+ });
63
+ it('returns null for malformed strings', () => {
64
+ expect(parseEditorValue('time', 'noon')).toBe(null);
65
+ expect(parseEditorValue('time', '12')).toBe(null);
66
+ });
67
+ it('normalizes HH:MM', () => {
68
+ expect(parseEditorValue('time', '9:05')).toBe('09:05');
69
+ });
70
+ it('keeps seconds when present', () => {
71
+ expect(parseEditorValue('time', '23:59:59')).toBe('23:59:59');
72
+ });
73
+ it('clamps out-of-range components', () => {
74
+ expect(parseEditorValue('time', '99:99:99')).toBe('23:59:59');
75
+ });
76
+ });
77
+ describe('parseEditorValue - password', () => {
78
+ it('keeps an empty string instead of nulling it', () => {
79
+ expect(parseEditorValue('password', '')).toBe('');
80
+ });
81
+ it('stringifies and preserves content', () => {
82
+ expect(parseEditorValue('password', 'hunter2')).toBe('hunter2');
83
+ expect(parseEditorValue('password', null)).toBe('');
84
+ });
85
+ });
86
+ describe('parseEditorValue - select / rich-select', () => {
87
+ it('returns scalar value as-is', () => {
88
+ expect(parseEditorValue('select', 'one')).toBe('one');
89
+ expect(parseEditorValue('rich-select', 5)).toBe(5);
90
+ });
91
+ it('takes the first element of an array', () => {
92
+ expect(parseEditorValue('select', ['a', 'b'])).toBe('a');
93
+ });
94
+ it('coerces nullish to null', () => {
95
+ expect(parseEditorValue('select', null)).toBe(null);
96
+ expect(parseEditorValue('select', undefined)).toBe(null);
97
+ expect(parseEditorValue('rich-select', [])).toBe(null);
98
+ });
99
+ });
100
+ describe('parseEditorValue - textarea', () => {
101
+ it('stringifies, nullish to empty', () => {
102
+ expect(parseEditorValue('textarea', 'multi\nline')).toBe('multi\nline');
103
+ expect(parseEditorValue('textarea', null)).toBe('');
104
+ });
105
+ });
106
+ describe('parseEditorValue - list / chips (single)', () => {
107
+ it('returns scalar as-is', () => {
108
+ expect(parseEditorValue('list', 'x')).toBe('x');
109
+ expect(parseEditorValue('chips', 7)).toBe(7);
110
+ });
111
+ it('takes first element of array', () => {
112
+ expect(parseEditorValue('list', ['a', 'b'])).toBe('a');
113
+ expect(parseEditorValue('chips', [])).toBe(null);
114
+ });
115
+ it('coerces nullish to null', () => {
116
+ expect(parseEditorValue('list', null)).toBe(null);
117
+ });
118
+ });
119
+ describe('parseEditorValue - list / chips (multiple)', () => {
120
+ it('clones an array', () => {
121
+ const input = ['a', 'b'];
122
+ const out = parseEditorValue('list', input, { multiple: true });
123
+ expect(out).toEqual(['a', 'b']);
124
+ expect(out).not.toBe(input);
125
+ });
126
+ it('returns [] for empty/nullish', () => {
127
+ expect(parseEditorValue('chips', null, { multiple: true })).toEqual([]);
128
+ expect(parseEditorValue('chips', '', { multiple: true })).toEqual([]);
129
+ });
130
+ it('wraps a scalar into a single-element array', () => {
131
+ expect(parseEditorValue('list', 'solo', { multiple: true })).toEqual(['solo']);
132
+ });
133
+ });
134
+ describe('parseEditorValue - unknown type fallback', () => {
135
+ it('stringifies, nullish to empty', () => {
136
+ expect(parseEditorValue('autocomplete', 'hey')).toBe('hey');
137
+ expect(parseEditorValue('autocomplete', undefined)).toBe('');
138
+ });
139
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,296 @@
1
+ /**
2
+ * Unit tests for the facet value-bucketing helpers: isBucketableColumn,
3
+ * buildBuckets, isInBucket. These are pure functions - the only grid
4
+ * coupling is the Column type (we use createSvGrid to mint real columns
5
+ * carrying an `editorType`) and the rawToNumber/format helpers.
6
+ */
7
+ import { describe, expect, it } from 'vitest';
8
+ import { createCoreRowModel, createSvGrid, tableFeatures, rowSortingFeature, } from './index';
9
+ import { isBucketableColumn, buildBuckets, isInBucket, } from './facet-buckets';
10
+ const features = tableFeatures({ rowSortingFeature });
11
+ /** Build a real grid and return its columns keyed by id so tests can grab
12
+ * a genuine Column<Row> object (with columnDef.editorType populated). */
13
+ function makeColumns() {
14
+ const columns = [
15
+ { field: 'id', header: 'ID', editorType: 'text' },
16
+ { field: 'num', header: 'Number', editorType: 'number' },
17
+ { field: 'when', header: 'When', editorType: 'date' },
18
+ { field: 'name', header: 'Name' }, // no editorType
19
+ { field: 'flag', header: 'Flag', editorType: 'checkbox' },
20
+ ];
21
+ const grid = createSvGrid({
22
+ _features: features,
23
+ _rowModels: { coreRowModel: createCoreRowModel() },
24
+ columns,
25
+ data: [],
26
+ });
27
+ const byId = {};
28
+ for (const c of grid.getAllColumns())
29
+ byId[c.id] = c;
30
+ return byId;
31
+ }
32
+ // Field accessor used by buildBuckets in production - read the property
33
+ // matching the column id off the row.
34
+ function accessor(row, column) {
35
+ return row[column.id];
36
+ }
37
+ describe('isBucketableColumn', () => {
38
+ const cols = makeColumns();
39
+ it('returns { isDate: false } for a number column', () => {
40
+ expect(isBucketableColumn(cols.num)).toEqual({ isDate: false });
41
+ });
42
+ it('returns { isDate: true } for a date column', () => {
43
+ expect(isBucketableColumn(cols.when)).toEqual({ isDate: true });
44
+ });
45
+ it('returns { isDate: true } for a datetime column', () => {
46
+ const grid = createSvGrid({
47
+ _features: features,
48
+ _rowModels: { coreRowModel: createCoreRowModel() },
49
+ columns: [{ field: 'when', header: 'When', editorType: 'datetime' }],
50
+ data: [],
51
+ });
52
+ const col = grid.getAllColumns().find((c) => c.id === 'when');
53
+ expect(isBucketableColumn(col)).toEqual({ isDate: true });
54
+ });
55
+ it('returns null for a text column', () => {
56
+ expect(isBucketableColumn(cols.id)).toBeNull();
57
+ });
58
+ it('returns null for a checkbox column', () => {
59
+ expect(isBucketableColumn(cols.flag)).toBeNull();
60
+ });
61
+ it('returns null for a column with no editorType', () => {
62
+ expect(isBucketableColumn(cols.name)).toBeNull();
63
+ });
64
+ });
65
+ describe('buildBuckets - numeric', () => {
66
+ const cols = makeColumns();
67
+ it('returns null when distinct values do not exceed the threshold (30)', () => {
68
+ // 30 distinct values -> not > 30 -> null
69
+ const data = Array.from({ length: 30 }, (_, i) => ({
70
+ id: String(i),
71
+ num: i,
72
+ when: '',
73
+ name: '',
74
+ flag: false,
75
+ }));
76
+ expect(buildBuckets(cols.num, false, data, accessor)).toBeNull();
77
+ });
78
+ it('builds 10 buckets when distinct values exceed the threshold', () => {
79
+ // 31 distinct values 0..30 -> > 30 -> buckets
80
+ const data = Array.from({ length: 31 }, (_, i) => ({
81
+ id: String(i),
82
+ num: i,
83
+ when: '',
84
+ name: '',
85
+ flag: false,
86
+ }));
87
+ const buckets = buildBuckets(cols.num, false, data, accessor);
88
+ expect(buckets).not.toBeNull();
89
+ expect(buckets).toHaveLength(10);
90
+ });
91
+ it('spans the full numeric range with contiguous edges', () => {
92
+ const data = Array.from({ length: 31 }, (_, i) => ({
93
+ id: String(i),
94
+ num: i, // 0..30
95
+ when: '',
96
+ name: '',
97
+ flag: false,
98
+ }));
99
+ const buckets = buildBuckets(cols.num, false, data, accessor);
100
+ expect(buckets[0].numericMin).toBe(0);
101
+ expect(buckets[9].numericMax).toBe(30);
102
+ // bucket i's max equals bucket i+1's min (contiguous, no gaps)
103
+ for (let i = 0; i < buckets.length - 1; i += 1) {
104
+ expect(buckets[i].numericMax).toBeCloseTo(buckets[i + 1].numericMin, 10);
105
+ }
106
+ });
107
+ it('flags only the final bucket as isLast and sets isDate=false', () => {
108
+ const data = Array.from({ length: 31 }, (_, i) => ({
109
+ id: String(i),
110
+ num: i,
111
+ when: '',
112
+ name: '',
113
+ flag: false,
114
+ }));
115
+ const buckets = buildBuckets(cols.num, false, data, accessor);
116
+ expect(buckets.filter((b) => b.isLast)).toHaveLength(1);
117
+ expect(buckets[9].isLast).toBe(true);
118
+ expect(buckets[0].isLast).toBe(false);
119
+ expect(buckets.every((b) => b.isDate === false)).toBe(true);
120
+ });
121
+ it('uses numeric labels containing an en-dash separator', () => {
122
+ const data = Array.from({ length: 31 }, (_, i) => ({
123
+ id: String(i),
124
+ num: i,
125
+ when: '',
126
+ name: '',
127
+ flag: false,
128
+ }));
129
+ const buckets = buildBuckets(cols.num, false, data, accessor);
130
+ expect(buckets[0].label).toContain('–');
131
+ });
132
+ it('ignores non-finite / blank values when computing min/max and distinct', () => {
133
+ // 31 numeric values plus a pile of null/undefined/empty/NaN-ish ones
134
+ const data = [
135
+ ...Array.from({ length: 31 }, (_, i) => ({
136
+ id: String(i),
137
+ num: i,
138
+ when: '',
139
+ name: '',
140
+ flag: false,
141
+ })),
142
+ { id: 'a', num: null, when: '', name: '', flag: false },
143
+ { id: 'b', num: undefined, when: '', name: '', flag: false },
144
+ { id: 'c', num: 'not-a-number', when: '', name: '', flag: false },
145
+ ];
146
+ const buckets = buildBuckets(cols.num, false, data, accessor);
147
+ // range is still 0..30 - the junk rows were skipped
148
+ expect(buckets[0].numericMin).toBe(0);
149
+ expect(buckets[9].numericMax).toBe(30);
150
+ });
151
+ it('returns null when all finite values are identical (min === max)', () => {
152
+ // 40 rows but every num is 5 -> distinct.size === 1 -> <= threshold -> null.
153
+ // (Also exercises the min === max guard via a distinct-but-uniform case below.)
154
+ const data = Array.from({ length: 40 }, (_, i) => ({
155
+ id: String(i),
156
+ num: 5,
157
+ when: '',
158
+ name: '',
159
+ flag: false,
160
+ }));
161
+ expect(buildBuckets(cols.num, false, data, accessor)).toBeNull();
162
+ });
163
+ it('returns null when there is no finite data at all', () => {
164
+ const data = Array.from({ length: 40 }, (_, i) => ({
165
+ id: String(i),
166
+ num: null,
167
+ when: '',
168
+ name: '',
169
+ flag: false,
170
+ }));
171
+ expect(buildBuckets(cols.num, false, data, accessor)).toBeNull();
172
+ });
173
+ it('returns null for an empty dataset', () => {
174
+ expect(buildBuckets(cols.num, false, [], accessor)).toBeNull();
175
+ });
176
+ it('handles negative ranges', () => {
177
+ const data = Array.from({ length: 41 }, (_, i) => ({
178
+ id: String(i),
179
+ num: i - 20, // -20..20, 41 distinct
180
+ when: '',
181
+ name: '',
182
+ flag: false,
183
+ }));
184
+ const buckets = buildBuckets(cols.num, false, data, accessor);
185
+ expect(buckets[0].numericMin).toBe(-20);
186
+ expect(buckets[9].numericMax).toBe(20);
187
+ });
188
+ });
189
+ describe('buildBuckets - date', () => {
190
+ const cols = makeColumns();
191
+ it('builds buckets from date strings with isDate=true and date labels', () => {
192
+ const start = Date.UTC(2020, 0, 1);
193
+ const day = 24 * 60 * 60 * 1000;
194
+ const data = Array.from({ length: 40 }, (_, i) => ({
195
+ id: String(i),
196
+ num: 0,
197
+ when: new Date(start + i * day).toISOString(),
198
+ name: '',
199
+ flag: false,
200
+ }));
201
+ const buckets = buildBuckets(cols.when, true, data, accessor);
202
+ expect(buckets).toHaveLength(10);
203
+ expect(buckets.every((b) => b.isDate === true)).toBe(true);
204
+ // labels should look like dates (contain a 4-digit year)
205
+ expect(buckets[0].label).toMatch(/\d{4}/);
206
+ });
207
+ it('accepts Date instances directly', () => {
208
+ const start = Date.UTC(2020, 0, 1);
209
+ const day = 24 * 60 * 60 * 1000;
210
+ const data = Array.from({ length: 40 }, (_, i) => ({ id: 'when' + i, when: new Date(start + i * day) }));
211
+ // accessor reads row[column.id]; column id is 'when'
212
+ const rows = data.map((d) => ({ when: d.when }));
213
+ const buckets = buildBuckets(cols.when, true, rows, accessor);
214
+ expect(buckets).toHaveLength(10);
215
+ expect(buckets[0].numericMin).toBe(start);
216
+ });
217
+ it('returns null when date values are too few', () => {
218
+ const data = Array.from({ length: 5 }, (_, i) => ({
219
+ id: String(i),
220
+ num: 0,
221
+ when: new Date(Date.UTC(2020, 0, 1 + i)).toISOString(),
222
+ name: '',
223
+ flag: false,
224
+ }));
225
+ expect(buildBuckets(cols.when, true, data, accessor)).toBeNull();
226
+ });
227
+ it('skips invalid date strings', () => {
228
+ const start = Date.UTC(2021, 0, 1);
229
+ const day = 24 * 60 * 60 * 1000;
230
+ const data = [
231
+ ...Array.from({ length: 40 }, (_, i) => ({
232
+ id: String(i),
233
+ num: 0,
234
+ when: new Date(start + i * day).toISOString(),
235
+ name: '',
236
+ flag: false,
237
+ })),
238
+ { id: 'bad', num: 0, when: 'not-a-date', name: '', flag: false },
239
+ ];
240
+ const buckets = buildBuckets(cols.when, true, data, accessor);
241
+ expect(buckets).toHaveLength(10);
242
+ expect(buckets[0].numericMin).toBe(start);
243
+ });
244
+ });
245
+ describe('isInBucket', () => {
246
+ const mid = {
247
+ label: '10 - 20',
248
+ numericMin: 10,
249
+ numericMax: 20,
250
+ isLast: false,
251
+ isDate: false,
252
+ };
253
+ const last = {
254
+ label: '20 - 30',
255
+ numericMin: 20,
256
+ numericMax: 30,
257
+ isLast: true,
258
+ isDate: false,
259
+ };
260
+ it('returns false for non-finite numbers', () => {
261
+ expect(isInBucket(Number.NaN, mid)).toBe(false);
262
+ expect(isInBucket(Number.POSITIVE_INFINITY, mid)).toBe(false);
263
+ });
264
+ it('returns false below the lower bound', () => {
265
+ expect(isInBucket(9.999, mid)).toBe(false);
266
+ });
267
+ it('includes the lower bound', () => {
268
+ expect(isInBucket(10, mid)).toBe(true);
269
+ });
270
+ it('excludes the upper bound for a non-last bucket (half-open interval)', () => {
271
+ expect(isInBucket(20, mid)).toBe(false);
272
+ expect(isInBucket(19.999, mid)).toBe(true);
273
+ });
274
+ it('includes the upper bound for the last bucket (closed interval)', () => {
275
+ expect(isInBucket(30, last)).toBe(true);
276
+ expect(isInBucket(20, last)).toBe(true);
277
+ });
278
+ it('rejects values past the upper bound even on the last bucket', () => {
279
+ expect(isInBucket(30.0001, last)).toBe(false);
280
+ });
281
+ it('partitions a value into exactly one bucket across a built set', () => {
282
+ const cols = makeColumns();
283
+ const data = Array.from({ length: 101 }, (_, i) => ({
284
+ id: String(i),
285
+ num: i, // 0..100
286
+ when: '',
287
+ name: '',
288
+ flag: false,
289
+ }));
290
+ const buckets = buildBuckets(cols.num, false, data, accessor);
291
+ for (const v of [0, 1, 15, 50, 99, 100]) {
292
+ const hits = buckets.filter((b) => isInBucket(v, b));
293
+ expect(hits).toHaveLength(1);
294
+ }
295
+ });
296
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,135 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { filterOperatorOptions, fallbackOperatorOption, TEXT_OPERATORS, NUMBER_OPERATORS, DATE_OPERATORS, CHECKBOX_OPERATORS, operatorOption, operatorsForColumn, defaultOperatorFor, operatorLabelFor, } from "./filter-operators";
3
+ // `operatorsForColumn` / `defaultOperatorFor` / `operatorLabelFor` only ever
4
+ // read `column?.columnDef.editorType`, so a minimal duck-typed stub is enough
5
+ // to exercise every branch without standing up a real grid/column instance.
6
+ function col(editorType) {
7
+ return editorType === undefined
8
+ ? { columnDef: {} }
9
+ : { columnDef: { editorType } };
10
+ }
11
+ describe("filter-operators: catalogue integrity", () => {
12
+ it("every option exposes value/label/iconName and a unique value", () => {
13
+ const values = filterOperatorOptions.map((o) => o.value);
14
+ expect(new Set(values).size).toBe(values.length);
15
+ for (const o of filterOperatorOptions) {
16
+ expect(typeof o.value).toBe("string");
17
+ expect(typeof o.label).toBe("string");
18
+ expect(o.iconName).toMatch(/^op-/);
19
+ }
20
+ });
21
+ it("fallback option is the 'contains' operator", () => {
22
+ expect(fallbackOperatorOption.value).toBe("contains");
23
+ expect(fallbackOperatorOption.label).toBe("Contains");
24
+ });
25
+ it("each type's operator list only references catalogued operators", () => {
26
+ const known = new Set(filterOperatorOptions.map((o) => o.value));
27
+ for (const list of [
28
+ TEXT_OPERATORS,
29
+ NUMBER_OPERATORS,
30
+ DATE_OPERATORS,
31
+ CHECKBOX_OPERATORS,
32
+ ]) {
33
+ for (const id of list)
34
+ expect(known.has(id)).toBe(true);
35
+ }
36
+ });
37
+ it("the catalogue includes between / isBlank for numeric + date filtering", () => {
38
+ const values = filterOperatorOptions.map((o) => o.value);
39
+ expect(values).toContain("between");
40
+ expect(values).toContain("isBlank");
41
+ });
42
+ });
43
+ describe("operatorOption", () => {
44
+ it("returns the matching option for every catalogued operator", () => {
45
+ for (const o of filterOperatorOptions) {
46
+ expect(operatorOption(o.value)).toBe(o);
47
+ }
48
+ });
49
+ it("looks up a specific operator by value", () => {
50
+ expect(operatorOption("between")).toMatchObject({
51
+ value: "between",
52
+ label: "Between",
53
+ });
54
+ expect(operatorOption("greaterThan").label).toBe("Greater than");
55
+ expect(operatorOption("lessThan").label).toBe("Less than");
56
+ expect(operatorOption("startsWith").label).toBe("Starts with");
57
+ });
58
+ it("falls back to 'contains' for an unknown operator value", () => {
59
+ const result = operatorOption("nope");
60
+ expect(result).toBe(fallbackOperatorOption);
61
+ expect(result.value).toBe("contains");
62
+ });
63
+ });
64
+ describe("operatorsForColumn", () => {
65
+ const labels = (opts) => opts.map((o) => o.value);
66
+ it("number columns get equals/greaterThan/lessThan/between/isBlank", () => {
67
+ expect(labels(operatorsForColumn(col("number")))).toEqual(NUMBER_OPERATORS);
68
+ });
69
+ it("date columns get the date operator set", () => {
70
+ expect(labels(operatorsForColumn(col("date")))).toEqual(DATE_OPERATORS);
71
+ });
72
+ it("datetime columns share the date operator set", () => {
73
+ expect(labels(operatorsForColumn(col("datetime")))).toEqual(DATE_OPERATORS);
74
+ });
75
+ it("checkbox columns get only equals + isBlank", () => {
76
+ expect(labels(operatorsForColumn(col("checkbox")))).toEqual(CHECKBOX_OPERATORS);
77
+ });
78
+ it("text columns get the text operator set", () => {
79
+ expect(labels(operatorsForColumn(col("text")))).toEqual(TEXT_OPERATORS);
80
+ });
81
+ it("an unknown editor type defaults to the text operator set", () => {
82
+ expect(labels(operatorsForColumn(col("currency")))).toEqual(TEXT_OPERATORS);
83
+ });
84
+ it("a column with no editorType defaults to text", () => {
85
+ expect(labels(operatorsForColumn(col()))).toEqual(TEXT_OPERATORS);
86
+ });
87
+ it("an undefined column defaults to the text operator set", () => {
88
+ expect(labels(operatorsForColumn(undefined))).toEqual(TEXT_OPERATORS);
89
+ });
90
+ it("returns full FilterOption objects (value+label+iconName), in order", () => {
91
+ const opts = operatorsForColumn(col("number"));
92
+ expect(opts[0]).toMatchObject({ value: "equals", iconName: "op-equals" });
93
+ for (const o of opts) {
94
+ expect(o).toHaveProperty("label");
95
+ expect(o).toHaveProperty("iconName");
96
+ }
97
+ });
98
+ });
99
+ describe("defaultOperatorFor", () => {
100
+ it("picks the first operator valid for the column type", () => {
101
+ expect(defaultOperatorFor(col("number"))).toBe("equals");
102
+ expect(defaultOperatorFor(col("date"))).toBe("equals");
103
+ expect(defaultOperatorFor(col("checkbox"))).toBe("equals");
104
+ expect(defaultOperatorFor(col("text"))).toBe("contains");
105
+ });
106
+ it("defaults to 'contains' for text / unknown / undefined columns", () => {
107
+ expect(defaultOperatorFor(col())).toBe("contains");
108
+ expect(defaultOperatorFor(undefined)).toBe("contains");
109
+ expect(defaultOperatorFor(col("mystery"))).toBe("contains");
110
+ });
111
+ });
112
+ describe("operatorLabelFor", () => {
113
+ const less = operatorOption("lessThan");
114
+ const greater = operatorOption("greaterThan");
115
+ const equals = operatorOption("equals");
116
+ it("renames lessThan/greaterThan to Before/After for date columns", () => {
117
+ expect(operatorLabelFor(less, col("date"))).toBe("Before");
118
+ expect(operatorLabelFor(greater, col("date"))).toBe("After");
119
+ });
120
+ it("applies the date relabelling for datetime columns too", () => {
121
+ expect(operatorLabelFor(less, col("datetime"))).toBe("Before");
122
+ expect(operatorLabelFor(greater, col("datetime"))).toBe("After");
123
+ });
124
+ it("leaves non-relabelled operators untouched on date columns", () => {
125
+ expect(operatorLabelFor(equals, col("date"))).toBe(equals.label);
126
+ });
127
+ it("keeps the catalogue label for non-date columns", () => {
128
+ expect(operatorLabelFor(less, col("number"))).toBe("Less than");
129
+ expect(operatorLabelFor(greater, col("number"))).toBe("Greater than");
130
+ expect(operatorLabelFor(less, col("text"))).toBe("Less than");
131
+ });
132
+ it("keeps the catalogue label when the column is undefined", () => {
133
+ expect(operatorLabelFor(greater, undefined)).toBe("Greater than");
134
+ });
135
+ });
@@ -0,0 +1 @@
1
+ export {};