-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b54db1
commit 936a1d1
Showing
6 changed files
with
407 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest'; | ||
import { BuyOnrampItem } from './BuyOnrampItem'; | ||
import { useBuyContext } from './BuyProvider'; | ||
|
||
vi.mock('./BuyProvider', () => ({ | ||
useBuyContext: vi.fn(), | ||
})); | ||
|
||
vi.mock('../../internal/svg', () => ({ | ||
appleSvg: <svg data-testid="appleSvg" />, | ||
cardSvg: <svg data-testid="cardSvg" />, | ||
coinbaseLogoSvg: <svg data-testid="coinbaseLogoSvg" />, | ||
})); | ||
|
||
describe('BuyOnrampItem', () => { | ||
const mockSetIsDropdownOpen = vi.fn(); | ||
const mockOnClick = vi.fn(); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
(useBuyContext as Mock).mockReturnValue({ | ||
setIsDropdownOpen: mockSetIsDropdownOpen, | ||
}); | ||
}); | ||
|
||
it('renders correctly with provided props', () => { | ||
render( | ||
<BuyOnrampItem | ||
name="Apple Pay" | ||
description="Fast and secure payments." | ||
onClick={mockOnClick} | ||
icon="applePay" | ||
/>, | ||
); | ||
|
||
expect(screen.getByRole('button')).toBeInTheDocument(); | ||
expect(screen.getByText('Apple Pay')).toBeInTheDocument(); | ||
expect(screen.getByText('Fast and secure payments.')).toBeInTheDocument(); | ||
expect(screen.getByTestId('appleSvg')).toBeInTheDocument(); | ||
}); | ||
|
||
it('handles icon rendering based on the icon prop', () => { | ||
render( | ||
<BuyOnrampItem | ||
name="Credit Card" | ||
description="Use your card to pay." | ||
onClick={mockOnClick} | ||
icon="creditCard" | ||
/>, | ||
); | ||
|
||
expect(screen.getByTestId('cardSvg')).toBeInTheDocument(); | ||
}); | ||
|
||
it('triggers onClick and closes dropdown on button click', () => { | ||
render( | ||
<BuyOnrampItem | ||
name="Coinbase Pay" | ||
description="Pay using your Coinbase account." | ||
onClick={mockOnClick} | ||
icon="coinbasePay" | ||
/>, | ||
); | ||
|
||
const button = screen.getByRole('button'); | ||
fireEvent.click(button); | ||
|
||
expect(mockSetIsDropdownOpen).toHaveBeenCalledWith(false); | ||
expect(mockOnClick).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('applies correct styling and attributes to the button', () => { | ||
render( | ||
<BuyOnrampItem | ||
name="Apple Pay" | ||
description="Fast and secure payments." | ||
onClick={mockOnClick} | ||
icon="applePay" | ||
/>, | ||
); | ||
|
||
const button = screen.getByRole('button'); | ||
expect(button).toHaveClass('flex items-center gap-2 rounded-lg p-2'); | ||
expect(button).toHaveAttribute('type', 'button'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest'; | ||
import { BuyTokenItem } from './BuyTokenItem'; | ||
import { useBuyContext } from './BuyProvider'; | ||
import { getRoundedAmount } from '../../core/utils/getRoundedAmount'; | ||
import { ethToken } from '../../token/constants'; | ||
|
||
vi.mock('./BuyProvider', () => ({ | ||
useBuyContext: vi.fn(), | ||
})); | ||
|
||
vi.mock('../../core/utils/getRoundedAmount', () => ({ | ||
getRoundedAmount: vi.fn((value) => value), | ||
})); | ||
|
||
const ethSwapUnit = { | ||
token: ethToken, | ||
amount: '10.5', | ||
balance: '20', | ||
amountUSD: '10.5', | ||
loading: false, | ||
setAmount: vi.fn(), | ||
setAmountUSD: vi.fn(), | ||
setLoading: vi.fn(), | ||
}; | ||
|
||
describe('BuyTokenItem', () => { | ||
const mockHandleSubmit = vi.fn(); | ||
const mockSetIsDropdownOpen = vi.fn(); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
(useBuyContext as Mock).mockReturnValue({ | ||
handleSubmit: mockHandleSubmit, | ||
setIsDropdownOpen: mockSetIsDropdownOpen, | ||
}); | ||
}); | ||
|
||
it('renders null when swapUnit is undefined or has no token', () => { | ||
const { container } = render(<BuyTokenItem swapUnit={undefined} />); | ||
expect(container.firstChild).toBeNull(); | ||
expect(screen.queryByRole('button')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('renders correctly with valid swapUnit', () => { | ||
render(<BuyTokenItem swapUnit={ethSwapUnit} />); | ||
|
||
expect(screen.getByRole('button')).toBeInTheDocument(); | ||
expect(screen.getByText('10.5 ETH')).toBeInTheDocument(); | ||
expect(screen.getByText('Balance: 20')).toBeInTheDocument(); | ||
const button = screen.getByRole('button'); | ||
expect(button).toHaveClass('hover:bg-[var(--ock-bg-inverse)]', { | ||
exact: false, | ||
}); | ||
}); | ||
|
||
it('disables button and applies muted styling when balance is insufficient', () => { | ||
const swapUnit = { | ||
token: ethToken, | ||
amount: '10.5', | ||
balance: '5', | ||
amountUSD: '10.5', | ||
loading: false, | ||
setAmount: vi.fn(), | ||
setAmountUSD: vi.fn(), | ||
setLoading: vi.fn(), | ||
}; | ||
|
||
render(<BuyTokenItem swapUnit={swapUnit} />); | ||
|
||
const button = screen.getByRole('button'); | ||
expect(button).toBeDisabled(); | ||
expect(button).not.toHaveClass('hover:bg-[var(--ock-bg-inverse)]', { | ||
exact: false, | ||
}); | ||
expect(screen.getByText('Balance: 5')).toHaveClass('text-xs'); | ||
}); | ||
|
||
it('triggers handleSubmit and closes dropdown on click', () => { | ||
render(<BuyTokenItem swapUnit={ethSwapUnit} />); | ||
|
||
const button = screen.getByRole('button'); | ||
fireEvent.click(button); | ||
|
||
expect(mockSetIsDropdownOpen).toHaveBeenCalledWith(false); | ||
expect(mockHandleSubmit).toHaveBeenCalledWith(ethSwapUnit); | ||
}); | ||
|
||
it('formats amount and balance using getRoundedAmount', () => { | ||
const swapUnit = { | ||
token: ethToken, | ||
amount: '10.5678', | ||
balance: '20.1234', | ||
amountUSD: '10.5', | ||
loading: false, | ||
setAmount: vi.fn(), | ||
setAmountUSD: vi.fn(), | ||
setLoading: vi.fn(), | ||
}; | ||
|
||
(getRoundedAmount as Mock).mockImplementation((value) => value.slice(0, 4)); | ||
|
||
render(<BuyTokenItem swapUnit={swapUnit} />); | ||
|
||
expect(getRoundedAmount).toHaveBeenCalledWith('10.5678', 10); | ||
expect(getRoundedAmount).toHaveBeenCalledWith('20.1234', 10); | ||
expect(screen.getByText('10.5 ETH')).toBeInTheDocument(); | ||
expect(screen.getByText('Balance: 20.1')).toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.