import { useOutsideClick } from '@/internal/hooks/useOutsideClick';
import { render, screen } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { ConnectWallet } from './ConnectWallet';
import { Wallet } from './Wallet';
import { WalletDropdown } from './WalletDropdown';
import { useWalletContext } from './WalletProvider';
import { PropsWithChildren } from 'react';
vi.mock('@/internal/hooks/useOutsideClick', () => ({
useOutsideClick: vi.fn(),
}));
vi.mock('./ConnectWallet', () => ({
ConnectWallet: () =>
Connect Wallet
,
}));
vi.mock('./WalletAdvanced', () => ({
WalletAdvanced: () => (
Wallet Advanced
),
}));
vi.mock('./WalletDropdown', () => ({
WalletDropdown: () => (
Wallet Advanced
),
}));
vi.mock('./WalletProvider', () => ({
useWalletContext: vi.fn(),
WalletProvider: ({ children }: PropsWithChildren) => <>{children}>,
}));
describe('Wallet Component', () => {
let mockHandleClose: ReturnType;
beforeEach(() => {
mockHandleClose = vi.fn();
(useWalletContext as ReturnType).mockReturnValue({
isSubComponentOpen: true,
handleClose: mockHandleClose,
containerRef: { current: document.createElement('div') },
connectRef: { current: document.createElement('div') },
});
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
vi.clearAllMocks();
});
it('should render the Wallet component with ConnectWallet', () => {
(useWalletContext as ReturnType).mockReturnValue({
isSubComponentOpen: false,
handleClose: mockHandleClose,
containerRef: { current: document.createElement('div') },
});
render(
Wallet Dropdown
,
);
expect(screen.getByTestId('connect-wallet')).toBeDefined();
expect(screen.queryByTestId('wallet-dropdown')).toBeNull();
});
it('should render default children', () => {
(useWalletContext as ReturnType).mockReturnValue({
isSubComponentOpen: false,
handleClose: mockHandleClose,
containerRef: { current: document.createElement('div') },
});
render();
expect(screen.getByTestId('connect-wallet')).toBeDefined();
expect(screen.queryByTestId('wallet-dropdown')).toBeNull();
});
it('should render default children when draggable', () => {
(useWalletContext as ReturnType).mockReturnValue({
isSubComponentOpen: false,
handleClose: mockHandleClose,
containerRef: { current: document.createElement('div') },
});
render();
expect(screen.getByTestId('connect-wallet')).toBeDefined();
expect(screen.queryByTestId('wallet-dropdown')).toBeNull();
});
it('should close the wallet when clicking outside', () => {
(useWalletContext as ReturnType).mockReturnValue({
isSubComponentOpen: true,
handleClose: mockHandleClose,
containerRef: { current: document.createElement('div') },
address: '0x123',
breakpoint: 'md',
});
const mockOutsideClickCallback = vi.fn();
(useOutsideClick as ReturnType).mockReturnValue({
handleOutsideClick: mockOutsideClickCallback,
});
render(
Wallet Dropdown
,
);
expect(screen.getByTestId('ockWalletDropdown')).toBeDefined();
mockOutsideClickCallback({} as MouseEvent);
expect(mockOutsideClickCallback).toHaveBeenCalled();
});
it('should not trigger click handler when wallet is closed', () => {
(useWalletContext as ReturnType).mockReturnValue({
isSubComponentOpen: false,
handleClose: mockHandleClose,
containerRef: { current: document.createElement('div') },
});
const mockOutsideClickCallback = vi.fn();
(useOutsideClick as ReturnType).mockReturnValue({
handleOutsideClick: mockOutsideClickCallback,
});
render(
Wallet Dropdown
,
);
expect(screen.queryByTestId('wallet-dropdown')).toBeNull();
mockOutsideClickCallback({} as MouseEvent);
expect(mockHandleClose).not.toHaveBeenCalled();
});
it('should render Draggable when draggable prop is true', () => {
(useWalletContext as ReturnType).mockReturnValue({
isSubComponentOpen: true,
handleClose: mockHandleClose,
containerRef: { current: document.createElement('div') },
});
render(
Wallet Advanced
,
);
expect(screen.getByTestId('ockDraggable')).toBeDefined();
});
it('should disable Draggable dragging when isConnectModalOpen or breakpoint is sm and isSubComponentOpen is true', () => {
(useWalletContext as ReturnType).mockReturnValue({
isSubComponentOpen: true,
handleClose: mockHandleClose,
containerRef: { current: document.createElement('div') },
breakpoint: 'sm',
});
render(
Wallet Advanced
,
);
expect(screen.getByTestId('ockDraggable')).toBeDefined();
});
});