@@ -8,17 +8,14 @@ import {
88} from './caip.js' ;
99
1010jest . mock ( 'eth-chainlist' , ( ) => ( {
11- ...jest . requireActual ( 'eth-chainlist' ) ,
1211 getChainById : jest . fn ( ) ,
1312} ) ) ;
1413
1514const mockGetChainById = jest . mocked ( getChainById ) ;
1615
1716describe ( 'caip helpers' , ( ) => {
1817 beforeEach ( ( ) => {
19- mockGetChainById . mockImplementation (
20- jest . requireActual ( 'eth-chainlist' ) . getChainById ,
21- ) ;
18+ mockGetChainById . mockReset ( ) ;
2219 } ) ;
2320
2421 describe ( 'formatChainIdToCaip' , ( ) => {
@@ -131,15 +128,24 @@ describe('caip helpers', () => {
131128 } ) ;
132129
133130 describe ( 'getNativeAsset' , ( ) => {
134- it ( 'resolves mainnet native asset from chainlist slip44' , ( ) => {
131+ it ( 'resolves native asset from chainlist slip44' , ( ) => {
132+ mockGetChainById . mockReturnValue ( {
133+ slip44 : 60 ,
134+ nativeCurrency : { name : 'Ether' , symbol : 'ETH' , decimals : 18 } ,
135+ } as ReturnType < typeof getChainById > ) ;
136+
135137 expect ( getNativeAsset ( 'eip155:1' ) ) . toStrictEqual ( {
136138 symbol : 'ETH' ,
137139 decimals : 18 ,
138140 assetId : 'eip155:1/slip44:60' ,
139141 } ) ;
140142 } ) ;
141143
142- it ( 'resolves arbitrum native asset via symbol fallback when chainlist omits slip44' , ( ) => {
144+ it ( 'falls back to symbol lookup when chainlist omits slip44' , ( ) => {
145+ mockGetChainById . mockReturnValue ( {
146+ nativeCurrency : { name : 'Ether' , symbol : 'ETH' , decimals : 18 } ,
147+ } as ReturnType < typeof getChainById > ) ;
148+
143149 expect ( getNativeAsset ( 'eip155:42161' ) ) . toStrictEqual ( {
144150 symbol : 'ETH' ,
145151 decimals : 18 ,
@@ -148,6 +154,11 @@ describe('caip helpers', () => {
148154 } ) ;
149155
150156 it ( 'ignores chainlist testnet slip44:1 and uses the native symbol coin type' , ( ) => {
157+ mockGetChainById . mockReturnValue ( {
158+ slip44 : 1 ,
159+ nativeCurrency : { name : 'Ether' , symbol : 'ETH' , decimals : 18 } ,
160+ } as ReturnType < typeof getChainById > ) ;
161+
151162 expect ( getNativeAsset ( 'eip155:11155111' ) ) . toStrictEqual ( {
152163 symbol : 'ETH' ,
153164 decimals : 18 ,
@@ -156,21 +167,29 @@ describe('caip helpers', () => {
156167 } ) ;
157168
158169 it ( 'prefers chainlist slip44 over the slip44 registry symbol mapping' , ( ) => {
170+ mockGetChainById . mockReturnValue ( {
171+ slip44 : 9005 ,
172+ nativeCurrency : { name : 'Avalanche' , symbol : 'AVAX' , decimals : 18 } ,
173+ } as ReturnType < typeof getChainById > ) ;
174+
159175 expect ( getNativeAsset ( 'eip155:43114' ) ) . toStrictEqual ( {
160176 symbol : 'AVAX' ,
161177 decimals : 18 ,
162178 assetId : 'eip155:43114/slip44:9005' ,
163179 } ) ;
164180 } ) ;
165181
166- it ( 'returns undefined for unknown chains' , ( ) => {
182+ it ( 'returns undefined when the chain is unknown' , ( ) => {
183+ mockGetChainById . mockReturnValue ( undefined ) ;
184+
167185 expect ( getNativeAsset ( 'eip155:999999991' ) ) . toBeUndefined ( ) ;
168186 } ) ;
169187
170188 it ( 'returns undefined for non-eip155 chain ids' , ( ) => {
171189 expect (
172190 getNativeAsset ( 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp' ) ,
173191 ) . toBeUndefined ( ) ;
192+ expect ( mockGetChainById ) . not . toHaveBeenCalled ( ) ;
174193 } ) ;
175194
176195 it ( 'returns undefined when chainlist omits native currency symbol' , ( ) => {
@@ -182,7 +201,28 @@ describe('caip helpers', () => {
182201 expect ( getNativeAsset ( 'eip155:1' ) ) . toBeUndefined ( ) ;
183202 } ) ;
184203
204+ it ( 'defaults decimals when chainlist omits native currency decimals' , ( ) => {
205+ mockGetChainById . mockReturnValue ( {
206+ slip44 : 60 ,
207+ nativeCurrency : { name : 'Ether' , symbol : 'ETH' } ,
208+ } as ReturnType < typeof getChainById > ) ;
209+
210+ expect ( getNativeAsset ( 'eip155:1' ) ) . toStrictEqual ( {
211+ symbol : 'ETH' ,
212+ decimals : 18 ,
213+ assetId : 'eip155:1/slip44:60' ,
214+ } ) ;
215+ } ) ;
216+
185217 it ( 'returns undefined when slip44 and symbol lookup both fail' , ( ) => {
218+ mockGetChainById . mockReturnValue ( {
219+ nativeCurrency : {
220+ name : 'Unknown' ,
221+ symbol : 'NOTACOIN' ,
222+ decimals : 18 ,
223+ } ,
224+ } as ReturnType < typeof getChainById > ) ;
225+
186226 expect ( getNativeAsset ( 'eip155:1088' ) ) . toBeUndefined ( ) ;
187227 } ) ;
188228 } ) ;
0 commit comments