TaxID.utils.test.ts 3.0 KB

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