Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAlec committed Sep 5, 2024
1 parent 402135b commit e23fd50
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/transaction/components/TransactionButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { act, fireEvent, render, screen } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { useAccount, useChainId } from 'wagmi';
import { useAccount, useChainId, useConnect } from 'wagmi';
import { useShowCallsStatus } from 'wagmi/experimental';
import { getChainExplorer } from '../../network/getChainExplorer';
import { TransactionButton } from './TransactionButton';
Expand All @@ -13,6 +13,7 @@ vi.mock('./TransactionProvider', () => ({
vi.mock('wagmi', () => ({
useChainId: vi.fn(),
useAccount: vi.fn(),
useConnect: vi.fn(),
}));

vi.mock('wagmi/experimental', () => ({
Expand All @@ -30,6 +31,10 @@ describe('TransactionButton', () => {
(useShowCallsStatus as vi.Mock).mockReturnValue({
showCallsStatus: vi.fn(),
});
(useConnect as vi.Mock).mockReturnValue({
connectAsync: vi.fn(),
connectors: {},
});
});

it('renders correctly', () => {
Expand Down Expand Up @@ -130,6 +135,34 @@ describe('TransactionButton', () => {
expect(button).not.toBeDisabled();
});

it('should prompt for connection when address is missing', async () => {
const onSubmit = vi.fn();
const mockConnectAsync = vi.fn();
const mockConnector = { id: 'test' };
(useAccount as vi.Mock).mockReturnValue({ address: undefined });
(useConnect as vi.Mock).mockReturnValue({
connectAsync: mockConnectAsync,
connectors: [mockConnector],
});
(useTransactionContext as vi.Mock).mockReturnValue({
contracts: {},
isLoading: false,
lifeCycleStatus: { statusName: 'init', statusData: null },
transactionId: undefined,
transactionHash: undefined,
receipt: undefined,
onSubmit,
});
render(<TransactionButton text="Transact" />);
const button = screen.getByText('Transact');
await act(async () => {
fireEvent.click(button);
});
expect(mockConnectAsync).toHaveBeenCalled();
expect(mockConnectAsync).toHaveBeenCalledWith({ connector: mockConnector });
expect(onSubmit).toHaveBeenCalled();
});

it('should open transaction link when only receipt exists', () => {
const onSubmit = vi.fn();
const chainExplorerUrl = 'https://explorer.com';
Expand Down

0 comments on commit e23fd50

Please sign in to comment.