Add format?: 'short'|'long' (default 'long') to date.ts formatDate and remove the duplicate from personFormat.ts. Update DocumentTopBar to import from date.ts directly. Move the formatDate tests from personFormat.spec to date.spec. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
130 lines
4.6 KiB
TypeScript
130 lines
4.6 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
||
import { formatDate, formatGermanDateInput, isoToGerman, germanToIso } from './date';
|
||
|
||
// ─── formatDate ──────────────────────────────────────────────────────────────
|
||
|
||
describe('formatDate', () => {
|
||
it('defaults to long format when no format arg is passed', () => {
|
||
expect(formatDate('1943-12-24')).toBe('24. Dezember 1943');
|
||
});
|
||
|
||
it('formats long date with German month name', () => {
|
||
expect(formatDate('1943-12-24', 'long')).toBe('24. Dezember 1943');
|
||
});
|
||
|
||
it('formats short date as dd.mm.yyyy', () => {
|
||
expect(formatDate('1943-12-24', 'short')).toBe('24.12.1943');
|
||
});
|
||
|
||
it('does not shift Dec 31 to Jan 1 (T12:00:00 UTC guard)', () => {
|
||
expect(formatDate('1943-12-31', 'short')).toBe('31.12.1943');
|
||
});
|
||
});
|
||
|
||
// ─── isoToGerman ─────────────────────────────────────────────────────────────
|
||
|
||
describe('isoToGerman', () => {
|
||
it('converts a valid ISO date to DD.MM.YYYY', () => {
|
||
expect(isoToGerman('2024-12-20')).toBe('20.12.2024');
|
||
});
|
||
|
||
it('returns empty string for empty input', () => {
|
||
expect(isoToGerman('')).toBe('');
|
||
});
|
||
|
||
it('returns empty string for invalid format', () => {
|
||
expect(isoToGerman('not-a-date')).toBe('');
|
||
});
|
||
});
|
||
|
||
// ─── germanToIso ─────────────────────────────────────────────────────────────
|
||
|
||
describe('germanToIso', () => {
|
||
it('converts DD.MM.YYYY to ISO', () => {
|
||
expect(germanToIso('20.12.2024')).toBe('2024-12-20');
|
||
});
|
||
|
||
it('returns empty string for partial input', () => {
|
||
expect(germanToIso('20.12')).toBe('');
|
||
});
|
||
|
||
it('returns empty string for empty input', () => {
|
||
expect(germanToIso('')).toBe('');
|
||
});
|
||
});
|
||
|
||
// ─── formatGermanDateInput ────────────────────────────────────────────────────
|
||
|
||
describe('formatGermanDateInput – digit stream (no dots typed)', () => {
|
||
it('leaves 1–2 digits as-is', () => {
|
||
expect(formatGermanDateInput('2')).toBe('2');
|
||
expect(formatGermanDateInput('20')).toBe('20');
|
||
});
|
||
|
||
it('auto-inserts dot after 2 digits for 3–4 digit input', () => {
|
||
expect(formatGermanDateInput('201')).toBe('20.1');
|
||
expect(formatGermanDateInput('2012')).toBe('20.12');
|
||
});
|
||
|
||
it('auto-inserts two dots for 5–8 digit input', () => {
|
||
expect(formatGermanDateInput('20121')).toBe('20.12.1');
|
||
expect(formatGermanDateInput('20122024')).toBe('20.12.2024');
|
||
});
|
||
|
||
it('ignores digits beyond 8', () => {
|
||
expect(formatGermanDateInput('201220249')).toBe('20.12.2024');
|
||
});
|
||
});
|
||
|
||
describe('formatGermanDateInput – manual dot entry with padding', () => {
|
||
it('pads single-digit day to 2 digits when dot is typed after it', () => {
|
||
expect(formatGermanDateInput('3.')).toBe('03.');
|
||
});
|
||
|
||
it('does not pad a 2-digit day', () => {
|
||
expect(formatGermanDateInput('03.')).toBe('03.');
|
||
expect(formatGermanDateInput('20.')).toBe('20.');
|
||
});
|
||
|
||
it('pads single-digit month to 2 digits when dot is typed after it', () => {
|
||
expect(formatGermanDateInput('03.3.')).toBe('03.03.');
|
||
});
|
||
|
||
it('does not pad a 2-digit month', () => {
|
||
expect(formatGermanDateInput('03.12.')).toBe('03.12.');
|
||
});
|
||
|
||
it('pads both day and month in a fully typed date', () => {
|
||
expect(formatGermanDateInput('3.3.2012')).toBe('03.03.2012');
|
||
});
|
||
|
||
it('pads only day when month is already 2 digits', () => {
|
||
expect(formatGermanDateInput('3.12.2024')).toBe('03.12.2024');
|
||
});
|
||
|
||
it('pads only month when day is already 2 digits', () => {
|
||
expect(formatGermanDateInput('20.3.2024')).toBe('20.03.2024');
|
||
});
|
||
|
||
it('handles a complete date entered with manual dots and no padding needed', () => {
|
||
expect(formatGermanDateInput('20.12.2024')).toBe('20.12.2024');
|
||
});
|
||
|
||
it('overflows excess day digits into month when dot follows', () => {
|
||
expect(formatGermanDateInput('123.')).toBe('12.3');
|
||
});
|
||
|
||
it('caps year digits at 4', () => {
|
||
expect(formatGermanDateInput('03.03.20249')).toBe('03.03.2024');
|
||
});
|
||
|
||
it('overflows excess month digits into year (digit stream then continue typing)', () => {
|
||
// User typed digits → auto-dot gave "20.12", then types "2" → raw becomes "20.122"
|
||
expect(formatGermanDateInput('20.122')).toBe('20.12.2');
|
||
});
|
||
|
||
it('continues building year after overflow', () => {
|
||
expect(formatGermanDateInput('20.12.2024')).toBe('20.12.2024');
|
||
});
|
||
});
|