Skip to content

Commit

Permalink
add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkas committed Aug 6, 2024
1 parent 7818c9d commit 4c0a980
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
29 changes: 28 additions & 1 deletion src/identity/hooks/useAvatar.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { renderHook, waitFor } from '@testing-library/react';
import { base, baseSepolia, mainnet } from 'viem/chains';
import { base, baseSepolia, mainnet, optimism } from 'viem/chains';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { publicClient } from '../../network/client';
import { getChainPublicClient } from '../../network/getChainPublicClient';
Expand Down Expand Up @@ -107,4 +107,31 @@ describe('useAvatar', () => {

expect(getChainPublicClient).toHaveBeenCalledWith(baseSepolia);
});

it('returns error for unsupported chain ', async () => {
const testEnsName = 'shrek.basetest.eth';
const testEnsAvatar = 'shrektestface';

// Mock the getEnsAvatar method of the publicClient
mockGetEnsAvatar.mockResolvedValue(testEnsAvatar);

// Use the renderHook function to create a test harness for the useAvatar hook
const { result } = renderHook(
() => useAvatar({ ensName: testEnsName, chain: optimism }),
{
wrapper: getNewReactQueryTestProvider(),
},
);

// Wait for the hook to finish fetching the ENS name
await waitFor(() => {
// Check that the ENS name and loading state are correct
expect(result.current.data).toBe(undefined);
expect(result.current.isLoading).toBe(false);
expect(result.current.isError).toBe(true);
expect(result.current.error).toBe(
'ChainId not supported, avatar resolution is only supported on Ethereum and Base.',
);
});
});
});
9 changes: 8 additions & 1 deletion src/identity/utils/getAvatar.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { base, baseSepolia, mainnet } from 'viem/chains';
import { base, baseSepolia, mainnet, optimism } from 'viem/chains';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { publicClient } from '../../network/client';
import { getChainPublicClient } from '../../network/getChainPublicClient';
Expand Down Expand Up @@ -108,4 +108,11 @@ describe('getAvatar', () => {
// getAvatar defaulted to mainnet
expect(getChainPublicClient).toHaveBeenCalledWith(mainnet);
});

it('should throw an error on unsupported chain', async () => {
const ensName = 'shrek.basetest.eth';
await expect(getAvatar({ ensName, chain: optimism })).rejects.toBe(
'ChainId not supported, avatar resolution is only supported on Ethereum and Base.',
);
});
});
14 changes: 12 additions & 2 deletions src/identity/utils/getAvatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { mainnet } from 'viem/chains';
import { normalize } from 'viem/ens';
import type { GetEnsAvatarReturnType } from 'wagmi/actions';
import { isBase } from '../../isBase';
import { isEthereum } from '../../isEthereum';
import { getChainPublicClient } from '../../network/getChainPublicClient';
import { RESOLVER_ADDRESSES_BY_CHAIN_ID } from '../constants';
import type { GetAvatar, GetAvatarReturnType } from '../types';
Expand All @@ -10,10 +11,19 @@ export const getAvatar = async ({
ensName,
chain = mainnet,
}: GetAvatar): Promise<GetAvatarReturnType> => {
const chainIsBase = isBase({ chainId: chain.id });
const chainIsEthereum = isEthereum({ chainId: chain.id });
const chainSupportsUniversalResolver = chainIsEthereum || chainIsBase;

if (!chainSupportsUniversalResolver) {
return Promise.reject(
'ChainId not supported, avatar resolution is only supported on Ethereum and Base.',
);
}

let client = getChainPublicClient(chain);

if (isBase({ chainId: chain.id })) {
client = getChainPublicClient(chain);
if (chainIsBase) {
try {
const baseEnsAvatar = await client.getEnsAvatar({
name: normalize(ensName),
Expand Down

0 comments on commit 4c0a980

Please sign in to comment.