Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zizzamia committed Aug 14, 2024
1 parent 3c29bde commit b9593d4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
41 changes: 39 additions & 2 deletions src/transaction/hooks/useWriteContracts.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { renderHook } from '@testing-library/react';
import type { TransactionExecutionError } from 'viem';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { useWriteContracts as useWriteContractsWagmi } from 'wagmi/experimental';
import { useWriteContracts } from './useWriteContracts';
Expand Down Expand Up @@ -38,13 +39,49 @@ describe('useWriteContracts', () => {
expect(mockSetLifeCycleStatus).toHaveBeenCalledWith({
statusName: 'error',
statusData: {
code: 'WRITE_CONTRACTS_ERROR',
code: 'TmUWCh01',
error: 'Something went wrong. Please try again.',
message: 'Something went wrong. Please try again.',
},
});
});

it('should handle userRejectedRequestError', () => {
let onErrorCallback:
| ((error: TransactionExecutionError) => void)
| undefined;
(useWriteContractsWagmi as ReturnType<typeof vi.fn>).mockImplementation(
({ mutation }: UseWriteContractsConfig) => {
onErrorCallback = mutation.onError;
return {
writeContracts: vi.fn(),
status: 'error',
};
},
);
renderHook(() =>
useWriteContracts({
setLifeCycleStatus: mockSetLifeCycleStatus,
setTransactionId: mockSetTransactionId,
}),
);
expect(onErrorCallback).toBeDefined();
onErrorCallback?.({
cause: {
name: 'UserRejectedRequestError',
},
message: 'Request denied.',
});
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 Down Expand Up @@ -86,7 +123,7 @@ describe('useWriteContracts', () => {
expect(mockSetLifeCycleStatus).toHaveBeenCalledWith({
statusName: 'error',
statusData: {
code: 'UNCAUGHT_WRITE_WRITE_CONTRACTS_ERROR',
code: 'TmUWCh02',
error: JSON.stringify(uncaughtError),
message: 'Something went wrong. Please try again.',
},
Expand Down
6 changes: 2 additions & 4 deletions src/transaction/hooks/useWriteContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { useWriteContracts as useWriteContractsWagmi } from 'wagmi/experimental'
import {
GENERIC_ERROR_MESSAGE,
METHOD_NOT_SUPPORTED_ERROR_SUBSTRING,
UNCAUGHT_WRITE_CONTRACTS_ERROR_CODE,
WRITE_CONTRACTS_ERROR_CODE,
} from '../constants';
import type { UseWriteContractsParams } from '../types';
import { isUserRejectedRequestError } from '../utils/isUserRejectedRequestError';
Expand Down Expand Up @@ -35,7 +33,7 @@ export function useWriteContracts({
setLifeCycleStatus({
statusName: 'error',
statusData: {
code: WRITE_CONTRACTS_ERROR_CODE,
code: 'TmUWCh01', // Transaction module UseWriteContracts hook 01 error
error: e.message,
message: errorMessage,
},
Expand All @@ -51,7 +49,7 @@ export function useWriteContracts({
setLifeCycleStatus({
statusName: 'error',
statusData: {
code: UNCAUGHT_WRITE_CONTRACTS_ERROR_CODE,
code: 'TmUWCh02',
error: JSON.stringify(err),
message: GENERIC_ERROR_MESSAGE,
},
Expand Down
8 changes: 4 additions & 4 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export default defineConfig({
],
reportOnFailure: true,
thresholds: {
statements: 99.43,
branches: 98.63,
functions: 96.72,
lines: 99.43,
statements: 99.47,
branches: 98.79,
functions: 96.74,
lines: 99.47,
},
},
environment: 'jsdom',
Expand Down

0 comments on commit b9593d4

Please sign in to comment.