Skip to content

Commit 1eae6cd

Browse files
authored
test: validate generated SVG output with DOMParser (JhaSourav07#2236)
## Description Fixes JhaSourav07#392 Added SVG validation coverage to 'lib/svg/generator.test.ts' by introducing an 'assertValidSVG()' helper that parses generated SVG output with 'DOMParser' and verifies that no parser errors are present. ### Changes made 1. Added 'assertValidSVG(svgString: string)' helper 2. Parses SVG output using 'DOMParser' with 'image/svg+xml' 3. Verifies generated SVGs are well-formed XML/SVG documents 4. Added validation checks to 5 existing SVG generation test cases 5. Preserved all existing test assertions ## Pillar * [ ] 🎨 Pillar 1 — New Theme Design * [ ] 📐 Pillar 2 — Geometric SVG Improvement * [ ] 🕐 Pillar 3 — Timezone Logic Optimization * [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview N/A — test-only change, no visual SVG output was modified. ## 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=Harshitbhardwaj468'). * [x] I have run 'npm run format' and 'npm run lint' locally and resolved all errors introduced by this change. * [x] I have run 'npm run test' and all tests pass locally. * [ ] I have run 'npm run test:coverage' and branch coverage is at or above 70%. * [x] My commits follow the Conventional Commits format. * [ ] 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. * [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no production SVG output was modified). * [x] (Recommended) I joined the CommitPulse Discord server for faster collaboration, mentorship, and PR support. ### Verification npm run test Result: No test failures introduced by this change
2 parents 185d0ae + adb5d82 commit 1eae6cd

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

lib/svg/generator.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ import type { BadgeParams, ContributionCalendar, StreakStats, MonthlyStats } fro
1616
import { hexColor } from './sanitizer';
1717
import { themes } from './themes';
1818

19+
function assertValidSVG(svgString: string): void {
20+
const doc = new DOMParser().parseFromString(svgString, 'image/svg+xml');
21+
22+
const parserError = doc.querySelector('parsererror');
23+
24+
expect(parserError).toBeNull();
25+
}
26+
1927
describe('generateSVG', () => {
2028
const mockStats: StreakStats = {
2129
currentStreak: 5,
@@ -50,6 +58,8 @@ describe('generateSVG', () => {
5058
mockCalendar
5159
);
5260

61+
assertValidSVG(svg);
62+
5363
expect(svg).not.toContain('CURRENT_STREAK');
5464
expect(svg).not.toContain('ANNUAL_SYNC_TOTAL');
5565
expect(svg).not.toContain('PEAK_STREAK');
@@ -70,6 +80,8 @@ describe('generateSVG', () => {
7080
mockCalendar
7181
);
7282

83+
assertValidSVG(svg);
84+
7385
expect(svg).toContain('CURRENT_STREAK');
7486
expect(svg).toContain('ANNUAL_SYNC_TOTAL');
7587
expect(svg).toContain('PEAK_STREAK');
@@ -78,6 +90,8 @@ describe('generateSVG', () => {
7890
it('uses default typography when no font is passed', () => {
7991
const svg = generateSVG(mockStats, { user: 'avi' } as unknown as BadgeParams, mockCalendar);
8092

93+
assertValidSVG(svg);
94+
8195
expect(svg).toContain('Syncopate');
8296
expect(svg).toContain('Space Grotesk');
8397
});
@@ -89,6 +103,8 @@ describe('generateSVG', () => {
89103
mockCalendar
90104
);
91105

106+
assertValidSVG(svg);
107+
92108
expect(svg).toContain('JetBrains Mono');
93109
});
94110

@@ -99,6 +115,8 @@ describe('generateSVG', () => {
99115
mockCalendar
100116
);
101117

118+
assertValidSVG(svg);
119+
102120
expect(svg).toContain('rx="0"');
103121
});
104122

0 commit comments

Comments
 (0)