Skip to content

Commit 639ebd9

Browse files
Copilotpethers
andcommitted
Fix Chart.js test mocks for tree-shaken imports and update documentation
Co-authored-by: pethers <1726836+pethers@users.noreply.github.com>
1 parent e5ad693 commit 639ebd9

4 files changed

Lines changed: 63 additions & 15 deletions

File tree

docs/bundle-optimization.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
## Overview
44

5-
This document describes the bundle size optimizations implemented in v0.9.1+ to reduce the JavaScript bundle size from 189KB to 117KB (gzip) for initial page load - a 37.9% reduction that significantly improves performance.
5+
This document describes the bundle size optimizations implemented in v0.9.1+ to reduce the JavaScript bundle size from 188KB to 117KB (gzip) for initial page load - a 37.9% reduction that significantly improves performance.
66

77
## Optimization Results
88

99
### Before Optimization
10-
- **Total Initial Load**: 189 KB (gzip)
10+
- **Total Initial Load**: 188 KB (gzip)
1111
- React + React DOM: 59.07 KB
1212
- Vendor (Chart.js): 72.21 KB
13-
- Application code: 57.83 KB
13+
- Application code: 56.72 KB
1414

1515
### After Optimization
1616
- **Total Initial Load**: 117.40 KB (gzip) ✅

src/components/charts/RadarChart.comprehensive.test.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ type ChartMockConstructor = ReturnType<typeof vi.fn> & {
1919
font: { family: string };
2020
plugins: { legend: { display: boolean } };
2121
};
22+
overrides: Record<string, any>;
23+
registry: {
24+
controllers: { items: Record<string, any> };
25+
};
2226
};
2327

2428
const mockChartConstructor = vi.hoisted(() => {
@@ -36,12 +40,24 @@ vi.hoisted(() => {
3640
font: { family: "Arial" },
3741
plugins: { legend: { display: false } },
3842
};
43+
// Add overrides for registration check
44+
mockChartConstructor.overrides = {};
45+
mockChartConstructor.registry = {
46+
controllers: { items: {} },
47+
};
3948
});
4049

41-
// Use vi.mock with factory pattern to ensure the mock is hoisted correctly
42-
vi.mock("chart.js/auto", () => ({
43-
__esModule: true,
44-
default: mockChartConstructor,
50+
// Use vi.mock with factory pattern to mock chart.js module
51+
vi.mock("chart.js", () => ({
52+
Chart: mockChartConstructor,
53+
RadarController: vi.fn(),
54+
RadialLinearScale: vi.fn(),
55+
PointElement: vi.fn(),
56+
LineElement: vi.fn(),
57+
Filler: vi.fn(),
58+
Tooltip: vi.fn(),
59+
Legend: vi.fn(),
60+
CategoryScale: vi.fn(),
4561
}));
4662

4763
// Import testing utilities and components after mock definitions

src/components/charts/RadarChart.enhanced.test.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ type ChartMockConstructor = ReturnType<typeof vi.fn> & {
1313
font: { family: string };
1414
plugins: { legend: { display: boolean } };
1515
};
16+
overrides: Record<string, any>;
17+
registry: {
18+
controllers: { items: Record<string, any> };
19+
};
1620
};
1721

1822
const mockChartConstructor = vi.hoisted(() => {
@@ -29,12 +33,24 @@ vi.hoisted(() => {
2933
font: { family: "Arial" },
3034
plugins: { legend: { display: false } },
3135
};
36+
// Add overrides for registration check
37+
mockChartConstructor.overrides = {};
38+
mockChartConstructor.registry = {
39+
controllers: { items: {} },
40+
};
3241
});
3342

34-
// Apply the mock with the hoisted implementation
35-
vi.mock("chart.js/auto", () => ({
36-
__esModule: true,
37-
default: mockChartConstructor,
43+
// Apply the mock for chart.js module
44+
vi.mock("chart.js", () => ({
45+
Chart: mockChartConstructor,
46+
RadarController: vi.fn(),
47+
RadialLinearScale: vi.fn(),
48+
PointElement: vi.fn(),
49+
LineElement: vi.fn(),
50+
Filler: vi.fn(),
51+
Tooltip: vi.fn(),
52+
Legend: vi.fn(),
53+
CategoryScale: vi.fn(),
3854
}));
3955

4056
// Then import all required modules

src/components/charts/RadarChart.extended.test.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ type ChartMockConstructor = ReturnType<typeof vi.fn> & {
1313
font: { family: string };
1414
plugins: { legend: { display: boolean } };
1515
};
16+
overrides: Record<string, any>;
17+
registry: {
18+
controllers: { items: Record<string, any> };
19+
};
1620
};
1721

1822
const mockChartConstructor = vi.hoisted(() => {
@@ -29,12 +33,24 @@ vi.hoisted(() => {
2933
font: { family: "Arial" },
3034
plugins: { legend: { display: false } },
3135
};
36+
// Add overrides for registration check
37+
mockChartConstructor.overrides = {};
38+
mockChartConstructor.registry = {
39+
controllers: { items: {} },
40+
};
3241
});
3342

34-
// Apply the mock with the hoisted implementation
35-
vi.mock("chart.js/auto", () => ({
36-
__esModule: true,
37-
default: mockChartConstructor,
43+
// Apply the mock for chart.js module
44+
vi.mock("chart.js", () => ({
45+
Chart: mockChartConstructor,
46+
RadarController: vi.fn(),
47+
RadialLinearScale: vi.fn(),
48+
PointElement: vi.fn(),
49+
LineElement: vi.fn(),
50+
Filler: vi.fn(),
51+
Tooltip: vi.fn(),
52+
Legend: vi.fn(),
53+
CategoryScale: vi.fn(),
3854
}));
3955

4056
// Then import all required modules

0 commit comments

Comments
 (0)