Skip to content

Commit

Permalink
more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAlec committed Aug 13, 2024
1 parent 4e75320 commit f6dcc66
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
66 changes: 66 additions & 0 deletions src/transaction/hooks/useWriteContracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,70 @@ describe('useWriteContracts', () => {
error: JSON.stringify(uncaughtError),
});
});

it('should ignore EOA-specific errors', () => {
const eoaError = new Error('Error: this request method is not supported');

let onErrorCallback: ((error: Error) => void) | undefined;

(useWriteContractsWagmi as ReturnType<typeof vi.fn>).mockImplementation(
({ mutation }) => {
onErrorCallback = mutation.onError;
return {
writeContracts: vi.fn(),
status: 'error',
};
},
);

renderHook(() =>
useWriteContracts({
setErrorMessage: mockSetErrorMessage,
setTransactionId: mockSetTransactionId,
onError: mockOnError,
}),
);

expect(onErrorCallback).toBeDefined();
onErrorCallback?.(eoaError);

expect(mockSetErrorMessage).not.toHaveBeenCalled();
expect(mockOnError).not.toHaveBeenCalled();
});

it('should handle user rejected request errors', () => {
const userRejectedError = new Error('User rejected request') as Error & {
cause: { name: string };
};
userRejectedError.cause = { name: 'UserRejectedRequestError' };

let onErrorCallback: ((error: Error) => void) | undefined;

(useWriteContractsWagmi as ReturnType<typeof vi.fn>).mockImplementation(
({ mutation }) => {
onErrorCallback = mutation.onError;
return {
writeContracts: vi.fn(),
status: 'error',
};
},
);

renderHook(() =>
useWriteContracts({
setErrorMessage: mockSetErrorMessage,
setTransactionId: mockSetTransactionId,
onError: mockOnError,
}),
);

expect(onErrorCallback).toBeDefined();
onErrorCallback?.(userRejectedError);

expect(mockSetErrorMessage).toHaveBeenCalledWith('Request denied.');
expect(mockOnError).toHaveBeenCalledWith({
code: 'WRITE_CONTRACTS_ERROR',
error: 'User rejected request',
});
});
});
4 changes: 1 addition & 3 deletions src/transaction/hooks/useWriteContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ export function useWriteContracts({
try {
const { status, writeContractsAsync } = useWriteContractsWagmi({
mutation: {
onSettled(data, error, variables, context) {
console.log('settled', data, error, variables, context);
},
onSettled() {},
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 f6dcc66

Please sign in to comment.