-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathuseAssetFiatFormatter.test.ts
More file actions
282 lines (229 loc) · 8.63 KB
/
Copy pathuseAssetFiatFormatter.test.ts
File metadata and controls
282 lines (229 loc) · 8.63 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { renderHook } from '@testing-library/react-hooks';
import { useSelector } from 'react-redux';
import { useAssetFiatFormatter } from './useAssetFiatFormatter';
import { useTransactionPayCurrency } from './useTransactionPayCurrency';
import {
selectCurrencyRates,
selectCurrentCurrency,
} from '../../../../../selectors/currencyRateController';
import { selectEvmNetworkConfigurationsByChainId } from '../../../../../selectors/networkController';
import { getIntlNumberFormatter } from '../../../../../util/intl';
jest.mock('react-redux', () => ({
useSelector: jest.fn(),
}));
jest.mock('./useTransactionPayCurrency', () => ({
useTransactionPayCurrency: jest.fn(),
}));
jest.mock('../../../../../selectors/currencyRateController', () => ({
selectCurrentCurrency: jest.fn(),
selectCurrencyRates: jest.fn(),
}));
jest.mock('../../../../../selectors/networkController', () => ({
selectEvmNetworkConfigurationsByChainId: jest.fn(),
}));
jest.mock('../../../../../util/intl', () => ({
getIntlNumberFormatter: jest.fn(),
}));
jest.mock('../../../../../../locales/i18n', () => ({
locale: 'en-US',
strings: jest.fn((key: string) => key),
}));
const mockUseSelector = jest.mocked(useSelector);
const mockUseTransactionPayCurrency = jest.mocked(useTransactionPayCurrency);
const mockGetIntlNumberFormatter = jest.mocked(getIntlNumberFormatter);
const mockFormatter = { format: jest.fn() };
function buildState({
preferredCurrency = 'usd',
currencyRates = {} as Record<
string,
{ conversionRate?: number; usdConversionRate?: number }
>,
networkConfigs = {} as Record<string, { nativeCurrency: string }>,
} = {}) {
mockUseSelector.mockImplementation((selector) => {
if (selector === selectCurrentCurrency) return preferredCurrency;
if (selector === selectCurrencyRates) return currencyRates;
if (selector === selectEvmNetworkConfigurationsByChainId)
return networkConfigs;
return undefined;
});
}
describe('useAssetFiatFormatter', () => {
beforeEach(() => {
jest.clearAllMocks();
mockUseTransactionPayCurrency.mockReturnValue(undefined);
mockGetIntlNumberFormatter.mockReturnValue(
mockFormatter as unknown as ReturnType<typeof getIntlNumberFormatter>,
);
mockFormatter.format.mockImplementation((n) => `formatted:${String(n)}`);
});
describe('outside pay flow', () => {
it('formats using the preferred currency', () => {
buildState({ preferredCurrency: 'eur' });
const { result } = renderHook(() => useAssetFiatFormatter());
const output = result.current.format('100', '0x1');
expect(mockGetIntlNumberFormatter).toHaveBeenCalledWith(
'en-US',
expect.objectContaining({ style: 'currency', currency: 'eur' }),
);
expect(mockFormatter.format).toHaveBeenCalledWith('100');
expect(output).toBe('formatted:100');
});
it('exposes the fiatCurrency being used', () => {
buildState({ preferredCurrency: 'eur' });
const { result } = renderHook(() => useAssetFiatFormatter());
expect(result.current.fiatCurrency).toBe('eur');
});
it('uses minimumFractionDigits=0 for integer amounts', () => {
buildState({ preferredCurrency: 'eur' });
renderHook(() => useAssetFiatFormatter()).result.current.format(
'100',
'0x1',
);
expect(mockGetIntlNumberFormatter).toHaveBeenCalledWith(
'en-US',
expect.objectContaining({ minimumFractionDigits: 0 }),
);
});
it('uses minimumFractionDigits=2 for non-integer amounts', () => {
buildState({ preferredCurrency: 'eur' });
renderHook(() => useAssetFiatFormatter()).result.current.format(
'100.5',
'0x1',
);
expect(mockGetIntlNumberFormatter).toHaveBeenCalledWith(
'en-US',
expect.objectContaining({ minimumFractionDigits: 2 }),
);
});
it('treats undefined/null balances as zero', () => {
buildState({ preferredCurrency: 'eur' });
renderHook(() => useAssetFiatFormatter()).result.current.format(
undefined,
'0x1',
);
expect(mockFormatter.format).toHaveBeenCalledWith('0');
});
});
describe('pay flow (USD forced, preferred is EUR)', () => {
const eurUsdRates = {
ETH: { conversionRate: 2000, usdConversionRate: 2200 },
};
const ethChainConfig = { '0x1': { nativeCurrency: 'ETH' } };
beforeEach(() => {
mockUseTransactionPayCurrency.mockReturnValue('USD');
});
it('re-scales the amount by usdRate/preferredRate', () => {
buildState({
preferredCurrency: 'eur',
currencyRates: eurUsdRates,
networkConfigs: ethChainConfig,
});
renderHook(() => useAssetFiatFormatter()).result.current.format(
'100',
'0x1',
);
// 100 EUR * (2200 USD/ETH / 2000 EUR/ETH) = 110 USD
expect(mockFormatter.format).toHaveBeenCalledWith('110');
expect(mockGetIntlNumberFormatter).toHaveBeenCalledWith(
'en-US',
expect.objectContaining({ currency: 'USD' }),
);
});
it('exposes fiatCurrency as USD', () => {
buildState({
preferredCurrency: 'eur',
currencyRates: eurUsdRates,
networkConfigs: ethChainConfig,
});
const { result } = renderHook(() => useAssetFiatFormatter());
expect(result.current.fiatCurrency).toBe('USD');
});
it('returns undefined when usdConversionRate is missing', () => {
buildState({
preferredCurrency: 'eur',
currencyRates: { ETH: { conversionRate: 2000 } },
networkConfigs: ethChainConfig,
});
const { result } = renderHook(() => useAssetFiatFormatter());
const output = result.current.format('100', '0x1');
expect(output).toBeUndefined();
expect(mockFormatter.format).not.toHaveBeenCalled();
});
it('returns undefined when preferred conversionRate is missing', () => {
buildState({
preferredCurrency: 'eur',
currencyRates: { ETH: { usdConversionRate: 2200 } },
networkConfigs: ethChainConfig,
});
const { result } = renderHook(() => useAssetFiatFormatter());
const output = result.current.format('100', '0x1');
expect(output).toBeUndefined();
expect(mockFormatter.format).not.toHaveBeenCalled();
});
it('returns undefined when chain has no EVM network config', () => {
buildState({
preferredCurrency: 'eur',
currencyRates: eurUsdRates,
networkConfigs: {},
});
const { result } = renderHook(() => useAssetFiatFormatter());
const output = result.current.format('100', '0x1');
expect(output).toBeUndefined();
expect(mockFormatter.format).not.toHaveBeenCalled();
});
it('formats zero even when rates or chain config are missing', () => {
buildState({
preferredCurrency: 'eur',
currencyRates: {},
networkConfigs: {},
});
const { result } = renderHook(() => useAssetFiatFormatter());
const output = result.current.format(0, undefined);
expect(output).toBe('formatted:0');
expect(mockGetIntlNumberFormatter).toHaveBeenCalledWith(
'en-US',
expect.objectContaining({ currency: 'USD' }),
);
});
});
describe('pay flow (USD forced, preferred is already USD)', () => {
it('does not re-scale (identity), so numeric value is unchanged', () => {
mockUseTransactionPayCurrency.mockReturnValue('USD');
buildState({
preferredCurrency: 'usd',
currencyRates: {
ETH: { conversionRate: 2200, usdConversionRate: 2200 },
},
networkConfigs: { '0x1': { nativeCurrency: 'ETH' } },
});
renderHook(() => useAssetFiatFormatter()).result.current.format(
'100',
'0x1',
);
expect(mockFormatter.format).toHaveBeenCalledWith('100');
});
it('still formats when currency rates are missing (no conversion needed)', () => {
mockUseTransactionPayCurrency.mockReturnValue('USD');
buildState({
preferredCurrency: 'usd',
currencyRates: {},
networkConfigs: {},
});
const { result } = renderHook(() => useAssetFiatFormatter());
const output = result.current.format('100', '0x1');
expect(output).toBe('formatted:100');
});
});
describe('formatter error fallback', () => {
it('falls back to `${value} ${currency}` when Intl throws', () => {
buildState({ preferredCurrency: 'eur' });
mockGetIntlNumberFormatter.mockImplementation(() => {
throw new Error('boom');
});
const { result } = renderHook(() => useAssetFiatFormatter());
const output = result.current.format('42', '0x1');
expect(output).toBe('42 eur');
});
});
});