|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { formatByCurrency, formatMoney, sumByCurrency } from '@/lib/payments/format-money' |
| 3 | + |
| 4 | +/** |
| 5 | + * The rule under test is the one #497 established and #531 found broken on the |
| 6 | + * school-facing payout history: payout amounts are grouped per currency and |
| 7 | + * never added across currencies, and the currency code always comes from the |
| 8 | + * data. Formatting assertions use `toContain` on the digits plus a separate |
| 9 | + * symbol check, so a change in ICU's spacing or grouping doesn't produce a false |
| 10 | + * failure about arithmetic. |
| 11 | + */ |
| 12 | + |
| 13 | +const rows = (...pairs: Array<[number | null, string | null]>) => |
| 14 | + pairs.map(([amount, currency]) => ({ amount, currency })) |
| 15 | + |
| 16 | +describe('sumByCurrency', () => { |
| 17 | + it('keeps each currency in its own bucket', () => { |
| 18 | + expect(sumByCurrency(rows([1240.5, 'usd'], [860, 'eur']))).toEqual({ USD: 1240.5, EUR: 860 }) |
| 19 | + }) |
| 20 | + |
| 21 | + it('sums multiple rows within one currency', () => { |
| 22 | + expect(sumByCurrency(rows([100, 'usd'], [25.5, 'usd'], [10, 'eur']))).toEqual({ |
| 23 | + USD: 125.5, |
| 24 | + EUR: 10, |
| 25 | + }) |
| 26 | + }) |
| 27 | + |
| 28 | + it('treats currency codes case-insensitively so one currency never splits into two lines', () => { |
| 29 | + expect(sumByCurrency(rows([100, 'usd'], [50, 'USD'], [1, 'Usd']))).toEqual({ USD: 151 }) |
| 30 | + }) |
| 31 | + |
| 32 | + it('buckets a null currency as USD (the payouts.currency column default) rather than dropping the row', () => { |
| 33 | + expect(sumByCurrency(rows([40, null], [60, 'usd']))).toEqual({ USD: 100 }) |
| 34 | + }) |
| 35 | + |
| 36 | + it('treats a null amount as zero', () => { |
| 37 | + expect(sumByCurrency(rows([null, 'usd'], [10, 'usd']))).toEqual({ USD: 10 }) |
| 38 | + }) |
| 39 | + |
| 40 | + it('returns an empty object for no rows', () => { |
| 41 | + expect(sumByCurrency([])).toEqual({}) |
| 42 | + }) |
| 43 | + |
| 44 | + it('never produces a total larger than any single currency bucket (the #531 regression)', () => { |
| 45 | + const totals = sumByCurrency(rows([1240.5, 'usd'], [860, 'eur'])) |
| 46 | + // The bug summed these to 2100.5 and labelled it USD. No bucket may hold that. |
| 47 | + expect(Object.values(totals)).not.toContain(2100.5) |
| 48 | + expect(totals.USD).toBe(1240.5) |
| 49 | + }) |
| 50 | +}) |
| 51 | + |
| 52 | +describe('formatMoney', () => { |
| 53 | + it('formats with the currency it is given, not a hardcoded one', () => { |
| 54 | + expect(formatMoney(860, 'eur', 'en')).toContain('860') |
| 55 | + expect(formatMoney(860, 'eur', 'en')).toContain('€') |
| 56 | + expect(formatMoney(1240.5, 'usd', 'en')).toContain('$') |
| 57 | + }) |
| 58 | + |
| 59 | + it('accepts lower-case codes', () => { |
| 60 | + expect(formatMoney(10, 'gbp', 'en')).toContain('£') |
| 61 | + }) |
| 62 | + |
| 63 | + it('falls back to USD for a missing currency and to zero for a missing amount', () => { |
| 64 | + expect(formatMoney(10, null, 'en')).toContain('$') |
| 65 | + expect(formatMoney(null, 'usd', 'en')).toContain('0') |
| 66 | + }) |
| 67 | + |
| 68 | + it('respects the reader locale', () => { |
| 69 | + // es-ES puts the symbol after the number; the point is that locale is honoured, |
| 70 | + // not which side it lands on. |
| 71 | + expect(formatMoney(1240.5, 'eur', 'es')).toContain('€') |
| 72 | + }) |
| 73 | +}) |
| 74 | + |
| 75 | +describe('formatByCurrency', () => { |
| 76 | + it('renders one figure per currency instead of a single mixed number', () => { |
| 77 | + const rendered = formatByCurrency({ USD: 1240.5, EUR: 860 }, 'en') |
| 78 | + expect(rendered).toContain('$') |
| 79 | + expect(rendered).toContain('€') |
| 80 | + expect(rendered).toContain('1,240.50') |
| 81 | + expect(rendered).toContain('860') |
| 82 | + expect(rendered).not.toContain('2,100.50') |
| 83 | + }) |
| 84 | + |
| 85 | + it('renders a single-currency school as one plain figure', () => { |
| 86 | + const rendered = formatByCurrency({ USD: 1240.5 }, 'en') |
| 87 | + expect(rendered).toContain('$1,240.50') |
| 88 | + expect(rendered).not.toContain('·') |
| 89 | + }) |
| 90 | + |
| 91 | + it('drops currencies that net to zero', () => { |
| 92 | + const rendered = formatByCurrency({ USD: 100, EUR: 0 }, 'en') |
| 93 | + expect(rendered).toContain('$100') |
| 94 | + expect(rendered).not.toContain('€') |
| 95 | + }) |
| 96 | + |
| 97 | + it('renders one zero rather than an empty card when there is nothing to show', () => { |
| 98 | + expect(formatByCurrency({}, 'en')).toContain('0') |
| 99 | + expect(formatByCurrency({ USD: 0, EUR: 0 }, 'en')).toContain('0') |
| 100 | + }) |
| 101 | + |
| 102 | + it('composes with sumByCurrency end to end for the #531 case', () => { |
| 103 | + const paid = rows([1240.5, 'usd'], [860, 'eur']) |
| 104 | + const rendered = formatByCurrency(sumByCurrency(paid), 'en') |
| 105 | + expect(rendered).toBe('$1,240.50 · €860.00') |
| 106 | + }) |
| 107 | +}) |
0 commit comments