48 lines
1.6 KiB
TypeScript
48 lines
1.6 KiB
TypeScript
|
|
import { formatYearRange } from './formatDate';
|
||
|
|
|
||
|
|
describe('formatYearRange', () => {
|
||
|
|
describe('Success Paths', () => {
|
||
|
|
it('formats a date range within the same year', () => {
|
||
|
|
const start = '2024-01-01 12:00:00.000Z';
|
||
|
|
const end = '2024-12-31 12:00:00.000Z';
|
||
|
|
expect(formatYearRange(start, end)).toBe('2024');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('formats a range between different years', () => {
|
||
|
|
const start = '2021-05-15 12:00:00.000Z';
|
||
|
|
const end = '2024-03-20 12:00:00.000Z';
|
||
|
|
expect(formatYearRange(start, end)).toBe('2021 — 2024');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('formats a range with null end date as "Present"', () => {
|
||
|
|
const start = '2022-08-01 12:00:00.000Z';
|
||
|
|
const end = null;
|
||
|
|
expect(formatYearRange(start, end)).toBe('2022 — Present');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('Error & Edge Cases', () => {
|
||
|
|
it('throws if start date is invalid', () => {
|
||
|
|
const start = 'not-a-date';
|
||
|
|
const end = '2024-01-01';
|
||
|
|
expect(() => formatYearRange(start, end)).toThrow('Invalid start date');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('throws if end date is provided but invalid', () => {
|
||
|
|
const start = '2024-01-01';
|
||
|
|
const end = 'invalid';
|
||
|
|
expect(() => formatYearRange(start, end)).toThrow('Invalid end date');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('throws if start year is after end year', () => {
|
||
|
|
const start = '2024-01-01';
|
||
|
|
const end = '2020-01-01';
|
||
|
|
expect(() => formatYearRange(start, end)).toThrow('Start year cannot be after end year');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('handles empty strings by throwing', () => {
|
||
|
|
expect(() => formatYearRange('', null)).toThrow('Invalid start date');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|