From 01f037897759b88c0f278c51f35f33b5b90f42e5 Mon Sep 17 00:00:00 2001 From: Alissa Crane Date: Wed, 14 Aug 2024 14:00:54 -0700 Subject: [PATCH 1/3] add test coverage --- .../components/TransactionButton.test.tsx | 36 +++++++++++- .../components/TransactionProvider.test.tsx | 28 +++++++++ src/transaction/hooks/useCallsStatus.test.ts | 57 +++++++++++++++++++ 3 files changed, 119 insertions(+), 2 deletions(-) diff --git a/src/transaction/components/TransactionButton.test.tsx b/src/transaction/components/TransactionButton.test.tsx index c311bfbb94..ea721ea497 100644 --- a/src/transaction/components/TransactionButton.test.tsx +++ b/src/transaction/components/TransactionButton.test.tsx @@ -1,5 +1,7 @@ -import { render, screen } from '@testing-library/react'; -import { describe, expect, it, vi } from 'vitest'; +import { fireEvent, render, screen } from '@testing-library/react'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { useChainId } from 'wagmi'; +import { useShowCallsStatus } from 'wagmi/experimental'; import { TransactionButton } from './TransactionButton'; import { useTransactionContext } from './TransactionProvider'; @@ -81,4 +83,34 @@ describe('TransactionButton', () => { const button = getByRole('button'); expect(button).toBeDisabled(); }); + + it('should call showCallsStatus when receipt and transactionId exist', () => { + const showCallsStatus = vi.fn(); + (useShowCallsStatus as vi.Mock).mockReturnValue({ showCallsStatus }); + (useTransactionContext as vi.Mock).mockReturnValue({ + receipt: '123', + transactionId: '456', + }); + + render(); + const button = screen.getByText('View transaction'); + fireEvent.click(button); + + expect(showCallsStatus).toHaveBeenCalledWith({ id: '456' }); + }); + + it('should enable button when not in progress, not missing props, and not waiting for receipt', () => { + (useTransactionContext as vi.Mock).mockReturnValue({ + isLoading: false, + contracts: {}, + address: '0x123', + transactionId: undefined, + transactionHash: undefined, + receipt: undefined, + }); + + const { getByRole } = render(); + const button = getByRole('button'); + expect(button).not.toBeDisabled(); + }); }); diff --git a/src/transaction/components/TransactionProvider.test.tsx b/src/transaction/components/TransactionProvider.test.tsx index 7e0fc9e370..c01dc6d591 100644 --- a/src/transaction/components/TransactionProvider.test.tsx +++ b/src/transaction/components/TransactionProvider.test.tsx @@ -329,6 +329,34 @@ describe('TransactionProvider', () => { ); }); }); + + it('should call onSuccess when receiptArray has receipts', async () => { + const onSuccessMock = vi.fn(); + const mockReceipt = { status: 'success' }; + + (useWaitForTransactionReceipt as ReturnType).mockReturnValue({ + data: mockReceipt, + }); + + render( + + + , + ); + + const button = screen.getByText('Submit'); + fireEvent.click(button); + + await waitFor(() => { + expect(onSuccessMock).toHaveBeenCalledWith({ + transactionReceipts: [mockReceipt], + }); + }); + }); }); describe('useTransactionContext', () => { diff --git a/src/transaction/hooks/useCallsStatus.test.ts b/src/transaction/hooks/useCallsStatus.test.ts index 60b65cb294..ce2e17409e 100644 --- a/src/transaction/hooks/useCallsStatus.test.ts +++ b/src/transaction/hooks/useCallsStatus.test.ts @@ -46,4 +46,61 @@ describe('useCallsStatus', () => { }, }); }); + + it('should set refetchInterval to 1000 ms when status is not CONFIRMED', () => { + const mockData = { + status: 'PENDING', + }; + + // Mocking useCallsStatusWagmi to return the specific data and simulate refetchInterval logic + (useCallsStatusWagmi as ReturnType).mockImplementation( + ({ query }) => { + const refetchInterval = query.refetchInterval({ + state: { data: mockData }, + }); + expect(refetchInterval).toBe(1000); + return { data: mockData }; + }, + ); + + renderHook(() => useCallsStatus({ transactionId })); + }); + + it('should set refetchInterval to false when status is CONFIRMED', () => { + const mockData = { + status: 'CONFIRMED', + }; + + // Mocking useCallsStatusWagmi to return the specific data and simulate refetchInterval logic + (useCallsStatusWagmi as ReturnType).mockImplementation( + ({ query }) => { + const refetchInterval = query.refetchInterval({ + state: { data: mockData }, + }); + expect(refetchInterval).toBe(false); + return { data: mockData }; + }, + ); + + renderHook(() => useCallsStatus({ transactionId })); + }); + + it('should not fetch data when transactionId is not provided', () => { + const mockSetLifeCycleStatus = vi.fn(); + const mockData = undefined; + + (useCallsStatusWagmi as ReturnType).mockReturnValue({ + data: mockData, + }); + + const { result } = renderHook(() => + useCallsStatus({ + setLifeCycleStatus: mockSetLifeCycleStatus, + transactionId: undefined, + }), + ); + + expect(result.current.status).toBeUndefined(); + expect(result.current.transactionHash).toBeUndefined(); + }); }); From d298fe5203f7ca7ef415a74bbf42cb22ee79922e Mon Sep 17 00:00:00 2001 From: Alissa Crane Date: Wed, 14 Aug 2024 14:24:12 -0700 Subject: [PATCH 2/3] fix test --- .../components/TransactionButton.test.tsx | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/transaction/components/TransactionButton.test.tsx b/src/transaction/components/TransactionButton.test.tsx index ea721ea497..f942737ca5 100644 --- a/src/transaction/components/TransactionButton.test.tsx +++ b/src/transaction/components/TransactionButton.test.tsx @@ -1,6 +1,5 @@ import { fireEvent, render, screen } from '@testing-library/react'; -import { beforeEach, describe, expect, it, vi } from 'vitest'; -import { useChainId } from 'wagmi'; +import { describe, expect, it, vi } from 'vitest'; import { useShowCallsStatus } from 'wagmi/experimental'; import { TransactionButton } from './TransactionButton'; import { useTransactionContext } from './TransactionProvider'; @@ -9,6 +8,10 @@ vi.mock('./TransactionProvider', () => ({ useTransactionContext: vi.fn(), })); +vi.mock('wagmi/experimental', () => ({ + useShowCallsStatus: vi.fn(), +})); + describe('TransactionButton', () => { it('renders correctly', () => { (useTransactionContext as vi.Mock).mockReturnValue({ @@ -84,21 +87,6 @@ describe('TransactionButton', () => { expect(button).toBeDisabled(); }); - it('should call showCallsStatus when receipt and transactionId exist', () => { - const showCallsStatus = vi.fn(); - (useShowCallsStatus as vi.Mock).mockReturnValue({ showCallsStatus }); - (useTransactionContext as vi.Mock).mockReturnValue({ - receipt: '123', - transactionId: '456', - }); - - render(); - const button = screen.getByText('View transaction'); - fireEvent.click(button); - - expect(showCallsStatus).toHaveBeenCalledWith({ id: '456' }); - }); - it('should enable button when not in progress, not missing props, and not waiting for receipt', () => { (useTransactionContext as vi.Mock).mockReturnValue({ isLoading: false, From 542cd8a39c2f1d7b926435778134b57290a6848f Mon Sep 17 00:00:00 2001 From: Alissa Crane Date: Wed, 14 Aug 2024 14:27:59 -0700 Subject: [PATCH 3/3] fix lint --- src/transaction/components/TransactionButton.test.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/transaction/components/TransactionButton.test.tsx b/src/transaction/components/TransactionButton.test.tsx index f942737ca5..bdd570671a 100644 --- a/src/transaction/components/TransactionButton.test.tsx +++ b/src/transaction/components/TransactionButton.test.tsx @@ -1,6 +1,5 @@ -import { fireEvent, render, screen } from '@testing-library/react'; +import { render, screen } from '@testing-library/react'; import { describe, expect, it, vi } from 'vitest'; -import { useShowCallsStatus } from 'wagmi/experimental'; import { TransactionButton } from './TransactionButton'; import { useTransactionContext } from './TransactionProvider';