Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 59 additions & 3 deletions apps/web/__tests__/lib/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { describe, it, expect } from 'vitest';
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import {
cn,
formatCurrency,
formatDate,
formatRelativeTime,
getBaseUrl,
toNumber,
} from '@/lib/utils';

describe('cn (class name merger)', () => {
Expand Down Expand Up @@ -56,11 +58,65 @@ describe('formatDate', () => {
});

describe('formatRelativeTime', () => {
it('returns a string', () => {
it('returns hours for recent timestamps', () => {
const date = new Date();
date.setHours(date.getHours() - 2);

const result = formatRelativeTime(date);
expect(typeof result).toBe('string');

expect(result).toContain('hour');
});

it('returns days for older timestamps', () => {
const date = new Date();
date.setDate(date.getDate() - 3);

const result = formatRelativeTime(date);

expect(result).toContain('day');
});
});

describe('toNumber', () => {
it('returns numbers unchanged', () => {
expect(toNumber(12.5)).toBe(12.5);
});

it('converts Decimal-like objects with toString', () => {
expect(toNumber({ toString: () => '42.75' })).toBe(42.75);
});

it('falls back to zero for nullish values', () => {
expect(toNumber(null)).toBe(0);
expect(toNumber(undefined)).toBe(0);
});
});

describe('getBaseUrl', () => {
const originalEnv = { ...process.env };

beforeEach(() => {
delete process.env.NEXT_PUBLIC_APP_URL;
delete process.env.VERCEL_URL;
});

afterEach(() => {
process.env = { ...originalEnv };
});

it('prefers NEXT_PUBLIC_APP_URL when set', () => {
process.env.NEXT_PUBLIC_APP_URL = 'https://app.example.com';

expect(getBaseUrl()).toBe('https://app.example.com');
});

it('falls back to VERCEL_URL when app url is missing', () => {
process.env.VERCEL_URL = 'preview.example.com';

expect(getBaseUrl()).toBe('https://preview.example.com');
});

it('returns localhost during local development', () => {
expect(getBaseUrl()).toBe('http://localhost:3000');
});
});