|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +/** Migrated from test/local/payments/currencies.js (Mocha → Jest). */ |
| 6 | + |
| 7 | +import { CurrencyHelper } from './currencies'; |
| 8 | + |
| 9 | +const payPalEnabledSubscriptionsConfig = { |
| 10 | + paypalNvpSigCredentials: { |
| 11 | + enabled: true, |
| 12 | + }, |
| 13 | +}; |
| 14 | + |
| 15 | +describe('currencyMapValidation in constructor', () => { |
| 16 | + it('assigns map to property if object is valid', () => { |
| 17 | + const currenciesToCountries = { |
| 18 | + ZAR: ['US', 'CA'], |
| 19 | + EUR: ['FR'], |
| 20 | + }; |
| 21 | + const expected = new Map([ |
| 22 | + ['ZAR', ['US', 'CA']], |
| 23 | + ['EUR', ['FR']], |
| 24 | + ]); |
| 25 | + const ch = new CurrencyHelper({ currenciesToCountries }); |
| 26 | + expect(ch.currencyToCountryMap).toEqual(expected); |
| 27 | + }); |
| 28 | + |
| 29 | + it('assigns payPalEnabled to value in config', () => { |
| 30 | + let ch = new CurrencyHelper({ |
| 31 | + currenciesToCountries: {}, |
| 32 | + subscriptions: { |
| 33 | + paypalNvpSigCredentials: { |
| 34 | + enabled: false, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }); |
| 38 | + expect(ch.payPalEnabled).toBe(false); |
| 39 | + ch = new CurrencyHelper({ |
| 40 | + currenciesToCountries: {}, |
| 41 | + subscriptions: payPalEnabledSubscriptionsConfig, |
| 42 | + }); |
| 43 | + expect(ch.payPalEnabled).toBe(true); |
| 44 | + }); |
| 45 | + |
| 46 | + it('throws an error if invalid currencyCode', () => { |
| 47 | + const invalidCurrency = { |
| 48 | + ZZZZZ: ['US', 'CA'], |
| 49 | + }; |
| 50 | + expect(() => { |
| 51 | + (CurrencyHelper as any)({ currenciesToCountries: invalidCurrency }); |
| 52 | + }).toThrow(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('throws an error if invalid countryCode', () => { |
| 56 | + const invalidCountry = { |
| 57 | + AUD: ['AUS'], |
| 58 | + }; |
| 59 | + expect(() => { |
| 60 | + (CurrencyHelper as any)({ currenciesToCountries: invalidCountry }); |
| 61 | + }).toThrow(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('throws an error if countries are duplicated', () => { |
| 65 | + const duplicateCountriesA = { |
| 66 | + AUD: ['AM', 'AM'], |
| 67 | + }; |
| 68 | + const duplicateCountriesB = { |
| 69 | + AUD: ['AM', 'US', 'CA'], |
| 70 | + USD: ['AM'], |
| 71 | + }; |
| 72 | + expect(() => { |
| 73 | + (CurrencyHelper as any)({ currenciesToCountries: duplicateCountriesA }); |
| 74 | + }).toThrow(); |
| 75 | + expect(() => { |
| 76 | + (CurrencyHelper as any)({ currenciesToCountries: duplicateCountriesB }); |
| 77 | + }).toThrow(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('throws an error if currency not in paypal supported, if paypalEnabled', () => { |
| 81 | + const currenciesToCountries = { |
| 82 | + USD: ['US', 'CA'], |
| 83 | + }; |
| 84 | + expect(() => { |
| 85 | + (CurrencyHelper as any)({ |
| 86 | + currenciesToCountries, |
| 87 | + subscriptions: payPalEnabledSubscriptionsConfig, |
| 88 | + }); |
| 89 | + }).toThrow(); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +describe('isCurrencyCompatibleWithCountry', () => { |
| 94 | + const currenciesToCountries = { EUR: ['FR', 'DE'] }; |
| 95 | + const ch = new CurrencyHelper({ currenciesToCountries }); |
| 96 | + |
| 97 | + it('returns true if valid', () => { |
| 98 | + expect(ch.isCurrencyCompatibleWithCountry('EUR', 'FR')).toBe(true); |
| 99 | + }); |
| 100 | + |
| 101 | + it('returns true if valid irrespective of case mismatch', () => { |
| 102 | + expect(ch.isCurrencyCompatibleWithCountry('EUr', 'FR')).toBe(true); |
| 103 | + expect(ch.isCurrencyCompatibleWithCountry('EUR', 'fR')).toBe(true); |
| 104 | + }); |
| 105 | + |
| 106 | + it('returns false if country not in values', () => { |
| 107 | + expect( |
| 108 | + ch.isCurrencyCompatibleWithCountry('EUR', 'Not a country') |
| 109 | + ).toBe(false); |
| 110 | + }); |
| 111 | + |
| 112 | + it('returns false if currency not in keys', () => { |
| 113 | + expect( |
| 114 | + ch.isCurrencyCompatibleWithCountry('Not a currency', 'FR') |
| 115 | + ).toBe(false); |
| 116 | + }); |
| 117 | +}); |
| 118 | + |
| 119 | +describe('getPayPalAmountStringFromAmountInCents', () => { |
| 120 | + const currenciesToCountries = { USD: ['US'], EUR: ['FR', 'DE'] }; |
| 121 | + const ch = new CurrencyHelper({ |
| 122 | + currenciesToCountries, |
| 123 | + subscriptions: payPalEnabledSubscriptionsConfig, |
| 124 | + }); |
| 125 | + |
| 126 | + it('converts amount in cents to amount string', () => { |
| 127 | + expect(ch.getPayPalAmountStringFromAmountInCents(1099)).toBe('10.99'); |
| 128 | + expect(ch.getPayPalAmountStringFromAmountInCents(9)).toBe('0.09'); |
| 129 | + expect(ch.getPayPalAmountStringFromAmountInCents(900000)).toBe('9000.00'); |
| 130 | + }); |
| 131 | + |
| 132 | + /* |
| 133 | + * https://developer.paypal.com/docs/nvp-soap-api/do-reference-transaction-nvp/#payment-details-fields |
| 134 | + * AMT: ....Value is typically a positive number that cannot exceed nine (9) digits in SOAP request/response... |
| 135 | + */ |
| 136 | + it('throws an error if value exceeds 9 digits', () => { |
| 137 | + expect(ch.getPayPalAmountStringFromAmountInCents(999999999)).toBe( |
| 138 | + '9999999.99' |
| 139 | + ); |
| 140 | + expect(() => { |
| 141 | + ch.getPayPalAmountStringFromAmountInCents(1000000000); |
| 142 | + }).toThrow(); |
| 143 | + }); |
| 144 | +}); |
0 commit comments