Commit 5deeabf
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- packages/assets-controllers
- src
3 files changed
+15
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
10 | 14 | | |
11 | 15 | | |
12 | 16 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
655 | 655 | | |
656 | 656 | | |
657 | 657 | | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
658 | 669 | | |
659 | 670 | | |
660 | 671 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | 154 | | |
159 | 155 | | |
160 | 156 | | |
| |||
0 commit comments