Files
lunarfront-app/packages/shared/__tests__/utils/currency.test.ts
Ryan Moon 01d6ff3fa3 Move tests to __tests__ folders, add unit tests for shared utils and services
Restructure tests into __tests__/ directories at package root so they can
be excluded from production builds. Add unit tests for dates, currency,
lookup service, payment method default logic, and tax exemption state
transitions.
2026-03-27 21:14:42 -05:00

69 lines
1.7 KiB
TypeScript

import { describe, it, expect } from 'bun:test'
import { formatCurrency, roundCurrency, toCents, toDollars } from '../../src/utils/currency.js'
describe('formatCurrency', () => {
it('formats whole dollars', () => {
expect(formatCurrency(100)).toBe('$100.00')
})
it('formats cents', () => {
expect(formatCurrency(49.99)).toBe('$49.99')
})
it('formats zero', () => {
expect(formatCurrency(0)).toBe('$0.00')
})
it('formats thousands with comma', () => {
expect(formatCurrency(1234.56)).toBe('$1,234.56')
})
it('formats negative amounts', () => {
expect(formatCurrency(-25.50)).toBe('-$25.50')
})
})
describe('roundCurrency', () => {
it('rounds to 2 decimal places', () => {
expect(roundCurrency(1.005)).toBe(1.01)
expect(roundCurrency(1.004)).toBe(1)
})
it('handles already-rounded values', () => {
expect(roundCurrency(10.50)).toBe(10.50)
})
it('handles zero', () => {
expect(roundCurrency(0)).toBe(0)
})
})
describe('toCents', () => {
it('converts dollars to cents', () => {
expect(toCents(1.00)).toBe(100)
expect(toCents(49.99)).toBe(4999)
})
it('rounds fractional cents', () => {
// 1.005 * 100 = 100.49999... in IEEE 754, so Math.round gives 100
// This is a known floating point limitation — use toCents(roundCurrency(x)) for precision
expect(toCents(1.005)).toBe(100)
expect(toCents(1.006)).toBe(101)
})
it('handles zero', () => {
expect(toCents(0)).toBe(0)
})
})
describe('toDollars', () => {
it('converts cents to dollars', () => {
expect(toDollars(100)).toBe(1.00)
expect(toDollars(4999)).toBe(49.99)
})
it('handles zero', () => {
expect(toDollars(0)).toBe(0)
})
})