Files
starr-philproperties/apps/api/tests/utils/datetime.test.js
T

75 lines
2.3 KiB
JavaScript

'use strict';
const { fmtDate, fmtDateTime, fmtTime } = require('../../utils/datetime.util');
const ISO = '2026-06-20T14:30:00.000Z'; // Saturday, June 20, 2026, 2:30 PM UTC
// ── fmtDate ───────────────────────────────────────────────────────────────────
describe('fmtDate()', () => {
test('null → returns —', () => {
expect(fmtDate(null)).toBe('—');
});
test('empty string → returns —', () => {
expect(fmtDate('')).toBe('—');
});
test('formats year correctly', () => {
expect(fmtDate(ISO)).toContain('2026');
});
test('formats month correctly (June)', () => {
expect(fmtDate(ISO)).toContain('June');
});
test('formats day correctly (20)', () => {
expect(fmtDate(ISO)).toContain('20');
});
test('accepts Date object as well as ISO string', () => {
const d = new Date(ISO);
expect(fmtDate(d)).toContain('2026');
});
});
// ── fmtDateTime ───────────────────────────────────────────────────────────────
describe('fmtDateTime()', () => {
test('null → returns —', () => {
expect(fmtDateTime(null)).toBe('—');
});
test('contains date portion', () => {
expect(fmtDateTime(ISO)).toContain('2026');
expect(fmtDateTime(ISO)).toContain('June');
});
test('contains time portion (2:30 PM UTC)', () => {
const out = fmtDateTime(ISO);
expect(out).toMatch(/2:30/);
expect(out).toContain('UTC');
});
});
// ── fmtTime ───────────────────────────────────────────────────────────────────
describe('fmtTime()', () => {
test('null → returns —', () => {
expect(fmtTime(null)).toBe('—');
});
test('outputs time with AM/PM', () => {
expect(fmtTime(ISO)).toMatch(/\d+:\d{2}\s*(AM|PM)/);
});
test('contains timezone label (UTC)', () => {
expect(fmtTime(ISO)).toContain('UTC');
});
test('locale option changes output', () => {
const en = fmtTime(ISO, { locale: 'en-US' });
expect(en).toBeTruthy();
});
});