-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathuseAssetFiatFormatter.test.ts
More file actions
145 lines (111 loc) · 4.71 KB
/
Copy pathuseAssetFiatFormatter.test.ts
File metadata and controls
145 lines (111 loc) · 4.71 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
import { renderHook } from '@testing-library/react-hooks';
import { useSelector } from 'react-redux';
import { useAssetFiatFormatter } from './useAssetFiatFormatter';
import { useTransactionPayCurrency } from './useTransactionPayCurrency';
import { selectCurrentCurrency } from '../../../../../selectors/currencyRateController';
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(),
}));
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' } = {}) {
mockUseSelector.mockImplementation((selector) => {
if (selector === selectCurrentCurrency) return preferredCurrency;
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('fiatCurrency selection', () => {
it('uses the preferred currency outside pay flow', () => {
buildState({ preferredCurrency: 'eur' });
const { result } = renderHook(() => useAssetFiatFormatter());
expect(result.current.fiatCurrency).toBe('eur');
});
it('uses the pay currency when useTransactionPayCurrency returns a value', () => {
buildState({ preferredCurrency: 'eur' });
mockUseTransactionPayCurrency.mockReturnValue('USD');
const { result } = renderHook(() => useAssetFiatFormatter());
expect(result.current.fiatCurrency).toBe('USD');
});
});
describe('format', () => {
it('formats a numeric amount using Intl currency', () => {
buildState({ preferredCurrency: 'eur' });
const { result } = renderHook(() => useAssetFiatFormatter());
const output = result.current.format('100');
expect(mockGetIntlNumberFormatter).toHaveBeenCalledWith(
'en-US',
expect.objectContaining({ style: 'currency', currency: 'eur' }),
);
expect(mockFormatter.format).toHaveBeenCalledWith('100');
expect(output).toBe('formatted:100');
});
it('formats with the pay currency when in pay flow', () => {
buildState({ preferredCurrency: 'eur' });
mockUseTransactionPayCurrency.mockReturnValue('USD');
const { result } = renderHook(() => useAssetFiatFormatter());
result.current.format('110');
expect(mockGetIntlNumberFormatter).toHaveBeenCalledWith(
'en-US',
expect.objectContaining({ currency: 'USD' }),
);
expect(mockFormatter.format).toHaveBeenCalledWith('110');
});
it('uses minimumFractionDigits=0 for integer amounts', () => {
buildState({ preferredCurrency: 'eur' });
renderHook(() => useAssetFiatFormatter()).result.current.format('100');
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');
expect(mockGetIntlNumberFormatter).toHaveBeenCalledWith(
'en-US',
expect.objectContaining({ minimumFractionDigits: 2 }),
);
});
it('returns undefined when input is undefined', () => {
buildState({ preferredCurrency: 'eur' });
const { result } = renderHook(() => useAssetFiatFormatter());
const output = result.current.format(undefined);
expect(output).toBeUndefined();
expect(mockFormatter.format).not.toHaveBeenCalled();
});
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');
expect(output).toBe('42 eur');
});
});
});