|
| 1 | +import nock, { cleanAll } from 'nock'; |
| 2 | + |
| 3 | +import { TokenDiscoveryApiService } from './token-discovery-api-service'; |
| 4 | +import { TEST_API_URLS } from '../test/constants'; |
| 5 | +import type { TokenTrendingResponseItem } from '../types'; |
| 6 | + |
| 7 | +describe('TokenDiscoveryApiService', () => { |
| 8 | + let service: TokenDiscoveryApiService; |
| 9 | + const mockTrendingResponse: TokenTrendingResponseItem[] = [ |
| 10 | + { |
| 11 | + chain_id: '1', |
| 12 | + token_address: '0x123', |
| 13 | + token_logo: 'https://example.com/logo.png', |
| 14 | + token_name: 'Test Token', |
| 15 | + token_symbol: 'TEST', |
| 16 | + price_usd: 100, |
| 17 | + token_age_in_days: 365, |
| 18 | + on_chain_strength_index: 85, |
| 19 | + security_score: 90, |
| 20 | + market_cap: 1000000, |
| 21 | + fully_diluted_valuation: 2000000, |
| 22 | + twitter_followers: 50000, |
| 23 | + holders_change: { |
| 24 | + '1h': 10, |
| 25 | + '1d': 100, |
| 26 | + '1w': 1000, |
| 27 | + '1M': 10000, |
| 28 | + }, |
| 29 | + liquidity_change_usd: { |
| 30 | + '1h': 1000, |
| 31 | + '1d': 10000, |
| 32 | + '1w': 100000, |
| 33 | + '1M': 1000000, |
| 34 | + }, |
| 35 | + experienced_net_buyers_change: { |
| 36 | + '1h': 5, |
| 37 | + '1d': 50, |
| 38 | + '1w': 500, |
| 39 | + '1M': 5000, |
| 40 | + }, |
| 41 | + volume_change_usd: { |
| 42 | + '1h': 10000, |
| 43 | + '1d': 100000, |
| 44 | + '1w': 1000000, |
| 45 | + '1M': 10000000, |
| 46 | + }, |
| 47 | + net_volume_change_usd: { |
| 48 | + '1h': 5000, |
| 49 | + '1d': 50000, |
| 50 | + '1w': 500000, |
| 51 | + '1M': 5000000, |
| 52 | + }, |
| 53 | + price_percent_change_usd: { |
| 54 | + '1h': 1, |
| 55 | + '1d': 10, |
| 56 | + '1w': 20, |
| 57 | + '1M': 30, |
| 58 | + }, |
| 59 | + }, |
| 60 | + ]; |
| 61 | + |
| 62 | + beforeEach(() => { |
| 63 | + service = new TokenDiscoveryApiService(TEST_API_URLS.PORTFOLIO_API); |
| 64 | + }); |
| 65 | + |
| 66 | + afterEach(() => { |
| 67 | + cleanAll(); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('constructor', () => { |
| 71 | + it('should throw if baseUrl is empty', () => { |
| 72 | + expect(() => new TokenDiscoveryApiService('')).toThrow( |
| 73 | + 'Portfolio API URL is not set', |
| 74 | + ); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('getTrendingTokensByChains', () => { |
| 79 | + it.each([ |
| 80 | + { |
| 81 | + params: { chains: ['1'], limit: '5' }, |
| 82 | + expectedPath: '/tokens-search/trending-by-chains?chains=1&limit=5', |
| 83 | + }, |
| 84 | + { |
| 85 | + params: { chains: ['1', '137'] }, |
| 86 | + expectedPath: '/tokens-search/trending-by-chains?chains=1,137', |
| 87 | + }, |
| 88 | + { |
| 89 | + params: { limit: '10' }, |
| 90 | + expectedPath: '/tokens-search/trending-by-chains?limit=10', |
| 91 | + }, |
| 92 | + { |
| 93 | + params: {}, |
| 94 | + expectedPath: '/tokens-search/trending-by-chains', |
| 95 | + }, |
| 96 | + ])( |
| 97 | + 'should construct correct URL for params: $params', |
| 98 | + async ({ params, expectedPath }) => { |
| 99 | + nock(TEST_API_URLS.PORTFOLIO_API) |
| 100 | + .get(expectedPath) |
| 101 | + .reply(200, mockTrendingResponse); |
| 102 | + |
| 103 | + const result = await service.getTrendingTokensByChains(params); |
| 104 | + expect(result).toStrictEqual(mockTrendingResponse); |
| 105 | + }, |
| 106 | + ); |
| 107 | + |
| 108 | + it('should handle API errors', async () => { |
| 109 | + nock(TEST_API_URLS.PORTFOLIO_API) |
| 110 | + .get('/tokens-search/trending-by-chains') |
| 111 | + .reply(500, 'Server Error'); |
| 112 | + |
| 113 | + await expect(service.getTrendingTokensByChains({})).rejects.toThrow( |
| 114 | + 'Portfolio API request failed with status: 500', |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should return trending results', async () => { |
| 119 | + nock(TEST_API_URLS.PORTFOLIO_API) |
| 120 | + .get('/tokens-search/trending-by-chains') |
| 121 | + .reply(200, mockTrendingResponse); |
| 122 | + |
| 123 | + const results = await service.getTrendingTokensByChains({}); |
| 124 | + expect(results).toStrictEqual(mockTrendingResponse); |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments