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

feat: Wallet Advanced Send (Fund Wallet) #2075

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion playground/nextjs-app-router/onchainkit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coinbase/onchainkit",
"version": "0.37.4",
"version": "0.37.5",
"type": "module",
"repository": "https://github.com/coinbase/onchainkit.git",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/api/getPriceQuote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('getPriceQuote', () => {
expect(result).toEqual({
code: 'INVALID_INPUT',
error: 'Invalid input: tokens must be an array of at least one token',
message: '',
message: 'Tokens must be an array of at least one token',
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/api/getPriceQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function getPriceQuote(
);
if (res.error) {
return {
code: `${res.error.code}`,
code: String(res.error.code),
error: 'Error fetching price quote',
message: res.error.message,
};
Expand All @@ -51,7 +51,7 @@ function validateGetPriceQuoteParams(params: GetPriceQuoteParams) {
return {
code: 'INVALID_INPUT',
error: 'Invalid input: tokens must be an array of at least one token',
message: '',
message: 'Tokens must be an array of at least one token',
};
}

Expand Down
8 changes: 4 additions & 4 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,15 +397,15 @@ export type GetPriceQuoteParams = {

type PriceQuote = {
/** The name of the token */
name: string | '';
name: string;
/** The symbol of the token */
symbol: string | '';
symbol: string;
/** The contract address of the token */
contractAddress: Address | '';
/** The price of the token */
price: string | '';
price: string;
/** The timestamp of the price quote */
timestamp: number | 0;
timestamp: number;
};

/**
Expand Down
36 changes: 36 additions & 0 deletions src/wallet/components/WalletAdvancedContent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ vi.mock('./WalletAdvancedSwap', () => ({
),
}));

vi.mock('./wallet-advanced-send/components/Send', () => ({
Send: ({ className }: { className?: string }) => (
<div data-testid="ockWalletAdvancedSend" className={className}>
WalletAdvancedSend
</div>
),
}));

vi.mock('./WalletProvider', () => ({
useWalletContext: vi.fn(),
WalletProvider: ({ children }: { children: React.ReactNode }) => (
Expand Down Expand Up @@ -267,6 +275,9 @@ describe('WalletAdvancedContent', () => {
expect(
screen.queryByTestId('ockWalletAdvancedSwap'),
).not.toBeInTheDocument();
expect(
screen.queryByTestId('ockWalletAdvancedSend'),
).not.toBeInTheDocument();
});

it('renders WalletAdvancedSwap when activeFeature is swap', () => {
Expand All @@ -286,6 +297,31 @@ describe('WalletAdvancedContent', () => {
expect(
screen.queryByTestId('ockWalletAdvancedQrReceive'),
).not.toBeInTheDocument();
expect(
screen.queryByTestId('ockWalletAdvancedSend'),
).not.toBeInTheDocument();
});

it('renders WalletAdvancedSend when activeFeature is send', () => {
mockUseWalletAdvancedContext.mockReturnValue({
...defaultMockUseWalletAdvancedContext,
activeFeature: 'send',
});

render(
<WalletAdvancedContent>
<div>WalletAdvancedContent</div>
</WalletAdvancedContent>,
);

expect(screen.getByTestId('ockWalletAdvancedSend')).toBeDefined();
expect(screen.queryByTestId('ockWalletAdvancedSend')).toBeInTheDocument();
expect(
screen.queryByTestId('ockWalletAdvancedQrReceive'),
).not.toBeInTheDocument();
expect(
screen.queryByTestId('ockWalletAdvancedSwap'),
).not.toBeInTheDocument();
});

it('correctly maps token balances to the swap component', () => {
Expand Down
9 changes: 9 additions & 0 deletions src/wallet/components/WalletAdvancedContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useWalletAdvancedContext } from './WalletAdvancedProvider';
import { WalletAdvancedQrReceive } from './WalletAdvancedQrReceive';
import { WalletAdvancedSwap } from './WalletAdvancedSwap';
import { useWalletContext } from './WalletProvider';
import { Send } from './wallet-advanced-send/components/Send';

export function WalletAdvancedContent({
children,
Expand Down Expand Up @@ -38,6 +39,14 @@ export function WalletAdvancedContent({
}, [isSubComponentClosing, setIsSubComponentOpen, setIsSubComponentClosing]);

const content = useMemo(() => {
if (activeFeature === 'send') {
return (
<ContentWrapper>
<Send className={cn('h-full w-full border-none')} />
</ContentWrapper>
);
}

if (activeFeature === 'qr') {
return (
<ContentWrapper>
Expand Down
13 changes: 8 additions & 5 deletions src/wallet/components/WalletAdvancedTransactionActions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,19 @@ describe('WalletAdvancedTransactionActons', () => {
expect(window.open).not.toHaveBeenCalled();
});

it('opens the send page when the send button is clicked', () => {
it('sets activeFeature to send when the send button is clicked', () => {
mockUseWalletAdvancedContext.mockReturnValue(
defaultMockUseWalletAdvancedContext,
);

render(<WalletAdvancedTransactionActions />);

const sendButton = screen.getByRole('button', { name: 'Send' });
fireEvent.click(sendButton);

expect(window.open).toHaveBeenCalledWith(
'https://wallet.coinbase.com',
'_blank',
);
expect(
defaultMockUseWalletAdvancedContext.setActiveFeature,
).toHaveBeenCalledWith('send');
});

it('sets activeFeature to swap when the swap button is clicked', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/components/WalletAdvancedTransactionActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export function WalletAdvancedTransactionActions({

const handleSend = useCallback(() => {
handleAnalyticsOptionSelected(WalletOption.Send);
window.open('https://wallet.coinbase.com', '_blank');
}, [handleAnalyticsOptionSelected]);
setActiveFeature('send');
}, [handleAnalyticsOptionSelected, setActiveFeature]);

const handleSwap = useCallback(() => {
handleAnalyticsOptionSelected(WalletOption.Swap);
Expand Down
188 changes: 188 additions & 0 deletions src/wallet/components/wallet-advanced-send/components/Send.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import { Skeleton } from '@/internal/components/Skeleton';
import { render, screen } from '@testing-library/react';
// import type { Address } from 'viem';
import { beforeEach, describe, expect, it, vi } from 'vitest';
// import { ETH_REQUIRED_FOR_SEND } from '../constants';
import type { SendContextType } from '../types';
import { Send } from './Send';
// import { SendAddressSelection } from './SendAddressSelection';
// import { SendAmountInput } from './SendAmountInput';
// import { SendButton } from './SendButton';
import { SendFundWallet } from './SendFundWallet';
import { SendHeader } from './SendHeader';
import { SendProvider, useSendContext } from './SendProvider';
// import { SendTokenSelector } from './SendTokenSelector';

// Mock all dependencies
vi.mock('@/internal/components/Skeleton');
vi.mock('@/internal/hooks/useTheme');
// vi.mock('./SendAddressSelection');
// vi.mock('./SendAmountInput');
// vi.mock('./SendButton');
vi.mock('./SendFundWallet');
vi.mock('./SendHeader');
vi.mock('./SendProvider', () => ({
SendProvider: vi.fn(({ children }) => (
<div data-testid="mock-send-provider">{children}</div>
)),
useSendContext: vi.fn(),
}));
vi.mock('./SendTokenSelector');

// const mockSelectedtoken = {
// name: 'USD Coin',
// address: '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913' as Address,
// symbol: 'USDC',
// decimals: 6,
// image:
// 'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/44/2b/442b80bd16af0c0d9b22e03a16753823fe826e5bfd457292b55fa0ba8c1ba213-ZWUzYjMZGUtMDYxNy00NDcyLTg0NjQtMWI4OGEwYjBiODE2',
// chainId: 8453,
// cryptoBalance: 69,
// fiatBalance: 69,
// };

describe('Send', () => {
beforeEach(() => {
vi.clearAllMocks();
});

it('renders with SendProvider and applies correct classes', () => {
vi.mocked(useSendContext).mockReturnValue({
isInitialized: false,
ethBalance: 0,
selectedRecipientAddress: { value: null, display: '' },
selectedToken: null,
} as SendContextType);

render(<Send />);

expect(SendProvider).toHaveBeenCalled();
const sendContainer = screen.getByTestId('ockSend');
expect(sendContainer).toHaveClass('h-96 w-88 flex flex-col p-4');
});

it('applies custom className when provided', () => {
vi.mocked(useSendContext).mockReturnValue({
isInitialized: false,
ethBalance: 0,
selectedRecipientAddress: { value: null, display: '' },
selectedToken: null,
} as SendContextType);

render(<Send className="custom-class" />);

const sendContainer = screen.getByTestId('ockSend');
expect(sendContainer).toHaveClass('custom-class');
});

it('renders custom children when provided', () => {
const customChildren = (
<div data-testid="custom-children">Custom Content</div>
);
render(<Send>{customChildren}</Send>);

expect(screen.getByTestId('custom-children')).toBeInTheDocument();
});

describe('SendDefaultChildren', () => {
beforeEach(() => {
vi.clearAllMocks();
});

it('renders skeleton when not initialized', () => {
vi.mocked(useSendContext).mockReturnValue({
isInitialized: false,
ethBalance: 0,
selectedRecipientAddress: { value: null, display: '' },
selectedToken: null,
} as SendContextType);

render(<Send />);

expect(Skeleton).toHaveBeenCalledWith(
expect.objectContaining({ className: 'h-full w-full' }),
{},
);
});

it('renders SendFundWallet when wallet has insufficient ETH', () => {
vi.mocked(useSendContext).mockReturnValue({
isInitialized: true,
ethBalance: 0,
} as SendContextType);

render(<Send />);

expect(SendHeader).toHaveBeenCalled();
expect(SendFundWallet).toHaveBeenCalled();
// expect(SendAddressSelection).not.toHaveBeenCalled();
});

it('renders a placeholder when wallet has sufficient ETH', () => {
vi.mocked(useSendContext).mockReturnValue({
isInitialized: true,
ethBalance: 0,
} as SendContextType);

render(<Send />);

expect(SendHeader).toHaveBeenCalled();
expect(screen.getByText('This wallet has ETH.')).toBeInTheDocument();

Check failure on line 130 in src/wallet/components/wallet-advanced-send/components/Send.test.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

src/wallet/components/wallet-advanced-send/components/Send.test.tsx > Send > SendDefaultChildren > renders a placeholder when wallet has sufficient ETH

TestingLibraryElementError: Unable to find an element with the text: This wallet has ETH.. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. Ignored nodes: comments, script, style <body> <div> <div data-testid="mock-send-provider" > <div class="ock-bg-default ock-border-radius ock-border-line-default border ock-text-foreground h-96 w-88 flex flex-col p-4" data-testid="ockSend" /> </div> </div> </body> ❯ Object.getElementError node_modules/@testing-library/react/node_modules/@testing-library/dom/dist/config.js:37:19 ❯ node_modules/@testing-library/react/node_modules/@testing-library/dom/dist/query-helpers.js:76:38 ❯ node_modules/@testing-library/react/node_modules/@testing-library/dom/dist/query-helpers.js:52:17 ❯ node_modules/@testing-library/react/node_modules/@testing-library/dom/dist/query-helpers.js:95:19 ❯ src/wallet/components/wallet-advanced-send/components/Send.test.tsx:130:21
});

// it('renders SendAddressSelection when wallet has sufficient ETH', () => {
// vi.mocked(useSendContext).mockReturnValue({
// isInitialized: true,
// ethBalance: ETH_REQUIRED_FOR_SEND + 0.0000000001, // de-minimis amount above ETH_REQUIRED_FOR_SEND
// selectedRecipientAddress: { value: null, display: '' },
// selectedToken: null,
// } as SendContextType);

// render(<Send />);

// expect(SendHeader).toHaveBeenCalled();
// expect(SendAddressSelection).toHaveBeenCalled();
// expect(SendFundWallet).not.toHaveBeenCalled();
// expect(SendTokenSelector).not.toHaveBeenCalled();
// });

// it('renders SendTokenSelector when recipient address is selected but token is not', () => {
// vi.mocked(useSendContext).mockReturnValue({
// isInitialized: true,
// ethBalance: ETH_REQUIRED_FOR_SEND + 0.0000000001, // de-minimis amount above ETH_REQUIRED_FOR_SEND
// selectedRecipientAddress: {
// value: '0x1234567890123456789012345678901234567890' as Address,
// display: '0x1234567890123456789012345678901234567890',
// },
// selectedToken: null,
// } as SendContextType);

// render(<Send />);

// expect(SendHeader).toHaveBeenCalled();
// expect(SendAddressSelection).toHaveBeenCalled();
// expect(SendTokenSelector).toHaveBeenCalled();
// expect(SendAmountInput).not.toHaveBeenCalled();
// });

// it('renders SendAmountInput and SendButton when both recipient and token are selected', () => {
// vi.mocked(useSendContext).mockReturnValue({
// isInitialized: true,
// ethBalance: ETH_REQUIRED_FOR_SEND + 0.0000001,
// selectedRecipientAddress: {
// value: '0x1234567890123456789012345678901234567890' as Address,
// display: '0x1234567890123456789012345678901234567890',
// },
// selectedToken: mockSelectedtoken,
// } as SendContextType);

// render(<Send />);

// expect(SendHeader).toHaveBeenCalled();
// expect(SendAddressSelection).toHaveBeenCalled();
// expect(SendAmountInput).toHaveBeenCalled();
// expect(SendTokenSelector).toHaveBeenCalled();
// expect(SendButton).toHaveBeenCalled();
// });
});
});
Loading
Loading