Files
TREK/client/src/components/Budget/expenseFx.test.ts
T
Maurice dbbbd07b1c fix(costs): amounts follow their currency's decimals and format, and payments count on the local day
Several small things in the costs views, on desktop and phone alike:

- The outstanding card no longer says "You're all settled up" while a
  payment is still open in Settle up.
- The custom split shows its suggested shares as a faint placeholder in
  the currency's own form (30000 for won, not 30000.00), so it no longer
  reads like an amount already typed.
- Amount fields take only the decimals the currency has: won and yen take
  none, KWD and the other three-decimal currencies three. A switch of
  currency cuts what was already typed. 1000.555 won was saved as typed,
  listed as 1,001 and reopened as 1001.
- The expense sheet on the phone prints shares and sums with the same
  formatter as the list, with grouping and the currency's decimals.
- A payment recorded with Settle or Settle all is dated on the viewer's
  own day, and one without a date of its own is grouped by its UTC record
  time read on the local calendar. In Seoul before 09:00 it landed on the
  day before.
- "1 expenses need a payer" now uses the plural forms of each language.

Refs #2604
2026-10-12 15:04:32 +02:00

67 lines
3.6 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { formatMoney } from '../../utils/formatters'
import { editingCurrencyOf, expenseFxPreview, splitShareLabel } from './expenseFx'
// A euro trip read in dollars, converted with the euro's own quote: one euro is 1.1551
// dollars today, and 1.17 on the day the hotel was booked (#2525).
const convert = (amount: number, from: string | null | undefined): number =>
(amount / ({ EUR: 1, USD: 1.1551, JPY: 170 } as Record<string, number>)[(from || 'USD').toUpperCase()]) * 1.1551
const hotel = { currency: 'USD', exchange_rate: 1.17 }
const base = { editing: hotel, editingCurrency: 'USD', tripCurrency: 'EUR', base: 'USD', convert }
describe('editingCurrencyOf', () => {
it('opens a saved expense without a currency in the trip currency', () => {
expect(editingCurrencyOf({ currency: null }, 'eur', 'USD')).toBe('EUR')
expect(editingCurrencyOf({ currency: 'jpy' }, 'EUR', 'USD')).toBe('JPY')
})
it('starts a new expense, or one on a trip without a currency, in the display currency', () => {
expect(editingCurrencyOf(null, 'EUR', 'usd')).toBe('USD')
expect(editingCurrencyOf({ currency: null }, null, 'usd')).toBe('USD')
})
})
describe('expenseFxPreview', () => {
it('previews an edit that keeps the currency at the rate frozen on entry', () => {
const fx = expenseFxPreview({ ...base, total: 801.76, currency: 'USD' })!
expect(fx.inTrip).toBeCloseTo(685.26, 2)
expect(fx.shown).toBeCloseTo(791.55, 2)
})
it('has nothing to preview for a bill in the display currency that reads back as typed', () => {
expect(expenseFxPreview({ ...base, editing: { currency: 'USD', exchange_rate: 1.1551 }, total: 12345.67, currency: 'USD' })).toBeNull()
expect(expenseFxPreview({ ...base, editing: null, total: 250, currency: 'USD' })).toBeNull()
expect(expenseFxPreview({ ...base, total: 0, currency: 'USD' })).toBeNull()
})
it('previews a new or re-currencied amount at today\'s rate', () => {
expect(expenseFxPreview({ ...base, editing: null, total: 1700, currency: 'JPY' })).toEqual({ inTrip: null, shown: convert(1700, 'JPY') })
// Switching the hotel to yen re-freezes on save, so the old rate does not apply.
expect(expenseFxPreview({ ...base, total: 1700, currency: 'JPY' })?.inTrip).toBeNull()
})
it('stops at the trip amount when the viewer reads the trip currency', () => {
expect(expenseFxPreview({ ...base, base: 'EUR', total: 801.76, currency: 'USD' })).toEqual({ inTrip: 801.76 / 1.17, shown: null })
})
})
describe('splitShareLabel', () => {
it('prints the entered share, then the share the row lends or borrows', () => {
const fx = expenseFxPreview({ ...base, total: 801.76, currency: 'USD' })
expect(splitShareLabel(400.88, 'USD', fx, 2, 'USD', 'en-US')).toBe('$400.88 → $395.77')
// Read in the trip currency, the share is the booked euros.
const inEur = expenseFxPreview({ ...base, base: 'EUR', total: 801.76, currency: 'USD' })
expect(splitShareLabel(400.88, 'USD', inEur, 2, 'EUR', 'en-US')).toBe(`$400.88 → ${formatMoney(342.63, 'EUR', 'en-US')}`)
})
it('prints the entered share alone when nothing is converted', () => {
expect(splitShareLabel(125, 'USD', null, 2, 'USD', 'en-US')).toBe('$125.00')
expect(splitShareLabel(0, 'USD', { inTrip: null, shown: 10 }, 0, 'USD', 'en-US')).toBe('$0.00')
})
it('prints a share the way the list prints its currency, grouped and with its own decimals (#2604)', () => {
expect(splitShareLabel(15000, 'KRW', null, 4, 'KRW', 'en-US')).toBe('₩15,000')
expect(splitShareLabel(1.5, 'KWD', null, 2, 'KWD', 'en-US')).toBe(formatMoney(1.5, 'KWD', 'en-US'))
})
})