53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { m } from '$lib/paraglide/messages.js';
|
|
|
|
const { relativeTime } = await import('./time');
|
|
|
|
function msAgo(ms: number, now: Date): string {
|
|
return new Date(now.getTime() - ms).toISOString();
|
|
}
|
|
|
|
describe('relativeTime', () => {
|
|
const now = new Date('2024-06-15T12:00:00.000Z');
|
|
|
|
it('returns "just now" for timestamps under 60 seconds ago', () => {
|
|
const ts = msAgo(30_000, now);
|
|
expect(relativeTime(ts, now)).toBe(m.comment_time_just_now());
|
|
});
|
|
|
|
it('returns 1-minute label for exactly 1 minute ago', () => {
|
|
const ts = msAgo(60_000, now);
|
|
expect(relativeTime(ts, now)).toBe(m.comment_time_minutes({ count: 1 }));
|
|
});
|
|
|
|
it('returns 59-minute label for exactly 59 minutes ago', () => {
|
|
const ts = msAgo(59 * 60_000, now);
|
|
expect(relativeTime(ts, now)).toBe(m.comment_time_minutes({ count: 59 }));
|
|
});
|
|
|
|
it('returns 1-hour label for exactly 1 hour ago', () => {
|
|
const ts = msAgo(60 * 60_000, now);
|
|
expect(relativeTime(ts, now)).toBe(m.comment_time_hours({ count: 1 }));
|
|
});
|
|
|
|
it('returns 23-hour label for 23 hours ago', () => {
|
|
const ts = msAgo(23 * 60 * 60_000, now);
|
|
expect(relativeTime(ts, now)).toBe(m.comment_time_hours({ count: 23 }));
|
|
});
|
|
|
|
it('returns 1-day label for exactly 24 hours ago', () => {
|
|
const ts = msAgo(24 * 60 * 60_000, now);
|
|
expect(relativeTime(ts, now)).toBe(m.comment_time_days({ count: 1 }));
|
|
});
|
|
|
|
it('returns 6-day label for 6 days ago', () => {
|
|
const ts = msAgo(6 * 24 * 60 * 60_000, now);
|
|
expect(relativeTime(ts, now)).toBe(m.comment_time_days({ count: 6 }));
|
|
});
|
|
|
|
it('defaults now to current time when omitted', () => {
|
|
const ts = new Date(Date.now() - 5 * 60_000).toISOString();
|
|
expect(relativeTime(ts)).toBeTruthy();
|
|
});
|
|
});
|