|
1 | 1 | import { toHex } from '@metamask/controller-utils'; |
| 2 | +import type { CaipChainId } from '@metamask/utils'; |
2 | 3 | import nock from 'nock'; |
3 | 4 |
|
4 | 5 | import { |
5 | 6 | fetchTokenListByChainId, |
6 | 7 | fetchTokenMetadata, |
| 8 | + searchTokens, |
7 | 9 | TOKEN_END_POINT_API, |
8 | 10 | TOKEN_METADATA_NO_SUPPORT_ERROR, |
9 | 11 | } from './token-service'; |
@@ -234,8 +236,54 @@ const sampleToken = { |
234 | 236 | name: 'Chainlink', |
235 | 237 | }; |
236 | 238 |
|
| 239 | +const sampleSearchResults = [ |
| 240 | + { |
| 241 | + address: '0xa0b86a33e6c166428cf041c73490a6b448b7f2c2', |
| 242 | + symbol: 'USDC', |
| 243 | + decimals: 6, |
| 244 | + name: 'USD Coin', |
| 245 | + occurrences: 12, |
| 246 | + aggregators: [ |
| 247 | + 'paraswap', |
| 248 | + 'pmm', |
| 249 | + 'airswapLight', |
| 250 | + 'zeroEx', |
| 251 | + 'bancor', |
| 252 | + 'coinGecko', |
| 253 | + 'zapper', |
| 254 | + 'kleros', |
| 255 | + 'zerion', |
| 256 | + 'cmc', |
| 257 | + 'oneInch', |
| 258 | + 'uniswap', |
| 259 | + ], |
| 260 | + }, |
| 261 | + { |
| 262 | + address: '0xdac17f958d2ee523a2206206994597c13d831ec7', |
| 263 | + symbol: 'USDT', |
| 264 | + decimals: 6, |
| 265 | + name: 'Tether USD', |
| 266 | + occurrences: 11, |
| 267 | + aggregators: [ |
| 268 | + 'paraswap', |
| 269 | + 'pmm', |
| 270 | + 'airswapLight', |
| 271 | + 'zeroEx', |
| 272 | + 'bancor', |
| 273 | + 'coinGecko', |
| 274 | + 'zapper', |
| 275 | + 'kleros', |
| 276 | + 'zerion', |
| 277 | + 'cmc', |
| 278 | + 'oneInch', |
| 279 | + ], |
| 280 | + }, |
| 281 | +]; |
| 282 | + |
237 | 283 | const sampleDecimalChainId = 1; |
238 | 284 | const sampleChainId = toHex(sampleDecimalChainId); |
| 285 | +const sampleCaipChainId: CaipChainId = 'eip155:1'; |
| 286 | +const polygonCaipChainId: CaipChainId = 'eip155:137'; |
239 | 287 |
|
240 | 288 | describe('Token service', () => { |
241 | 289 | describe('fetchTokenListByChainId', () => { |
@@ -437,4 +485,228 @@ describe('Token service', () => { |
437 | 485 | ).rejects.toThrow(TOKEN_METADATA_NO_SUPPORT_ERROR); |
438 | 486 | }); |
439 | 487 | }); |
| 488 | + |
| 489 | + describe('searchTokens', () => { |
| 490 | + it('should call the search api and return the list of matching tokens for single chain', async () => { |
| 491 | + const searchQuery = 'USD'; |
| 492 | + const mockResponse = { |
| 493 | + count: sampleSearchResults.length, |
| 494 | + data: sampleSearchResults, |
| 495 | + pageInfo: { hasNextPage: false, endCursor: null }, |
| 496 | + }; |
| 497 | + |
| 498 | + nock(TOKEN_END_POINT_API) |
| 499 | + .get( |
| 500 | + `/tokens/search?chainIds=${encodeURIComponent(sampleCaipChainId)}&query=${searchQuery}&limit=10`, |
| 501 | + ) |
| 502 | + .reply(200, mockResponse) |
| 503 | + .persist(); |
| 504 | + |
| 505 | + const results = await searchTokens([sampleCaipChainId], searchQuery); |
| 506 | + |
| 507 | + expect(results).toStrictEqual({ |
| 508 | + count: sampleSearchResults.length, |
| 509 | + data: sampleSearchResults, |
| 510 | + }); |
| 511 | + }); |
| 512 | + |
| 513 | + it('should call the search api with custom limit parameter', async () => { |
| 514 | + const searchQuery = 'USDC'; |
| 515 | + const customLimit = 5; |
| 516 | + const mockResponse = { |
| 517 | + count: 1, |
| 518 | + data: [sampleSearchResults[0]], |
| 519 | + pageInfo: { hasNextPage: false, endCursor: null }, |
| 520 | + }; |
| 521 | + |
| 522 | + nock(TOKEN_END_POINT_API) |
| 523 | + .get( |
| 524 | + `/tokens/search?chainIds=${encodeURIComponent(sampleCaipChainId)}&query=${searchQuery}&limit=${customLimit}`, |
| 525 | + ) |
| 526 | + .reply(200, mockResponse) |
| 527 | + .persist(); |
| 528 | + |
| 529 | + const results = await searchTokens([sampleCaipChainId], searchQuery, { |
| 530 | + limit: customLimit, |
| 531 | + }); |
| 532 | + |
| 533 | + expect(results).toStrictEqual({ |
| 534 | + count: 1, |
| 535 | + data: [sampleSearchResults[0]], |
| 536 | + }); |
| 537 | + }); |
| 538 | + |
| 539 | + it('should properly encode search queries with special characters', async () => { |
| 540 | + const searchQuery = 'USD Coin & Token'; |
| 541 | + const encodedQuery = 'USD%20Coin%20%26%20Token'; |
| 542 | + const mockResponse = { |
| 543 | + count: sampleSearchResults.length, |
| 544 | + data: sampleSearchResults, |
| 545 | + pageInfo: { hasNextPage: false, endCursor: null }, |
| 546 | + }; |
| 547 | + |
| 548 | + nock(TOKEN_END_POINT_API) |
| 549 | + .get( |
| 550 | + `/tokens/search?chainIds=${encodeURIComponent(sampleCaipChainId)}&query=${encodedQuery}&limit=10`, |
| 551 | + ) |
| 552 | + .reply(200, mockResponse) |
| 553 | + .persist(); |
| 554 | + |
| 555 | + const results = await searchTokens([sampleCaipChainId], searchQuery); |
| 556 | + |
| 557 | + expect(results).toStrictEqual({ |
| 558 | + count: sampleSearchResults.length, |
| 559 | + data: sampleSearchResults, |
| 560 | + }); |
| 561 | + }); |
| 562 | + |
| 563 | + it('should search across multiple chains in a single request', async () => { |
| 564 | + const searchQuery = 'USD'; |
| 565 | + const encodedChainIds = [sampleCaipChainId, polygonCaipChainId] |
| 566 | + .map((id) => encodeURIComponent(id)) |
| 567 | + .join(','); |
| 568 | + const mockResponse = { |
| 569 | + count: sampleSearchResults.length, |
| 570 | + data: sampleSearchResults, |
| 571 | + pageInfo: { hasNextPage: false, endCursor: null }, |
| 572 | + }; |
| 573 | + |
| 574 | + nock(TOKEN_END_POINT_API) |
| 575 | + .get( |
| 576 | + `/tokens/search?chainIds=${encodedChainIds}&query=${searchQuery}&limit=10`, |
| 577 | + ) |
| 578 | + .reply(200, mockResponse) |
| 579 | + .persist(); |
| 580 | + |
| 581 | + const results = await searchTokens( |
| 582 | + [sampleCaipChainId, polygonCaipChainId], |
| 583 | + searchQuery, |
| 584 | + ); |
| 585 | + |
| 586 | + expect(results).toStrictEqual({ |
| 587 | + count: sampleSearchResults.length, |
| 588 | + data: sampleSearchResults, |
| 589 | + }); |
| 590 | + }); |
| 591 | + |
| 592 | + it('should return empty array if the fetch fails with a network error', async () => { |
| 593 | + const searchQuery = 'USD'; |
| 594 | + nock(TOKEN_END_POINT_API) |
| 595 | + .get( |
| 596 | + `/tokens/search?chainIds=${encodeURIComponent(sampleCaipChainId)}&query=${searchQuery}&limit=10`, |
| 597 | + ) |
| 598 | + .replyWithError('Example network error') |
| 599 | + .persist(); |
| 600 | + |
| 601 | + const result = await searchTokens([sampleCaipChainId], searchQuery); |
| 602 | + |
| 603 | + expect(result).toStrictEqual({ count: 0, data: [] }); |
| 604 | + }); |
| 605 | + |
| 606 | + it('should return empty array if the fetch fails with 400 error', async () => { |
| 607 | + const searchQuery = 'USD'; |
| 608 | + nock(TOKEN_END_POINT_API) |
| 609 | + .get( |
| 610 | + `/tokens/search?chainIds=${encodeURIComponent(sampleCaipChainId)}&query=${searchQuery}&limit=10`, |
| 611 | + ) |
| 612 | + .reply(400, { error: 'Bad Request' }) |
| 613 | + .persist(); |
| 614 | + |
| 615 | + const result = await searchTokens([sampleCaipChainId], searchQuery); |
| 616 | + |
| 617 | + expect(result).toStrictEqual({ count: 0, data: [] }); |
| 618 | + }); |
| 619 | + |
| 620 | + it('should return empty array if the fetch fails with 500 error', async () => { |
| 621 | + const searchQuery = 'USD'; |
| 622 | + nock(TOKEN_END_POINT_API) |
| 623 | + .get( |
| 624 | + `/tokens/search?chainIds=${encodeURIComponent(sampleCaipChainId)}&query=${searchQuery}&limit=10`, |
| 625 | + ) |
| 626 | + .reply(500) |
| 627 | + .persist(); |
| 628 | + |
| 629 | + const result = await searchTokens([sampleCaipChainId], searchQuery); |
| 630 | + |
| 631 | + expect(result).toStrictEqual({ count: 0, data: [] }); |
| 632 | + }); |
| 633 | + |
| 634 | + it('should handle empty search results', async () => { |
| 635 | + const searchQuery = 'NONEXISTENT'; |
| 636 | + const mockResponse = { |
| 637 | + count: 0, |
| 638 | + data: [], |
| 639 | + pageInfo: { hasNextPage: false, endCursor: null }, |
| 640 | + }; |
| 641 | + |
| 642 | + nock(TOKEN_END_POINT_API) |
| 643 | + .get( |
| 644 | + `/tokens/search?chainIds=${encodeURIComponent(sampleCaipChainId)}&query=${searchQuery}&limit=10`, |
| 645 | + ) |
| 646 | + .reply(200, mockResponse) |
| 647 | + .persist(); |
| 648 | + |
| 649 | + const results = await searchTokens([sampleCaipChainId], searchQuery); |
| 650 | + |
| 651 | + expect(results).toStrictEqual({ count: 0, data: [] }); |
| 652 | + }); |
| 653 | + |
| 654 | + it('should return empty array when no chainIds are provided', async () => { |
| 655 | + const searchQuery = 'USD'; |
| 656 | + const results = await searchTokens([], searchQuery); |
| 657 | + |
| 658 | + expect(results).toStrictEqual({ count: 0, data: [] }); |
| 659 | + }); |
| 660 | + |
| 661 | + it('should handle API error responses in JSON format', async () => { |
| 662 | + const searchQuery = 'USD'; |
| 663 | + const errorResponse = { error: 'Invalid search query' }; |
| 664 | + nock(TOKEN_END_POINT_API) |
| 665 | + .get( |
| 666 | + `/tokens/search?chainIds=${encodeURIComponent(sampleCaipChainId)}&query=${searchQuery}&limit=10`, |
| 667 | + ) |
| 668 | + .reply(200, errorResponse) |
| 669 | + .persist(); |
| 670 | + |
| 671 | + const result = await searchTokens([sampleCaipChainId], searchQuery); |
| 672 | + |
| 673 | + // Non-array responses should be converted to empty object with count 0 |
| 674 | + expect(result).toStrictEqual({ count: 0, data: [] }); |
| 675 | + }); |
| 676 | + |
| 677 | + it('should handle supported CAIP format chain IDs', async () => { |
| 678 | + const searchQuery = 'USD'; |
| 679 | + const solanaChainId: CaipChainId = |
| 680 | + 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'; |
| 681 | + const tronChainId: CaipChainId = 'tron:728126428'; |
| 682 | + |
| 683 | + const multiChainIds: CaipChainId[] = [ |
| 684 | + sampleCaipChainId, |
| 685 | + solanaChainId, |
| 686 | + tronChainId, |
| 687 | + ]; |
| 688 | + const encodedChainIds = multiChainIds |
| 689 | + .map((id) => encodeURIComponent(id)) |
| 690 | + .join(','); |
| 691 | + const mockResponse = { |
| 692 | + count: sampleSearchResults.length, |
| 693 | + data: sampleSearchResults, |
| 694 | + pageInfo: { hasNextPage: false, endCursor: null }, |
| 695 | + }; |
| 696 | + |
| 697 | + nock(TOKEN_END_POINT_API) |
| 698 | + .get( |
| 699 | + `/tokens/search?chainIds=${encodedChainIds}&query=${searchQuery}&limit=10`, |
| 700 | + ) |
| 701 | + .reply(200, mockResponse) |
| 702 | + .persist(); |
| 703 | + |
| 704 | + const result = await searchTokens(multiChainIds, searchQuery); |
| 705 | + |
| 706 | + expect(result).toStrictEqual({ |
| 707 | + count: sampleSearchResults.length, |
| 708 | + data: sampleSearchResults, |
| 709 | + }); |
| 710 | + }); |
| 711 | + }); |
440 | 712 | }); |
0 commit comments