Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add test coverage and remove console.log statement #1048

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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