Files
portfolio/src/shared/lib/utils/formatDate/formatDate.test.ts
T

48 lines
1.7 KiB
TypeScript
Raw Normal View History

import { formatMonthYearRange } from './formatDate';
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('uses abbreviated month name', () => {
expect(formatMonthYearRange('2020-08-15T00:00:00Z', null)).toBe('Aug 2020 — Present');
});
});
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', () => {
expect(() => formatMonthYearRange('not-a-date', null)).toThrow('Invalid start date');
});
it('throws if end date is provided but invalid', () => {
expect(() => formatMonthYearRange('2024-01-01T00:00:00Z', 'invalid')).toThrow('Invalid end date');
});
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('throws on empty string', () => {
expect(() => formatMonthYearRange('', null)).toThrow('Invalid start date');
});
});
});