Skip to content

Commit d6f2ce9

Browse files
authored
test(svg): add escapeXML unit tests (JhaSourav07#527)
2 parents cf06d45 + 025587a commit d6f2ce9

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

lib/svg/generator.test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from 'vitest';
2-
import { generateSVG, generateMonthlySVG, particleCount } from './generator';
2+
import { generateSVG, generateMonthlySVG, particleCount, escapeXML } from './generator';
33
import type { BadgeParams, ContributionCalendar, StreakStats, MonthlyStats } from '../../types';
44

55
describe('generateSVG', () => {
@@ -483,6 +483,39 @@ describe('generateMonthlySVG', () => {
483483
});
484484
});
485485

486+
describe('escapeXML', () => {
487+
it('escapes ampersands (&)', () => {
488+
expect(escapeXML('foo & bar')).toBe('foo & bar');
489+
});
490+
491+
it('escapes less-than signs (<)', () => {
492+
expect(escapeXML('<div>')).toBe('&lt;div&gt;');
493+
});
494+
495+
it('escapes greater-than signs (>)', () => {
496+
expect(escapeXML('a > b')).toBe('a &gt; b');
497+
});
498+
499+
it('escapes double quotes (")', () => {
500+
expect(escapeXML('class="btn"')).toBe('class=&quot;btn&quot;');
501+
});
502+
503+
it("escapes single quotes (')", () => {
504+
expect(escapeXML("it's working")).toBe('it&#39;s working');
505+
});
506+
507+
it('escapes a string combining all five special characters', () => {
508+
const combined = `<element attr="val" data-quote='yes'>&</element>`;
509+
const expected = `&lt;element attr=&quot;val&quot; data-quote=&#39;yes&#39;&gt;&amp;&lt;/element&gt;`;
510+
expect(escapeXML(combined)).toBe(expected);
511+
});
512+
513+
it('leaves a safe string unchanged', () => {
514+
const safe = 'Hello World 123!@#%^*()_+-=[]{}|;:,./?`~';
515+
expect(escapeXML(safe)).toBe(safe);
516+
});
517+
});
518+
486519
describe('particleCount', () => {
487520
it('returns 0 when count is 0', () => {
488521
expect(particleCount(0)).toBe(0);

0 commit comments

Comments
 (0)