1+ import { Asset } from '@metamask/assets-controllers' ;
2+ import { Hex } from '@metamask/utils' ;
13import { renderHookWithProvider } from '../../../../util/test/renderWithProvider' ;
24import { useTokenBalance } from './useTokenBalance' ;
35import { TokenI } from '../../Tokens/types' ;
@@ -8,6 +10,24 @@ import {
810} from '../../../../selectors/assets/assets-list' ;
911import { createStakedTrxAsset } from '../../AssetOverview/utils/createStakedTrxAsset' ;
1012
13+ const TRON_MAINNET_CHAIN_ID = 'tron:728126428' ;
14+
15+ const createMockTronAsset = ( symbol : string , balance : string ) : Asset =>
16+ ( {
17+ accountType : 'tron:eoa' ,
18+ assetId : `${ TRON_MAINNET_CHAIN_ID } /slip44:${ symbol } ` ,
19+ chainId : TRON_MAINNET_CHAIN_ID ,
20+ accountId : 'mock-account-id' ,
21+ image : '' ,
22+ name : symbol ,
23+ symbol,
24+ decimals : 6 ,
25+ isNative : false ,
26+ rawBalance : '0x0' as Hex ,
27+ balance,
28+ fiat : { balance : 0 , currency : 'usd' , conversionRate : 1 } ,
29+ } ) as Asset ;
30+
1131const createEmptySpecialAssetsMap = ( ) : TronSpecialAssetsMap => ( {
1232 energy : undefined ,
1333 bandwidth : undefined ,
@@ -74,7 +94,6 @@ describe('useTokenBalance', () => {
7494
7595 const { result } = renderHookWithProvider ( ( ) => useTokenBalance ( token ) ) ;
7696
77- // Address is normalized to checksum format for consistent lookup
7897 expect ( mockSelectAsset ) . toHaveBeenCalledWith ( expect . any ( Object ) , {
7998 address : '0x6B175474E89094C44Da98b954EedeAC495271d0F' ,
8099 chainId : token . chainId ,
@@ -111,7 +130,7 @@ describe('useTokenBalance', () => {
111130 it ( 'returns staked TRX asset for Tron native token' , ( ) => {
112131 const tronToken = {
113132 address : '' ,
114- chainId : 'tron:0x2b6653dc' ,
133+ chainId : TRON_MAINNET_CHAIN_ID ,
115134 ticker : 'TRX' ,
116135 symbol : 'TRX' ,
117136 } as TokenI ;
@@ -126,9 +145,9 @@ describe('useTokenBalance', () => {
126145
127146 mockSelectTronResources . mockReturnValue ( {
128147 ...createEmptySpecialAssetsMap ( ) ,
129- stakedTrxForEnergy : { symbol : 'strx-energy' , balance : '100' } ,
130- stakedTrxForBandwidth : { symbol : 'strx-bandwidth' , balance : '200' } ,
131- } as TronSpecialAssetsMap ) ;
148+ stakedTrxForEnergy : createMockTronAsset ( 'strx-energy' , '100' ) ,
149+ stakedTrxForBandwidth : createMockTronAsset ( 'strx-bandwidth' , '200' ) ,
150+ } ) ;
132151
133152 mockCreateStakedTrxAsset . mockReturnValue ( mockStakedAsset ) ;
134153
@@ -150,4 +169,124 @@ describe('useTokenBalance', () => {
150169 '200' ,
151170 ) ;
152171 } ) ;
172+
173+ it ( 'returns in-lock-period balance for Tron native token' , ( ) => {
174+ const tronToken = {
175+ address : '' ,
176+ chainId : TRON_MAINNET_CHAIN_ID ,
177+ ticker : 'TRX' ,
178+ symbol : 'TRX' ,
179+ } as TokenI ;
180+
181+ mockSelectAsset . mockReturnValue ( {
182+ balance : '1000' ,
183+ balanceFiat : '$100.00' ,
184+ symbol : 'TRX' ,
185+ } as TokenI ) ;
186+
187+ mockSelectTronResources . mockReturnValue ( {
188+ ...createEmptySpecialAssetsMap ( ) ,
189+ trxInLockPeriod : createMockTronAsset ( 'trx-in-lock-period' , '20' ) ,
190+ } ) ;
191+
192+ const { result } = renderHookWithProvider ( ( ) => useTokenBalance ( tronToken ) ) ;
193+
194+ expect ( result . current . inLockPeriodBalance ) . toBe ( '20' ) ;
195+ } ) ;
196+
197+ it ( 'returns undefined for in-lock-period when balance is zero' , ( ) => {
198+ const tronToken = {
199+ address : '' ,
200+ chainId : TRON_MAINNET_CHAIN_ID ,
201+ ticker : 'TRX' ,
202+ symbol : 'TRX' ,
203+ } as TokenI ;
204+
205+ mockSelectAsset . mockReturnValue ( {
206+ balance : '1000' ,
207+ balanceFiat : '$100.00' ,
208+ symbol : 'TRX' ,
209+ } as TokenI ) ;
210+
211+ mockSelectTronResources . mockReturnValue ( {
212+ ...createEmptySpecialAssetsMap ( ) ,
213+ trxInLockPeriod : createMockTronAsset ( 'trx-in-lock-period' , '0' ) ,
214+ } ) ;
215+
216+ const { result } = renderHookWithProvider ( ( ) => useTokenBalance ( tronToken ) ) ;
217+
218+ expect ( result . current . inLockPeriodBalance ) . toBeUndefined ( ) ;
219+ } ) ;
220+
221+ it ( 'returns undefined for in-lock-period when balance is non-numeric' , ( ) => {
222+ const tronToken = {
223+ address : '' ,
224+ chainId : TRON_MAINNET_CHAIN_ID ,
225+ ticker : 'TRX' ,
226+ symbol : 'TRX' ,
227+ } as TokenI ;
228+
229+ mockSelectAsset . mockReturnValue ( {
230+ balance : '1000' ,
231+ balanceFiat : '$100.00' ,
232+ symbol : 'TRX' ,
233+ } as TokenI ) ;
234+
235+ mockSelectTronResources . mockReturnValue ( {
236+ ...createEmptySpecialAssetsMap ( ) ,
237+ trxInLockPeriod : createMockTronAsset (
238+ 'trx-in-lock-period' ,
239+ 'not-a-number' ,
240+ ) ,
241+ } ) ;
242+
243+ const { result } = renderHookWithProvider ( ( ) => useTokenBalance ( tronToken ) ) ;
244+
245+ expect ( result . current . inLockPeriodBalance ) . toBeUndefined ( ) ;
246+ } ) ;
247+
248+ it ( 'returns undefined for in-lock-period when balance is empty string' , ( ) => {
249+ const tronToken = {
250+ address : '' ,
251+ chainId : TRON_MAINNET_CHAIN_ID ,
252+ ticker : 'TRX' ,
253+ symbol : 'TRX' ,
254+ } as TokenI ;
255+
256+ mockSelectAsset . mockReturnValue ( {
257+ balance : '1000' ,
258+ balanceFiat : '$100.00' ,
259+ symbol : 'TRX' ,
260+ } as TokenI ) ;
261+
262+ mockSelectTronResources . mockReturnValue ( {
263+ ...createEmptySpecialAssetsMap ( ) ,
264+ trxInLockPeriod : createMockTronAsset ( 'trx-in-lock-period' , '' ) ,
265+ } ) ;
266+
267+ const { result } = renderHookWithProvider ( ( ) => useTokenBalance ( tronToken ) ) ;
268+
269+ expect ( result . current . inLockPeriodBalance ) . toBeUndefined ( ) ;
270+ } ) ;
271+
272+ it ( 'returns undefined for in-lock-period when resources are not available' , ( ) => {
273+ const tronToken = {
274+ address : '' ,
275+ chainId : TRON_MAINNET_CHAIN_ID ,
276+ ticker : 'TRX' ,
277+ symbol : 'TRX' ,
278+ } as TokenI ;
279+
280+ mockSelectAsset . mockReturnValue ( {
281+ balance : '1000' ,
282+ balanceFiat : '$100.00' ,
283+ symbol : 'TRX' ,
284+ } as TokenI ) ;
285+
286+ mockSelectTronResources . mockReturnValue ( createEmptySpecialAssetsMap ( ) ) ;
287+
288+ const { result } = renderHookWithProvider ( ( ) => useTokenBalance ( tronToken ) ) ;
289+
290+ expect ( result . current . inLockPeriodBalance ) . toBeUndefined ( ) ;
291+ } ) ;
153292} ) ;
0 commit comments