|
1 | 1 | import { describe, expect, it } from 'vitest'; |
2 | 2 |
|
3 | | -import { getDifferenceInSeconds, getRelativeTimeFormatUnit, getSetIntervalTime } from './dates'; |
| 3 | +import { formatDateLabel, getDifferenceInSeconds, getRelativeTimeFormatUnit, getSetIntervalTime } from './dates'; |
| 4 | + |
| 5 | +describe('formatDateLabel', () => { |
| 6 | + it('preserves local date and relative-label behavior without a timezone override', () => { |
| 7 | + // Arrange |
| 8 | + const current = new Date(2026, 0, 2, 12); |
| 9 | + |
| 10 | + // Act / Assert |
| 11 | + expect(formatDateLabel(new Date(2026, 0, 2), current)).toBe('Today'); |
| 12 | + expect(formatDateLabel(new Date(2026, 0, 1), current)).toBe('Yesterday'); |
| 13 | + expect(formatDateLabel(new Date(2026, 0, 2, 14, 30), current)).toContain('Today at '); |
| 14 | + }); |
| 15 | + |
| 16 | + it('uses the requested timezone for relative days and year boundaries', () => { |
| 17 | + // Arrange |
| 18 | + const date = new Date('2025-12-31T23:00:00Z'); |
| 19 | + const current = new Date('2026-01-01T12:00:00Z'); |
| 20 | + |
| 21 | + // Act |
| 22 | + const label = formatDateLabel(date, current, { timeZone: 'Pacific/Auckland' }); |
| 23 | + |
| 24 | + // Assert |
| 25 | + expect(label).toContain('Yesterday at '); |
| 26 | + const absoluteLabel = formatDateLabel(date, current, { includeRelative: false, timeZone: 'Pacific/Auckland' }); |
| 27 | + expect(absoluteLabel).not.toContain('2025'); |
| 28 | + expect(absoluteLabel).not.toContain('2026'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('uses the requested timezone to determine midnight', () => { |
| 32 | + // Arrange |
| 33 | + const date = new Date('2026-01-01T00:00:00Z'); |
| 34 | + |
| 35 | + // Act |
| 36 | + const label = formatDateLabel(date, new Date('2026-01-02T12:00:00Z'), { includeRelative: false, month: 'short', timeZone: 'UTC' }); |
| 37 | + |
| 38 | + // Assert |
| 39 | + expect(label).toBe(new Intl.DateTimeFormat(undefined, { day: 'numeric', month: 'short', timeZone: 'UTC' }).format(date)); |
| 40 | + }); |
| 41 | + |
| 42 | + it('preserves time when local midnight is not midnight in the requested timezone', () => { |
| 43 | + // Arrange |
| 44 | + const date = new Date(2026, 0, 1); |
| 45 | + const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone === 'Asia/Kolkata' ? 'UTC' : 'Asia/Kolkata'; |
| 46 | + |
| 47 | + // Act |
| 48 | + const label = formatDateLabel(date, new Date(2026, 0, 2), { includeRelative: false, month: 'short', timeZone }); |
| 49 | + |
| 50 | + // Assert |
| 51 | + expect(label).toContain(' at '); |
| 52 | + expect(label).toContain(new Intl.DateTimeFormat(undefined, { hour: 'numeric', hour12: true, minute: '2-digit', timeZone }).format(date)); |
| 53 | + }); |
| 54 | +}); |
4 | 55 |
|
5 | 56 | const Time = { |
6 | 57 | days: (n: number) => n * 60 * 60 * 24, |
|
0 commit comments