Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add more test coverage #1050

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/transaction/components/TransactionButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ vi.mock('./TransactionProvider', () => ({
useTransactionContext: vi.fn(),
}));

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

describe('TransactionButton', () => {
it('renders correctly', () => {
(useTransactionContext as vi.Mock).mockReturnValue({
Expand Down Expand Up @@ -81,4 +85,19 @@ describe('TransactionButton', () => {
const button = getByRole('button');
expect(button).toBeDisabled();
});

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(<TransactionButton text="Submit" />);
const button = getByRole('button');
expect(button).not.toBeDisabled();
});
});
28 changes: 28 additions & 0 deletions src/transaction/components/TransactionProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof vi.fn>).mockReturnValue({
data: mockReceipt,
});

render(
<TransactionProvider
address="0x123"
contracts={[]}
onSuccess={onSuccessMock}
>
<TestComponent />
</TransactionProvider>,
);

const button = screen.getByText('Submit');
fireEvent.click(button);

await waitFor(() => {
expect(onSuccessMock).toHaveBeenCalledWith({
transactionReceipts: [mockReceipt],
});
});
});
});

describe('useTransactionContext', () => {
Expand Down
57 changes: 57 additions & 0 deletions src/transaction/hooks/useCallsStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof vi.fn>).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<typeof vi.fn>).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<typeof vi.fn>).mockReturnValue({
data: mockData,
});

const { result } = renderHook(() =>
useCallsStatus({
setLifeCycleStatus: mockSetLifeCycleStatus,
transactionId: undefined,
}),
);

expect(result.current.status).toBeUndefined();
expect(result.current.transactionHash).toBeUndefined();
});
});