Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zizzamia committed Aug 13, 2024
1 parent bb15a98 commit 4c26083
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/transaction/components/TransactionProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ vi.mock('../hooks/useWriteContracts', () => ({

const TestComponent = () => {
const context = useTransactionContext();
const handleSetLifeCycleStatus = async () => {
context.setLifeCycleStatus({
statusName: 'error',
statusData: { code: 'code', error: 'error_long_messages' },
});
};
return (
<div data-testid="test-component">
<button type="button" onClick={context.onSubmit}>
Expand All @@ -47,6 +53,9 @@ const TestComponent = () => {
<span data-testid="context-value-isToastVisible">
{`${context.isToastVisible}`}
</span>
<button type="button" onClick={handleSetLifeCycleStatus}>
setLifeCycleStatus.error
</button>
</div>
);
};
Expand Down Expand Up @@ -76,6 +85,18 @@ describe('TransactionProvider', () => {
});
});

it('should emit onError when setLifeCycleStatus is called with error', async () => {
const onErrorMock = vi.fn();
render(
<TransactionProvider address="0x123" contracts={[]} onError={onErrorMock}>
<TestComponent />
</TransactionProvider>,
);
const button = screen.getByText('setLifeCycleStatus.error');
fireEvent.click(button);
expect(onErrorMock).toHaveBeenCalled();
});

it('should update context on handleSubmit', async () => {
const writeContractsAsyncMock = vi.fn();
(useWriteContracts as ReturnType<typeof vi.fn>).mockReturnValue({
Expand Down
1 change: 1 addition & 0 deletions src/transaction/components/TransactionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export function TransactionProvider({
receipt,
setErrorMessage,
setIsToastVisible,
setLifeCycleStatus,
setTransactionId,
statusWriteContracts,
statusWriteContract,
Expand Down
1 change: 1 addition & 0 deletions src/transaction/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type TransactionContextType = {
receipt?: TransactionReceipt; // The receipt of the transaction
setErrorMessage: (error: string) => void; // A function to set the error message for the transaction.
setIsToastVisible: (isVisible: boolean) => void; // A function to set the visibility of the transaction toast.
setLifeCycleStatus: (state: LifeCycleStatus) => void; // A function to set the lifecycle status of the component
setTransactionId: (id: string) => void; // A function to set the transaction ID.
statusWriteContract?: string; // An optional string indicating the current status of the transaction.
statusWriteContracts?: string; // An optional string indicating the current status of the transaction.
Expand Down

0 comments on commit 4c26083

Please sign in to comment.