Files
gridpilot.gg/apps/website/lib/formatters/AvatarFormatter.test.ts
Marc Mintel 3db2209d2a
Some checks failed
CI / lint-typecheck (push) Failing after 4m52s
CI / tests (push) Has been skipped
CI / contract-tests (push) Has been skipped
CI / e2e-tests (push) Has been skipped
CI / comment-pr (push) Has been skipped
CI / commit-types (push) Has been skipped
formatter tests
2026-01-25 11:17:47 +01:00

76 lines
2.4 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { AvatarFormatter } from './AvatarFormatter';
describe('AvatarFormatter', () => {
describe('bufferToBase64', () => {
it('should convert ArrayBuffer to base64 string', () => {
const buffer = new ArrayBuffer(3);
const view = new Uint8Array(buffer);
view[0] = 72; // 'H'
view[1] = 101; // 'e'
view[2] = 108; // 'l'
const result = AvatarFormatter.bufferToBase64(buffer);
expect(result).toBe('SGVs');
});
it('should handle empty buffer', () => {
const buffer = new ArrayBuffer(0);
const result = AvatarFormatter.bufferToBase64(buffer);
expect(result).toBe('');
});
it('should handle buffer with special characters', () => {
const buffer = new ArrayBuffer(4);
const view = new Uint8Array(buffer);
view[0] = 255; // ÿ
view[1] = 254; // Þ
view[2] = 253; // Ý
view[3] = 252; // Ü
const result = AvatarFormatter.bufferToBase64(buffer);
expect(result).toBe('/v7+/v4=');
});
});
describe('hasValidData', () => {
it('should return true for valid buffer and content type', () => {
expect(AvatarFormatter.hasValidData('base64data', 'image/png')).toBe(true);
});
it('should return false for empty buffer', () => {
expect(AvatarFormatter.hasValidData('', 'image/png')).toBe(false);
});
it('should return false for empty content type', () => {
expect(AvatarFormatter.hasValidData('base64data', '')).toBe(false);
});
it('should return false for both empty', () => {
expect(AvatarFormatter.hasValidData('', '')).toBe(false);
});
});
describe('formatContentType', () => {
it('should format image/png to PNG', () => {
expect(AvatarFormatter.formatContentType('image/png')).toBe('PNG');
});
it('should format image/jpeg to JPEG', () => {
expect(AvatarFormatter.formatContentType('image/jpeg')).toBe('JPEG');
});
it('should format image/gif to GIF', () => {
expect(AvatarFormatter.formatContentType('image/gif')).toBe('GIF');
});
it('should handle content type without slash', () => {
expect(AvatarFormatter.formatContentType('png')).toBe('png');
});
it('should handle content type with multiple slashes', () => {
expect(AvatarFormatter.formatContentType('image/png/test')).toBe('PNG');
});
});
});