import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { useAuth, type UseAuthResult } from '@sunnyside-io/privacy-boost-react';
import { WalletButton } from '../components/WalletButton';
function mockAuth(overrides: Partial<UseAuthResult>) {
jest.mocked(useAuth).mockReturnValue({
state: { status: 'signedOut' },
status: 'disconnected',
privacyAddress: null,
signInWithWalletAdapter: jest.fn(),
...overrides,
} as Partial<UseAuthResult> as UseAuthResult);
}
describe('WalletButton', () => {
it('shows the sign-in button when signed out', () => {
mockAuth({});
render(<WalletButton />);
expect(screen.getByText('Sign in')).toBeInTheDocument();
});
it('shows the privacy address on a locked session', () => {
mockAuth({
status: 'locked',
state: {
status: 'signedIn',
locked: true,
account: { walletAddress: '0x123', privacyAddress: '0x04abc123', mpk: '0x1' },
},
privacyAddress: '0x04abc123',
});
render(<WalletButton />);
expect(screen.getByText(/0x04abc123/)).toBeInTheDocument();
});
it('signs in on click', async () => {
const signInWithWalletAdapter = jest.fn().mockResolvedValue({ status: 'signedIn' });
mockAuth({ signInWithWalletAdapter });
render(<WalletButton />);
fireEvent.click(screen.getByText('Sign in'));
await waitFor(() => {
expect(signInWithWalletAdapter).toHaveBeenCalled();
});
});
});