Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcramer committed Aug 2, 2024
1 parent 9538ec4 commit f31f486
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
26 changes: 16 additions & 10 deletions src/identity/hooks/useName.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @vitest-environment jsdom
*/
import { renderHook, waitFor } from '@testing-library/react';
import { base, optimism } from 'viem/chains';
import { base, mainnet, optimism } from 'viem/chains';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { Mock } from 'vitest';
import { publicClient } from '../../network/client';
Expand Down Expand Up @@ -31,9 +31,12 @@ describe('useName', () => {
mockGetEnsName.mockResolvedValue(testEnsName);

// Use the renderHook function to create a test harness for the useName hook
const { result } = renderHook(() => useName({ address: testAddress }), {
wrapper: getNewReactQueryTestProvider(),
});
const { result } = renderHook(
() => useName({ address: testAddress, chainId: mainnet.id }),
{
wrapper: getNewReactQueryTestProvider(),
},
);

// Wait for the hook to finish fetching the ENS name
await waitFor(() => {
Expand All @@ -45,9 +48,12 @@ describe('useName', () => {

it('returns the loading state true while still fetching from ens action', async () => {
// Use the renderHook function to create a test harness for the useName hook
const { result } = renderHook(() => useName({ address: testAddress }), {
wrapper: getNewReactQueryTestProvider(),
});
const { result } = renderHook(
() => useName({ address: testAddress, chainId: mainnet.id }),
{
wrapper: getNewReactQueryTestProvider(),
},
);

// Wait for the hook to finish fetching the ENS name
await waitFor(() => {
Expand All @@ -65,7 +71,7 @@ describe('useName', () => {

// Use the renderHook function to create a test harness for the useName hook
const { result } = renderHook(
() => useName({ address: testAddress, chain: base }),
() => useName({ address: testAddress, chainId: base.id }),
{
wrapper: getNewReactQueryTestProvider(),
},
Expand All @@ -89,7 +95,7 @@ describe('useName', () => {

// Use the renderHook function to create a test harness for the useName hook
const { result } = renderHook(
() => useName({ address: testAddress, chain: base }),
() => useName({ address: testAddress, chainId: base.id }),
{
wrapper: getNewReactQueryTestProvider(),
},
Expand All @@ -106,7 +112,7 @@ describe('useName', () => {
it('returns error for unsupported chain ', async () => {
// Use the renderHook function to create a test harness for the useName hook
const { result } = renderHook(
() => useName({ address: testAddress, chain: optimism }),
() => useName({ address: testAddress, chainId: optimism.id }),
{
wrapper: getNewReactQueryTestProvider(),
},
Expand Down
14 changes: 7 additions & 7 deletions src/identity/utils/getName.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ describe('getName', () => {
mockGetEnsName.mockResolvedValue(expectedEnsName);
const name = await getName({ address: walletAddress, chainId: mainnet.id });
expect(name).toBe(expectedEnsName);
expect(getChainPublicClient).toHaveBeenCalledWith(mainnet);
expect(getChainPublicClient).toHaveBeenCalledWith(mainnet.id);
});

it('should return sepolia username', async () => {
const expectedEnsName = 'leo.test.eth';
mockGetEnsName.mockResolvedValue(expectedEnsName);
const name = await getName({ address: walletAddress, chainId: sepolia.id });
expect(name).toBe(expectedEnsName);
expect(getChainPublicClient).toHaveBeenCalledWith(sepolia);
expect(getChainPublicClient).toHaveBeenCalledWith(sepolia.id);
});

it('should return custom testnet chain username', async () => {
Expand All @@ -79,15 +79,15 @@ describe('getName', () => {
chainId: baseSepolia.id,
});
expect(name).toBe(expectedEnsName);
expect(getChainPublicClient).toHaveBeenCalledWith(baseSepolia);
expect(getChainPublicClient).toHaveBeenCalledWith(baseSepolia.id);
});

it('should return custom mainnet username', async () => {
const expectedEnsName = 'leo.custommainnet.eth';
mockReadContract.mockResolvedValue(expectedEnsName);
const name = await getName({ address: walletAddress, chainId: base.id });
expect(name).toBe(expectedEnsName);
expect(getChainPublicClient).toHaveBeenCalledWith(base);
expect(getChainPublicClient).toHaveBeenCalledWith(base.id);
});

it('should return null if user is not registered', async () => {
Expand All @@ -96,7 +96,7 @@ describe('getName', () => {
mockGetEnsName.mockResolvedValue(expectedEnsName);
const name = await getName({ address: walletAddress, chainId: base.id });
expect(name).toBe(expectedEnsName);
expect(getChainPublicClient).toHaveBeenCalledWith(base);
expect(getChainPublicClient).toHaveBeenCalledWith(base.id);
});

it('should default to ENS name if custom chain name is not registered', async () => {
Expand All @@ -106,7 +106,7 @@ describe('getName', () => {
mockGetEnsName.mockResolvedValue(expectedEnsName);
const name = await getName({ address: walletAddress, chainId: base.id });
expect(name).toBe(expectedEnsName);
expect(getChainPublicClient).toHaveBeenCalledWith(base);
expect(getChainPublicClient).toHaveBeenCalledWith(base.id);
});

it('should throw an error on unsupported chain', async () => {
Expand All @@ -123,6 +123,6 @@ describe('getName', () => {
mockGetEnsName.mockResolvedValue(expectedEnsName);
const name = await getName({ address: walletAddress, chainId: base.id });
expect(name).toBe(expectedEnsName);
expect(getChainPublicClient).toHaveBeenCalledWith(base);
expect(getChainPublicClient).toHaveBeenCalledWith(base.id);
});
});
2 changes: 1 addition & 1 deletion src/network/getChainPublicClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('getChainPublicClient', () => {
});

it('should return a public client matching the given chain', async () => {
const publicClient = getChainPublicClient(base);
const publicClient = getChainPublicClient(base.id);
expect(publicClient.chain.id).toBe(base.id);
});
});
8 changes: 4 additions & 4 deletions src/wallet/components/WalletDropdownBaseName.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('WalletDropdownBaseName', () => {
isConnected: true,
});
(useWalletContext as vi.Mock).mockReturnValue({
chain: base,
chainId: base.id,
});
(
useName as vi.Mock<[], Partial<UseQueryResult<string | null, Error>>>
Expand All @@ -49,7 +49,7 @@ describe('WalletDropdownBaseName', () => {
isConnected: true,
});
(useWalletContext as vi.Mock).mockReturnValue({
chain: base,
chainId: base.id,
});
(
useName as vi.Mock<[], Partial<UseQueryResult<string | null, Error>>>
Expand All @@ -71,7 +71,7 @@ describe('WalletDropdownBaseName', () => {
isConnected: true,
});
(useWalletContext as vi.Mock).mockReturnValue({
chain: base,
chainId: base.id,
});
(
useName as vi.Mock<[], Partial<UseQueryResult<string | null, Error>>>
Expand All @@ -95,7 +95,7 @@ describe('WalletDropdownBaseName', () => {
isConnected: false,
});
(useWalletContext as vi.Mock).mockReturnValue({
chain: base,
chainId: base.id,
});

const { container } = render(<WalletDropdownBaseName />);
Expand Down

0 comments on commit f31f486

Please sign in to comment.