Skip to content

Commit 5deeabf

Browse files
authored
refactor: allow searchTokens to handle empty chainIds array (#7083)
## Explanation ### What is the current state and why does it need to change? The `searchTokens` function previously included an early return that would immediately return `{ count: 0, data: [] }` when an empty `chainIds` array was passed, without making an API call. This behavior was inconsistent with how the API endpoint should handle empty chain IDs and prevented the backend from potentially returning valid responses or proper error handling. ### What is the solution and how does it work? This PR removes the early return check for empty `chainIds` in the `searchTokens` function. Now, when an empty array is passed, the function proceeds to construct the API URL and make the fetch request with `chainIds=` (empty parameter). This allows the backend API to handle the empty chainIds case according to its own logic, which may include validation, error responses, or even returning results. The corresponding test has been updated to: - Mock the API endpoint with an empty `chainIds` parameter (`/tokens/search?chainIds=&query=USD&limit=10`) - Verify that the function still returns `{ count: 0, data: [] }` based on the API response - Maintain the same expected behavior while testing the actual API interaction ### Changes that might not be obvious The test change ensures that we're now testing the actual network behavior rather than just validating client-side logic. This provides better coverage and ensures the function properly handles API responses even in edge cases. ## References <!-- Add any related issues or PRs here --> ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [ ] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Makes searchTokens call the API even when chainIds is empty; updates tests and changelog accordingly. > > - **Token Service (`src/token-service.ts`)** > - Remove early return for empty `chainIds` in `searchTokens`, allowing requests with `chainIds=` to hit the API. > - **Tests (`src/token-service.test.ts`)** > - Add mock and assertion for empty `chainIds` case in `searchTokens` (requests `/tokens/search?chainIds=&...`). > - **Changelog** > - Document change under Unreleased for `searchTokens` handling of empty `chainIds`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit c508801. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 9255484 commit 5deeabf

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

packages/assets-controllers/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Remove early return for empty `chainIds` in `searchTokens` function to allow API to handle empty chain IDs ([#7083](https://github.com/MetaMask/core/pull/7083))
13+
1014
## [87.1.0]
1115

1216
### Added

packages/assets-controllers/src/token-service.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,17 @@ describe('Token service', () => {
655655

656656
it('should return empty array when no chainIds are provided', async () => {
657657
const searchQuery = 'USD';
658+
const mockResponse = {
659+
count: 0,
660+
data: [],
661+
pageInfo: { hasNextPage: false, endCursor: null },
662+
};
663+
664+
nock(TOKEN_END_POINT_API)
665+
.get(`/tokens/search?chainIds=&query=${searchQuery}&limit=10`)
666+
.reply(200, mockResponse)
667+
.persist();
668+
658669
const results = await searchTokens([], searchQuery);
659670

660671
expect(results).toStrictEqual({ count: 0, data: [] });

packages/assets-controllers/src/token-service.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,6 @@ export async function searchTokens(
151151
query: string,
152152
{ limit = 10 } = {},
153153
): Promise<{ count: number; data: unknown[] }> {
154-
if (chainIds.length === 0) {
155-
return { count: 0, data: [] };
156-
}
157-
158154
const tokenSearchURL = getTokenSearchURL(chainIds, query, limit);
159155

160156
try {

0 commit comments

Comments
 (0)