-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add new unit test for findAssetByAddress util
- Loading branch information
Showing
2 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { findAssetByAddress } from './util'; | ||
import { Token } from '@metamask/assets-controllers'; | ||
|
||
describe('findAssetByAddress', () => { | ||
const mockTokens: Record<string, Token[]> = { | ||
'0x1': [ | ||
{ address: '0xabc', decimals: 18, symbol: 'ABC', name: 'Token ABC' }, | ||
{ address: '0xdef', decimals: 18, symbol: 'DEF', name: 'Token DEF' }, | ||
], | ||
'0x2': [ | ||
{ address: '0x123', decimals: 18, symbol: 'XYZ', name: 'Token XYZ' }, | ||
{ address: '0x456', decimals: 18, symbol: 'LMN', name: 'Token LMN' }, | ||
], | ||
}; | ||
|
||
it('should return null and log error when chainId is not provided', () => { | ||
console.error = jest.fn(); | ||
expect(findAssetByAddress(mockTokens, '0xabc')).toBeNull(); | ||
expect(console.error).toHaveBeenCalledWith('Chain ID is required.'); | ||
}); | ||
|
||
it('should return null and log warning when no tokens are found for chainId', () => { | ||
console.warn = jest.fn(); | ||
expect(findAssetByAddress(mockTokens, '0x123', '0x99')).toBeNull(); | ||
expect(console.warn).toHaveBeenCalledWith( | ||
'No tokens found for chainId: 0x99', | ||
); | ||
}); | ||
|
||
it('should return undefined if address is not provided and no token without address is found', () => { | ||
expect(findAssetByAddress(mockTokens, undefined, '0x1')).toBeUndefined(); | ||
}); | ||
|
||
it('should return the token without address if address is not provided and a token without address exists', () => { | ||
const tokensWithNullAddress: Record<string, Token[]> = { | ||
'0x1': [ | ||
{ address: '', decimals: 18, symbol: 'NULL', name: 'Token NULL' }, | ||
], | ||
}; | ||
expect(findAssetByAddress(tokensWithNullAddress, undefined, '0x1')).toEqual( | ||
{ | ||
address: '', | ||
decimals: 18, | ||
symbol: 'NULL', | ||
name: 'Token NULL', | ||
}, | ||
); | ||
}); | ||
|
||
it('should return the correct token when address and chainId are provided', () => { | ||
expect(findAssetByAddress(mockTokens, '0xabc', '0x1')).toEqual({ | ||
address: '0xabc', | ||
decimals: 18, | ||
symbol: 'ABC', | ||
name: 'Token ABC', | ||
}); | ||
}); | ||
|
||
it('should return undefined if no token matches the provided address on the chainId', () => { | ||
expect(findAssetByAddress(mockTokens, '0x999', '0x1')).toBeUndefined(); | ||
}); | ||
|
||
it('should be case insensitive when matching addresses', () => { | ||
expect(findAssetByAddress(mockTokens, '0xABC', '0x1')).toEqual({ | ||
address: '0xabc', | ||
decimals: 18, | ||
symbol: 'ABC', | ||
name: 'Token ABC', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters