Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: trim the query when searching for wallets #2167

Merged
merged 8 commits into from
Apr 25, 2024
2 changes: 1 addition & 1 deletion packages/core/src/controllers/ApiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(',')
Expand Down
60 changes: 59 additions & 1 deletion packages/core/tests/controllers/ApiController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -383,6 +383,64 @@ 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 ' })
chris13524 marked this conversation as resolved.
Show resolved Hide resolved

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)

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
Loading