Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alissacrane-cb committed Dec 17, 2024
1 parent a43a105 commit c9daec4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
17 changes: 13 additions & 4 deletions src/buy/components/Buy.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest';
import { useOnchainKit } from '../../core-react/useOnchainKit';
import { degenToken } from '../../token/constants';
import { useOutsideClick } from '../../ui/react/internal/hooks/useOutsideClick';
import { Buy } from './Buy';
Expand All @@ -24,6 +25,10 @@ vi.mock('../../ui/react/internal/hooks/useOutsideClick', () => ({
useOutsideClick: vi.fn(),
}));

vi.mock('../../core-react/useOnchainKit', () => ({
useOnchainKit: vi.fn(),
}));

type useOutsideClickType = ReturnType<
typeof vi.fn<
(
Expand Down Expand Up @@ -61,11 +66,15 @@ describe('Buy', () => {
},
);

(useOnchainKit as Mock).mockReturnValue({
projectId: 'mock-project-id',
});

vi.clearAllMocks();
});

it('renders the Buy component', () => {
render(<Buy className="test-class" toToken={degenToken} projectId="123" />);
render(<Buy className="test-class" toToken={degenToken} />);

expect(screen.getByText('Buy')).toBeInTheDocument();
expect(screen.getByText('DEGEN')).toBeInTheDocument();
Expand All @@ -88,7 +97,7 @@ describe('Buy', () => {
},
});

render(<Buy className="test-class" toToken={degenToken} projectId="123" />);
render(<Buy className="test-class" toToken={degenToken} />);

expect(screen.getByTestId('mock-BuyDropdown')).toBeDefined();
mockOutsideClickCallback({} as MouseEvent);
Expand All @@ -113,15 +122,15 @@ describe('Buy', () => {
},
});

render(<Buy className="test-class" toToken={degenToken} projectId="123" />);
render(<Buy className="test-class" toToken={degenToken} />);

expect(screen.getByTestId('mock-BuyDropdown')).toBeDefined();
fireEvent.click(screen.getByTestId('mock-BuyDropdown'));
expect(mockSetIsOpen).not.toHaveBeenCalled();
});

it('should not trigger click handler when dropdown is closed', () => {
render(<Buy className="test-class" toToken={degenToken} projectId="123" />);
render(<Buy className="test-class" toToken={degenToken} />);
expect(screen.queryByTestId('mock-BuyDropdown')).not.toBeInTheDocument();
});
});
31 changes: 28 additions & 3 deletions src/buy/components/BuyProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import React, { act, useCallback, useEffect } from 'react';
import type { TransactionReceipt } from 'viem';
import {
type Mock,
afterEach,
beforeEach,
describe,
Expand All @@ -30,6 +31,7 @@ import { mock } from 'wagmi/connectors';
import { useSendCalls } from 'wagmi/experimental';
import { useCapabilitiesSafe } from '../../core-react/internal/hooks/useCapabilitiesSafe';
import { buildSwapTransaction } from '../../core/api/buildSwapTransaction';
import { useOnchainKit } from '../../core-react/useOnchainKit';
import { getBuyQuote } from '../utils/getBuyQuote';
import {
daiToken,
Expand Down Expand Up @@ -62,6 +64,10 @@ vi.mock('../../swap/utils/processSwapTransaction', () => ({
processSwapTransaction: vi.fn(),
}));

vi.mock('../../core-react/useOnchainKit', () => ({
useOnchainKit: vi.fn(),
}));

// vi.mock('../../swap/utils/isSwapError', () => ({
// isSwapError: vi.fn(),
// }));
Expand Down Expand Up @@ -165,6 +171,12 @@ const renderWithProviders = ({
}) => {
const config = { maxSlippage: 10 };
const mockExperimental = { useAggregator: true };
(useOnchainKit as Mock).mockReturnValue({
projectId: 'mock-project-id',
config: {
paymaster: undefined,
},
});
return render(
<WagmiProvider config={accountConfig}>
<QueryClientProvider client={queryClient}>
Expand Down Expand Up @@ -266,6 +278,12 @@ const TestSwapComponent = () => {

describe('useBuyContext', () => {
beforeEach(async () => {
(useOnchainKit as Mock).mockReturnValue({
projectId: 'mock-project-id',
config: {
paymaster: undefined,
},
});
vi.resetAllMocks();
(useAccount as ReturnType<typeof vi.fn>).mockReturnValue({
address: '0x123',
Expand All @@ -278,6 +296,7 @@ describe('useBuyContext', () => {
(useSwitchChain as ReturnType<typeof vi.fn>).mockReturnValue({
switchChainAsync: mockSwitchChain,
});

await act(async () => {
renderWithProviders({ Component: () => null });
});
Expand Down Expand Up @@ -332,6 +351,12 @@ describe('BuyProvider', () => {
switchChainAsync: mockSwitchChain,
});
(useCapabilitiesSafe as ReturnType<typeof vi.fn>).mockReturnValue({});
(useOnchainKit as Mock).mockReturnValue({
projectId: 'mock-project-id',
config: {
paymaster: undefined,
},
});
});

it('should reset inputs when setLifecycleStatus is called with success', async () => {
Expand Down Expand Up @@ -705,7 +730,7 @@ describe('BuyProvider', () => {
expect(result.current.lifecycleStatus).toEqual({
statusName: 'error',
statusData: expect.objectContaining({
code: 'TmSPc01',
code: 'TmBPc02',
error: JSON.stringify(mockError),
message: '',
}),
Expand Down Expand Up @@ -773,7 +798,7 @@ describe('BuyProvider', () => {
expect(
screen.getByTestId('context-value-lifecycleStatus-statusData-code')
.textContent,
).toBe('TmSPc02');
).toBe('TmBPc03');
});
});

Expand All @@ -790,7 +815,7 @@ describe('BuyProvider', () => {
expect(
screen.getByTestId('context-value-lifecycleStatus-statusData-code')
.textContent,
).toBe('TmSPc02');
).toBe('TmBPc03');
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/buy/components/BuyProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export function BuyProvider({
}: BuyProviderReact) {
const {
config: { paymaster } = { paymaster: undefined },
projectId,
} = useOnchainKit();
const { address, chainId } = useAccount();
const { switchChainAsync } = useSwitchChain();
Expand Down Expand Up @@ -89,7 +90,6 @@ export function BuyProvider({
// Refreshes balances and inputs post-swap
const resetInputs = useResetBuyInputs({ fromETH, fromUSDC, from, to });

const { projectId } = useOnchainKit();
if (!projectId) {
throw new Error(
'Buy: Project ID is required, please set the projectId in the OnchainKitProvider',
Expand Down
2 changes: 1 addition & 1 deletion src/internal/svg/appleSvg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const appleSvg = (
id="Artwork"
data-testid="appleSvg"
>
<title>Apple Pay</title>
<title>Apple Pay Onramp</title>
<path
id="XMLID_4_"
d="M150.7 0h-139c-1 0-2.1.1-3.1.3-1 .2-2 .5-3 1-.9.4-1.8 1.1-2.5 1.8S1.7 4.7 1.3 5.6c-.5.9-.8 1.9-1 3-.2 1-.2 2.1-.3 3.1v82.5c0 1 .1 2.1.3 3.1.2 1 .5 2 1 3 .5.9 1.1 1.8 1.8 2.5s1.6 1.4 2.5 1.8c.9.5 1.9.8 3 1 1 .2 2.1.2 3.1.3h142.1c1 0 2.1-.1 3.1-.3 1-.2 2-.5 3-1 .9-.5 1.8-1.1 2.5-1.8s1.4-1.6 1.8-2.5c.5-.9.8-1.9 1-3 .2-1 .2-2.1.3-3.1v-1.4-78-1.7-1.4c0-1-.1-2.1-.3-3.1-.2-1-.5-2-1-3-.5-.9-1.1-1.8-1.8-2.5s-1.6-1.4-2.5-1.8c-.9-.5-1.9-.8-3-1-1-.2-2.1-.2-3.1-.3H150.7z"
Expand Down
2 changes: 1 addition & 1 deletion src/internal/svg/cardSvg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const cardSvg = (
strokeLinejoin="round"
data-testid="cardSvg"
>
<title>Debit Card</title>
<title>Debit Card Onramp</title>
<rect x="2" y="4" width="20" height="16" rx="2" />
<path d="M7 15h0M2 9.5h20" />
</svg>
Expand Down
2 changes: 1 addition & 1 deletion src/internal/svg/coinbaseLogoSvg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const coinbaseLogoSvg = (
className={cn(icon.foreground)}
data-testid="coinbaseLogoSvg"
>
<title>Coinbase Pay</title>
<title>Coinbase Pay Onramp</title>
<path
d="M7.93154 12.5C5.74045 12.5 3.96577 10.6354 3.96577 8.33333C3.96577 6.03125 5.74045 4.16667 7.93154 4.16667C9.8946 4.16667 11.5239 5.67014 11.8378 7.63889H15.8333C15.4962 3.36111 12.089 0 7.93154 0C3.55267 0 0 3.73264 0 8.33333C0 12.934 3.55267 16.6667 7.93154 16.6667C12.089 16.6667 15.4962 13.3056 15.8333 9.02778H11.8378C11.5239 10.9965 9.8946 12.5 7.93154 12.5Z"
fill="#0052FF"
Expand Down

0 comments on commit c9daec4

Please sign in to comment.