@svgrid/grid 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +137 -39
- package/dist/GridFooter.svelte +164 -0
- package/dist/GridFooter.svelte.d.ts +29 -0
- package/dist/GridMenus.svelte +570 -0
- package/dist/GridMenus.svelte.d.ts +31 -0
- package/dist/SvGrid.controller.svelte.d.ts +429 -0
- package/dist/SvGrid.controller.svelte.js +1732 -0
- package/dist/SvGrid.css +1709 -0
- package/dist/SvGrid.helpers.d.ts +35 -0
- package/dist/SvGrid.helpers.js +160 -0
- package/dist/SvGrid.svelte +344 -7043
- package/dist/SvGrid.svelte.d.ts +4 -357
- package/dist/SvGrid.types.d.ts +436 -0
- package/dist/SvGrid.types.js +1 -0
- package/dist/SvGridChart.svelte +1060 -23
- package/dist/SvGridChart.svelte.d.ts +17 -0
- package/dist/build-api.d.ts +5 -0
- package/dist/build-api.js +527 -0
- package/dist/cell-render.d.ts +15 -0
- package/dist/cell-render.js +246 -0
- package/dist/cell-values.d.ts +28 -0
- package/dist/cell-values.js +89 -0
- package/dist/chart.d.ts +370 -3
- package/dist/chart.js +1135 -42
- package/dist/clipboard.d.ts +15 -0
- package/dist/clipboard.js +356 -0
- package/dist/columns.d.ts +30 -0
- package/dist/columns.js +277 -0
- package/dist/core.d.ts +1 -1
- package/dist/css.d.ts +3 -0
- package/dist/editing.d.ts +24 -0
- package/dist/editing.js +343 -0
- package/dist/editors/cell-editors.d.ts +1 -1
- package/dist/facet-buckets.d.ts +13 -0
- package/dist/facet-buckets.js +54 -0
- package/dist/features.d.ts +5 -0
- package/dist/features.js +30 -0
- package/dist/filter-operators.d.ts +16 -0
- package/dist/filter-operators.js +69 -0
- package/dist/hyperformula-adapter.d.ts +82 -0
- package/dist/hyperformula-adapter.js +73 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.js +5 -2
- package/dist/keyboard-handlers.d.ts +7 -0
- package/dist/keyboard-handlers.js +197 -0
- package/dist/menus.d.ts +40 -0
- package/dist/menus.js +389 -0
- package/dist/named-views.d.ts +27 -0
- package/dist/named-views.js +39 -0
- package/dist/row-resize.d.ts +43 -0
- package/dist/row-resize.js +158 -0
- package/dist/scroll-sync.d.ts +9 -0
- package/dist/scroll-sync.js +86 -0
- package/dist/selection.d.ts +26 -0
- package/dist/selection.js +387 -0
- package/dist/spreadsheet.d.ts +80 -0
- package/dist/spreadsheet.js +194 -0
- package/dist/summaries.d.ts +12 -0
- package/dist/summaries.js +65 -0
- package/dist/svgrid-wrapper.types.d.ts +12 -1
- package/dist/svgrid.comments-autocomplete.test.d.ts +1 -0
- package/dist/svgrid.comments-autocomplete.test.js +96 -0
- package/dist/svgrid.context-menu.test.d.ts +1 -0
- package/dist/svgrid.context-menu.test.js +102 -0
- package/dist/svgrid.new-features.wrapper.test.js +30 -4
- package/dist/svgrid.wrapper.test.js +27 -1
- package/dist/virtualization/column-virtualizer.d.ts +2 -0
- package/dist/virtualization/scroll-scaling.d.ts +28 -0
- package/dist/virtualization/scroll-scaling.js +64 -0
- package/dist/virtualization/scroll-scaling.test.d.ts +1 -0
- package/dist/virtualization/scroll-scaling.test.js +86 -0
- package/dist/virtualization/svelte-virtualizer.svelte.d.ts +2 -0
- package/dist/virtualization/svelte-virtualizer.svelte.js +2 -0
- package/dist/virtualization/virtualizer.d.ts +7 -0
- package/dist/virtualization/virtualizer.js +30 -0
- package/package.json +1 -1
- package/src/GridFooter.svelte +164 -0
- package/src/GridMenus.svelte +570 -0
- package/src/SvGrid.controller.svelte.ts +2195 -0
- package/src/SvGrid.css +1747 -0
- package/src/SvGrid.helpers.test.ts +415 -0
- package/src/SvGrid.helpers.ts +185 -0
- package/src/SvGrid.svelte +348 -7043
- package/src/SvGrid.types.ts +456 -0
- package/src/SvGridChart.svelte +1060 -23
- package/src/build-api.coverage.test.ts +532 -0
- package/src/build-api.ts +663 -0
- package/src/cell-render.test.ts +451 -0
- package/src/cell-render.ts +426 -0
- package/src/cell-values.ts +114 -0
- package/src/chart-export.test.ts +370 -0
- package/src/chart.coverage.test.ts +814 -0
- package/src/chart.ts +1352 -47
- package/src/clipboard.test.ts +731 -0
- package/src/clipboard.ts +524 -0
- package/src/collaboration.coverage.test.ts +220 -0
- package/src/columns.test.ts +702 -0
- package/src/columns.ts +419 -0
- package/src/core.ts +8 -0
- package/src/css.d.ts +3 -0
- package/src/editing.test.ts +837 -0
- package/src/editing.ts +513 -0
- package/src/editors/cell-editors.coverage.test.ts +156 -0
- package/src/editors/cell-editors.ts +1 -0
- package/src/facet-buckets.test.ts +353 -0
- package/src/facet-buckets.ts +67 -0
- package/src/features.ts +128 -0
- package/src/filter-operators.test.ts +174 -0
- package/src/filter-operators.ts +87 -0
- package/src/hyperformula-adapter.test.ts +256 -0
- package/src/hyperformula-adapter.ts +124 -0
- package/src/index.ts +37 -0
- package/src/keyboard-handlers.coverage.test.ts +560 -0
- package/src/keyboard-handlers.ts +353 -0
- package/src/keyboard.ts +97 -97
- package/src/menus.test.ts +620 -0
- package/src/menus.ts +554 -0
- package/src/named-views.coverage.test.ts +210 -0
- package/src/named-views.ts +48 -0
- package/src/row-resize.test.ts +369 -0
- package/src/row-resize.ts +171 -0
- package/src/scroll-sync.test.ts +330 -0
- package/src/scroll-sync.ts +216 -0
- package/src/selection.test.ts +722 -0
- package/src/selection.ts +545 -0
- package/src/server-data-source.coverage.test.ts +180 -0
- package/src/spreadsheet.test.ts +445 -0
- package/src/spreadsheet.ts +246 -0
- package/src/summaries.ts +204 -0
- package/src/sv-grid-scrollbar.ts +13 -1
- package/src/svgrid-wrapper.types.ts +12 -1
- package/src/svgrid.behavior.test.ts +22 -0
- package/src/svgrid.comments-autocomplete.test.ts +112 -0
- package/src/svgrid.context-menu.test.ts +126 -0
- package/src/svgrid.interaction.test.ts +30 -0
- package/src/svgrid.new-features.wrapper.test.ts +65 -4
- package/src/svgrid.wrapper.test.ts +27 -1
- package/src/test-setup.ts +9 -6
- package/src/virtualization/scroll-scaling.test.ts +148 -0
- package/src/virtualization/scroll-scaling.ts +121 -0
- package/src/virtualization/svelte-virtualizer.svelte.ts +2 -0
- package/src/virtualization/virtualizer.ts +26 -0
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { createCellRender } from './cell-render'
|
|
3
|
+
|
|
4
|
+
// --- minimal fakes -------------------------------------------------------
|
|
5
|
+
//
|
|
6
|
+
// createCellRender only reaches into a small slice of the controller: it
|
|
7
|
+
// reads `ctx.props`, `ctx.grid`, `ctx.conditionalColumnStats`,
|
|
8
|
+
// `ctx.noteOverrides` and `ctx.editorOptionsCache`. We build a row/column
|
|
9
|
+
// shape that mirrors the @svgrid/grid runtime closely enough to exercise
|
|
10
|
+
// every branch.
|
|
11
|
+
|
|
12
|
+
type AnyRow = {
|
|
13
|
+
id: string
|
|
14
|
+
original: Record<string, unknown>
|
|
15
|
+
getCellValueByColumnId: (colId: string) => unknown
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function makeRow(id: string, data: Record<string, unknown>): AnyRow {
|
|
19
|
+
return {
|
|
20
|
+
id,
|
|
21
|
+
original: data,
|
|
22
|
+
getCellValueByColumnId: (colId: string) => data[colId],
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function makeColumn(id: string, def: Record<string, unknown> = {}): any {
|
|
27
|
+
return { id, columnDef: { ...def } }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function makeCtx(overrides: Partial<any> = {}): any {
|
|
31
|
+
return {
|
|
32
|
+
props: {},
|
|
33
|
+
grid: { id: 'grid' },
|
|
34
|
+
conditionalColumnStats: new Map(),
|
|
35
|
+
noteOverrides: {},
|
|
36
|
+
editorOptionsCache: {},
|
|
37
|
+
...overrides,
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
describe('createCellRender / cellConditionalFormat', () => {
|
|
42
|
+
it('returns null when no conditionalFormats are configured', () => {
|
|
43
|
+
const cr = createCellRender(makeCtx())
|
|
44
|
+
const out = cr.cellConditionalFormat(makeRow('r1', { amt: 5 }), makeColumn('amt'), 5)
|
|
45
|
+
expect(out).toBeNull()
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('returns null when conditionalFormats is an empty array', () => {
|
|
49
|
+
const cr = createCellRender(makeCtx({ props: { conditionalFormats: [] } }))
|
|
50
|
+
expect(cr.cellConditionalFormat(makeRow('r', {}), makeColumn('amt'), 1)).toBeNull()
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('resolves a rule format against the value', () => {
|
|
54
|
+
const formats = [
|
|
55
|
+
{
|
|
56
|
+
type: 'rule',
|
|
57
|
+
columns: ['amt'],
|
|
58
|
+
when: ({ value }: { value: unknown }) => Number(value) > 10,
|
|
59
|
+
background: '#ff0000',
|
|
60
|
+
fontWeight: 700,
|
|
61
|
+
},
|
|
62
|
+
]
|
|
63
|
+
const cr = createCellRender(makeCtx({ props: { conditionalFormats: formats } }))
|
|
64
|
+
const hit = cr.cellConditionalFormat(makeRow('r', { amt: 20 }), makeColumn('amt'), 20)
|
|
65
|
+
expect(hit?.background).toBe('#ff0000')
|
|
66
|
+
expect(hit?.fontWeight).toBe(700)
|
|
67
|
+
|
|
68
|
+
const miss = cr.cellConditionalFormat(makeRow('r', { amt: 1 }), makeColumn('amt'), 1)
|
|
69
|
+
expect(miss).toEqual({})
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('passes the precomputed column stat through to colorScale formats', () => {
|
|
73
|
+
const formats = [{ type: 'colorScale', min: '#fff', max: '#000' } as any]
|
|
74
|
+
const stats = new Map([['amt', { min: 0, max: 100 }]])
|
|
75
|
+
const cr = createCellRender(
|
|
76
|
+
makeCtx({ props: { conditionalFormats: formats }, conditionalColumnStats: stats }),
|
|
77
|
+
)
|
|
78
|
+
const out = cr.cellConditionalFormat(makeRow('r', { amt: 50 }), makeColumn('amt'), 50)
|
|
79
|
+
expect(out).not.toBeNull()
|
|
80
|
+
expect(out?.background).toBeDefined()
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
describe('createCellRender / computeRowClass', () => {
|
|
85
|
+
it('returns empty string when no rowClass configured', () => {
|
|
86
|
+
const cr = createCellRender(makeCtx())
|
|
87
|
+
expect(cr.computeRowClass(makeRow('r', {}), 0)).toBe('')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('resolves a string rowClass', () => {
|
|
91
|
+
const rowClass = vi.fn(() => 'highlight')
|
|
92
|
+
const cr = createCellRender(makeCtx({ props: { rowClass } }))
|
|
93
|
+
const row = makeRow('r', { a: 1 })
|
|
94
|
+
expect(cr.computeRowClass(row, 3)).toBe('highlight')
|
|
95
|
+
expect(rowClass).toHaveBeenCalledWith({ row: row.original, rowIndex: 3 })
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('resolves an array / object rowClass', () => {
|
|
99
|
+
const cr = createCellRender(
|
|
100
|
+
makeCtx({ props: { rowClass: () => ['a', '', 'b'] } }),
|
|
101
|
+
)
|
|
102
|
+
expect(cr.computeRowClass(makeRow('r', {}), 0)).toBe('a b')
|
|
103
|
+
|
|
104
|
+
const cr2 = createCellRender(
|
|
105
|
+
makeCtx({ props: { rowClass: () => ({ on: true, off: false }) } }),
|
|
106
|
+
)
|
|
107
|
+
expect(cr2.computeRowClass(makeRow('r', {}), 0)).toBe('on')
|
|
108
|
+
})
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
describe('createCellRender / computeCellClass', () => {
|
|
112
|
+
it('returns empty string when cellClass is null/undefined', () => {
|
|
113
|
+
const cr = createCellRender(makeCtx())
|
|
114
|
+
expect(cr.computeCellClass(makeRow('r', {}), makeColumn('a'))).toBe('')
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('resolves a string cellClass', () => {
|
|
118
|
+
const cr = createCellRender(makeCtx())
|
|
119
|
+
expect(
|
|
120
|
+
cr.computeCellClass(makeRow('r', {}), makeColumn('a', { cellClass: 'red' })),
|
|
121
|
+
).toBe('red')
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('resolves an array cellClass', () => {
|
|
125
|
+
const cr = createCellRender(makeCtx())
|
|
126
|
+
expect(
|
|
127
|
+
cr.computeCellClass(makeRow('r', {}), makeColumn('a', { cellClass: ['x', 'y'] })),
|
|
128
|
+
).toBe('x y')
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('invokes a function cellClass with a minimal cell context', () => {
|
|
132
|
+
const cellClass = vi.fn((c: any) => (c.getValue() > 5 ? 'big' : 'small'))
|
|
133
|
+
const cr = createCellRender(makeCtx())
|
|
134
|
+
const row = makeRow('r', { score: 9 })
|
|
135
|
+
expect(cr.computeCellClass(row, makeColumn('score', { cellClass }))).toBe('big')
|
|
136
|
+
expect(cellClass).toHaveBeenCalledTimes(1)
|
|
137
|
+
const passed = cellClass.mock.calls[0][0]
|
|
138
|
+
expect(passed.row).toBe(row)
|
|
139
|
+
expect(passed.getValue()).toBe(9)
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
it('returns empty string for an unsupported cellClass type (number)', () => {
|
|
143
|
+
const cr = createCellRender(makeCtx())
|
|
144
|
+
expect(
|
|
145
|
+
cr.computeCellClass(makeRow('r', {}), makeColumn('a', { cellClass: 123 as any })),
|
|
146
|
+
).toBe('')
|
|
147
|
+
})
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
describe('createCellRender / computeCellTooltip', () => {
|
|
151
|
+
it('returns null when no tooltip configured', () => {
|
|
152
|
+
const cr = createCellRender(makeCtx())
|
|
153
|
+
expect(cr.computeCellTooltip(makeRow('r', {}), makeColumn('a'))).toBeNull()
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
it('returns a static string tooltip, but null for empty string', () => {
|
|
157
|
+
const cr = createCellRender(makeCtx())
|
|
158
|
+
expect(
|
|
159
|
+
cr.computeCellTooltip(makeRow('r', {}), makeColumn('a', { tooltip: 'help' })),
|
|
160
|
+
).toBe('help')
|
|
161
|
+
expect(
|
|
162
|
+
cr.computeCellTooltip(makeRow('r', {}), makeColumn('a', { tooltip: '' })),
|
|
163
|
+
).toBeNull()
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('invokes a function tooltip and stringifies the result', () => {
|
|
167
|
+
const tooltip = (c: any) => `val=${c.getValue()}`
|
|
168
|
+
const cr = createCellRender(makeCtx())
|
|
169
|
+
const row = makeRow('r', { a: 42 })
|
|
170
|
+
expect(cr.computeCellTooltip(row, makeColumn('a', { tooltip }))).toBe('val=42')
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
it('returns null when the function tooltip returns a falsy value', () => {
|
|
174
|
+
const cr = createCellRender(makeCtx())
|
|
175
|
+
expect(
|
|
176
|
+
cr.computeCellTooltip(makeRow('r', {}), makeColumn('a', { tooltip: () => '' })),
|
|
177
|
+
).toBeNull()
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
it('returns null for an unsupported tooltip type', () => {
|
|
181
|
+
const cr = createCellRender(makeCtx())
|
|
182
|
+
expect(
|
|
183
|
+
cr.computeCellTooltip(makeRow('r', {}), makeColumn('a', { tooltip: 5 as any })),
|
|
184
|
+
).toBeNull()
|
|
185
|
+
})
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
describe('createCellRender / computeCellNote', () => {
|
|
189
|
+
it('returns null when neither overrides nor notes have an entry', () => {
|
|
190
|
+
const cr = createCellRender(makeCtx())
|
|
191
|
+
expect(cr.computeCellNote(makeRow('r', {}), makeColumn('a'))).toBeNull()
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
it('prefers the internal note override over props.notes', () => {
|
|
195
|
+
const cr = createCellRender(
|
|
196
|
+
makeCtx({
|
|
197
|
+
noteOverrides: { r: { a: 'overlay' } },
|
|
198
|
+
props: { notes: { r: { a: 'stored' } } },
|
|
199
|
+
}),
|
|
200
|
+
)
|
|
201
|
+
expect(cr.computeCellNote(makeRow('r', {}), makeColumn('a'))).toBe('overlay')
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
it('treats a blank override as a removal (null)', () => {
|
|
205
|
+
const cr = createCellRender(
|
|
206
|
+
makeCtx({
|
|
207
|
+
noteOverrides: { r: { a: ' ' } },
|
|
208
|
+
props: { notes: { r: { a: 'stored' } } },
|
|
209
|
+
}),
|
|
210
|
+
)
|
|
211
|
+
expect(cr.computeCellNote(makeRow('r', {}), makeColumn('a'))).toBeNull()
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
it('falls back to props.notes when no override exists', () => {
|
|
215
|
+
const cr = createCellRender(
|
|
216
|
+
makeCtx({ props: { notes: { r: { a: 'stored note' } } } }),
|
|
217
|
+
)
|
|
218
|
+
expect(cr.computeCellNote(makeRow('r', {}), makeColumn('a'))).toBe('stored note')
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
it('returns null for a whitespace-only stored note', () => {
|
|
222
|
+
const cr = createCellRender(makeCtx({ props: { notes: { r: { a: ' ' } } } }))
|
|
223
|
+
expect(cr.computeCellNote(makeRow('r', {}), makeColumn('a'))).toBeNull()
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
it('returns null when the row or column is absent in the notes map', () => {
|
|
227
|
+
const cr = createCellRender(makeCtx({ props: { notes: { other: { a: 'x' } } } }))
|
|
228
|
+
expect(cr.computeCellNote(makeRow('r', {}), makeColumn('a'))).toBeNull()
|
|
229
|
+
const cr2 = createCellRender(makeCtx({ props: { notes: { r: { b: 'x' } } } }))
|
|
230
|
+
expect(cr2.computeCellNote(makeRow('r', {}), makeColumn('a'))).toBeNull()
|
|
231
|
+
})
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
describe('createCellRender / getColumnEditorOptions', () => {
|
|
235
|
+
it('returns [] for a function editorOptions when row has no original', () => {
|
|
236
|
+
const cr = createCellRender(makeCtx())
|
|
237
|
+
const col = makeColumn('a', { editorOptions: () => [{ value: 1, label: 'one' }] })
|
|
238
|
+
expect(cr.getColumnEditorOptions(col, null)).toEqual([])
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
it('evaluates a function editorOptions per row', () => {
|
|
242
|
+
const cr = createCellRender(makeCtx())
|
|
243
|
+
const col = makeColumn('a', {
|
|
244
|
+
editorOptions: (data: any) => [{ value: data.code, label: data.name }],
|
|
245
|
+
})
|
|
246
|
+
const row = makeRow('r', { code: 'x', name: 'X' })
|
|
247
|
+
const opts = cr.getColumnEditorOptions(col, row)
|
|
248
|
+
expect(opts).toEqual([{ value: 'x', label: 'X' }])
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
it('caches normalized static editorOptions and reuses them', () => {
|
|
252
|
+
const ctx = makeCtx()
|
|
253
|
+
const cr = createCellRender(ctx)
|
|
254
|
+
const src = ['a', 'b']
|
|
255
|
+
const col = makeColumn('a', { editorOptions: src })
|
|
256
|
+
const first = cr.getColumnEditorOptions(col)
|
|
257
|
+
const second = cr.getColumnEditorOptions(col)
|
|
258
|
+
expect(first).toBe(second) // same cached array reference
|
|
259
|
+
expect(ctx.editorOptionsCache['a']).toBe(first)
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
it('re-normalizes when the editorOptions source changes', () => {
|
|
263
|
+
const ctx = makeCtx()
|
|
264
|
+
const cr = createCellRender(ctx)
|
|
265
|
+
const first = cr.getColumnEditorOptions(makeColumn('a', { editorOptions: ['a'] }))
|
|
266
|
+
const second = cr.getColumnEditorOptions(makeColumn('a', { editorOptions: ['b'] }))
|
|
267
|
+
expect(first).not.toBe(second)
|
|
268
|
+
})
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
describe('createCellRender / formatListCellValue', () => {
|
|
272
|
+
it('joins an array of values using the default separator', () => {
|
|
273
|
+
const cr = createCellRender(makeCtx())
|
|
274
|
+
const col = makeColumn('a', {
|
|
275
|
+
editorOptions: [
|
|
276
|
+
{ value: 1, label: 'One' },
|
|
277
|
+
{ value: 2, label: 'Two' },
|
|
278
|
+
],
|
|
279
|
+
})
|
|
280
|
+
expect(cr.formatListCellValue(col, [1, 2])).toBe('One, Two')
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
it('honors a custom editorSeparator', () => {
|
|
284
|
+
const cr = createCellRender(makeCtx())
|
|
285
|
+
const col = makeColumn('a', {
|
|
286
|
+
editorSeparator: ' | ',
|
|
287
|
+
editorOptions: [
|
|
288
|
+
{ value: 1, label: 'One' },
|
|
289
|
+
{ value: 2, label: 'Two' },
|
|
290
|
+
],
|
|
291
|
+
})
|
|
292
|
+
expect(cr.formatListCellValue(col, [1, 2])).toBe('One | Two')
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
it('returns empty string for null / empty scalar values', () => {
|
|
296
|
+
const cr = createCellRender(makeCtx())
|
|
297
|
+
const col = makeColumn('a', { editorOptions: [] })
|
|
298
|
+
expect(cr.formatListCellValue(col, null)).toBe('')
|
|
299
|
+
expect(cr.formatListCellValue(col, '')).toBe('')
|
|
300
|
+
})
|
|
301
|
+
|
|
302
|
+
it('maps a single scalar value to its label', () => {
|
|
303
|
+
const cr = createCellRender(makeCtx())
|
|
304
|
+
const col = makeColumn('a', {
|
|
305
|
+
editorOptions: [{ value: 'k', label: 'Known' }],
|
|
306
|
+
})
|
|
307
|
+
expect(cr.formatListCellValue(col, 'k')).toBe('Known')
|
|
308
|
+
// unknown value falls back to its string form
|
|
309
|
+
expect(cr.formatListCellValue(col, 'z')).toBe('z')
|
|
310
|
+
})
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
describe('createCellRender / formatCellValue', () => {
|
|
314
|
+
const row = makeRow('r', {})
|
|
315
|
+
|
|
316
|
+
it('uses a custom formatter callback when present', () => {
|
|
317
|
+
const cr = createCellRender(makeCtx())
|
|
318
|
+
const col = makeColumn('a', {
|
|
319
|
+
formatter: ({ value }: any) => `<<${value}>>`,
|
|
320
|
+
})
|
|
321
|
+
expect(cr.formatCellValue(col, 7, row)).toBe('<<7>>')
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
it('coerces a nullish formatter result to empty string', () => {
|
|
325
|
+
const cr = createCellRender(makeCtx())
|
|
326
|
+
const col = makeColumn('a', { formatter: () => null })
|
|
327
|
+
expect(cr.formatCellValue(col, 1, row)).toBe('')
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
it('masks password columns with bullets, capped at 12', () => {
|
|
331
|
+
const cr = createCellRender(makeCtx())
|
|
332
|
+
const col = makeColumn('a', { editorType: 'password' })
|
|
333
|
+
expect(cr.formatCellValue(col, 'abc', row)).toBe('•••')
|
|
334
|
+
expect(cr.formatCellValue(col, '', row)).toBe('')
|
|
335
|
+
expect(cr.formatCellValue(col, 'x'.repeat(50), row)).toBe('•'.repeat(12))
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
it('stringifies plain values when there is no format config', () => {
|
|
339
|
+
const cr = createCellRender(makeCtx())
|
|
340
|
+
const col = makeColumn('a')
|
|
341
|
+
expect(cr.formatCellValue(col, 'hello', row)).toBe('hello')
|
|
342
|
+
expect(cr.formatCellValue(col, null, row)).toBe('')
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
it('applies numeric / currency / percent format configs', () => {
|
|
346
|
+
const cr = createCellRender(makeCtx())
|
|
347
|
+
expect(
|
|
348
|
+
cr.formatCellValue(makeColumn('a', { format: { type: 'number' } }), 1234.5, row),
|
|
349
|
+
).toContain('1,234')
|
|
350
|
+
const cur = cr.formatCellValue(
|
|
351
|
+
makeColumn('a', { format: { type: 'currency', currency: 'USD' } }),
|
|
352
|
+
1000,
|
|
353
|
+
row,
|
|
354
|
+
)
|
|
355
|
+
expect(cur).toContain('$')
|
|
356
|
+
const pct = cr.formatCellValue(
|
|
357
|
+
makeColumn('a', { format: { type: 'percent' } }),
|
|
358
|
+
0.25,
|
|
359
|
+
row,
|
|
360
|
+
)
|
|
361
|
+
expect(pct).toContain('%')
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
it('formats date configs and falls back when the value is not a date', () => {
|
|
365
|
+
const cr = createCellRender(makeCtx())
|
|
366
|
+
const dateCol = makeColumn('a', { format: { type: 'date' } })
|
|
367
|
+
const out = cr.formatCellValue(dateCol, '2024-01-15T00:00:00Z', row)
|
|
368
|
+
expect(out).toMatch(/2024/)
|
|
369
|
+
|
|
370
|
+
// Unparseable -> returns String(value)
|
|
371
|
+
expect(cr.formatCellValue(dateCol, 'not-a-date', row)).toBe('not-a-date')
|
|
372
|
+
})
|
|
373
|
+
|
|
374
|
+
it('formats datetime configs with merged default options', () => {
|
|
375
|
+
const cr = createCellRender(makeCtx())
|
|
376
|
+
const col = makeColumn('a', { format: { type: 'datetime' } })
|
|
377
|
+
const out = cr.formatCellValue(col, '2024-03-10T13:45:00Z', row)
|
|
378
|
+
expect(out).toMatch(/2024/)
|
|
379
|
+
})
|
|
380
|
+
})
|
|
381
|
+
|
|
382
|
+
describe('createCellRender / formatPinnedValue', () => {
|
|
383
|
+
it('invokes a formatter with a null row', () => {
|
|
384
|
+
const cr = createCellRender(makeCtx())
|
|
385
|
+
const formatter = vi.fn(({ value, row }: any) => `${value}:${row}`)
|
|
386
|
+
const col = makeColumn('a', { formatter })
|
|
387
|
+
expect(cr.formatPinnedValue(col, 5)).toBe('5:null')
|
|
388
|
+
expect(formatter.mock.calls[0][0].row).toBeNull()
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
it('masks password pinned values', () => {
|
|
392
|
+
const cr = createCellRender(makeCtx())
|
|
393
|
+
expect(cr.formatPinnedValue(makeColumn('a', { editorType: 'password' }), 'pw')).toBe('••')
|
|
394
|
+
expect(cr.formatPinnedValue(makeColumn('a', { editorType: 'password' }), '')).toBe('')
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
it('stringifies when no format config', () => {
|
|
398
|
+
const cr = createCellRender(makeCtx())
|
|
399
|
+
expect(cr.formatPinnedValue(makeColumn('a'), 'total')).toBe('total')
|
|
400
|
+
expect(cr.formatPinnedValue(makeColumn('a'), null)).toBe('')
|
|
401
|
+
})
|
|
402
|
+
|
|
403
|
+
it('applies numeric and date format configs', () => {
|
|
404
|
+
const cr = createCellRender(makeCtx())
|
|
405
|
+
expect(
|
|
406
|
+
cr.formatPinnedValue(makeColumn('a', { format: { type: 'number' } }), 9999),
|
|
407
|
+
).toContain('9,999')
|
|
408
|
+
const d = cr.formatPinnedValue(
|
|
409
|
+
makeColumn('a', { format: { type: 'date' } }),
|
|
410
|
+
'2022-06-01T00:00:00Z',
|
|
411
|
+
)
|
|
412
|
+
expect(d).toMatch(/2022/)
|
|
413
|
+
// unparseable date falls through to String()
|
|
414
|
+
expect(
|
|
415
|
+
cr.formatPinnedValue(makeColumn('a', { format: { type: 'date' } }), 'xyz'),
|
|
416
|
+
).toBe('xyz')
|
|
417
|
+
})
|
|
418
|
+
|
|
419
|
+
it('formats datetime pinned configs', () => {
|
|
420
|
+
const cr = createCellRender(makeCtx())
|
|
421
|
+
const out = cr.formatPinnedValue(
|
|
422
|
+
makeColumn('a', { format: { type: 'datetime' } }),
|
|
423
|
+
'2021-12-31T08:00:00Z',
|
|
424
|
+
)
|
|
425
|
+
expect(out).toMatch(/2021/)
|
|
426
|
+
})
|
|
427
|
+
})
|
|
428
|
+
|
|
429
|
+
describe('createCellRender / computePinnedCellClass', () => {
|
|
430
|
+
it('returns empty string when cellClass is not a function', () => {
|
|
431
|
+
const cr = createCellRender(makeCtx())
|
|
432
|
+
expect(cr.computePinnedCellClass({ a: 1 }, makeColumn('a', { cellClass: 'x' }))).toBe('')
|
|
433
|
+
})
|
|
434
|
+
|
|
435
|
+
it('invokes a function cellClass with a null row and the pinned value', () => {
|
|
436
|
+
const cr = createCellRender(makeCtx())
|
|
437
|
+
const cellClass = vi.fn((c: any) => (c.value > 0 ? 'pos' : 'neg'))
|
|
438
|
+
const col = makeColumn('a', {
|
|
439
|
+
cellClass,
|
|
440
|
+
field: 'a',
|
|
441
|
+
})
|
|
442
|
+
expect(cr.computePinnedCellClass({ a: 3 }, col)).toBe('pos')
|
|
443
|
+
expect(cellClass.mock.calls[0][0].row).toBeNull()
|
|
444
|
+
})
|
|
445
|
+
|
|
446
|
+
it('coerces a falsy cellClass result to empty string', () => {
|
|
447
|
+
const cr = createCellRender(makeCtx())
|
|
448
|
+
const col = makeColumn('a', { cellClass: () => undefined, field: 'a' })
|
|
449
|
+
expect(cr.computePinnedCellClass({ a: 1 }, col)).toBe('')
|
|
450
|
+
})
|
|
451
|
+
})
|