diff --git a/frontend/src/lib/geschichte/utils.test.ts b/frontend/src/lib/geschichte/utils.test.ts new file mode 100644 index 00000000..e5f78253 --- /dev/null +++ b/frontend/src/lib/geschichte/utils.test.ts @@ -0,0 +1,67 @@ +import { describe, it, expect } from 'vitest'; +import { formatAuthorName, formatAuthorDisplayName, formatPublishedAt } from './utils'; + +describe('formatAuthorName', () => { + it('joins firstName and lastName with a space', () => { + expect(formatAuthorName({ firstName: 'Anna', lastName: 'Schmidt', email: 'a@x' })).toBe( + 'Anna Schmidt' + ); + }); + + it('returns firstName alone when lastName is absent', () => { + expect(formatAuthorName({ firstName: 'Anna', email: 'a@x' })).toBe('Anna'); + }); + + it('returns lastName alone when firstName is absent', () => { + expect(formatAuthorName({ lastName: 'Schmidt', email: 'a@x' })).toBe('Schmidt'); + }); + + it('falls back to email when both names are absent', () => { + expect(formatAuthorName({ email: 'fallback@example.com' })).toBe('fallback@example.com'); + }); + + it('returns empty string for null input', () => { + expect(formatAuthorName(null)).toBe(''); + }); + + it('returns empty string for undefined input', () => { + expect(formatAuthorName(undefined)).toBe(''); + }); +}); + +describe('formatAuthorDisplayName', () => { + it('returns displayName when present', () => { + expect(formatAuthorDisplayName({ displayName: 'Anna Schmidt' })).toBe('Anna Schmidt'); + }); + + it('returns empty string for null input', () => { + expect(formatAuthorDisplayName(null)).toBe(''); + }); + + it('returns empty string for undefined input', () => { + expect(formatAuthorDisplayName(undefined)).toBe(''); + }); +}); + +describe('formatPublishedAt', () => { + it('returns null for null input', () => { + expect(formatPublishedAt(null)).toBeNull(); + }); + + it('returns null for undefined input', () => { + expect(formatPublishedAt(undefined)).toBeNull(); + }); + + it('formats an ISO datetime string to a localised date', () => { + const result = formatPublishedAt('2026-04-15T10:00:00Z', 'short'); + expect(result).not.toBeNull(); + expect(result).toContain('2026'); + }); + + it('slices to date-only before formatting (no TZ off-by-one)', () => { + // Both dates should format identically regardless of timezone offset + const a = formatPublishedAt('2026-04-15T00:00:00Z', 'short'); + const b = formatPublishedAt('2026-04-15T23:59:59Z', 'short'); + expect(a).toBe(b); + }); +});