|
| 1 | +import type { BillingMoneyAmountJSON, BillingPaymentTotalsJSON } from '@clerk/shared/types'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { billingPaymentTotalsFromJSON } from '../billing'; |
| 5 | + |
| 6 | +const moneyJSON = (amount: number): BillingMoneyAmountJSON => ({ |
| 7 | + amount, |
| 8 | + amount_formatted: (amount / 100).toFixed(2), |
| 9 | + currency: 'USD', |
| 10 | + currency_symbol: '$', |
| 11 | +}); |
| 12 | + |
| 13 | +describe('billingPaymentTotalsFromJSON', () => { |
| 14 | + it('maps subtotal, grand_total, and tax_total', () => { |
| 15 | + const data: BillingPaymentTotalsJSON = { |
| 16 | + subtotal: moneyJSON(4500), |
| 17 | + grand_total: moneyJSON(5000), |
| 18 | + tax_total: moneyJSON(500), |
| 19 | + }; |
| 20 | + |
| 21 | + const totals = billingPaymentTotalsFromJSON(data); |
| 22 | + |
| 23 | + expect(totals.subtotal.amount).toBe(4500); |
| 24 | + expect(totals.grandTotal.amount).toBe(5000); |
| 25 | + expect(totals.taxTotal.amount).toBe(500); |
| 26 | + expect(totals.baseFee).toBeNull(); |
| 27 | + expect(totals.perUnitTotals).toBeUndefined(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('maps base_fee when present', () => { |
| 31 | + const data: BillingPaymentTotalsJSON = { |
| 32 | + subtotal: moneyJSON(5000), |
| 33 | + grand_total: moneyJSON(5000), |
| 34 | + tax_total: moneyJSON(0), |
| 35 | + base_fee: moneyJSON(1000), |
| 36 | + }; |
| 37 | + |
| 38 | + expect(billingPaymentTotalsFromJSON(data).baseFee?.amount).toBe(1000); |
| 39 | + }); |
| 40 | + |
| 41 | + it('maps per_unit_totals tiers with snake_case → camelCase conversion', () => { |
| 42 | + const data: BillingPaymentTotalsJSON = { |
| 43 | + subtotal: moneyJSON(5000), |
| 44 | + grand_total: moneyJSON(5000), |
| 45 | + tax_total: moneyJSON(0), |
| 46 | + per_unit_totals: [ |
| 47 | + { |
| 48 | + name: 'seats', |
| 49 | + block_size: 1, |
| 50 | + tiers: [ |
| 51 | + { quantity: 5, fee_per_block: moneyJSON(1000), total: moneyJSON(5000) }, |
| 52 | + { quantity: null, fee_per_block: moneyJSON(0), total: moneyJSON(0) }, |
| 53 | + ], |
| 54 | + }, |
| 55 | + ], |
| 56 | + }; |
| 57 | + |
| 58 | + const totals = billingPaymentTotalsFromJSON(data); |
| 59 | + |
| 60 | + expect(totals.perUnitTotals).toHaveLength(1); |
| 61 | + expect(totals.perUnitTotals?.[0].name).toBe('seats'); |
| 62 | + expect(totals.perUnitTotals?.[0].blockSize).toBe(1); |
| 63 | + expect(totals.perUnitTotals?.[0].tiers[0]).toMatchObject({ |
| 64 | + quantity: 5, |
| 65 | + feePerBlock: { amount: 1000 }, |
| 66 | + total: { amount: 5000 }, |
| 67 | + }); |
| 68 | + expect(totals.perUnitTotals?.[0].tiers[1].quantity).toBeNull(); |
| 69 | + }); |
| 70 | +}); |
0 commit comments