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 7eb02d5 commit 56fbe08
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
42 changes: 40 additions & 2 deletions src/transaction/components/TransactionProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const TestComponent = () => {
<button type="button" onClick={context.onSubmit}>
Submit
</button>
<span data-testid="context-value-errorCode">{context.errorCode}</span>
<span data-testid="context-value-errorMessage">
{context.errorMessage}
</span>
Expand Down Expand Up @@ -172,8 +173,10 @@ describe('TransactionProvider', () => {
const button = screen.getByText('Submit');
fireEvent.click(button);
await waitFor(() => {
const testComponent = screen.getByTestId('context-value-errorMessage');
expect(testComponent.textContent).toBe(
expect(screen.getByTestId('context-value-errorCode').textContent).toBe(
'TmTPc03',
);
expect(screen.getByTestId('context-value-errorMessage').textContent).toBe(
'Something went wrong. Please try again.',
);
});
Expand Down Expand Up @@ -339,6 +342,41 @@ describe('TransactionProvider', () => {
const button = screen.getByText('Submit');
fireEvent.click(button);
await waitFor(() => {
expect(screen.getByTestId('context-value-errorCode').textContent).toBe(
'TmTPc03',
);
expect(screen.getByTestId('context-value-errorMessage').textContent).toBe(
'Something went wrong. Please try again.',
);
});
});

it('should call setLifeCycleStatus when calling fallbackToWriteContract when executeContracts fails', async () => {
const writeContractsAsyncMock = vi
.fn()
.mockRejectedValue(new Error(METHOD_NOT_SUPPORTED_ERROR_SUBSTRING));
const writeContractAsyncMock = vi
.fn()
.mockRejectedValue(new Error('Basic error'));
(useWriteContracts as ReturnType<typeof vi.fn>).mockReturnValue({
statusWriteContracts: 'IDLE',
writeContractsAsync: writeContractsAsyncMock,
});
(useWriteContract as ReturnType<typeof vi.fn>).mockReturnValue({
status: 'IDLE',
writeContractAsync: writeContractAsyncMock,
});
render(
<TransactionProvider address="0x123" contracts={[{}]}>
<TestComponent />
</TransactionProvider>,
);
const button = screen.getByText('Submit');
fireEvent.click(button);
await waitFor(() => {
expect(screen.getByTestId('context-value-errorCode').textContent).toBe(
'TmTPc02',
);
expect(screen.getByTestId('context-value-errorMessage').textContent).toBe(
'Something went wrong. Please try again.',
);
Expand Down
3 changes: 3 additions & 0 deletions src/transaction/components/TransactionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export function TransactionProvider({
const account = useAccount();
const config = useConfig();
const [errorMessage, setErrorMessage] = useState('');
const [errorCode, setErrorCode] = useState('');
const [isToastVisible, setIsToastVisible] = useState(false);
const [lifeCycleStatus, setLifeCycleStatus] = useState<LifeCycleStatus>({
statusName: 'init',
Expand Down Expand Up @@ -96,6 +97,7 @@ export function TransactionProvider({
// Emit Error
if (lifeCycleStatus.statusName === 'error') {
setErrorMessage(lifeCycleStatus.statusData.message);
setErrorCode(lifeCycleStatus.statusData.code);
onError?.(lifeCycleStatus.statusData);
}
// Emit State
Expand Down Expand Up @@ -219,6 +221,7 @@ export function TransactionProvider({
address,
chainId,
contracts,
errorCode,
errorMessage,
hasPaymaster: !!capabilities?.paymasterService?.url,
isLoading: callStatus === 'PENDING',
Expand Down
1 change: 1 addition & 0 deletions src/transaction/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type TransactionContextType = {
address: Address; // The wallet address involved in the transaction.
chainId?: number; // The chainId for the transaction.
contracts: ContractFunctionParameters[]; // An array of contracts for the transaction.
errorCode?: string; // An error code string if the transaction encounters an issue.
errorMessage?: string; // An error message string if the transaction encounters an issue.
hasPaymaster?: boolean; // A boolean indicating if app has paymaster configured
isLoading: boolean; // A boolean indicating if the transaction is currently loading.
Expand Down
6 changes: 3 additions & 3 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.5,
branches: 98.96,
statements: 99.56,
branches: 98.97,
functions: 97.19,
lines: 99.5,
lines: 99.56,
},
},
environment: 'jsdom',
Expand Down

0 comments on commit 56fbe08

Please sign in to comment.