Skip to content

Commit

Permalink
chore: Add new unit test for findAssetByAddress util
Browse files Browse the repository at this point in the history
  • Loading branch information
gambinish committed Nov 13, 2024
1 parent f9fa8da commit 1a1737a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
71 changes: 71 additions & 0 deletions ui/pages/asset/util.test.ts
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',
});
});
});
1 change: 0 additions & 1 deletion ui/pages/asset/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export const chainSupportsPricing = (chainId: `0x${string}`) =>
/** The opacity components should set during transition */
export const loadingOpacity = 0.2;

// TODO: Add unit tests
export const findAssetByAddress = (
data: Record<string, Token[]>,
address?: string,
Expand Down

0 comments on commit 1a1737a

Please sign in to comment.