diff --git a/packages/core/src/controllers/ApiController.ts b/packages/core/src/controllers/ApiController.ts index 307bbf98de..0608851aff 100644 --- a/packages/core/src/controllers/ApiController.ts +++ b/packages/core/src/controllers/ApiController.ts @@ -202,7 +202,7 @@ export const ApiController = { params: { page: '1', entries: '100', - search, + search: search?.trim(), chains: NetworkController.state.caipNetwork?.id, include: includeWalletIds?.join(','), exclude: excludeWalletIds?.join(',') diff --git a/packages/core/tests/controllers/ApiController.test.ts b/packages/core/tests/controllers/ApiController.test.ts index 057ed99b76..830071d227 100644 --- a/packages/core/tests/controllers/ApiController.test.ts +++ b/packages/core/tests/controllers/ApiController.test.ts @@ -348,7 +348,7 @@ describe('ApiController', () => { expect(ApiController.state.wallets).toEqual(data) }) - // Search Wallet + // Wallet search with exact wallet name it('should search wallet with search term', async () => { const includeWalletIds = ['12341', '12342'] const excludeWalletIds = ['12343'] @@ -383,6 +383,96 @@ describe('ApiController', () => { expect(ApiController.state.search).toEqual(data) }) + // Wallet search with whitespace and multiple words + it('should search wallet with search term', async () => { + const includeWalletIds = ['12341', '12342'] + const excludeWalletIds = ['12343'] + let data = [ + { + id: '12341', + name: 'MetaMask', + image_id: '12341' + } + ] + OptionsController.setIncludeWalletIds(includeWalletIds) + OptionsController.setExcludeWalletIds(excludeWalletIds) + + let fetchSpy = vi.spyOn(api, 'get').mockResolvedValue({ data }) + const fetchImageSpy = vi.spyOn(ApiController, '_fetchWalletImage').mockResolvedValue() + + // Whitespace + await ApiController.searchWallet({ search: 'MetaMask ' }) + + expect(fetchSpy).toHaveBeenCalledWith({ + path: '/getWallets', + headers: ApiController._getApiHeaders(), + params: { + page: '1', + entries: '100', + search: 'MetaMask', + include: '12341,12342', + exclude: '12343' + } + }) + expect(fetchImageSpy).toHaveBeenCalledOnce() + expect(ApiController.state.search).toEqual(data) + + // Leading Whitespace + await ApiController.searchWallet({ search: ' Metamask' }) + + expect(fetchSpy).toHaveBeenCalledWith({ + path: '/getWallets', + headers: ApiController._getApiHeaders(), + params: { + page: '1', + entries: '100', + search: 'MetaMask', + include: '12341,12342', + exclude: '12343' + } + }) + expect(ApiController.state.search).toEqual(data) + + // Leading and Trailing Whitespace + await ApiController.searchWallet({ search: ' Metamask ' }) + + expect(fetchSpy).toHaveBeenCalledWith({ + path: '/getWallets', + headers: ApiController._getApiHeaders(), + params: { + page: '1', + entries: '100', + search: 'MetaMask', + include: '12341,12342', + exclude: '12343' + } + }) + expect(ApiController.state.search).toEqual(data) + + data = [ + { + id: '12341', + name: 'Safe Wallet', + image_id: '12341' + } + ] + fetchSpy = vi.spyOn(api, 'get').mockResolvedValue({ data }) + await ApiController.searchWallet({ search: 'Safe Wallet' }) + + expect(fetchSpy).toHaveBeenCalledWith({ + path: '/getWallets', + headers: ApiController._getApiHeaders(), + params: { + page: '1', + entries: '100', + search: 'Safe Wallet', + include: '12341,12342', + exclude: '12343' + } + }) + expect(ApiController.state.search).toEqual(data) + }) + // Prefetch it('should prefetch without analytics', () => { OptionsController.state.enableAnalytics = false diff --git a/packages/scaffold/src/partials/w3m-all-wallets-search/index.ts b/packages/scaffold/src/partials/w3m-all-wallets-search/index.ts index 95fdbb22ff..8fef2889ae 100644 --- a/packages/scaffold/src/partials/w3m-all-wallets-search/index.ts +++ b/packages/scaffold/src/partials/w3m-all-wallets-search/index.ts @@ -30,7 +30,7 @@ export class W3mAllWalletsSearch extends LitElement { // Private Methods ------------------------------------- // private async onSearch() { - if (this.query !== this.prevQuery) { + if (this.query.trim() !== this.prevQuery.trim()) { this.prevQuery = this.query this.loading = true await ApiController.searchWallet({ search: this.query })