forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrencyUtilsTest.ts
200 lines (182 loc) · 7.93 KB
/
CurrencyUtilsTest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import Onyx from 'react-native-onyx';
import CONST from '@src/CONST';
import * as CurrencyUtils from '@src/libs/CurrencyUtils';
import LocaleListener from '@src/libs/Localize/LocaleListener';
import ONYXKEYS from '@src/ONYXKEYS';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
// This file can get outdated. In that case, you can follow these steps to update it:
// - open your browser console and navigate to the Network tab
// - refresh the App
// - click on the OpenApp request and in the preview tab locate the key `currencyList`
// - copy the value and format it to valid json using some external tool
// - update currencyList.json
import currencyList from './currencyList.json';
const currencyCodeList = Object.keys(currencyList);
const AVAILABLE_LOCALES = [CONST.LOCALES.EN, CONST.LOCALES.ES];
describe('CurrencyUtils', () => {
beforeAll(() => {
Onyx.init({
keys: {
NVP_PREFERRED_LOCALE: ONYXKEYS.NVP_PREFERRED_LOCALE,
CURRENCY_LIST: ONYXKEYS.CURRENCY_LIST,
},
initialKeyStates: {
[ONYXKEYS.NVP_PREFERRED_LOCALE]: CONST.LOCALES.DEFAULT,
[ONYXKEYS.CURRENCY_LIST]: currencyList,
},
});
LocaleListener.connect();
return waitForBatchedUpdates();
});
afterEach(() => Onyx.clear());
describe('getLocalizedCurrencySymbol', () => {
test.each(AVAILABLE_LOCALES)('Returns non empty string for all currencyCode with preferredLocale %s', (prefrredLocale) =>
Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, prefrredLocale).then(() => {
currencyCodeList.forEach((currencyCode: string) => {
const localizedSymbol = CurrencyUtils.getLocalizedCurrencySymbol(currencyCode);
expect(localizedSymbol).toBeTruthy();
});
}),
);
});
describe('isCurrencySymbolLTR', () => {
test.each([
[true, CONST.LOCALES.EN, 'USD'],
[false, CONST.LOCALES.ES, 'USD'],
])('Returns %s for preferredLocale %s and currencyCode %s', (isLeft, locale, currencyCode) =>
Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, locale).then(() => {
const isSymbolLeft = CurrencyUtils.isCurrencySymbolLTR(currencyCode);
expect(isSymbolLeft).toBe(isLeft);
}),
);
});
describe('getCurrencyDecimals', () => {
test('Currency decimals smaller than or equal 2', () => {
expect(CurrencyUtils.getCurrencyDecimals('JPY')).toBe(0);
expect(CurrencyUtils.getCurrencyDecimals('USD')).toBe(2);
expect(CurrencyUtils.getCurrencyDecimals('RSD')).toBe(2);
});
test('Currency decimals larger than 2 should return 2', () => {
// Actual: 3
expect(CurrencyUtils.getCurrencyDecimals('LYD')).toBe(2);
// Actual: 4
expect(CurrencyUtils.getCurrencyDecimals('UYW')).toBe(2);
});
});
describe('getCurrencyUnit', () => {
test('Currency with decimals smaller than or equal 2', () => {
expect(CurrencyUtils.getCurrencyUnit('JPY')).toBe(1);
expect(CurrencyUtils.getCurrencyUnit('USD')).toBe(100);
});
test('Currency with decimals larger than 2 should be floor to 2', () => {
expect(CurrencyUtils.getCurrencyUnit('LYD')).toBe(100);
});
});
describe('convertToBackendAmount', () => {
test.each([
[25, 2500],
[25.25, 2525],
[25.5, 2550],
[2500, 250000],
[80.6, 8060],
[80.9, 8090],
[80.99, 8099],
[25, 2500],
[25.25, 2525],
[25.5, 2550],
[2500, 250000],
[80.6, 8060],
[80.9, 8090],
[80.99, 8099],
])('Correctly converts %s to amount in cents (subunit) handled in backend', (amount, expectedResult) => {
expect(CurrencyUtils.convertToBackendAmount(amount)).toBe(expectedResult);
});
});
describe('convertToFrontendAmountAsInteger', () => {
test.each([
[2500, 25, 'USD'],
[2550, 25.5, 'USD'],
[25, 0.25, 'USD'],
[2500, 25, 'USD'],
[2500.5, 25, 'USD'], // The backend should never send a decimal .5 value
[2500, 25, 'VND'],
[2550, 26, 'VND'],
[25, 0, 'VND'],
[2586, 26, 'VND'],
[2500.5, 25, 'VND'], // The backend should never send a decimal .5 value
])('Correctly converts %s to amount in units handled in frontend as an integer', (amount, expectedResult, currency) => {
expect(CurrencyUtils.convertToFrontendAmountAsInteger(amount, currency)).toBe(expectedResult);
});
});
describe('convertToFrontendAmountAsString', () => {
test.each([
[2500, '25.00', 'USD'],
[2550, '25.50', 'USD'],
[25, '0.25', 'USD'],
[2500.5, '25.00', 'USD'],
[null, '', 'USD'],
[undefined, '', 'USD'],
[0, '0.00', 'USD'],
[2500, '25', 'VND'],
[2550, '26', 'VND'],
[25, '0', 'VND'],
[2500.5, '25', 'VND'],
[null, '', 'VND'],
[undefined, '', 'VND'],
[0, '0', 'VND'],
[2586, '26', 'VND'],
])('Correctly converts %s to amount in units handled in frontend as a string', (input, expectedResult, currency) => {
expect(CurrencyUtils.convertToFrontendAmountAsString(input, currency ?? CONST.CURRENCY.USD)).toBe(expectedResult);
});
});
describe('convertToDisplayString', () => {
test.each([
[CONST.CURRENCY.USD, 25, '$0.25'],
[CONST.CURRENCY.USD, 2500, '$25.00'],
[CONST.CURRENCY.USD, 150, '$1.50'],
[CONST.CURRENCY.USD, 250000, '$2,500.00'],
['JPY', 2500, '¥25'],
['JPY', 250000, '¥2,500'],
['JPY', 2500.5, '¥25'],
['RSD', 100, 'RSD\xa01.00'],
['RSD', 145, 'RSD\xa01.45'],
['BHD', 12345, 'BHD\xa0123.45'],
['BHD', 1, 'BHD\xa00.01'],
])('Correctly displays %s', (currency, amount, expectedResult) => {
expect(CurrencyUtils.convertToDisplayString(amount, currency)).toBe(expectedResult);
});
test.each([
['EUR', 25, '0,25\xa0€'],
['EUR', 2500, '25,00\xa0€'],
['EUR', 250000, '2500,00\xa0€'],
['EUR', 250000000, '2.500.000,00\xa0€'],
])('Correctly displays %s in ES locale', (currency, amount, expectedResult) =>
Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, CONST.LOCALES.ES).then(() => expect(CurrencyUtils.convertToDisplayString(amount, currency)).toBe(expectedResult)),
);
});
describe('convertToShortDisplayString', () => {
test.each([
[CONST.CURRENCY.USD, 25, '$0'],
[CONST.CURRENCY.USD, 2500, '$25'],
[CONST.CURRENCY.USD, 150, '$2'],
[CONST.CURRENCY.USD, 250000, '$2,500'],
['JPY', 2500, '¥25'],
['JPY', 250000, '¥2,500'],
['JPY', 2500.5, '¥25'],
['RSD', 100, 'RSD\xa01'],
['RSD', 145, 'RSD\xa01'],
['BHD', 12345, 'BHD\xa0123'],
['BHD', 1, 'BHD\xa00'],
])('Correctly displays %s', (currency, amount, expectedResult) => {
expect(CurrencyUtils.convertToShortDisplayString(amount, currency)).toBe(expectedResult);
});
test.each([
['EUR', 25, '0\xa0€'],
['EUR', 2500, '25\xa0€'],
['EUR', 250000, '2500\xa0€'],
['EUR', 250000000, '2.500.000\xa0€'],
])('Correctly displays %s in ES locale', (currency, amount, expectedResult) =>
Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, CONST.LOCALES.ES).then(() => expect(CurrencyUtils.convertToShortDisplayString(amount, currency)).toBe(expectedResult)),
);
});
});