Skip to content

Commit cccf6c7

Browse files
authored
test(dashboard): add LanguageChart gradient tests (JhaSourav07#610)
## Description Fixes JhaSourav07#339 Extracted LanguageChart gradient computation into a reusable helper function and added unit tests for gradient generation logic. ### Changes made * extracted `buildGradientStops(languages)` helper * added unit tests for: * single language gradients * two-language gradients * decimal/rounded percentage handling * verified existing LanguageChart render tests still pass ## Pillar * [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview N/A (test/refactor-only changes) ## Checklist before requesting a review: * [x] I have read the `CONTRIBUTING.md` file. * [x] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). * [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). * [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). * [ ] I have updated `README.md` if I added a new theme or URL parameter. * [x] I have started the repo. * [x] I have made sure that i have only one commit to merge in this PR. * [ ] The SVG output matches the CommitPulse "premium quality" aesthetic standard (not applicable). * [ ] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support. Closes JhaSourav07#339
2 parents de272b5 + 334e7cc commit cccf6c7

2 files changed

Lines changed: 67 additions & 10 deletions

File tree

components/dashboard/LanguageChart.test.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { render, screen } from '@testing-library/react';
33
import { describe, expect, it, vi } from 'vitest';
44
import LanguageChart from './LanguageChart';
5+
import { buildGradientStops } from './LanguageChart';
56

67
vi.mock('framer-motion', () => ({
78
motion: {
@@ -44,3 +45,51 @@ describe('LanguageChart', () => {
4445
expect(screen.getByText('JavaScript')).toBeDefined();
4546
});
4647
});
48+
49+
describe('buildGradientStops', () => {
50+
it('builds gradient for one language', () => {
51+
const result = buildGradientStops([
52+
{
53+
name: 'TypeScript',
54+
percentage: 100,
55+
color: '#3178c6',
56+
},
57+
]);
58+
59+
expect(result).toBe('#3178c6 0% 100%');
60+
});
61+
62+
it('builds gradient for two languages', () => {
63+
const result = buildGradientStops([
64+
{
65+
name: 'TypeScript',
66+
percentage: 60,
67+
color: '#3178c6',
68+
},
69+
{
70+
name: 'JavaScript',
71+
percentage: 40,
72+
color: '#f7df1e',
73+
},
74+
]);
75+
76+
expect(result).toBe('#3178c6 0% 60%, #f7df1e 60% 100%');
77+
});
78+
79+
it('handles decimal percentages correctly', () => {
80+
const result = buildGradientStops([
81+
{
82+
name: 'TS',
83+
percentage: 33.3,
84+
color: '#3178c6',
85+
},
86+
{
87+
name: 'JS',
88+
percentage: 66.7,
89+
color: '#f7df1e',
90+
},
91+
]);
92+
93+
expect(result).toBe('#3178c6 0% 33.3%, #f7df1e 33.3% 100%');
94+
});
95+
});

components/dashboard/LanguageChart.tsx

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@
33
import { motion } from 'framer-motion';
44
import { LanguageData } from '@/types/dashboard';
55

6+
export function buildGradientStops(languages: LanguageData[]): string {
7+
return languages
8+
.reduce<{ stops: string[]; current: number }>(
9+
(acc, lang) => {
10+
const next = acc.current + lang.percentage;
11+
12+
acc.stops.push(`${lang.color} ${acc.current}% ${next}%`);
13+
14+
return {
15+
stops: acc.stops,
16+
current: next,
17+
};
18+
},
19+
{ stops: [], current: 0 }
20+
)
21+
.stops.join(', ');
22+
}
623
export default function LanguageChart({ languages }: { languages: LanguageData[] }) {
724
if (languages.length === 0) {
825
return (
@@ -24,16 +41,7 @@ export default function LanguageChart({ languages }: { languages: LanguageData[]
2441
);
2542
}
2643

27-
const gradientStops = languages
28-
.reduce<{ stops: string[]; current: number }>(
29-
(acc, lang) => {
30-
const next = acc.current + lang.percentage;
31-
acc.stops.push(`${lang.color} ${acc.current}% ${next}%`);
32-
return { stops: acc.stops, current: next };
33-
},
34-
{ stops: [], current: 0 }
35-
)
36-
.stops.join(', ');
44+
const gradientStops = buildGradientStops(languages);
3745

3846
return (
3947
<motion.div

0 commit comments

Comments
 (0)