Skip to content

Commit

Permalink
chore: add transaction provider tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alessey committed Aug 23, 2024
1 parent bf5c8d5 commit 89a3195
Showing 1 changed file with 73 additions and 14 deletions.
87 changes: 73 additions & 14 deletions src/transaction/components/TransactionProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useSwitchChain,
useWaitForTransactionReceipt,
} from 'wagmi';
import { waitForTransactionReceipt } from 'wagmi/actions';
import { METHOD_NOT_SUPPORTED_ERROR_SUBSTRING } from '../constants';
import { useCallsStatus } from '../hooks/useCallsStatus';
import { useWriteContract } from '../hooks/useWriteContract';
Expand All @@ -22,6 +23,10 @@ vi.mock('wagmi', () => ({
waitForTransactionReceipt: vi.fn(),
}));

vi.mock('wagmi/actions', () => ({
waitForTransactionReceipt: vi.fn(),
}));

vi.mock('../hooks/useCallsStatus', () => ({
useCallsStatus: vi.fn(),
}));
Expand Down Expand Up @@ -51,6 +56,15 @@ const TestComponent = () => {
},
});
};
const handleStatusTransactionLegacyExecutedMultipleContracts = async () => {
context.setLifeCycleStatus({
statusName: 'transactionLegacyExecuted',
statusData: {
transactionHashList: ['hash12345678', 'hash12345678'],
},
});
};

return (
<div data-testid="test-component">
<button type="button" onClick={context.onSubmit}>
Expand All @@ -72,6 +86,12 @@ const TestComponent = () => {
<button type="button" onClick={handleStatusTransactionLegacyExecuted}>
setLifeCycleStatus.transactionLegacyExecuted
</button>
<button
type="button"
onClick={handleStatusTransactionLegacyExecutedMultipleContracts}
>
setLifeCycleStatus.transactionLegacyExecutedMultipleContracts
</button>
</div>
);
};
Expand Down Expand Up @@ -170,6 +190,59 @@ describe('TransactionProvider', () => {
});
});

it('should emit onSuccess for multiple contracts using legacy transactions', async () => {
const onSuccessMock = vi.fn();
(waitForTransactionReceipt as ReturnType<typeof vi.fn>).mockReturnValue(
'hash12345678',
);
render(
<TransactionProvider
contracts={[
{ address: '0x123', method: 'method' },
{ address: '0x123', method: 'method' },
]}
onSuccess={onSuccessMock}
>
<TestComponent />
</TransactionProvider>,
);
const button = screen.getByText(
'setLifeCycleStatus.transactionLegacyExecutedMultipleContracts',
);
fireEvent.click(button);
await waitFor(() => {
expect(onSuccessMock).toHaveBeenCalled();
expect(onSuccessMock).toHaveBeenCalledWith({
transactionReceipts: ['hash12345678', 'hash12345678'],
});
});
});

it('should emit onError when legacy transactions fail', async () => {
const onErrorMock = vi.fn();
(waitForTransactionReceipt as ReturnType<typeof vi.fn>).mockRejectedValue(
new Error('error getting transaction receipt'),
);
render(
<TransactionProvider
contracts={[
{ address: '0x123', method: 'method' },
{ address: '0x123', method: 'method' },
]}
onError={onErrorMock}
>
<TestComponent />
</TransactionProvider>,
);
const button = screen.getByText(
'setLifeCycleStatus.transactionLegacyExecutedMultipleContracts',
);
fireEvent.click(button);
await waitFor(() => {
expect(onErrorMock).toHaveBeenCalled();
});
});

it('should set setLifeCycleStatus to transactionPending when writeContractsAsync is pending', async () => {
const writeContractsAsyncMock = vi.fn();
(useWriteContracts as ReturnType<typeof vi.fn>).mockReturnValue({
Expand Down Expand Up @@ -290,20 +363,6 @@ describe('TransactionProvider', () => {
});
});

it('should not fetch receipts if contract list is empty', async () => {
const waitForTransactionReceiptMock = vi.fn();
render(
<TransactionProvider contracts={[]}>
<TestComponent />
</TransactionProvider>,
);
const button = screen.getByText('Submit');
fireEvent.click(button);
await waitFor(() => {
expect(waitForTransactionReceiptMock).not.toHaveBeenCalled();
});
});

it('should handle user rejected request', async () => {
const writeContractsAsyncMock = vi
.fn()
Expand Down

0 comments on commit 89a3195

Please sign in to comment.