TaxID.utils.test.ts90 lines · main
1import { describe, expect, test } from 'vitest'
2
3import { TAX_IDS } from '@/components/interfaces/Organization/BillingSettings/BillingCustomerData/TaxID.constants'
4import {
5 getEffectiveTaxCountry,
6 resolveStoredTaxId,
7 sanitizeTaxIdValue,
8} from '@/components/interfaces/Organization/BillingSettings/BillingCustomerData/TaxID.utils'
9
10/**
11 * We're sanitizing EU tax ids. Stripe expects a prefixed tax id (ATU12345678),
12 * but users might not realize this and enter only the numbers (12345678) without the
13 * country code prefix.
14 *
15 * Our sanitize function should handle 3 cases:
16 * 1. take an un-prefixed tax id (12345678) and add the country prefix to it (ATU12345678)
17 * 2. take a correctly prefixed tax id (ATU12345678) and just pass it through
18 * 3. take a non-EU tax id and just pass it through
19 */
20describe('TaxID utils: sanitizeTaxID', () => {
21 test('should prefix an EU tax ID correctly', () => {
22 const austriaTaxID = {
23 id: 'txi_1LoPlYJDPojXS6LNVd1FOTk2',
24 type: 'eu_vat',
25 value: '12345678',
26 name: 'AT VAT',
27 }
28
29 const sanitizedID = sanitizeTaxIdValue(austriaTaxID)
30 expect(sanitizedID).toBe('ATU12345678')
31 })
32
33 test('should check that EU prefix is correct', () => {
34 const austriaTaxID = {
35 id: 'txi_1LoPlYJDPojXS6LNVd1FOTk2',
36 type: 'eu_vat',
37 value: 'ATU12345678',
38 name: 'AT VAT',
39 }
40
41 const sanitizedID = sanitizeTaxIdValue(austriaTaxID)
42 expect(sanitizedID).toBe('ATU12345678')
43 })
44
45 test('should not prefix an non-EU tax ID', () => {
46 const unitedStatesID = {
47 id: 'txi_1LoPlYJDPojXS6LNVd1FOTk2',
48 type: 'us_ein',
49 value: '12-3456789',
50 name: 'US EIN',
51 }
52
53 const sanitizedID = sanitizeTaxIdValue(unitedStatesID)
54 expect(sanitizedID).toBe('12-3456789')
55 })
56})
57
58describe('TaxID utils: getEffectiveTaxCountry', () => {
59 test('returns countryIso2 when no override is set', () => {
60 const ukVat = TAX_IDS.find((t) => t.name === 'UK VAT')!
61 expect(getEffectiveTaxCountry(ukVat)).toBe('GB')
62 })
63
64 test('returns taxCountryIso2 when override is set', () => {
65 const imVat = TAX_IDS.find((t) => t.name === 'IM VAT')!
66 expect(getEffectiveTaxCountry(imVat)).toBe('GB')
67 })
68})
69
70describe('TaxID utils: resolveStoredTaxId', () => {
71 test('resolves GB customer to UK VAT with billingCountry', () => {
72 const ukVat = TAX_IDS.find((t) => t.name === 'UK VAT')!
73 expect(resolveStoredTaxId('gb_vat', 'GB', 'GB')).toBe(ukVat)
74 })
75
76 test('resolves IM customer to IM VAT with billingCountry', () => {
77 const imVat = TAX_IDS.find((t) => t.name === 'IM VAT')!
78 expect(resolveStoredTaxId('gb_vat', 'GB', 'IM')).toBe(imVat)
79 })
80
81 test('resolves GB customer to UK VAT without billingCountry', () => {
82 // Without billingCountry, falls back to countryIso2 match — UK VAT has countryIso2 'GB'
83 const ukVat = TAX_IDS.find((t) => t.name === 'UK VAT')!
84 expect(resolveStoredTaxId('gb_vat', 'GB')).toBe(ukVat)
85 })
86
87 test('returns undefined for unknown type', () => {
88 expect(resolveStoredTaxId('unknown_type', 'XX')).toBeUndefined()
89 })
90})