Skip to content

Commit

Permalink
add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
alissacrane-cb committed Aug 15, 2024
1 parent 9dd6143 commit 9a1a45a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/transaction/components/TransactionButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useChainId } from 'wagmi';
import { useShowCallsStatus } from 'wagmi/experimental';
import { TransactionButton } from './TransactionButton';
import { useTransactionContext } from './TransactionProvider';
import { getChainExplorer } from '../../network/getChainExplorer';

vi.mock('./TransactionProvider', () => ({
useTransactionContext: vi.fn(),
Expand All @@ -17,6 +18,10 @@ vi.mock('wagmi/experimental', () => ({
useShowCallsStatus: vi.fn(),
}));

vi.mock('../../network/getChainExplorer', () => ({
getChainExplorer: vi.fn(),
}));

describe('TransactionButton', () => {
beforeEach(() => {
(useChainId as vi.Mock).mockReturnValue(123);
Expand Down Expand Up @@ -127,4 +132,45 @@ describe('TransactionButton', () => {
const button = getByRole('button');
expect(button).not.toBeDisabled();
});

it('should open transaction link when only receipt exists', () => {
const onSubmit = vi.fn();
const chainExplorerUrl = 'https://explorer.com';
(useTransactionContext as vi.Mock).mockReturnValue({
receipt: 'receipt-123',
transactionId: undefined,
transactionHash: 'hash-789',
onSubmit,
});
(getChainExplorer as vi.Mock).mockReturnValue(chainExplorerUrl);
window.open = vi.fn();

render(<TransactionButton text="Transact" />);
const button = screen.getByText('View transaction');
fireEvent.click(button);

expect(window.open).toHaveBeenCalledWith(
`${chainExplorerUrl}/tx/hash-789`,
'_blank',
'noopener,noreferrer',
);
expect(onSubmit).not.toHaveBeenCalled();
});

it('should call onSubmit when neither receipt nor transactionId exists', () => {
const onSubmit = vi.fn();
(useTransactionContext as vi.Mock).mockReturnValue({
receipt: undefined,
transactionId: undefined,
onSubmit,
address: '123',
contracts: [{}],
});

render(<TransactionButton text="Transact" />);
const button = screen.getByText('Transact');
fireEvent.click(button);

expect(onSubmit).toHaveBeenCalled();
});
});
15 changes: 15 additions & 0 deletions src/transaction/hooks/useGetTransactionStatusAction.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,19 @@ describe('useGetTransactionStatusAction', () => {

expect(result.current.actionElement).toBeNull();
});

it('should call showCallsStatus when button is clicked', () => {
const showCallsStatus = vi.fn();
(useShowCallsStatus as vi.Mock).mockReturnValue({ showCallsStatus });
(useTransactionContext as vi.Mock).mockReturnValue({
transactionId: 'ab123',
});

const { result } = renderHook(() => useGetTransactionStatusAction());

const button = result.current.actionElement as JSX.Element;
button.props.onClick();

expect(showCallsStatus).toHaveBeenCalledWith({ id: 'ab123' });
});
});
15 changes: 15 additions & 0 deletions src/transaction/hooks/useGetTransactionToastAction.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,19 @@ describe('useGetTransactionToastAction', () => {
</a>
`);
});

it('should call showCallsStatus when button is clicked', () => {
const showCallsStatus = vi.fn();
(useShowCallsStatus as vi.Mock).mockReturnValue({ showCallsStatus });
(useTransactionContext as vi.Mock).mockReturnValue({
transactionId: 'ab123',
});

const { result } = renderHook(() => useGetTransactionToastAction());

const button = result.current.actionElement as JSX.Element;
button.props.onClick();

expect(showCallsStatus).toHaveBeenCalledWith({ id: 'ab123' });
});
});

0 comments on commit 9a1a45a

Please sign in to comment.