Skip to content

Commit

Permalink
chore: added test coverage and remove console.log statement (#1048)
Browse files Browse the repository at this point in the history
Co-authored-by: Alissa Crane <[email protected]>
  • Loading branch information
abcrane123 and alissacrane-cb authored Aug 14, 2024
1 parent 00fd3af commit b222427
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
65 changes: 65 additions & 0 deletions src/transaction/hooks/useWriteContract.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { renderHook } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { useWriteContract as useWriteContractWagmi } from 'wagmi';
import { isUserRejectedRequestError } from '../utils/isUserRejectedRequestError';
import { useWriteContract } from './useWriteContract';

vi.mock('wagmi', () => ({
useWriteContract: vi.fn(),
}));

vi.mock('../utils/isUserRejectedRequestError', () => ({
isUserRejectedRequestError: vi.fn(),
}));

type UseWriteContractConfig = {
mutation: {
onError: (error: Error) => void;
Expand Down Expand Up @@ -78,6 +83,38 @@ describe('useWriteContract', () => {
});
});

it('should handle user rejected error', () => {
const useRejectedError = new Error('Request denied.');
let onErrorCallback: ((error: Error) => void) | undefined;
(useWriteContractWagmi as ReturnType<typeof vi.fn>).mockImplementation(
({ mutation }: UseWriteContractConfig) => {
onErrorCallback = mutation.onError;
return {
writeContractAsync: vi.fn(),
data: null,
status: 'error',
} as MockUseWriteContractReturn;
},
);
(isUserRejectedRequestError as vi.Mock).mockReturnValue(true);
renderHook(() =>
useWriteContract({
setLifeCycleStatus: mockSetLifeCycleStatus,
setTransactionHashArray: mockSetTransactionHashArray,
}),
);
expect(onErrorCallback).toBeDefined();
onErrorCallback?.(useRejectedError);
expect(mockSetLifeCycleStatus).toHaveBeenCalledWith({
statusName: 'error',
statusData: {
code: 'TmUWCh01',
error: 'Request denied.',
message: 'Request denied.',
},
});
});

it('should handle successful transaction', () => {
const transactionId = '0x123';
let onSuccessCallback: ((id: string) => void) | undefined;
Expand All @@ -102,6 +139,34 @@ describe('useWriteContract', () => {
expect(mockSetTransactionHashArray).toHaveBeenCalledWith([transactionId]);
});

it('should handle multiple successful transactions', () => {
const transactionId = '0x123';
let onSuccessCallback: ((id: string) => void) | undefined;
(useWriteContractWagmi as ReturnType<typeof vi.fn>).mockImplementation(
({ mutation }: UseWriteContractConfig) => {
onSuccessCallback = mutation.onSuccess;
return {
writeContractAsync: vi.fn(),
data: transactionId,
status: 'success',
} as MockUseWriteContractReturn;
},
);
renderHook(() =>
useWriteContract({
setLifeCycleStatus: mockSetLifeCycleStatus,
setTransactionHashArray: mockSetTransactionHashArray,
transactionHashArray: ['0x1234'],
}),
);
expect(onSuccessCallback).toBeDefined();
onSuccessCallback?.(transactionId);
expect(mockSetTransactionHashArray).toHaveBeenCalledWith([
'0x1234',
transactionId,
]);
});

it('should handle uncaught errors', () => {
const uncaughtError = new Error('Uncaught error');
(useWriteContractWagmi as ReturnType<typeof vi.fn>).mockImplementation(
Expand Down
30 changes: 30 additions & 0 deletions src/transaction/hooks/useWriteContracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,36 @@ describe('useWriteContracts', () => {
});
});

it('should handle EOA specific error to fallback to writeContract', () => {
let onErrorCallback:
| ((error: TransactionExecutionError) => void)
| undefined;
(useWriteContractsWagmi as ReturnType<typeof vi.fn>).mockImplementation(
({ mutation }: UseWriteContractsConfig) => {
onErrorCallback = mutation.onError;
return {
writeContracts: vi.fn(),
status: 'error',
message: 'this request method is not supported',
};
},
);
renderHook(() =>
useWriteContracts({
setLifeCycleStatus: mockSetLifeCycleStatus,
setTransactionId: mockSetTransactionId,
}),
);
expect(onErrorCallback).toBeDefined();
onErrorCallback?.({
cause: {
name: 'eoa-error',
},
message: 'this request method is not supported',
});
expect(mockSetLifeCycleStatus).not.toHaveBeenCalled();
});

it('should handle successful transaction', () => {
const transactionId = '0x123';
let onSuccessCallback: ((id: string) => void) | undefined;
Expand Down
3 changes: 0 additions & 3 deletions src/transaction/hooks/useWriteContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ export function useWriteContracts({
try {
const { status, writeContractsAsync } = useWriteContractsWagmi({
mutation: {
onSettled(data, error, variables, context) {
console.log('settled', data, error, variables, context);
},
onError: (e) => {
// Ignore EOA-specific error to fallback to writeContract
if (e.message.includes(METHOD_NOT_SUPPORTED_ERROR_SUBSTRING)) {
Expand Down

0 comments on commit b222427

Please sign in to comment.