|
| 1 | +import type { Chain } from 'viem' |
| 2 | +import { describe, expect, it } from 'vitest' |
| 3 | +import { getExplorerLink } from './getExplorerLink' |
| 4 | + |
| 5 | +const chain: Chain = { |
| 6 | + id: 1, |
| 7 | + name: 'Mock Chain', |
| 8 | + nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 }, |
| 9 | + rpcUrls: { default: { http: ['https://mock.rpc.url'] } }, |
| 10 | + blockExplorers: { |
| 11 | + default: { name: 'MockExplorer', url: 'https://mock.explorer.url' }, |
| 12 | + }, |
| 13 | +} as Chain |
| 14 | + |
| 15 | +const address = '0x71C7656EC7ab88b098defB751B7401B5f6d8976F' as const |
| 16 | +const txHash = '0xd85ef8c70dc31a4f8d5bf0331e1eac886935905f15d32e71b348df745cd38e19' as const |
| 17 | + |
| 18 | +describe('getExplorerLink', () => { |
| 19 | + it('returns address URL using chain block explorer', () => { |
| 20 | + expect(getExplorerLink({ chain, hashOrAddress: address })).toBe( |
| 21 | + `https://mock.explorer.url/address/${address}`, |
| 22 | + ) |
| 23 | + }) |
| 24 | + |
| 25 | + it('returns tx URL using chain block explorer for a hash', () => { |
| 26 | + expect(getExplorerLink({ chain, hashOrAddress: txHash })).toBe( |
| 27 | + `https://mock.explorer.url/tx/${txHash}`, |
| 28 | + ) |
| 29 | + }) |
| 30 | + |
| 31 | + it('uses custom explorerUrl when provided', () => { |
| 32 | + const explorerUrl = 'https://custom.explorer.io' |
| 33 | + expect(getExplorerLink({ chain, hashOrAddress: address, explorerUrl })).toBe( |
| 34 | + `${explorerUrl}/address/${address}`, |
| 35 | + ) |
| 36 | + }) |
| 37 | + |
| 38 | + it('throws when chain has no block explorer and no explorerUrl is provided', () => { |
| 39 | + const chainWithoutExplorer: Chain = { ...chain, blockExplorers: undefined } |
| 40 | + expect(() => getExplorerLink({ chain: chainWithoutExplorer, hashOrAddress: address })).toThrow( |
| 41 | + 'No block explorer URL available for this chain', |
| 42 | + ) |
| 43 | + }) |
| 44 | + |
| 45 | + it('throws for an invalid hash or address', () => { |
| 46 | + expect(() => |
| 47 | + // biome-ignore lint/suspicious/noExplicitAny: intentionally testing invalid input |
| 48 | + getExplorerLink({ chain, hashOrAddress: 'not-valid' as any }), |
| 49 | + ).toThrow('Invalid hash or address') |
| 50 | + }) |
| 51 | +}) |
0 commit comments