-
Notifications
You must be signed in to change notification settings - Fork 237
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: add earn balance cards #1873
Merged
+370
−1
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
90dbd24
add earn balance cards
alissacrane-cb 5d5f730
add test coverage
alissacrane-cb 417c772
fix lint
alissacrane-cb 9aae183
fix lint
alissacrane-cb cbef981
add test coverage
alissacrane-cb 7a3f9ee
add aria lael
alissacrane-cb db999a5
remove keydown
alissacrane-cb 8e4dc30
rename func
alissacrane-cb 32ee35e
fix lint
alissacrane-cb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,77 @@ | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import type { Address } from 'viem'; | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import { DepositBalance } from './DepositBalance'; | ||
import { useEarnContext } from './EarnProvider'; | ||
|
||
vi.mock('./EarnProvider', () => ({ | ||
useEarnContext: vi.fn(), | ||
})); | ||
|
||
const baseContext = { | ||
convertedBalance: '1000', | ||
setDepositAmount: vi.fn(), | ||
vaultAddress: '0x123' as Address, | ||
depositAmount: '0', | ||
depositedAmount: '0', | ||
withdrawAmount: '0', | ||
setWithdrawAmount: vi.fn(), | ||
}; | ||
|
||
describe('DepositBalance', () => { | ||
it('renders the converted balance and subtitle correctly', () => { | ||
vi.mocked(useEarnContext).mockReturnValue(baseContext); | ||
|
||
render(<DepositBalance className="test-class" />); | ||
|
||
expect(screen.getByText('1000 USDC')).toBeInTheDocument(); | ||
expect(screen.getByText('Available to deposit')).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls setDepositAmount with convertedBalance when the action button is clicked', () => { | ||
const mockSetDepositAmount = vi.fn(); | ||
const mockContext = { | ||
...baseContext, | ||
convertedBalance: '1000', | ||
setDepositAmount: mockSetDepositAmount, | ||
}; | ||
|
||
vi.mocked(useEarnContext).mockReturnValue(mockContext); | ||
|
||
render(<DepositBalance className="test-class" />); | ||
|
||
const actionButton = screen.getByText('Use max'); | ||
fireEvent.click(actionButton); | ||
|
||
expect(mockSetDepositAmount).toHaveBeenCalledWith('1000'); | ||
}); | ||
|
||
it('does not render the action button when convertedBalance is null', () => { | ||
const mockContext = { | ||
...baseContext, | ||
convertedBalance: '', | ||
setDepositAmount: vi.fn(), | ||
}; | ||
|
||
vi.mocked(useEarnContext).mockReturnValue(mockContext); | ||
|
||
render(<DepositBalance className="test-class" />); | ||
|
||
expect(screen.queryByText('Use max')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('applies custom className', () => { | ||
const mockContext = { | ||
...baseContext, | ||
convertedBalance: '1000', | ||
setDepositAmount: vi.fn(), | ||
}; | ||
|
||
vi.mocked(useEarnContext).mockReturnValue(mockContext); | ||
|
||
render(<DepositBalance className="custom-class" />); | ||
|
||
const container = screen.getByTestId('ockEarnBalance'); | ||
expect(container).toHaveClass('custom-class'); | ||
}); | ||
}); |
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,24 @@ | ||
import { useCallback } from 'react'; | ||
import type { DepositBalanceReact } from '../types'; | ||
import { EarnBalance } from './EarnBalance'; | ||
import { useEarnContext } from './EarnProvider'; | ||
|
||
export function DepositBalance({ className }: DepositBalanceReact) { | ||
const { convertedBalance, setDepositAmount } = useEarnContext(); | ||
|
||
const handleMaxPress = useCallback(() => { | ||
if (convertedBalance) { | ||
setDepositAmount(convertedBalance); | ||
} | ||
}, [convertedBalance, setDepositAmount]); | ||
|
||
return ( | ||
<EarnBalance | ||
className={className} | ||
title={`${convertedBalance} USDC`} | ||
subtitle="Available to deposit" | ||
onActionPress={handleMaxPress} | ||
showAction={!!convertedBalance} | ||
/> | ||
); | ||
} |
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,80 @@ | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import { EarnBalance } from './EarnBalance'; | ||
|
||
describe('EarnBalance', () => { | ||
it('renders the title and subtitle correctly', () => { | ||
render( | ||
<EarnBalance | ||
title="Test Title" | ||
subtitle="Test Subtitle" | ||
showAction={false} | ||
onActionPress={() => {}} | ||
/>, | ||
); | ||
|
||
expect(screen.getByText('Test Title')).toBeInTheDocument(); | ||
expect(screen.getByText('Test Subtitle')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders the action button when showAction is true', () => { | ||
render( | ||
<EarnBalance | ||
title="Test Title" | ||
subtitle="Test Subtitle" | ||
showAction={true} | ||
onActionPress={() => {}} | ||
/>, | ||
); | ||
|
||
expect(screen.getByText('Use max')).toBeInTheDocument(); | ||
}); | ||
|
||
it('does not render the action button when showAction is false', () => { | ||
render( | ||
<EarnBalance | ||
title="Test Title" | ||
subtitle="Test Subtitle" | ||
showAction={false} | ||
onActionPress={() => {}} | ||
/>, | ||
); | ||
|
||
expect(screen.queryByText('Use max')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('calls onActionPress when the action button is clicked', () => { | ||
const mockOnActionPress = vi.fn(); | ||
|
||
render( | ||
<EarnBalance | ||
title="Test Title" | ||
subtitle="Test Subtitle" | ||
showAction={true} | ||
onActionPress={mockOnActionPress} | ||
/>, | ||
); | ||
|
||
const actionButton = screen.getByText('Use max'); | ||
fireEvent.click(actionButton); | ||
|
||
expect(mockOnActionPress).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('applies custom className', () => { | ||
const customClass = 'custom-class'; | ||
|
||
render( | ||
<EarnBalance | ||
title="Test Title" | ||
subtitle="Test Subtitle" | ||
className={customClass} | ||
showAction={false} | ||
onActionPress={() => {}} | ||
/>, | ||
); | ||
|
||
const container = screen.getByTestId('ockEarnBalance'); | ||
expect(container).toHaveClass(customClass); | ||
}); | ||
}); |
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,37 @@ | ||
import { background, border, cn, color, text } from '@/styles/theme'; | ||
import type { EarnBalanceReact } from '../types'; | ||
|
||
export function EarnBalance({ | ||
className, | ||
onActionPress, | ||
title, | ||
subtitle, | ||
showAction = false, | ||
}: EarnBalanceReact) { | ||
return ( | ||
<div | ||
className={cn( | ||
background.alternate, | ||
border.radius, | ||
'flex items-center justify-between gap-4 p-3 px-4', | ||
className, | ||
)} | ||
data-testid="ockEarnBalance" | ||
> | ||
<div className={cn('flex flex-col', color.foreground)}> | ||
<div className={text.headline}>{title}</div> | ||
<div className={cn(text.label2, color.foregroundMuted)}>{subtitle}</div> | ||
</div> | ||
{showAction && ( | ||
<button | ||
onClick={onActionPress} | ||
className={cn(text.label2, color.primary)} | ||
type="button" | ||
aria-label="Use max" | ||
> | ||
Use max | ||
</button> | ||
)} | ||
</div> | ||
); | ||
} |
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
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
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,77 @@ | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import type { Address } from 'viem'; | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import { useEarnContext } from './EarnProvider'; | ||
import { WithdrawBalance } from './WithdrawBalance'; | ||
|
||
vi.mock('./EarnProvider', () => ({ | ||
useEarnContext: vi.fn(), | ||
})); | ||
|
||
const baseContext = { | ||
convertedBalance: '0', | ||
setDepositAmount: vi.fn(), | ||
vaultAddress: '0x123' as Address, | ||
depositAmount: '0', | ||
depositedAmount: '1000', | ||
withdrawAmount: '0', | ||
setWithdrawAmount: vi.fn(), | ||
}; | ||
|
||
describe('WithdrawBalance', () => { | ||
it('renders the converted balance and subtitle correctly', () => { | ||
vi.mocked(useEarnContext).mockReturnValue(baseContext); | ||
|
||
render(<WithdrawBalance className="test-class" />); | ||
|
||
expect(screen.getByText('1000 USDC')).toBeInTheDocument(); | ||
expect(screen.getByText('Available to withdraw')).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls setWithdrawAmount with convertedBalance when the action button is clicked', () => { | ||
const mocksetWithdrawAmount = vi.fn(); | ||
const mockContext = { | ||
...baseContext, | ||
depositedAmount: '1000', | ||
setWithdrawAmount: mocksetWithdrawAmount, | ||
}; | ||
|
||
vi.mocked(useEarnContext).mockReturnValue(mockContext); | ||
|
||
render(<WithdrawBalance className="test-class" />); | ||
|
||
const actionButton = screen.getByText('Use max'); | ||
fireEvent.click(actionButton); | ||
|
||
expect(mocksetWithdrawAmount).toHaveBeenCalledWith('1000'); | ||
}); | ||
|
||
it('does not render the action button when convertedBalance is null', () => { | ||
const mockContext = { | ||
...baseContext, | ||
depositedAmount: '', | ||
setWithdrawAmount: vi.fn(), | ||
}; | ||
|
||
vi.mocked(useEarnContext).mockReturnValue(mockContext); | ||
|
||
render(<WithdrawBalance className="test-class" />); | ||
|
||
expect(screen.queryByText('Use max')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('applies custom className', () => { | ||
const mockContext = { | ||
...baseContext, | ||
depositedAmount: '1000', | ||
setWithdrawAmount: vi.fn(), | ||
}; | ||
|
||
vi.mocked(useEarnContext).mockReturnValue(mockContext); | ||
|
||
render(<WithdrawBalance className="custom-class" />); | ||
|
||
const container = screen.getByTestId('ockEarnBalance'); | ||
expect(container).toHaveClass('custom-class'); | ||
}); | ||
}); |
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,24 @@ | ||
import { useCallback } from 'react'; | ||
import type { WithdrawBalanceReact } from '../types'; | ||
import { EarnBalance } from './EarnBalance'; | ||
import { useEarnContext } from './EarnProvider'; | ||
|
||
export function WithdrawBalance({ className }: WithdrawBalanceReact) { | ||
const { depositedAmount, setWithdrawAmount } = useEarnContext(); | ||
|
||
const handleMaxPress = useCallback(() => { | ||
if (depositedAmount) { | ||
setWithdrawAmount(depositedAmount); | ||
} | ||
}, [depositedAmount, setWithdrawAmount]); | ||
|
||
return ( | ||
<EarnBalance | ||
className={className} | ||
title={`${depositedAmount} USDC`} | ||
subtitle="Available to withdraw" | ||
onActionPress={handleMaxPress} | ||
showAction={!!depositedAmount} | ||
/> | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be fine for testing but will probably have to make this more generic given we will try and support all vaults not just USDC
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah lets leave as is and revisit when we're hooking up the actions