From a0e254734a355960e2c7d34f12c5398780ae3534 Mon Sep 17 00:00:00 2001 From: Leonardo Zizzamia Date: Sun, 11 Aug 2024 00:19:33 -0700 Subject: [PATCH] chore: tests (#1031) --- src/identity/utils/getAttestations.test.ts | 22 +++------ src/identity/utils/getAttestations.ts | 14 ++---- .../components/TransactionProvider.test.tsx | 48 ++++++------------- vitest.config.ts | 8 ++-- 4 files changed, 30 insertions(+), 62 deletions(-) diff --git a/src/identity/utils/getAttestations.test.ts b/src/identity/utils/getAttestations.test.ts index cd30b1f84c..5b555db2a1 100644 --- a/src/identity/utils/getAttestations.test.ts +++ b/src/identity/utils/getAttestations.test.ts @@ -5,7 +5,6 @@ import { base, opBNBTestnet } from 'viem/chains'; import { vi } from 'vitest'; import { getAttestationsByFilter } from '../../network/attestations'; import type { GetAttestationsOptions } from '../types'; -import { easSupportedChains } from './easSupportedChains'; import { getAttestations } from './getAttestations'; vi.mock('../../network/attestations'); @@ -31,22 +30,17 @@ describe('getAttestations', () => { vi.clearAllMocks(); }); - it('throws an error for unsupported chains', () => { - try { - getAttestations(mockAddress, opBNBTestnet, mockOptions); - } catch (e) { - expect(e).toHaveProperty( - 'message', - `Chain is not supported. Supported chains: ${Object.keys( - easSupportedChains, - ).join(', ')}`, - ); - } + it('should return and empty array for unsupported chains', async () => { + const result = await getAttestations( + mockAddress, + opBNBTestnet, + mockOptions, + ); + expect(result).toEqual([]); }); it('fetches attestations for supported chains', async () => { (getAttestationsByFilter as vi.Mock).mockResolvedValue(mockAttestations); - const result = await getAttestations(mockAddress, base, mockOptions); expect(result).toEqual(mockAttestations); // Replace [] with expected mockAttestations once implemented }); @@ -66,9 +60,7 @@ describe('getAttestations', () => { (getAttestationsByFilter as vi.Mock).mockRejectedValue( new Error('Network error'), ); - const result = await getAttestations(mockAddress, base); - expect(result).toEqual([]); }); diff --git a/src/identity/utils/getAttestations.ts b/src/identity/utils/getAttestations.ts index c9492bb16d..86a58176bc 100644 --- a/src/identity/utils/getAttestations.ts +++ b/src/identity/utils/getAttestations.ts @@ -1,7 +1,7 @@ import type { Address, Chain } from 'viem'; import { getAttestationsByFilter } from '../../network/attestations'; import type { Attestation, GetAttestationsOptions } from '../types'; -import { easSupportedChains, isChainSupported } from './easSupportedChains'; +import { isChainSupported } from './easSupportedChains'; /** * Fetches Ethereum Attestation Service (EAS) attestations for a given address and chain, @@ -12,22 +12,18 @@ export async function getAttestations( chain: Chain, options?: GetAttestationsOptions, ): Promise { + if (!isChainSupported(chain)) { + console.log('Error in getAttestation: Chain is not supported'); + return []; + } try { - if (!isChainSupported(chain)) { - throw new Error( - `Chain is not supported. Supported chains: ${Object.keys(easSupportedChains).join(', ')}`, - ); - } - // Default query filter values const defaultQueryVariablesFilter = { revoked: false, expirationTime: Math.round(Date.now() / 1000), limit: 10, }; - const queryVariablesFilter = { ...defaultQueryVariablesFilter, ...options }; - return await getAttestationsByFilter(address, chain, queryVariablesFilter); } catch (error) { console.log(`Error in getAttestation: ${(error as Error).message}`); diff --git a/src/transaction/components/TransactionProvider.test.tsx b/src/transaction/components/TransactionProvider.test.tsx index 02fd2b8f0e..b6a2508005 100644 --- a/src/transaction/components/TransactionProvider.test.tsx +++ b/src/transaction/components/TransactionProvider.test.tsx @@ -41,7 +41,12 @@ const TestComponent = () => { - {JSON.stringify(context)} + + {context.errorMessage} + + + {`${context.isToastVisible}`} + ); }; @@ -77,16 +82,13 @@ describe('TransactionProvider', () => { statusWriteContracts: 'IDLE', writeContractsAsync: writeContractsAsyncMock, }); - render( - {}}> + , ); - const button = screen.getByText('Submit'); fireEvent.click(button); - await waitFor(() => { expect(writeContractsAsyncMock).toHaveBeenCalled(); }); @@ -97,25 +99,20 @@ describe('TransactionProvider', () => { (useWaitForTransactionReceipt as ReturnType).mockReturnValue({ data: '123', }); - (useCallsStatus as ReturnType).mockReturnValue({ transactionHash: 'hash', }); - render( {}} onSuccess={onSuccessMock} > , ); - const button = screen.getByText('Submit'); fireEvent.click(button); - await waitFor(() => { expect(onSuccessMock).toHaveBeenCalled(); }); @@ -129,20 +126,16 @@ describe('TransactionProvider', () => { statusWriteContracts: 'IDLE', writeContractsAsync: writeContractsAsyncMock, }); - render( - {}}> + , ); - const button = screen.getByText('Submit'); fireEvent.click(button); - await waitFor(() => { - const testComponent = screen.getByTestId('context-value'); - const updatedContext = JSON.parse(testComponent.textContent || '{}'); - expect(updatedContext.errorMessage).toBe( + const testComponent = screen.getByTestId('context-value-errorMessage'); + expect(testComponent.textContent).toBe( 'Something went wrong. Please try again.', ); }); @@ -153,21 +146,13 @@ describe('TransactionProvider', () => { (useSwitchChain as ReturnType).mockReturnValue({ switchChainAsync: switchChainAsyncMock, }); - render( - {}} - > + , ); - const button = screen.getByText('Submit'); fireEvent.click(button); - await waitFor(() => { expect(switchChainAsyncMock).toHaveBeenCalled(); }); @@ -178,20 +163,16 @@ describe('TransactionProvider', () => { statusWriteContracts: 'IDLE', writeContractsAsync: vi.fn().mockRejectedValue(new Error('Test error')), }); - render( - {}}> + , ); - const button = screen.getByText('Submit'); fireEvent.click(button); - await waitFor(() => { - const testComponent = screen.getByTestId('context-value'); - const updatedContext = JSON.parse(testComponent.textContent || '{}'); - expect(updatedContext.isToastVisible).toBe(true); + const testComponent = screen.getByTestId('context-value-isToastVisible'); + expect(testComponent.textContent).toBe('true'); }); }); }); @@ -202,7 +183,6 @@ describe('useTransactionContext', () => { useTransactionContext(); return null; }; - const consoleError = vi .spyOn(console, 'error') .mockImplementation(() => {}); // Suppress error logging diff --git a/vitest.config.ts b/vitest.config.ts index 03ec0a5704..594da7377f 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -22,10 +22,10 @@ export default defineConfig({ ], reportOnFailure: true, thresholds: { - statements: 99.28, - branches: 98.27, - functions: 94.47, - lines: 99.28, + statements: 99.33, + branches: 98.44, + functions: 96.69, + lines: 99.33, }, }, environment: 'jsdom',