forked from Qortal/q-blog
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
import moment from 'moment';
|
|
import { formatTimestamp, formatDate } from '@/utils/time';
|
|
|
|
describe('time utils', () => {
|
|
const fixed = 1700000000000; // fixed ms timestamp
|
|
|
|
beforeEach(() => {
|
|
vi.useFakeTimers();
|
|
vi.setSystemTime(fixed);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('formatTimestamp: Just now for <1m', () => {
|
|
expect(formatTimestamp(fixed - 10 * 1000)).toBe('Just now');
|
|
});
|
|
|
|
it('formatTimestamp: minutes for <60m', () => {
|
|
expect(formatTimestamp(fixed - 23 * 60 * 1000)).toBe('23m');
|
|
});
|
|
|
|
it('formatTimestamp: hours for <24h', () => {
|
|
expect(formatTimestamp(fixed - 3 * 60 * 60 * 1000)).toBe('3h');
|
|
});
|
|
|
|
it('formatTimestamp: MMM D for >=24h', () => {
|
|
const s = formatTimestamp(fixed - 3 * 24 * 60 * 60 * 1000);
|
|
expect(s).toMatch(/^[A-Z][a-z]{2} \d{1,2}$/);
|
|
});
|
|
|
|
it('formatDate: relative time', () => {
|
|
const ts = fixed - 2 * 60 * 60 * 1000;
|
|
// moment.fromNow() value depends on locale; check contains 'ago'
|
|
const out = formatDate(ts);
|
|
expect(typeof out).toBe('string');
|
|
});
|
|
});
|