import { describe, it, expect, vi } from 'vitest'; import { getTokenName, getTokenDetails } from './token-detail'; import { Address, PublicActions } from 'viem'; describe('token-detail', () => { const mockClient: PublicActions = { readContract: vi.fn(), multicall: vi.fn(), } as unknown as PublicActions; const mockAddress: Address = '0x1234567890abcdef1234567890abcdef12345678'; describe('getTokenName', () => { it('should return the token name', async () => { const mockName = 'TestToken'; vi.mocked(mockClient.readContract).mockResolvedValue(mockName); const result = await getTokenName(mockClient, mockAddress); expect(mockClient.readContract).toHaveBeenCalledWith({ address: mockAddress, abi: expect.anything(), functionName: 'name', }); expect(result).toBe(mockName); }); }); describe('getTokenDetails', () => { it('should return the token details', async () => { const mockName = { status: 'success', result: 'TestToken' }; const mockSymbol = { status: 'success', result: 'TT' }; const mockDecimals = { status: 'success', result: 18 }; vi.mocked(mockClient.multicall).mockResolvedValue([ mockName, mockSymbol, mockDecimals, ]); const result = await getTokenDetails(mockClient, mockAddress); expect(mockClient.multicall).toHaveBeenCalledWith({ contracts: [ { address: mockAddress, abi: expect.anything(), functionName: 'name', }, { address: mockAddress, abi: expect.anything(), functionName: 'symbol', }, { address: mockAddress, abi: expect.anything(), functionName: 'decimals', }, ], }); expect(result).toEqual({ name: 'TestToken', symbol: 'TT', decimals: 18, }); }); it('should throw an error if any token detail fetch fails', async () => { const mockName = { status: 'success', result: 'TestToken' }; const mockSymbol = { status: 'failure' }; const mockDecimals = { status: 'success', result: 18 }; vi.mocked(mockClient.multicall).mockResolvedValue([ mockName, mockSymbol, mockDecimals, ]); await expect(getTokenDetails(mockClient, mockAddress)).rejects.toThrow( 'Failed to fetch token details' ); }); }); });