11import type { CaipChainId } from '@metamask/utils' ;
22import {
33 caipChainIdToHex ,
4- formatCompactUSD ,
4+ formatCompactCurrency ,
55 formatMarketStats ,
66 getCaipChainIdFromAssetId ,
77 getNetworkBadgeSource ,
88 getPriceChangeFieldKey ,
99} from './utils' ;
1010import { TimeOption } from '../TrendingTokensBottomSheet/TrendingTokenTimeBottomSheet' ;
1111
12- describe ( 'formatCompactUSD ' , ( ) => {
12+ describe ( 'formatCompactCurrency ' , ( ) => {
1313 describe ( 'Billions formatting' , ( ) => {
1414 it ( 'formats billions with B suffix and no decimals' , ( ) => {
1515 const value = 13000000000 ;
1616
17- const result = formatCompactUSD ( value ) ;
17+ const result = formatCompactCurrency ( value ) ;
1818
1919 expect ( result ) . toBe ( '$13B' ) ;
2020 } ) ;
2121
2222 it ( 'formats large billions correctly' , ( ) => {
2323 const value = 999999999999 ;
2424
25- const result = formatCompactUSD ( value ) ;
25+ const result = formatCompactCurrency ( value ) ;
2626
2727 expect ( result ) . toBe ( '$1000B' ) ;
2828 } ) ;
2929
3030 it ( 'formats billions with rounding' , ( ) => {
3131 const value = 2500000000 ;
3232
33- const result = formatCompactUSD ( value ) ;
33+ const result = formatCompactCurrency ( value ) ;
3434
3535 expect ( result ) . toBe ( '$3B' ) ;
3636 } ) ;
@@ -40,23 +40,23 @@ describe('formatCompactUSD', () => {
4040 it ( 'formats millions with M suffix and one decimal' , ( ) => {
4141 const value = 34200000 ;
4242
43- const result = formatCompactUSD ( value ) ;
43+ const result = formatCompactCurrency ( value ) ;
4444
4545 expect ( result ) . toBe ( '$34.2M' ) ;
4646 } ) ;
4747
4848 it ( 'formats millions with rounding' , ( ) => {
4949 const value = 974248822.2 ;
5050
51- const result = formatCompactUSD ( value ) ;
51+ const result = formatCompactCurrency ( value ) ;
5252
5353 expect ( result ) . toBe ( '$974.2M' ) ;
5454 } ) ;
5555
5656 it ( 'formats large millions correctly' , ( ) => {
5757 const value = 999999999 ;
5858
59- const result = formatCompactUSD ( value ) ;
59+ const result = formatCompactCurrency ( value ) ;
6060
6161 expect ( result ) . toBe ( '$1000.0M' ) ;
6262 } ) ;
@@ -66,23 +66,23 @@ describe('formatCompactUSD', () => {
6666 it ( 'formats thousands with K suffix and one decimal' , ( ) => {
6767 const value = 850500 ;
6868
69- const result = formatCompactUSD ( value ) ;
69+ const result = formatCompactCurrency ( value ) ;
7070
7171 expect ( result ) . toBe ( '$850.5K' ) ;
7272 } ) ;
7373
7474 it ( 'formats thousands with rounding' , ( ) => {
7575 const value = 123456 ;
7676
77- const result = formatCompactUSD ( value ) ;
77+ const result = formatCompactCurrency ( value ) ;
7878
7979 expect ( result ) . toBe ( '$123.5K' ) ;
8080 } ) ;
8181
8282 it ( 'formats large thousands correctly' , ( ) => {
8383 const value = 999999 ;
8484
85- const result = formatCompactUSD ( value ) ;
85+ const result = formatCompactCurrency ( value ) ;
8686
8787 expect ( result ) . toBe ( '$1000.0K' ) ;
8888 } ) ;
@@ -92,23 +92,23 @@ describe('formatCompactUSD', () => {
9292 it ( 'formats values less than 1000 with two decimals' , ( ) => {
9393 const value = 532.5 ;
9494
95- const result = formatCompactUSD ( value ) ;
95+ const result = formatCompactCurrency ( value ) ;
9696
9797 expect ( result ) . toBe ( '$532.50' ) ;
9898 } ) ;
9999
100100 it ( 'formats small decimal values correctly' , ( ) => {
101101 const value = 123.45 ;
102102
103- const result = formatCompactUSD ( value ) ;
103+ const result = formatCompactCurrency ( value ) ;
104104
105105 expect ( result ) . toBe ( '$123.45' ) ;
106106 } ) ;
107107
108108 it ( 'formats very small values correctly' , ( ) => {
109109 const value = 0.01 ;
110110
111- const result = formatCompactUSD ( value ) ;
111+ const result = formatCompactCurrency ( value ) ;
112112
113113 expect ( result ) . toBe ( '$0.01' ) ;
114114 } ) ;
@@ -118,31 +118,31 @@ describe('formatCompactUSD', () => {
118118 it ( 'formats zero correctly' , ( ) => {
119119 const value = 0 ;
120120
121- const result = formatCompactUSD ( value ) ;
121+ const result = formatCompactCurrency ( value ) ;
122122
123123 expect ( result ) . toBe ( '$0.00' ) ;
124124 } ) ;
125125
126126 it ( 'formats exactly 1000 correctly' , ( ) => {
127127 const value = 1000 ;
128128
129- const result = formatCompactUSD ( value ) ;
129+ const result = formatCompactCurrency ( value ) ;
130130
131131 expect ( result ) . toBe ( '$1.0K' ) ;
132132 } ) ;
133133
134134 it ( 'formats exactly 1000000 correctly' , ( ) => {
135135 const value = 1000000 ;
136136
137- const result = formatCompactUSD ( value ) ;
137+ const result = formatCompactCurrency ( value ) ;
138138
139139 expect ( result ) . toBe ( '$1.0M' ) ;
140140 } ) ;
141141
142142 it ( 'formats exactly 1000000000 correctly' , ( ) => {
143143 const value = 1000000000 ;
144144
145- const result = formatCompactUSD ( value ) ;
145+ const result = formatCompactCurrency ( value ) ;
146146
147147 expect ( result ) . toBe ( '$1B' ) ;
148148 } ) ;
@@ -151,15 +151,15 @@ describe('formatCompactUSD', () => {
151151 it ( 'returns Invalid number for NaN' , ( ) => {
152152 const value = NaN ;
153153
154- const result = formatCompactUSD ( value ) ;
154+ const result = formatCompactCurrency ( value ) ;
155155
156156 expect ( result ) . toBe ( 'Invalid number' ) ;
157157 } ) ;
158158
159159 it ( 'returns Invalid number for string that converts to NaN' , ( ) => {
160160 const value = Number ( 'invalid' ) ;
161161
162- const result = formatCompactUSD ( value ) ;
162+ const result = formatCompactCurrency ( value ) ;
163163
164164 expect ( result ) . toBe ( 'Invalid number' ) ;
165165 } ) ;
@@ -169,51 +169,93 @@ describe('formatCompactUSD', () => {
169169 it ( 'formats value just below 1000' , ( ) => {
170170 const value = 999.99 ;
171171
172- const result = formatCompactUSD ( value ) ;
172+ const result = formatCompactCurrency ( value ) ;
173173
174174 expect ( result ) . toBe ( '$999.99' ) ;
175175 } ) ;
176176
177177 it ( 'formats value just above 1000' , ( ) => {
178178 const value = 1000.01 ;
179179
180- const result = formatCompactUSD ( value ) ;
180+ const result = formatCompactCurrency ( value ) ;
181181
182182 expect ( result ) . toBe ( '$1.0K' ) ;
183183 } ) ;
184184
185185 it ( 'formats value just below 1000000' , ( ) => {
186186 const value = 999999.99 ;
187187
188- const result = formatCompactUSD ( value ) ;
188+ const result = formatCompactCurrency ( value ) ;
189189
190190 expect ( result ) . toBe ( '$1000.0K' ) ;
191191 } ) ;
192192
193193 it ( 'formats value just above 1000000' , ( ) => {
194194 const value = 1000000.01 ;
195195
196- const result = formatCompactUSD ( value ) ;
196+ const result = formatCompactCurrency ( value ) ;
197197
198198 expect ( result ) . toBe ( '$1.0M' ) ;
199199 } ) ;
200200
201201 it ( 'formats value just below 1000000000' , ( ) => {
202202 const value = 999999999.99 ;
203203
204- const result = formatCompactUSD ( value ) ;
204+ const result = formatCompactCurrency ( value ) ;
205205
206206 expect ( result ) . toBe ( '$1000.0M' ) ;
207207 } ) ;
208208
209209 it ( 'formats value just above 1000000000' , ( ) => {
210210 const value = 1000000000.01 ;
211211
212- const result = formatCompactUSD ( value ) ;
212+ const result = formatCompactCurrency ( value ) ;
213213
214214 expect ( result ) . toBe ( '$1B' ) ;
215215 } ) ;
216216 } ) ;
217+
218+ describe ( 'Currency parameter' , ( ) => {
219+ it ( 'formats with EUR symbol' , ( ) => {
220+ const value = 13000000000 ;
221+
222+ const result = formatCompactCurrency ( value , 'EUR' ) ;
223+
224+ expect ( result ) . toBe ( '€13B' ) ;
225+ } ) ;
226+
227+ it ( 'formats with JPY symbol' , ( ) => {
228+ const value = 50000000 ;
229+
230+ const result = formatCompactCurrency ( value , 'JPY' ) ;
231+
232+ expect ( result ) . toBe ( '¥50.0M' ) ;
233+ } ) ;
234+
235+ it ( 'formats with lowercase currency code' , ( ) => {
236+ const value = 1500000 ;
237+
238+ const result = formatCompactCurrency ( value , 'eur' ) ;
239+
240+ expect ( result ) . toBe ( '€1.5M' ) ;
241+ } ) ;
242+
243+ it ( 'defaults to USD when no currency provided' , ( ) => {
244+ const value = 1000000 ;
245+
246+ const result = formatCompactCurrency ( value ) ;
247+
248+ expect ( result ) . toBe ( '$1.0M' ) ;
249+ } ) ;
250+
251+ it ( 'uses currency code for unknown currencies' , ( ) => {
252+ const value = 5000000 ;
253+
254+ const result = formatCompactCurrency ( value , 'GBP' ) ;
255+
256+ expect ( result ) . toBe ( 'GBP5.0M' ) ;
257+ } ) ;
258+ } ) ;
217259} ) ;
218260
219261describe ( 'formatMarketStats' , ( ) => {
@@ -372,6 +414,53 @@ describe('formatMarketStats', () => {
372414 expect ( result ) . toBe ( '$0.01 cap • $0.01 vol' ) ;
373415 } ) ;
374416 } ) ;
417+
418+ describe ( 'Currency parameter' , ( ) => {
419+ it ( 'formats with EUR symbol' , ( ) => {
420+ const marketCap = 13000000000 ;
421+ const volume = 50000000 ;
422+
423+ const result = formatMarketStats ( marketCap , volume , 'EUR' ) ;
424+
425+ expect ( result ) . toBe ( '€13B cap • €50.0M vol' ) ;
426+ } ) ;
427+
428+ it ( 'formats with JPY symbol' , ( ) => {
429+ const marketCap = 5000000 ;
430+ const volume = 123400 ;
431+
432+ const result = formatMarketStats ( marketCap , volume , 'JPY' ) ;
433+
434+ expect ( result ) . toBe ( '¥5.0M cap • ¥123.4K vol' ) ;
435+ } ) ;
436+
437+ it ( 'formats with lowercase currency code' , ( ) => {
438+ const marketCap = 1000000 ;
439+ const volume = 500000 ;
440+
441+ const result = formatMarketStats ( marketCap , volume , 'eur' ) ;
442+
443+ expect ( result ) . toBe ( '€1.0M cap • €500.0K vol' ) ;
444+ } ) ;
445+
446+ it ( 'defaults to USD when no currency provided' , ( ) => {
447+ const marketCap = 1000000 ;
448+ const volume = 500000 ;
449+
450+ const result = formatMarketStats ( marketCap , volume ) ;
451+
452+ expect ( result ) . toBe ( '$1.0M cap • $500.0K vol' ) ;
453+ } ) ;
454+
455+ it ( 'handles zero values with EUR' , ( ) => {
456+ const marketCap = 0 ;
457+ const volume = 1000000 ;
458+
459+ const result = formatMarketStats ( marketCap , volume , 'EUR' ) ;
460+
461+ expect ( result ) . toBe ( '- cap • €1.0M vol' ) ;
462+ } ) ;
463+ } ) ;
375464} ) ;
376465
377466describe ( 'getCaipChainIdFromAssetId' , ( ) => {
0 commit comments