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 Connect Wallet tests #1036

Merged
merged 2 commits into from
Aug 12, 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
1 change: 1 addition & 0 deletions .changeset/many-ravens-cry.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
---

-**chore**: Add testing to Transaction Toast. By @cpcramer #1023
-**chore**: Add Connect Wallet tests and refactor to use Vitest. By @cpcramer #1036
85 changes: 69 additions & 16 deletions src/wallet/components/ConnectWallet.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { fireEvent, render, screen } from '@testing-library/react';
/**
* @vitest-environment jsdom
*/
import type { ReactNode } from 'react';
import '@testing-library/jest-dom';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { useAccount, useConnect } from 'wagmi';
import { ConnectWallet } from './ConnectWallet';
import { useWalletContext } from './WalletProvider';
Expand All @@ -26,18 +23,27 @@ vi.mock('./WalletProvider', () => ({
),
}));

vi.mock('@rainbow-me/rainbowkit', () => ({
ConnectButton: {
Custom: ({
children,
}: { children: (props: { openConnectModal: () => void }) => ReactNode }) =>
children({ openConnectModal: vi.fn() }),
},
}));

describe('ConnectWallet', () => {
beforeEach(() => {
(useAccount as vi.Mock).mockReturnValue({
vi.mocked(useAccount).mockReturnValue({
address: '',
status: 'disconnected',
});
(useConnect as vi.Mock).mockReturnValue({
vi.mocked(useConnect).mockReturnValue({
connectors: [{ id: 'mockConnector' }],
connect: vi.fn(),
status: 'idle',
});
(useWalletContext as vi.Mock).mockReturnValue({
vi.mocked(useWalletContext).mockReturnValue({
isOpen: false,
setIsOpen: vi.fn(),
});
Expand All @@ -52,12 +58,12 @@ describe('ConnectWallet', () => {
});

it('renders spinner when loading', () => {
(useConnect as vi.Mock).mockReturnValue({
vi.mocked(useConnect).mockReturnValue({
connectors: [{ id: 'mockConnector' }],
connect: vi.fn(),
status: 'pending',
});
(useAccount as vi.Mock).mockReturnValue({
vi.mocked(useAccount).mockReturnValue({
address: '',
status: 'connecting',
});
Expand All @@ -69,7 +75,7 @@ describe('ConnectWallet', () => {
});

it('renders children when connected', () => {
(useAccount as vi.Mock).mockReturnValue({
vi.mocked(useAccount).mockReturnValue({
address: '0x123',
status: 'connected',
});
Expand All @@ -86,7 +92,7 @@ describe('ConnectWallet', () => {

it('calls connect function when connect button is clicked', () => {
const connectMock = vi.fn();
(useConnect as vi.Mock).mockReturnValue({
vi.mocked(useConnect).mockReturnValue({
connectors: [{ id: 'mockConnector' }],
connect: connectMock,
status: 'idle',
Expand All @@ -104,11 +110,11 @@ describe('ConnectWallet', () => {

it('toggles wallet modal on button click when connected', () => {
const setIsOpenMock = vi.fn();
(useAccount as vi.Mock).mockReturnValue({
vi.mocked(useAccount).mockReturnValue({
address: '0x123',
status: 'connected',
});
(useWalletContext as vi.Mock).mockReturnValue({
vi.mocked(useWalletContext).mockReturnValue({
isOpen: false,
setIsOpen: setIsOpenMock,
});
Expand All @@ -126,15 +132,15 @@ describe('ConnectWallet', () => {
});

it('applies bg-ock-secondary-active class when isOpen is true', () => {
(useWalletContext as vi.Mock).mockReturnValue({
vi.mocked(useWalletContext).mockReturnValue({
isOpen: true,
setIsOpen: vi.fn(),
});
(useAccount as vi.Mock).mockReturnValue({
vi.mocked(useAccount).mockReturnValue({
address: '0x123',
status: 'connected',
});
(useConnect as vi.Mock).mockReturnValue({
vi.mocked(useConnect).mockReturnValue({
connectors: [{ id: 'test-connector' }],
connect: vi.fn(),
status: 'idle',
Expand All @@ -149,4 +155,51 @@ describe('ConnectWallet', () => {
const button = screen.getByTestId('ockConnectWallet_Connected');
expect(button).toHaveClass('bg-ock-secondary-active');
});

describe('withWalletAggregator', () => {
beforeEach(() => {
vi.mocked(useAccount).mockReturnValue({
address: '',
status: 'disconnected',
});
vi.mocked(useConnect).mockReturnValue({
connectors: [{ id: 'mockConnector' }],
connect: vi.fn(),
status: 'idle',
});
});

it('renders ConnectButtonRainboKit when withWalletAggregator is true', () => {
render(
<ConnectWallet text="Connect Wallet" withWalletAggregator={true} />,
);

const container = screen.getByTestId('ockConnectWallet_Container');
expect(container).toBeInTheDocument();

const connectButton = screen.getByTestId('ockConnectButton');
expect(connectButton).toBeInTheDocument();
expect(connectButton).toHaveTextContent('Connect Wallet');
});

it('renders regular ConnectButton when withWalletAggregator is false', () => {
const connectMock = vi.fn();
vi.mocked(useConnect).mockReturnValue({
connectors: [{ id: 'mockConnector' }],
connect: connectMock,
status: 'idle',
});

render(
<ConnectWallet text="Connect Wallet" withWalletAggregator={false} />,
);

const connectButton = screen.getByTestId('ockConnectButton');
fireEvent.click(connectButton);

expect(connectMock).toHaveBeenCalledWith({
connector: { id: 'mockConnector' },
});
});
});
});