feat: formatMonthYearRange — period now includes abbreviated month

This commit is contained in:
Ilia Mashkov
2026-05-18 13:01:58 +03:00
parent 1550989fd9
commit 48a08ec3fb
4 changed files with 52 additions and 45 deletions
@@ -1,47 +1,47 @@
import { formatYearRange } from './formatDate';
import { formatMonthYearRange } 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');
describe('formatMonthYearRange', () => {
describe('open-ended range', () => {
it('formats start date with Present when end is null', () => {
expect(formatMonthYearRange('2022-01-01T00:00:00Z', null)).toBe('Jan 2022 — Present');
});
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');
it('uses abbreviated month name', () => {
expect(formatMonthYearRange('2020-08-15T00:00:00Z', null)).toBe('Aug 2020 — Present');
});
});
describe('Error & Edge Cases', () => {
describe('closed range', () => {
it('formats start and end with month and year', () => {
expect(formatMonthYearRange('2021-05-01T00:00:00Z', '2024-03-31T00:00:00Z')).toBe('May 2021 — Mar 2024');
});
it('handles same year with different months', () => {
expect(formatMonthYearRange('2024-01-01T00:00:00Z', '2024-12-31T00:00:00Z')).toBe('Jan 2024 — Dec 2024');
});
it('handles same month and year', () => {
expect(formatMonthYearRange('2024-06-01T00:00:00Z', '2024-06-30T00:00:00Z')).toBe('Jun 2024');
});
});
describe('error 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');
expect(() => formatMonthYearRange('not-a-date', null)).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');
expect(() => formatMonthYearRange('2024-01-01T00:00:00Z', 'invalid')).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('throws if start is after end', () => {
expect(() => formatMonthYearRange('2024-01-01T00:00:00Z', '2020-01-01T00:00:00Z')).toThrow(
'Start date cannot be after end date',
);
});
it('handles empty strings by throwing', () => {
expect(() => formatYearRange('', null)).toThrow('Invalid start date');
it('throws on empty string', () => {
expect(() => formatMonthYearRange('', null)).toThrow('Invalid start date');
});
});
});