|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { formatEpochSeconds, formatIsoDateTime } from '../utils/dateTime' |
| 3 | + |
| 4 | +const DATE_TIME_FORMAT_OPTIONS: Intl.DateTimeFormatOptions = { |
| 5 | + year: 'numeric', |
| 6 | + month: '2-digit', |
| 7 | + day: '2-digit', |
| 8 | + hour: '2-digit', |
| 9 | + minute: '2-digit', |
| 10 | + second: '2-digit', |
| 11 | + hour12: false, |
| 12 | + timeZone: 'UTC', |
| 13 | +} |
| 14 | + |
| 15 | +describe('formatEpochSeconds', () => { |
| 16 | + it('returns placeholder for undefined, null, and non-finite values', () => { |
| 17 | + expect(formatEpochSeconds(undefined)).toBe('-') |
| 18 | + expect(formatEpochSeconds(null)).toBe('-') |
| 19 | + expect(formatEpochSeconds(Number.NaN)).toBe('-') |
| 20 | + expect(formatEpochSeconds(Number.POSITIVE_INFINITY)).toBe('-') |
| 21 | + }) |
| 22 | + |
| 23 | + it('formats epoch zero as a valid date instead of placeholder', () => { |
| 24 | + const expected = new Date(0).toLocaleString('en-US', DATE_TIME_FORMAT_OPTIONS) |
| 25 | + |
| 26 | + expect(formatEpochSeconds(0, DATE_TIME_FORMAT_OPTIONS, 'en-US')).toBe(expected) |
| 27 | + }) |
| 28 | + |
| 29 | + it('formats a valid epoch value using locale and options', () => { |
| 30 | + const epochInSeconds = 1710000000 |
| 31 | + const expected = new Date(epochInSeconds * 1000).toLocaleString( |
| 32 | + 'en-US', |
| 33 | + DATE_TIME_FORMAT_OPTIONS, |
| 34 | + ) |
| 35 | + |
| 36 | + expect(formatEpochSeconds(epochInSeconds, DATE_TIME_FORMAT_OPTIONS, 'en-US')).toBe(expected) |
| 37 | + }) |
| 38 | +}) |
| 39 | + |
| 40 | +describe('formatIsoDateTime', () => { |
| 41 | + it('returns placeholder for empty or invalid values', () => { |
| 42 | + expect(formatIsoDateTime(undefined)).toBe('-') |
| 43 | + expect(formatIsoDateTime(null)).toBe('-') |
| 44 | + expect(formatIsoDateTime('')).toBe('-') |
| 45 | + expect(formatIsoDateTime('not-a-date')).toBe('-') |
| 46 | + }) |
| 47 | + |
| 48 | + it('formats valid ISO date-time using locale and options', () => { |
| 49 | + const isoDateTime = '2026-03-02T15:29:57Z' |
| 50 | + const expected = new Date(isoDateTime).toLocaleString('en-US', DATE_TIME_FORMAT_OPTIONS) |
| 51 | + |
| 52 | + expect(formatIsoDateTime(isoDateTime, DATE_TIME_FORMAT_OPTIONS, 'en-US')).toBe(expected) |
| 53 | + }) |
| 54 | +}) |
0 commit comments