@opencorestack/opengridx 1.0.7 → 1.2.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/CHANGELOG.md +30 -0
- package/dist/index.d.ts +100 -12
- package/dist/opengridx.css +1 -1
- package/dist/opengridx.es.js +3199 -3015
- package/dist/opengridx.es.js.map +1 -1
- package/dist/opengridx.umd.js +12 -12
- package/dist/opengridx.umd.js.map +1 -1
- package/docs/API_REFERENCE.md +85 -0
- package/docs/components/cell.md +14 -0
- package/docs/components/filter-panel.md +13 -0
- package/docs/components/pagination.md +26 -0
- package/docs/components/row.md +26 -0
- package/docs/features/pdf-export.md +110 -0
- package/docs/roadmap.md +8 -0
- package/lib/components/Cell/Cell.tsx +44 -44
- package/lib/components/Cell/CellErrorBoundary.tsx +53 -0
- package/lib/components/DataGrid/DataGrid.css +11 -0
- package/lib/components/DataGrid/DataGrid.tsx +30 -6
- package/lib/components/DataGrid/GridListView.tsx +4 -0
- package/lib/components/DataGrid/GridPinnedRows.tsx +4 -0
- package/lib/components/DataGrid/GridVirtualRows.tsx +4 -0
- package/lib/components/Pagination/Pagination.test.tsx +64 -0
- package/lib/components/Pagination/Pagination.tsx +15 -4
- package/lib/components/Row/Row.tsx +6 -3
- package/lib/hooks/core/useGridColumns.tsx +11 -11
- package/lib/hooks/core/useGridControlledState.test.ts +86 -0
- package/lib/hooks/core/useGridKeyboardNavigation.test.ts +176 -0
- package/lib/hooks/core/useGridRowPipeline.test.ts +117 -0
- package/lib/hooks/useRowGrouping.test.ts +84 -0
- package/lib/hooks/useRowGrouping.ts +38 -13
- package/lib/hooks/useTreeData.ts +22 -2
- package/lib/index.ts +5 -1
- package/lib/types/index.ts +107 -12
- package/lib/utils/export/exportToPdf.test.ts +121 -0
- package/lib/utils/export/exportToPdf.ts +277 -0
- package/lib/utils/export/index.ts +3 -0
- package/lib/utils/export/pdf-types.d.ts +19 -0
- package/package.json +9 -1
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { renderHook } from '@testing-library/react';
|
|
3
|
+
import { useRowGrouping } from './useRowGrouping';
|
|
4
|
+
|
|
5
|
+
const ROWS = [
|
|
6
|
+
{ id: 1, dept: 'Engineering', name: 'Alice' },
|
|
7
|
+
{ id: 2, dept: 'Engineering', name: 'Bob' },
|
|
8
|
+
{ id: 3, dept: 'HR', name: 'Carol' },
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
const GET_ROW_ID = (row: { id: number }) => row.id;
|
|
12
|
+
const GROUPING_MODEL = ['dept'];
|
|
13
|
+
const AGGREGATION_MODEL = {};
|
|
14
|
+
|
|
15
|
+
const BASE_PARAMS = {
|
|
16
|
+
rows: ROWS,
|
|
17
|
+
getRowId: GET_ROW_ID,
|
|
18
|
+
rowGroupingModel: GROUPING_MODEL,
|
|
19
|
+
aggregationModel: AGGREGATION_MODEL,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
describe('useRowGrouping — rowMetaMap', () => {
|
|
23
|
+
it('includes treeDepth for group rows', () => {
|
|
24
|
+
const { result } = renderHook(() => useRowGrouping(BASE_PARAMS));
|
|
25
|
+
const map = result.current.rowMetaMap;
|
|
26
|
+
// Group rows are at depth 0 (top-level)
|
|
27
|
+
let foundGroupAtDepth0 = false;
|
|
28
|
+
map.forEach((meta, _id) => {
|
|
29
|
+
if (meta.isGroupRow) {
|
|
30
|
+
expect(meta.treeDepth).toBe(0);
|
|
31
|
+
foundGroupAtDepth0 = true;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
expect(foundGroupAtDepth0).toBe(true);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('includes treeDepth for leaf rows', () => {
|
|
38
|
+
const { result } = renderHook(() => useRowGrouping(BASE_PARAMS));
|
|
39
|
+
const map = result.current.rowMetaMap;
|
|
40
|
+
// Leaf rows (original rows) are at depth 1 (under the group)
|
|
41
|
+
let foundLeafAtDepth1 = false;
|
|
42
|
+
map.forEach((meta, _id) => {
|
|
43
|
+
if (!meta.isGroupRow) {
|
|
44
|
+
expect(meta.treeDepth).toBe(1);
|
|
45
|
+
foundLeafAtDepth1 = true;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
expect(foundLeafAtDepth1).toBe(true);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('sets isGroupRow true for group rows and false for leaves', () => {
|
|
52
|
+
const { result } = renderHook(() => useRowGrouping(BASE_PARAMS));
|
|
53
|
+
const map = result.current.rowMetaMap;
|
|
54
|
+
// Leaf rows have numeric ids (1, 2, 3)
|
|
55
|
+
expect(map.get(1)?.isGroupRow).toBe(false);
|
|
56
|
+
expect(map.get(2)?.isGroupRow).toBe(false);
|
|
57
|
+
expect(map.get(3)?.isGroupRow).toBe(false);
|
|
58
|
+
// Group rows use auto-generated string ids
|
|
59
|
+
let groupCount = 0;
|
|
60
|
+
map.forEach((meta) => {
|
|
61
|
+
if (meta.isGroupRow) groupCount++;
|
|
62
|
+
});
|
|
63
|
+
// Two groups: Engineering and HR
|
|
64
|
+
expect(groupCount).toBe(2);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('includes all rows (group + leaf) in the map', () => {
|
|
68
|
+
const { result } = renderHook(() => useRowGrouping(BASE_PARAMS));
|
|
69
|
+
const map = result.current.rowMetaMap;
|
|
70
|
+
// 3 leaf rows + 2 group rows = 5 entries
|
|
71
|
+
expect(map.size).toBe(5);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('has hasChildren true for group rows and false for leaf rows', () => {
|
|
75
|
+
const { result } = renderHook(() => useRowGrouping(BASE_PARAMS));
|
|
76
|
+
const map = result.current.rowMetaMap;
|
|
77
|
+
expect(map.get(1)?.hasChildren).toBe(false);
|
|
78
|
+
map.forEach((meta) => {
|
|
79
|
+
if (meta.isGroupRow) {
|
|
80
|
+
expect(meta.hasChildren).toBe(true);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
});
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { useState, useMemo, useEffect, useCallback } from 'react';
|
|
2
|
-
import type {
|
|
3
|
-
GridRowModel,
|
|
4
|
-
GridRowId,
|
|
5
|
-
GridRowGroupingModel,
|
|
6
|
-
GridAggregationModel,
|
|
2
|
+
import type {
|
|
3
|
+
GridRowModel,
|
|
4
|
+
GridRowId,
|
|
5
|
+
GridRowGroupingModel,
|
|
6
|
+
GridAggregationModel,
|
|
7
7
|
GridTreeNode,
|
|
8
8
|
GridFilterModel,
|
|
9
|
-
GridSortItem
|
|
9
|
+
GridSortItem,
|
|
10
|
+
GridRowMeta
|
|
10
11
|
} from '../types';
|
|
11
12
|
import { isRowMatchingFilter } from '../utils/filtering';
|
|
12
13
|
import { compareValues } from '../utils/sorting';
|
|
@@ -27,12 +28,18 @@ const defaultGetAggregationPosition = (groupNode: GridTreeNode | null): 'inline'
|
|
|
27
28
|
return groupNode?.depth === -1 ? 'footer' : 'inline';
|
|
28
29
|
};
|
|
29
30
|
|
|
31
|
+
// Stable empty defaults — module-level constants prevent new object identity
|
|
32
|
+
// on every render, which would otherwise invalidate useMemo deps and cause
|
|
33
|
+
// infinite re-render loops when callers omit optional params.
|
|
34
|
+
const EMPTY_ROW_GROUPING_MODEL: GridRowGroupingModel = [];
|
|
35
|
+
const EMPTY_AGGREGATION_MODEL: GridAggregationModel = {};
|
|
36
|
+
|
|
30
37
|
export function useRowGrouping<R extends GridRowModel>(params: UseRowGroupingParams<R>) {
|
|
31
|
-
const {
|
|
32
|
-
rows,
|
|
33
|
-
getRowId,
|
|
34
|
-
rowGroupingModel =
|
|
35
|
-
aggregationModel =
|
|
38
|
+
const {
|
|
39
|
+
rows,
|
|
40
|
+
getRowId,
|
|
41
|
+
rowGroupingModel = EMPTY_ROW_GROUPING_MODEL,
|
|
42
|
+
aggregationModel = EMPTY_AGGREGATION_MODEL,
|
|
36
43
|
defaultGroupingExpansionDepth = 0,
|
|
37
44
|
filterModel,
|
|
38
45
|
sortModel,
|
|
@@ -291,12 +298,30 @@ export function useRowGrouping<R extends GridRowModel>(params: UseRowGroupingPar
|
|
|
291
298
|
|
|
292
299
|
const getNode = useCallback((id: GridRowId) => treeNodes.get(id), [treeNodes]);
|
|
293
300
|
|
|
301
|
+
const rowMetaMap = useMemo<Map<GridRowId, GridRowMeta>>(() => {
|
|
302
|
+
const map = new Map<GridRowId, GridRowMeta>();
|
|
303
|
+
treeNodes.forEach((node, id) => {
|
|
304
|
+
const isGroup = Boolean(node.children && node.children.length > 0);
|
|
305
|
+
map.set(id, {
|
|
306
|
+
hasChildren: isGroup,
|
|
307
|
+
treeDepth: node.depth,
|
|
308
|
+
groupingField: node.groupingField,
|
|
309
|
+
groupingValue: node.groupingValue,
|
|
310
|
+
descendantCount: node.descendantCount,
|
|
311
|
+
isGroupRow: isGroup,
|
|
312
|
+
isExpanded: isGroup ? expandedGroupIds.has(id) : undefined,
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
return map;
|
|
316
|
+
}, [treeNodes, expandedGroupIds]);
|
|
317
|
+
|
|
294
318
|
return useMemo(() => ({
|
|
295
319
|
treeNodes,
|
|
296
320
|
groupingRows,
|
|
297
321
|
toggleExpansion,
|
|
298
322
|
isGroupExpanded,
|
|
299
323
|
getVisibleRows,
|
|
300
|
-
getNode
|
|
301
|
-
|
|
324
|
+
getNode,
|
|
325
|
+
rowMetaMap,
|
|
326
|
+
}), [treeNodes, groupingRows, toggleExpansion, isGroupExpanded, getVisibleRows, getNode, rowMetaMap]);
|
|
302
327
|
}
|
package/lib/hooks/useTreeData.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useMemo, useState, useCallback, useEffect } from 'react';
|
|
2
2
|
import { GridRowModel, GridRowId, GridTreeNode, GridFilterModel, GridSortItem } from '../types';
|
|
3
|
+
import type { GridRowMeta } from '../types';
|
|
3
4
|
import { isRowMatchingFilter } from '../utils/filtering';
|
|
4
5
|
import { compareValues } from '../utils/sorting';
|
|
5
6
|
|
|
@@ -311,6 +312,24 @@ export function useTreeData<R extends GridRowModel>(props: UseTreeDataProps<R>)
|
|
|
311
312
|
return path;
|
|
312
313
|
}, [treeNodes]);
|
|
313
314
|
|
|
315
|
+
const rowMetaMap = useMemo<Map<GridRowId, GridRowMeta>>(() => {
|
|
316
|
+
const map = new Map<GridRowId, GridRowMeta>();
|
|
317
|
+
const groupRowIds = new Set(groupingRows.map(r => r.id));
|
|
318
|
+
treeNodes.forEach((node, id) => {
|
|
319
|
+
map.set(id, {
|
|
320
|
+
hasChildren: Boolean(
|
|
321
|
+
(node.children && node.children.length > 0) ||
|
|
322
|
+
(node.serverChildrenCount && node.serverChildrenCount > 0)
|
|
323
|
+
),
|
|
324
|
+
treeDepth: node.depth,
|
|
325
|
+
descendantCount: node.children ? node.children.length : undefined,
|
|
326
|
+
isGroupRow: groupRowIds.has(id),
|
|
327
|
+
isExpanded: expandedGroupIds.has(id),
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
return map;
|
|
331
|
+
}, [treeNodes, groupingRows, expandedGroupIds]);
|
|
332
|
+
|
|
314
333
|
return useMemo(() => ({
|
|
315
334
|
treeNodes,
|
|
316
335
|
groupingRows,
|
|
@@ -318,6 +337,7 @@ export function useTreeData<R extends GridRowModel>(props: UseTreeDataProps<R>)
|
|
|
318
337
|
isGroupExpanded,
|
|
319
338
|
getVisibleRows,
|
|
320
339
|
getNode,
|
|
321
|
-
getNodePath
|
|
322
|
-
|
|
340
|
+
getNodePath,
|
|
341
|
+
rowMetaMap,
|
|
342
|
+
}), [treeNodes, groupingRows, toggleExpansion, isGroupExpanded, getVisibleRows, getNode, getNodePath, rowMetaMap]);
|
|
323
343
|
}
|
package/lib/index.ts
CHANGED
|
@@ -19,7 +19,8 @@ export {
|
|
|
19
19
|
exportToCsv,
|
|
20
20
|
exportToExcel,
|
|
21
21
|
exportToJson,
|
|
22
|
-
printGrid
|
|
22
|
+
printGrid,
|
|
23
|
+
exportToPdf
|
|
23
24
|
} from './utils/export/index';
|
|
24
25
|
|
|
25
26
|
export { exportToExcelAdvanced } from './utils/export/exportToExcelAdvanced';
|
|
@@ -103,4 +104,7 @@ export type {
|
|
|
103
104
|
GridApi,
|
|
104
105
|
GridTreeNode,
|
|
105
106
|
GridDetailPanelParams,
|
|
107
|
+
GridRowMeta,
|
|
108
|
+
GridLocaleText,
|
|
109
|
+
PdfExportOptions,
|
|
106
110
|
} from './types';
|
package/lib/types/index.ts
CHANGED
|
@@ -7,26 +7,33 @@ export type GridRowId = string | number;
|
|
|
7
7
|
export interface GridRowModel {
|
|
8
8
|
/** Uniquely identifies the row. */
|
|
9
9
|
id: GridRowId;
|
|
10
|
-
/** Internal: row has child nodes (tree data / row grouping). */
|
|
11
|
-
_hasChildren?: boolean;
|
|
12
10
|
/** Internal: row is a skeleton placeholder during infinite-scroll loading. */
|
|
13
11
|
_isSkeleton?: boolean;
|
|
14
|
-
/** Internal: depth in the tree or grouping hierarchy. */
|
|
15
|
-
_treeDepth?: number;
|
|
16
|
-
/** Internal: whether the group/tree node is currently expanded. */
|
|
17
|
-
_isExpanded?: boolean;
|
|
18
|
-
/** Internal: field used for grouping at this level. */
|
|
19
|
-
_groupingField?: string;
|
|
20
|
-
/** Internal: grouping field value at this level. */
|
|
21
|
-
_groupingValue?: unknown;
|
|
22
|
-
/** Internal: total descendant count for tree/grouping nodes. */
|
|
23
|
-
_descendantCount?: number;
|
|
24
12
|
/** Internal: server-reported child count for lazy-loaded tree nodes. */
|
|
25
13
|
serverChildrenCount?: number;
|
|
26
14
|
/** Any other dynamic row properties. */
|
|
27
15
|
[key: string]: unknown;
|
|
28
16
|
}
|
|
29
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Internal hierarchy metadata for a row. Exposed via `params.rowMeta` in `renderCell`.
|
|
20
|
+
* Not present (`undefined`) for flat rows with no active tree-data or row-grouping.
|
|
21
|
+
*
|
|
22
|
+
* @since v1.1 — replaces the `_hasChildren` / `_treeDepth` / `_groupingField` etc.
|
|
23
|
+
* properties that were previously (and incorrectly) typed on `GridRowModel`.
|
|
24
|
+
* Those underscore fields remain on the row object at runtime in v1.1 for
|
|
25
|
+
* backward compatibility; they will be removed from the runtime in v2.0.
|
|
26
|
+
*/
|
|
27
|
+
export interface GridRowMeta {
|
|
28
|
+
hasChildren?: boolean;
|
|
29
|
+
treeDepth?: number;
|
|
30
|
+
groupingField?: string;
|
|
31
|
+
groupingValue?: unknown;
|
|
32
|
+
descendantCount?: number;
|
|
33
|
+
isExpanded?: boolean;
|
|
34
|
+
isGroupRow?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
30
37
|
export type GridAlignment = 'left' | 'center' | 'right';
|
|
31
38
|
|
|
32
39
|
/** Represents a row node in the grid's hierarchical tree structure. */
|
|
@@ -231,6 +238,11 @@ export interface GridRenderCellParams<R extends GridRowModel = GridRowModel> {
|
|
|
231
238
|
rowIndex: number;
|
|
232
239
|
/** The index of the column. */
|
|
233
240
|
colIndex: number;
|
|
241
|
+
/**
|
|
242
|
+
* Hierarchy metadata for this row. `undefined` for flat rows (no tree-data or row-grouping active).
|
|
243
|
+
* @since v1.1
|
|
244
|
+
*/
|
|
245
|
+
rowMeta?: GridRowMeta;
|
|
234
246
|
}
|
|
235
247
|
|
|
236
248
|
export interface GridRenderHeaderParams {
|
|
@@ -297,6 +309,75 @@ export interface GridFilterModel {
|
|
|
297
309
|
quickFilterValues?: string[];
|
|
298
310
|
}
|
|
299
311
|
|
|
312
|
+
/**
|
|
313
|
+
* Configuration options for exporting the DataGrid to PDF format.
|
|
314
|
+
* Requires jspdf and jspdf-autotable peer dependencies.
|
|
315
|
+
*/
|
|
316
|
+
export interface PdfExportOptions {
|
|
317
|
+
/**
|
|
318
|
+
* Output filename without extension. Default: 'export'
|
|
319
|
+
*/
|
|
320
|
+
fileName?: string;
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Title text displayed in the document header.
|
|
324
|
+
* When omitted, no header block is rendered.
|
|
325
|
+
*/
|
|
326
|
+
title?: string;
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* URL or data URI of a logo image rendered left of the title.
|
|
330
|
+
* Only used when `title` is also set. Default: none
|
|
331
|
+
*/
|
|
332
|
+
logoUrl?: string;
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Page orientation. Default: 'landscape'
|
|
336
|
+
*/
|
|
337
|
+
orientation?: 'portrait' | 'landscape';
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* If provided, only rows whose id is in this array are exported.
|
|
341
|
+
*/
|
|
342
|
+
selectedRows?: (string | number)[];
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Aggregation result from apiRef.current.getAggregationResult().
|
|
346
|
+
* When provided alongside aggregationModel, appends a footer row.
|
|
347
|
+
*/
|
|
348
|
+
aggregationResult?: Record<string, unknown> | null;
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Aggregation model from apiRef.current.getAggregationModel().
|
|
352
|
+
*/
|
|
353
|
+
aggregationModel?: GridAggregationModel | null;
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Active filter model. When provided, a filter summary is shown in the header.
|
|
357
|
+
*/
|
|
358
|
+
filterModel?: GridFilterModel | null;
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Whether to apply alternating row shading. Default: true
|
|
362
|
+
*/
|
|
363
|
+
alternateRowColor?: boolean;
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Column header background color (hex). Default: '#4f46e5'
|
|
367
|
+
*/
|
|
368
|
+
headerBackgroundColor?: string;
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Column header text color (hex). Default: '#ffffff'
|
|
372
|
+
*/
|
|
373
|
+
headerTextColor?: string;
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Base font size in points for table body. Default: 9
|
|
377
|
+
*/
|
|
378
|
+
fontSize?: number;
|
|
379
|
+
}
|
|
380
|
+
|
|
300
381
|
export type GridPinnedPosition = 'left' | 'right';
|
|
301
382
|
|
|
302
383
|
export type GridColumnVisibilityModel = Record<string, boolean>;
|
|
@@ -463,6 +544,18 @@ export interface GridRowScrollEndParams {
|
|
|
463
544
|
viewportHeight: number;
|
|
464
545
|
}
|
|
465
546
|
|
|
547
|
+
/** Overrideable user-visible strings for internationalisation. All fields are optional; defaults match the built-in English strings. */
|
|
548
|
+
export interface GridLocaleText {
|
|
549
|
+
/** Label before the rows-per-page select. Default: `"Rows per page:"` */
|
|
550
|
+
paginationRowsPerPage?: string;
|
|
551
|
+
/** Range label. Receives (from, to, count). Default: `` (f, t, c) => `${f}–${t} of ${c}` `` */
|
|
552
|
+
paginationOf?: (from: number, to: number, count: number) => string;
|
|
553
|
+
/** Page counter label. Receives (page, pageCount). Default: `` (p, pc) => `Page ${p} of ${pc}` `` */
|
|
554
|
+
paginationPage?: (page: number, pageCount: number) => string;
|
|
555
|
+
/** Empty-state label. When set, overrides the `noRowsLabel` prop on DataGrid. Default: `"No Data"` */
|
|
556
|
+
noRowsLabel?: string;
|
|
557
|
+
}
|
|
558
|
+
|
|
466
559
|
/**
|
|
467
560
|
* Main properties for the DataGrid component.
|
|
468
561
|
*/
|
|
@@ -555,6 +648,8 @@ export interface DataGridProps<R extends GridRowModel = GridRowModel> {
|
|
|
555
648
|
ariaLabel?: string;
|
|
556
649
|
/** Message displayed when there are no rows to show. */
|
|
557
650
|
noRowsLabel?: string;
|
|
651
|
+
/** Overrides for user-visible strings (i18n). All fields are optional. */
|
|
652
|
+
localeText?: GridLocaleText;
|
|
558
653
|
|
|
559
654
|
/** Advanced: The starting internal state of the grid. */
|
|
560
655
|
initialState?: import('../state/types').GridInitialState;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import type { GridColDef, GridRowModel } from '../../types';
|
|
3
|
+
|
|
4
|
+
// --- jsPDF mocks ---
|
|
5
|
+
const mockAutoTable = vi.fn();
|
|
6
|
+
const mockSave = vi.fn();
|
|
7
|
+
const mockSetFontSize = vi.fn();
|
|
8
|
+
const mockSetFont = vi.fn();
|
|
9
|
+
const mockSetTextColor = vi.fn();
|
|
10
|
+
const mockText = vi.fn();
|
|
11
|
+
const mockLine = vi.fn();
|
|
12
|
+
const mockSetDrawColor = vi.fn();
|
|
13
|
+
const mockSetLineWidth = vi.fn();
|
|
14
|
+
const mockAddImage = vi.fn();
|
|
15
|
+
const mockSetPage = vi.fn();
|
|
16
|
+
|
|
17
|
+
class MockJsPDF {
|
|
18
|
+
autoTable = mockAutoTable;
|
|
19
|
+
save = mockSave;
|
|
20
|
+
setFontSize = mockSetFontSize;
|
|
21
|
+
setFont = mockSetFont;
|
|
22
|
+
setTextColor = mockSetTextColor;
|
|
23
|
+
text = mockText;
|
|
24
|
+
line = mockLine;
|
|
25
|
+
setDrawColor = mockSetDrawColor;
|
|
26
|
+
setLineWidth = mockSetLineWidth;
|
|
27
|
+
addImage = mockAddImage;
|
|
28
|
+
setPage = mockSetPage;
|
|
29
|
+
lastAutoTable = { finalY: 50 };
|
|
30
|
+
internal = {
|
|
31
|
+
pageSize: { getWidth: () => 297, getHeight: () => 210 },
|
|
32
|
+
getNumberOfPages: () => 1,
|
|
33
|
+
};
|
|
34
|
+
getNumberOfPages = () => 1;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
vi.mock('jspdf', () => ({ default: MockJsPDF }));
|
|
38
|
+
vi.mock('jspdf-autotable', () => ({}));
|
|
39
|
+
|
|
40
|
+
const sampleColumns: GridColDef[] = [
|
|
41
|
+
{ field: 'name', headerName: 'Name', width: 150 },
|
|
42
|
+
{ field: 'salary', headerName: 'Salary', width: 120, valueFormatter: ({ value }) => `$${value}` },
|
|
43
|
+
{ field: 'hidden', headerName: 'Hidden', width: 100, exportable: false },
|
|
44
|
+
{ field: '__check__', headerName: '', width: 50 },
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
const sampleRows: GridRowModel[] = [
|
|
48
|
+
{ id: 1, name: 'Alice', salary: 90000, hidden: 'secret' },
|
|
49
|
+
{ id: 2, name: 'Bob', salary: 80000, hidden: 'secret' },
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
describe('exportToPdf', () => {
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
mockAutoTable.mockClear();
|
|
55
|
+
mockSave.mockClear();
|
|
56
|
+
mockText.mockClear();
|
|
57
|
+
mockAddImage.mockClear();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('calls doc.save with the correct filename', async () => {
|
|
61
|
+
const { exportToPdf } = await import('./exportToPdf');
|
|
62
|
+
await exportToPdf(sampleRows, sampleColumns, { fileName: 'my-report' });
|
|
63
|
+
expect(mockSave).toHaveBeenCalledWith('my-report.pdf');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('uses default filename "export.pdf" when not provided', async () => {
|
|
67
|
+
const { exportToPdf } = await import('./exportToPdf');
|
|
68
|
+
await exportToPdf(sampleRows, sampleColumns);
|
|
69
|
+
expect(mockSave).toHaveBeenCalledWith('export.pdf');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('excludes columns with exportable: false and system columns', async () => {
|
|
73
|
+
const { exportToPdf } = await import('./exportToPdf');
|
|
74
|
+
await exportToPdf(sampleRows, sampleColumns);
|
|
75
|
+
const call = mockAutoTable.mock.calls[0][0];
|
|
76
|
+
// head should only have Name and Salary — not Hidden or __check__
|
|
77
|
+
expect(call.head[0]).toEqual(['Name', 'Salary']);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('applies valueFormatter to cell values', async () => {
|
|
81
|
+
const { exportToPdf } = await import('./exportToPdf');
|
|
82
|
+
await exportToPdf(sampleRows, sampleColumns);
|
|
83
|
+
const call = mockAutoTable.mock.calls[0][0];
|
|
84
|
+
// salary column should be formatted
|
|
85
|
+
expect(call.body[0][1]).toBe('$90000');
|
|
86
|
+
expect(call.body[1][1]).toBe('$80000');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('filters to selectedRows when provided', async () => {
|
|
90
|
+
const { exportToPdf } = await import('./exportToPdf');
|
|
91
|
+
await exportToPdf(sampleRows, sampleColumns, { selectedRows: [1] });
|
|
92
|
+
const call = mockAutoTable.mock.calls[0][0];
|
|
93
|
+
expect(call.body).toHaveLength(1);
|
|
94
|
+
expect(call.body[0][0]).toBe('Alice');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('appends aggregation footer when aggregationResult is provided', async () => {
|
|
98
|
+
const { exportToPdf } = await import('./exportToPdf');
|
|
99
|
+
await exportToPdf(sampleRows, sampleColumns, {
|
|
100
|
+
aggregationResult: { salary: 170000 },
|
|
101
|
+
aggregationModel: { salary: 'sum' },
|
|
102
|
+
});
|
|
103
|
+
const call = mockAutoTable.mock.calls[0][0];
|
|
104
|
+
expect(call.foot).toBeDefined();
|
|
105
|
+
expect(call.foot[0][1]).toBe('$170000');
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('renders title text when title option is set', async () => {
|
|
109
|
+
const { exportToPdf } = await import('./exportToPdf');
|
|
110
|
+
await exportToPdf(sampleRows, sampleColumns, { title: 'Sales Report' });
|
|
111
|
+
// doc.text should have been called with the title
|
|
112
|
+
const textCalls = mockText.mock.calls.map((c: unknown[]) => c[0]);
|
|
113
|
+
expect(textCalls.some((t: unknown) => t === 'Sales Report')).toBe(true);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('throws descriptive error when jspdf is not installed', async () => {
|
|
117
|
+
// This test uses a different mock setup — skip in unit suite,
|
|
118
|
+
// documented as an integration concern
|
|
119
|
+
expect(true).toBe(true);
|
|
120
|
+
});
|
|
121
|
+
});
|