Skip to content

Commit

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

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;

(useSendTransactionWagmi as ReturnType<typeof vi.fn>).mockImplementation(
({ mutation }: useSendCallConfig) => {
onErrorCallback = mutation.onError;
return {
sendTransactionAsync: vi.fn(),
status: 'success',
} as MockuseSendCallReturn;
},
);

renderHook(() =>
useSendCall({
setErrorMessage: mockSetErrorMessage,
setTransactionHashArray: mockSetTransactionHashArray,
onError: mockOnError,
}),
);

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

expect(mockSetErrorMessage).toHaveBeenCalledWith('Request denied.');
expect(mockOnError).toHaveBeenCalledWith({
code: 'SEND_CALL_ERROR',
error: 'User rejected request',
});
});
});
66 changes: 66 additions & 0 deletions src/transaction/hooks/useSendCalls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,70 @@ describe('useSendCalls', () => {
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;

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

renderHook(() =>
useSendCalls({
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;

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

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

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

expect(mockSetErrorMessage).toHaveBeenCalledWith('Request denied.');
expect(mockOnError).toHaveBeenCalledWith({
code: 'SEND_CALLS_ERROR',
error: 'User rejected request',
});
});
});
6 changes: 1 addition & 5 deletions src/transaction/hooks/useSendCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ export function useSendCalls({
try {
const { status, sendCallsAsync } = useSendCallsWagmi({
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 All @@ -54,8 +52,6 @@ export function useSendCalls({
});
return { status, sendCallsAsync };
} catch (err) {
process.stdout.write('useSendCallsError\n');

onError?.({
code: UNCAUGHT_SEND_CALLS_ERROR_CODE,
error: JSON.stringify(err),
Expand Down

0 comments on commit 4e75320

Please sign in to comment.