|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { formatDate, formatDateTime } from '../../src/runtime/internal/preview/utils' |
| 3 | + |
| 4 | +describe('formatDate', () => { |
| 5 | + it('formats a date string as YYYY-MM-DD', () => { |
| 6 | + // formatDate uses local time (getFullYear/getMonth/getDate), so we |
| 7 | + // construct expected values the same way to stay timezone-agnostic. |
| 8 | + const input = '2022-06-15T12:00:00.000Z' |
| 9 | + const d = new Date(input) |
| 10 | + const expected = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}` |
| 11 | + expect(formatDate(input)).toBe(expected) |
| 12 | + }) |
| 13 | + |
| 14 | + it('pads single-digit month and day', () => { |
| 15 | + const input = '2022-01-05T12:00:00.000Z' |
| 16 | + const result = formatDate(input) |
| 17 | + // Format is always YYYY-MM-DD with zero-padded segments |
| 18 | + expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/) |
| 19 | + expect(result).toContain('-05') |
| 20 | + }) |
| 21 | + |
| 22 | + it('handles end-of-year dates', () => { |
| 23 | + const input = '2022-12-31T12:00:00.000Z' |
| 24 | + const result = formatDate(input) |
| 25 | + expect(result).toMatch(/^\d{4}-12-31$/) |
| 26 | + }) |
| 27 | + |
| 28 | + it('throws on invalid date', () => { |
| 29 | + expect(() => formatDate('not-a-date')).toThrow(TypeError) |
| 30 | + expect(() => formatDate('not-a-date')).toThrow('Invalid date value') |
| 31 | + }) |
| 32 | + |
| 33 | + it('produces same output as the build-time copy', async () => { |
| 34 | + // Guard against the two copies drifting apart. |
| 35 | + const buildTime = await import('../../src/utils/content/transformers/utils') |
| 36 | + const inputs = ['2022-06-15T12:00:00.000Z', '2023-01-01T00:00:00.000Z', '2024-12-31T23:59:59.000Z'] |
| 37 | + for (const input of inputs) { |
| 38 | + expect(formatDate(input)).toBe(buildTime.formatDate(input)) |
| 39 | + } |
| 40 | + }) |
| 41 | +}) |
| 42 | + |
| 43 | +describe('formatDateTime', () => { |
| 44 | + it('formats a datetime string as YYYY-MM-DD HH:mm:ss', () => { |
| 45 | + const input = '2022-06-15T14:30:45.000Z' |
| 46 | + const result = formatDateTime(input) |
| 47 | + expect(result).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/) |
| 48 | + // The date portion must match formatDate |
| 49 | + expect(result.split(' ')[0]).toBe(formatDate(input)) |
| 50 | + }) |
| 51 | + |
| 52 | + it('pads single-digit hours, minutes, and seconds', () => { |
| 53 | + const result = formatDateTime('2022-01-01T01:02:03.000Z') |
| 54 | + expect(result).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/) |
| 55 | + }) |
| 56 | + |
| 57 | + it('throws on invalid datetime', () => { |
| 58 | + expect(() => formatDateTime('garbage')).toThrow(TypeError) |
| 59 | + expect(() => formatDateTime('garbage')).toThrow('Invalid datetime value') |
| 60 | + }) |
| 61 | + |
| 62 | + it('produces same output as the build-time copy', async () => { |
| 63 | + const buildTime = await import('../../src/utils/content/transformers/utils') |
| 64 | + const inputs = ['2022-06-15T14:30:45.000Z', '2023-01-01T00:00:00.000Z'] |
| 65 | + for (const input of inputs) { |
| 66 | + expect(formatDateTime(input)).toBe(buildTime.formatDateTime(input)) |
| 67 | + } |
| 68 | + }) |
| 69 | +}) |
0 commit comments