|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent } from '@testing-library/react-native'; |
| 3 | +import SecurityTrustEntryCard from './SecurityTrustEntryCard'; |
| 4 | +import { strings } from '../../../../../../locales/i18n'; |
| 5 | +import type { TokenSecurityData } from '../../types'; |
| 6 | +import type { TokenDetailsRouteParams } from '../../../TokenDetails/constants/constants'; |
| 7 | +import Routes from '../../../../../constants/navigation/Routes'; |
| 8 | + |
| 9 | +const mockNavigate = jest.fn(); |
| 10 | + |
| 11 | +jest.mock('@react-navigation/native', () => ({ |
| 12 | + ...jest.requireActual('@react-navigation/native'), |
| 13 | + useNavigation: () => ({ |
| 14 | + navigate: mockNavigate, |
| 15 | + }), |
| 16 | +})); |
| 17 | + |
| 18 | +const mockToken: TokenDetailsRouteParams = { |
| 19 | + address: '0x1234567890abcdef', |
| 20 | + chainId: '0x1', |
| 21 | + symbol: 'TEST', |
| 22 | + decimals: 18, |
| 23 | + name: 'Test Token', |
| 24 | + isNative: false, |
| 25 | + image: 'https://example.com/token.png', |
| 26 | + balance: '1000000000000000000', |
| 27 | + logo: 'https://example.com/logo.png', |
| 28 | + isETH: false, |
| 29 | +}; |
| 30 | + |
| 31 | +const mockSecurityData: TokenSecurityData = { |
| 32 | + resultType: 'Verified', |
| 33 | + maliciousScore: '0', |
| 34 | + features: [ |
| 35 | + { |
| 36 | + featureId: 'liquidity_pools', |
| 37 | + type: 'info', |
| 38 | + description: 'Has liquidity pools', |
| 39 | + }, |
| 40 | + { |
| 41 | + featureId: 'verified_contract', |
| 42 | + type: 'info', |
| 43 | + description: 'Contract is verified', |
| 44 | + }, |
| 45 | + ], |
| 46 | + fees: { |
| 47 | + transfer: 0, |
| 48 | + transferFeeMaxAmount: null, |
| 49 | + buy: 0.01, |
| 50 | + sell: 0.02, |
| 51 | + }, |
| 52 | + financialStats: { |
| 53 | + supply: 1000000000000000000000000, |
| 54 | + topHolders: [], |
| 55 | + holdersCount: 5000, |
| 56 | + tradeVolume24h: null, |
| 57 | + lockedLiquidityPct: null, |
| 58 | + markets: [], |
| 59 | + }, |
| 60 | + metadata: { |
| 61 | + externalLinks: { |
| 62 | + homepage: 'https://example.com', |
| 63 | + twitterPage: 'testtoken', |
| 64 | + telegramChannelId: null, |
| 65 | + }, |
| 66 | + }, |
| 67 | + created: '2023-01-01T00:00:00Z', |
| 68 | +}; |
| 69 | + |
| 70 | +describe('SecurityTrustEntryCard', () => { |
| 71 | + beforeEach(() => { |
| 72 | + jest.clearAllMocks(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('renders loading state with skeletons', () => { |
| 76 | + const { queryByText, getByTestId } = render( |
| 77 | + <SecurityTrustEntryCard |
| 78 | + securityData={null} |
| 79 | + isLoading |
| 80 | + token={mockToken} |
| 81 | + />, |
| 82 | + ); |
| 83 | + |
| 84 | + expect(queryByText(strings('security_trust.title'))).toBeNull(); |
| 85 | + expect(getByTestId('security-trust-entry-card')).toBeTruthy(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('renders security data with title and result label', () => { |
| 89 | + const { getByText } = render( |
| 90 | + <SecurityTrustEntryCard |
| 91 | + securityData={mockSecurityData} |
| 92 | + isLoading={false} |
| 93 | + token={mockToken} |
| 94 | + />, |
| 95 | + ); |
| 96 | + |
| 97 | + expect(getByText(strings('security_trust.title'))).toBeTruthy(); |
| 98 | + expect(getByText(strings('security_trust.known'))).toBeTruthy(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('displays arrow icon when details are available', () => { |
| 102 | + const { getByText } = render( |
| 103 | + <SecurityTrustEntryCard |
| 104 | + securityData={mockSecurityData} |
| 105 | + isLoading={false} |
| 106 | + token={mockToken} |
| 107 | + />, |
| 108 | + ); |
| 109 | + |
| 110 | + expect(getByText(strings('security_trust.title'))).toBeTruthy(); |
| 111 | + expect(getByText(strings('security_trust.known'))).toBeTruthy(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('navigates to security trust screen when pressed with details', () => { |
| 115 | + const { getByTestId } = render( |
| 116 | + <SecurityTrustEntryCard |
| 117 | + securityData={mockSecurityData} |
| 118 | + isLoading={false} |
| 119 | + token={mockToken} |
| 120 | + />, |
| 121 | + ); |
| 122 | + |
| 123 | + fireEvent.press(getByTestId('security-trust-entry-card')); |
| 124 | + |
| 125 | + expect(mockNavigate).toHaveBeenCalledWith(Routes.SECURITY_TRUST, { |
| 126 | + ...mockToken, |
| 127 | + securityData: mockSecurityData, |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + it('does not navigate when pressed without details', () => { |
| 132 | + const securityDataNoFeatures: TokenSecurityData = { |
| 133 | + resultType: 'Verified', |
| 134 | + maliciousScore: '0', |
| 135 | + features: [], |
| 136 | + fees: { |
| 137 | + transfer: 0, |
| 138 | + transferFeeMaxAmount: null, |
| 139 | + buy: 0, |
| 140 | + sell: null, |
| 141 | + }, |
| 142 | + financialStats: { |
| 143 | + supply: 0, |
| 144 | + topHolders: [], |
| 145 | + holdersCount: 0, |
| 146 | + tradeVolume24h: null, |
| 147 | + lockedLiquidityPct: null, |
| 148 | + markets: [], |
| 149 | + }, |
| 150 | + metadata: { |
| 151 | + externalLinks: { |
| 152 | + homepage: null, |
| 153 | + twitterPage: null, |
| 154 | + telegramChannelId: null, |
| 155 | + }, |
| 156 | + }, |
| 157 | + created: '2023-01-01T00:00:00Z', |
| 158 | + }; |
| 159 | + |
| 160 | + const { getByTestId } = render( |
| 161 | + <SecurityTrustEntryCard |
| 162 | + securityData={securityDataNoFeatures} |
| 163 | + isLoading={false} |
| 164 | + token={mockToken} |
| 165 | + />, |
| 166 | + ); |
| 167 | + |
| 168 | + fireEvent.press(getByTestId('security-trust-entry-card')); |
| 169 | + |
| 170 | + expect(mockNavigate).not.toHaveBeenCalled(); |
| 171 | + }); |
| 172 | + |
| 173 | + it('does not display arrow icon when no details available', () => { |
| 174 | + const securityDataNoFeatures: TokenSecurityData = { |
| 175 | + resultType: 'Verified', |
| 176 | + maliciousScore: '0', |
| 177 | + features: [], |
| 178 | + fees: { |
| 179 | + transfer: 0, |
| 180 | + transferFeeMaxAmount: null, |
| 181 | + buy: 0, |
| 182 | + sell: null, |
| 183 | + }, |
| 184 | + financialStats: { |
| 185 | + supply: 0, |
| 186 | + topHolders: [], |
| 187 | + holdersCount: 0, |
| 188 | + tradeVolume24h: null, |
| 189 | + lockedLiquidityPct: null, |
| 190 | + markets: [], |
| 191 | + }, |
| 192 | + metadata: { |
| 193 | + externalLinks: { |
| 194 | + homepage: null, |
| 195 | + twitterPage: null, |
| 196 | + telegramChannelId: null, |
| 197 | + }, |
| 198 | + }, |
| 199 | + created: '2023-01-01T00:00:00Z', |
| 200 | + }; |
| 201 | + |
| 202 | + const { queryByTestId } = render( |
| 203 | + <SecurityTrustEntryCard |
| 204 | + securityData={securityDataNoFeatures} |
| 205 | + isLoading={false} |
| 206 | + token={mockToken} |
| 207 | + />, |
| 208 | + ); |
| 209 | + |
| 210 | + expect(queryByTestId('icon-arrow-right')).toBeNull(); |
| 211 | + }); |
| 212 | + |
| 213 | + it('displays subtitle when no features but subtitle exists', () => { |
| 214 | + const securityDataNoFeatures: TokenSecurityData = { |
| 215 | + resultType: 'NotEnoughData', |
| 216 | + maliciousScore: '0', |
| 217 | + features: [], |
| 218 | + fees: { |
| 219 | + transfer: 0, |
| 220 | + transferFeeMaxAmount: null, |
| 221 | + buy: 0, |
| 222 | + sell: null, |
| 223 | + }, |
| 224 | + financialStats: { |
| 225 | + supply: 0, |
| 226 | + topHolders: [], |
| 227 | + holdersCount: 0, |
| 228 | + tradeVolume24h: null, |
| 229 | + lockedLiquidityPct: null, |
| 230 | + markets: [], |
| 231 | + }, |
| 232 | + metadata: { |
| 233 | + externalLinks: { |
| 234 | + homepage: null, |
| 235 | + twitterPage: null, |
| 236 | + telegramChannelId: null, |
| 237 | + }, |
| 238 | + }, |
| 239 | + created: '2023-01-01T00:00:00Z', |
| 240 | + }; |
| 241 | + |
| 242 | + const { getByText } = render( |
| 243 | + <SecurityTrustEntryCard |
| 244 | + securityData={securityDataNoFeatures} |
| 245 | + isLoading={false} |
| 246 | + token={mockToken} |
| 247 | + />, |
| 248 | + ); |
| 249 | + |
| 250 | + expect( |
| 251 | + getByText('Security analysis could not be loaded for this token.'), |
| 252 | + ).toBeTruthy(); |
| 253 | + }); |
| 254 | +}); |
0 commit comments