Skip to content

Commit

Permalink
feat: add WalletDefault component (#1302)
Browse files Browse the repository at this point in the history
Co-authored-by: Alissa Crane <alissa.crane@coinbase.com>
  • Loading branch information
abcrane123 and alissacrane-cb authored Oct 3, 2024
1 parent 1bf3c38 commit 06a0093
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 2 deletions.
1 change: 1 addition & 0 deletions playground/nextjs-app-router/components/AppProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum OnchainKitComponent {
SwapDefault = 'swap-default',
Transaction = 'transaction',
Wallet = 'wallet',
WalletDefault = 'wallet-default',
}

export enum TransactionTypes {
Expand Down
10 changes: 8 additions & 2 deletions playground/nextjs-app-router/components/Demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SwapDemo from './demo/Swap';
import SwapDefaultDemo from './demo/SwapDefault';
import TransactionDemo from './demo/Transaction';
import WalletDemo from './demo/Wallet';
import WalletDefaultDemo from './demo/WalletDefault';
import { ActiveComponent } from './form/active-component';
import { TransactionOptions } from './form/transaction-options';

Expand Down Expand Up @@ -41,6 +42,7 @@ function Demo() {
: 'border-gray-300 bg-white text-black hover:bg-gray-100'
}`;

// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO: refactor
function renderActiveComponent() {
if (activeComponent === OnchainKitComponent.Fund) {
return <FundDemo />;
Expand All @@ -58,12 +60,16 @@ function Demo() {
return <SwapDemo />;
}

if (activeComponent === OnchainKitComponent.SwapDefault) {
return <SwapDefaultDemo />;
}

if (activeComponent === OnchainKitComponent.Wallet) {
return <WalletDemo />;
}

if (activeComponent === OnchainKitComponent.SwapDefault) {
return <SwapDefaultDemo />;
if (activeComponent === OnchainKitComponent.WalletDefault) {
return <WalletDefaultDemo />;
}

return <></>;
Expand Down
11 changes: 11 additions & 0 deletions playground/nextjs-app-router/components/demo/WalletDefault.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { WalletDefault } from '@coinbase/onchainkit/wallet';

export default function WalletDemo() {
return (
<div className="mx-auto">
<div className="flex justify-end">
<WalletDefault />
</div>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export function ActiveComponent() {
SwapDefault
</SelectItem>
<SelectItem value={OnchainKitComponent.Wallet}>Wallet</SelectItem>
<SelectItem value={OnchainKitComponent.WalletDefault}>
WalletDefault
</SelectItem>
</SelectContent>
</Select>
</div>
Expand Down
82 changes: 82 additions & 0 deletions src/wallet/components/WalletDefault.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { render, screen } from '@testing-library/react';
import { type Mock, describe, expect, it, vi } from 'vitest';
import { useAccount, useConnect } from 'wagmi';
import { useAvatar } from '../../identity/hooks/useAvatar';
import { useName } from '../../identity/hooks/useName';
import { WalletDefault } from './WalletDefault';
import { useWalletContext } from './WalletProvider';

vi.mock('../../useBreakpoints', () => ({
useBreakpoints: vi.fn(),
}));

vi.mock('wagmi', () => ({
useAccount: vi.fn(),
useConnect: vi.fn(),
useConfig: vi.fn(),
}));

vi.mock('./WalletProvider', () => ({
WalletProvider: ({ children }) => (
<div data-testid="mock-WalletProvider">{children}</div>
),
useWalletContext: vi.fn(),
}));

vi.mock('../../identity/hooks/useName', () => ({
useName: vi.fn(),
}));
vi.mock('../../identity/hooks/useAvatar', () => ({
useAvatar: vi.fn(),
}));

function mock<T>(func: T) {
return func as Mock;
}
const useNameMock = mock(useName);
const useAvatarMock = mock(useAvatar);

describe('WalletDefault Component', () => {
beforeEach(() => {
(useConnect as ReturnType<typeof vi.fn>).mockReturnValue({
connectors: [],
status: 'disconnected',
});
(useAccount as ReturnType<typeof vi.fn>).mockReturnValue({
status: 'disconnected',
address: '',
});
(useWalletContext as Mock).mockReturnValue({
isOpen: false,
});
useNameMock.mockReturnValue({ data: null, isLoading: true });
useAvatarMock.mockReturnValue({ data: null, isLoading: true });
});

it('renders the ConnectWallet component when disconnected', () => {
render(<WalletDefault />);
expect(screen.getByText('Connect Wallet')).toBeInTheDocument();
expect(
screen.getByTestId('ockConnectWallet_Container'),
).toBeInTheDocument();
});

it('renders the wallet address when connected', () => {
(useAccount as ReturnType<typeof vi.fn>).mockReturnValue({
status: 'connected',
address: '0x123',
});
(useConnect as ReturnType<typeof vi.fn>).mockReturnValue({
connectors: [],
status: 'connected',
});
(useWalletContext as Mock).mockReturnValue({
isOpen: false,
address: '0x123',
});
useNameMock.mockReturnValue({ data: null, isLoading: false });
render(<WalletDefault />);
const address = screen.getByText('0x123...x123');
expect(address).toBeDefined();
});
});
38 changes: 38 additions & 0 deletions src/wallet/components/WalletDefault.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Address, Avatar, EthBalance, Identity, Name } from '../../identity';
import { color } from '../../styles/theme';
import {
ConnectWallet,
ConnectWalletText,
Wallet,
WalletDropdown,
WalletDropdownDisconnect,
WalletDropdownLink,
} from '../index';

export function WalletDefault() {
return (
<Wallet>
<ConnectWallet>
<ConnectWalletText>Connect Wallet</ConnectWalletText>
<Avatar className="h-6 w-6" />
<Name />
</ConnectWallet>
<WalletDropdown>
<Identity className="px-4 pt-3 pb-2" hasCopyAddressOnClick={true}>
<Avatar />
<Name />
<Address className={color.foregroundMuted} />
<EthBalance />
</Identity>
<WalletDropdownLink
icon="wallet"
href="https://keys.coinbase.com"
target="_blank"
>
Wallet
</WalletDropdownLink>
<WalletDropdownDisconnect />
</WalletDropdown>
</Wallet>
);
}
1 change: 1 addition & 0 deletions src/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export { ConnectWallet } from './components/ConnectWallet';
export { ConnectWalletText } from './components/ConnectWalletText';
export { Wallet } from './components/Wallet';
export { WalletDefault } from './components/WalletDefault';
export { WalletDropdown } from './components/WalletDropdown';
export { WalletDropdownBasename } from './components/WalletDropdownBasename';
export { WalletDropdownDisconnect } from './components/WalletDropdownDisconnect';
Expand Down

0 comments on commit 06a0093

Please sign in to comment.