Files
badgedex/test/svg-utils.test.js
T
echomike e7f0aa1c12
Node.js CI / build (push) Successful in 3m10s
rect padding implemet
2026-07-17 14:48:22 +02:00

50 lines
1.5 KiB
JavaScript

const { FONT_FAMILY, calcFontSize, estimateTextWidth } = require('../src/lib/svg-utils');
describe('svg-utils', () => {
describe('FONT_FAMILY', () => {
it('should be a non-empty string', () => {
expect(typeof FONT_FAMILY).toBe('string');
expect(FONT_FAMILY.length).toBeGreaterThan(0);
});
it('should contain Verdana', () => {
expect(FONT_FAMILY).toContain('Verdana');
});
});
describe('calcFontSize', () => {
it('should return 60% of size rounded', () => {
expect(calcFontSize(24)).toBe(14);
expect(calcFontSize(100)).toBe(60);
expect(calcFontSize(16)).toBe(10);
});
it('should round correctly', () => {
expect(calcFontSize(25)).toBe(15);
expect(calcFontSize(33)).toBe(20); // 33 * 0.6 = 19.8 → 20
});
});
describe('estimateTextWidth', () => {
it('should estimate width based on text length and font size', () => {
const width = estimateTextWidth('Hello', 14);
// formula: text.length * fontSize * 0.65 + fontSize * 0.8
expect(width).toBe(5 * 14 * 0.65 + 14 * 0.8); // 56.7
});
it('should include padding for empty string', () => {
// Even empty strings get the base padding
expect(estimateTextWidth('', 14)).toBe(14 * 0.8); // 11.2
});
it('should scale with font size', () => {
const w1 = estimateTextWidth('test', 10);
const w2 = estimateTextWidth('test', 20);
// w1 = 4*10*0.65 + 10*0.8 = 34
// w2 = 4*20*0.65 + 20*0.8 = 68
expect(w2).toBe(68);
});
});
});