95 lines
2.7 KiB
TypeScript
95 lines
2.7 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { DashboardDateDisplay } from './DashboardDateDisplay';
|
|
|
|
describe('DashboardDateDisplay', () => {
|
|
describe('happy paths', () => {
|
|
it('should format future date correctly', () => {
|
|
const now = new Date();
|
|
const futureDate = new Date(now.getTime() + 24 * 60 * 60 * 1000); // 24 hours from now
|
|
|
|
const result = DashboardDateDisplay.format(futureDate);
|
|
|
|
expect(result.date).toMatch(/^[A-Za-z]{3}, [A-Za-z]{3} \d{1,2}, \d{4}$/);
|
|
expect(result.time).toMatch(/^\d{2}:\d{2}$/);
|
|
expect(result.relative).toBe('1d');
|
|
});
|
|
|
|
it('should format date less than 24 hours correctly', () => {
|
|
const now = new Date();
|
|
const futureDate = new Date(now.getTime() + 6 * 60 * 60 * 1000); // 6 hours from now
|
|
|
|
const result = DashboardDateDisplay.format(futureDate);
|
|
|
|
expect(result.relative).toBe('6h');
|
|
});
|
|
|
|
it('should format date more than 24 hours correctly', () => {
|
|
const now = new Date();
|
|
const futureDate = new Date(now.getTime() + 48 * 60 * 60 * 1000); // 2 days from now
|
|
|
|
const result = DashboardDateDisplay.format(futureDate);
|
|
|
|
expect(result.relative).toBe('2d');
|
|
});
|
|
|
|
it('should format past date correctly', () => {
|
|
const now = new Date();
|
|
const pastDate = new Date(now.getTime() - 24 * 60 * 60 * 1000); // 24 hours ago
|
|
|
|
const result = DashboardDateDisplay.format(pastDate);
|
|
|
|
expect(result.relative).toBe('Past');
|
|
});
|
|
|
|
it('should format current date correctly', () => {
|
|
const now = new Date();
|
|
|
|
const result = DashboardDateDisplay.format(now);
|
|
|
|
expect(result.relative).toBe('Now');
|
|
});
|
|
|
|
it('should format date with leading zeros in time', () => {
|
|
const date = new Date('2024-01-15T05:03:00');
|
|
|
|
const result = DashboardDateDisplay.format(date);
|
|
|
|
expect(result.time).toBe('05:03');
|
|
});
|
|
});
|
|
|
|
describe('edge cases', () => {
|
|
it('should handle midnight correctly', () => {
|
|
const date = new Date('2024-01-15T00:00:00');
|
|
|
|
const result = DashboardDateDisplay.format(date);
|
|
|
|
expect(result.time).toBe('00:00');
|
|
});
|
|
|
|
it('should handle end of day correctly', () => {
|
|
const date = new Date('2024-01-15T23:59:59');
|
|
|
|
const result = DashboardDateDisplay.format(date);
|
|
|
|
expect(result.time).toBe('23:59');
|
|
});
|
|
|
|
it('should handle different days of week', () => {
|
|
const date = new Date('2024-01-15'); // Monday
|
|
|
|
const result = DashboardDateDisplay.format(date);
|
|
|
|
expect(result.date).toContain('Mon');
|
|
});
|
|
|
|
it('should handle different months', () => {
|
|
const date = new Date('2024-01-15');
|
|
|
|
const result = DashboardDateDisplay.format(date);
|
|
|
|
expect(result.date).toContain('Jan');
|
|
});
|
|
});
|
|
});
|