115 lines
3.1 KiB
TypeScript
115 lines
3.1 KiB
TypeScript
|
|
import { describe, it, expect, vi, afterEach } from 'vitest';
|
||
|
|
import { toISODateString, isFutureDate, formatRelative } from '@app/shared/utils/date.util';
|
||
|
|
|
||
|
|
describe('date.util', () => {
|
||
|
|
afterEach(() => {
|
||
|
|
vi.useRealTimers();
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('toISODateString', () => {
|
||
|
|
it('should return date in YYYY-MM-DD format', () => {
|
||
|
|
const date = new Date('2026-05-13T10:30:00Z');
|
||
|
|
|
||
|
|
const result = toISODateString(date);
|
||
|
|
|
||
|
|
expect(result).toBe('2026-05-13');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('isFutureDate', () => {
|
||
|
|
it('should return true for future date as Date object', () => {
|
||
|
|
vi.useFakeTimers();
|
||
|
|
vi.setSystemTime(new Date('2026-05-13T12:00:00'));
|
||
|
|
|
||
|
|
const futureDate = new Date('2026-12-25');
|
||
|
|
|
||
|
|
const result = isFutureDate(futureDate);
|
||
|
|
|
||
|
|
expect(result).toBe(true);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should return false for past date as string', () => {
|
||
|
|
vi.useFakeTimers();
|
||
|
|
vi.setSystemTime(new Date('2026-05-13T12:00:00'));
|
||
|
|
|
||
|
|
const result = isFutureDate('2020-01-01');
|
||
|
|
|
||
|
|
expect(result).toBe(false);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should return false for today', () => {
|
||
|
|
vi.useFakeTimers();
|
||
|
|
vi.setSystemTime(new Date('2026-05-13T12:00:00'));
|
||
|
|
|
||
|
|
const result = isFutureDate(new Date('2026-05-13'));
|
||
|
|
|
||
|
|
expect(result).toBe(false);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('formatRelative', () => {
|
||
|
|
it('should return Today for current date', () => {
|
||
|
|
vi.useFakeTimers();
|
||
|
|
vi.setSystemTime(new Date('2026-05-13T12:00:00'));
|
||
|
|
|
||
|
|
const result = formatRelative(new Date('2026-05-13T10:00:00'));
|
||
|
|
|
||
|
|
expect(result).toBe('Today');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should return Yesterday for previous day', () => {
|
||
|
|
vi.useFakeTimers();
|
||
|
|
vi.setSystemTime(new Date('2026-05-13T12:00:00'));
|
||
|
|
|
||
|
|
const result = formatRelative(new Date('2026-05-12T10:00:00'));
|
||
|
|
|
||
|
|
expect(result).toBe('Yesterday');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should return days ago for less than 7 days', () => {
|
||
|
|
vi.useFakeTimers();
|
||
|
|
vi.setSystemTime(new Date('2026-05-13T12:00:00'));
|
||
|
|
|
||
|
|
const result = formatRelative(new Date('2026-05-10T12:00:00'));
|
||
|
|
|
||
|
|
expect(result).toBe('3 days ago');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should return weeks ago for less than 30 days', () => {
|
||
|
|
vi.useFakeTimers();
|
||
|
|
vi.setSystemTime(new Date('2026-05-13T12:00:00'));
|
||
|
|
|
||
|
|
const result = formatRelative(new Date('2026-04-27T12:00:00'));
|
||
|
|
|
||
|
|
expect(result).toBe('2 weeks ago');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should return months ago for less than 365 days', () => {
|
||
|
|
vi.useFakeTimers();
|
||
|
|
vi.setSystemTime(new Date('2026-06-15T12:00:00'));
|
||
|
|
|
||
|
|
const result = formatRelative(new Date('2026-02-13T12:00:00'));
|
||
|
|
|
||
|
|
expect(result).toBe('4 months ago');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should return years ago for more than 365 days', () => {
|
||
|
|
vi.useFakeTimers();
|
||
|
|
vi.setSystemTime(new Date('2026-05-13T12:00:00'));
|
||
|
|
|
||
|
|
const result = formatRelative(new Date('2024-05-13T12:00:00'));
|
||
|
|
|
||
|
|
expect(result).toBe('2 years ago');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should accept string input', () => {
|
||
|
|
vi.useFakeTimers();
|
||
|
|
vi.setSystemTime(new Date('2026-05-13T12:00:00'));
|
||
|
|
|
||
|
|
const result = formatRelative('2026-05-13T10:00:00');
|
||
|
|
|
||
|
|
expect(result).toBe('Today');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|