- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.4k
fix: resolve prop override in PerpsEmptyState component #20534
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -11,12 +11,10 @@ import emptyStatePerpsLight from '../../../../../images/empty-state-perps-light. | |
| import emptyStatePerpsDark from '../../../../../images/empty-state-perps-dark.png'; | ||
|  | ||
| export interface PerpsEmptyStateProps extends TabEmptyStateProps { | ||
| onStartTrading: () => void; | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing  | ||
| testID?: string; | ||
| } | ||
|  | ||
| export const PerpsEmptyState: React.FC<PerpsEmptyStateProps> = ({ | ||
| onStartTrading, | ||
| testID, | ||
| ...props | ||
| }) => { | ||
|  | @@ -36,7 +34,6 @@ export const PerpsEmptyState: React.FC<PerpsEmptyStateProps> = ({ | |
| } | ||
| description={strings('perps.position.list.first_time_description')} | ||
| actionButtonText={strings('perps.position.list.start_trading')} | ||
| onAction={onStartTrading} | ||
| testID={testID} | ||
| {...props} | ||
| /> | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -24,6 +24,7 @@ import type { Position } from '../../controllers/types'; | |
| import { usePerpsLivePositions, usePerpsTPSLUpdate } from '../../hooks'; | ||
| import { usePerpsLiveAccount } from '../../hooks/stream'; | ||
| import { formatPnl, formatPrice } from '../../utils/formatUtils'; | ||
| import { getPositionDirection } from '../../utils/positionCalculations'; | ||
| import { calculateTotalPnL } from '../../utils/pnlCalculations'; | ||
| import { createStyles } from './PerpsPositionsView.styles'; | ||
| import { SafeAreaView } from 'react-native-safe-area-context'; | ||
|  | @@ -125,14 +126,7 @@ const PerpsPositionsView: React.FC = () => { | |
| </Text> | ||
| </View> | ||
| {positions.map((position, index) => { | ||
| const sizeValue = parseFloat(position.size); | ||
| const directionSegment = Number.isFinite(sizeValue) | ||
| ? sizeValue > 0 | ||
| ? 'long' | ||
| : sizeValue < 0 | ||
| ? 'short' | ||
| : 'unknown' | ||
| : 'unknown'; | ||
| const directionSegment = getPositionDirection(position.size); | ||
| 
      Comment on lines
    
      -128
     to 
      +129
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was getting SonarCloud code quality errors in this PR due to these ternary operators, which were unrelated to my changes. I moved the logic into a      | ||
| return ( | ||
| <View | ||
| key={`${position.coin}-${index}`} | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -184,17 +184,17 @@ jest.mock('../../components/PerpsBottomSheetTooltip', () => ({ | |
| // Mock PerpsEmptyState component to avoid Redux context issues while preserving testID | ||
| jest.mock('../PerpsEmptyState', () => ({ | ||
| PerpsEmptyState: ({ | ||
| onStartTrading, | ||
| onAction, | ||
| testID, | ||
| }: { | ||
| onStartTrading: () => void; | ||
| onAction?: () => void; | ||
| testID?: string; | ||
| }) => { | ||
| const { TouchableOpacity, Text, View } = jest.requireActual('react-native'); | ||
| return ( | ||
| <View testID={testID}> | ||
| <Text>Bet on price movements with up to 40x leverage.</Text> | ||
| <TouchableOpacity onPress={onStartTrading}> | ||
| <TouchableOpacity onPress={onAction}> | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updating mock | ||
| <Text>Start trading</Text> | ||
| </TouchableOpacity> | ||
| </View> | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -38,6 +38,7 @@ import { | |
| usePerpsLivePositions, | ||
| usePerpsPerformance, | ||
| } from '../../hooks'; | ||
| import { getPositionDirection } from '../../utils/positionCalculations'; | ||
| import { usePerpsLiveAccount, usePerpsLiveOrders } from '../../hooks/stream'; | ||
| import { selectPerpsEligibility } from '../../selectors/perpsController'; | ||
| import styleSheet from './PerpsTabView.styles'; | ||
|  | @@ -208,14 +209,7 @@ const PerpsTabView: React.FC<PerpsTabViewProps> = () => { | |
| </View> | ||
| <View> | ||
| {positions.map((position, index) => { | ||
| const sizeValue = parseFloat(position.size); | ||
| const directionSegment = Number.isFinite(sizeValue) | ||
| ? sizeValue > 0 | ||
| ? 'long' | ||
| : sizeValue < 0 | ||
| ? 'short' | ||
| : 'unknown' | ||
| : 'unknown'; | ||
| const directionSegment = getPositionDirection(position.size); | ||
| return ( | ||
| <View | ||
| key={`${position.coin}-${index}`} | ||
|  | @@ -246,7 +240,7 @@ const PerpsTabView: React.FC<PerpsTabViewProps> = () => { | |
| <View style={styles.contentContainer}> | ||
| {!isInitialLoading && hasNoPositionsOrOrders ? ( | ||
| <PerpsEmptyState | ||
| onStartTrading={handleNewTrade} | ||
| onAction={handleNewTrade} | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updating instance in PerpsTabView | ||
| testID="perps-empty-state" | ||
| twClassName="mx-auto" | ||
| /> | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -5,6 +5,7 @@ import { | |
| calculateCloseValue, | ||
| calculatePercentageFromTokenAmount, | ||
| calculatePercentageFromUSDAmount, | ||
| getPositionDirection, | ||
| } from './positionCalculations'; | ||
|  | ||
| describe('Position Calculations Utils', () => { | ||
|  | @@ -193,4 +194,50 @@ describe('Position Calculations Utils', () => { | |
| expect(result).toBe(0); | ||
| }); | ||
| }); | ||
|  | ||
| describe('getPositionDirection', () => { | ||
| it('returns "long" for positive position sizes', () => { | ||
| expect(getPositionDirection('10.5')).toBe('long'); | ||
| expect(getPositionDirection('0.01')).toBe('long'); | ||
| expect(getPositionDirection('1000')).toBe('long'); | ||
| }); | ||
|  | ||
| it('returns "short" for negative position sizes', () => { | ||
| expect(getPositionDirection('-10.5')).toBe('short'); | ||
| expect(getPositionDirection('-0.01')).toBe('short'); | ||
| expect(getPositionDirection('-1000')).toBe('short'); | ||
| }); | ||
|  | ||
| it('returns "unknown" for zero position size', () => { | ||
| expect(getPositionDirection('0')).toBe('unknown'); | ||
| expect(getPositionDirection('0.0')).toBe('unknown'); | ||
| expect(getPositionDirection('-0')).toBe('unknown'); | ||
| }); | ||
|  | ||
| it('returns "unknown" for invalid strings', () => { | ||
| expect(getPositionDirection('abc')).toBe('unknown'); | ||
| expect(getPositionDirection('not a number')).toBe('unknown'); | ||
| expect(getPositionDirection('')).toBe('unknown'); | ||
| expect(getPositionDirection(' ')).toBe('unknown'); | ||
| }); | ||
|  | ||
| it('returns "unknown" for non-finite values', () => { | ||
| expect(getPositionDirection('Infinity')).toBe('unknown'); | ||
| expect(getPositionDirection('-Infinity')).toBe('unknown'); | ||
| expect(getPositionDirection('NaN')).toBe('unknown'); | ||
| }); | ||
|  | ||
| it('handles edge cases correctly', () => { | ||
| expect(getPositionDirection('1e-10')).toBe('long'); // Very small positive | ||
| expect(getPositionDirection('-1e-10')).toBe('short'); // Very small negative | ||
| expect(getPositionDirection('1.23e15')).toBe('long'); // Large positive | ||
| expect(getPositionDirection('-1.23e15')).toBe('short'); // Large negative | ||
| }); | ||
|  | ||
| it('handles strings with whitespace', () => { | ||
| expect(getPositionDirection(' 10.5 ')).toBe('long'); | ||
| expect(getPositionDirection(' -10.5 ')).toBe('short'); | ||
| expect(getPositionDirection(' 0 ')).toBe('unknown'); | ||
| }); | ||
| }); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding tests | ||
| }); | ||
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.
Updating tests