-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
app/components/hooks/useTokenSearchDiscovery/useTokenSearchDiscovery.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useSelector } from 'react-redux'; | ||
import Engine from '../../../core/Engine'; | ||
import useTokenSearchDiscovery from './useTokenSearchDiscovery'; | ||
|
||
jest.mock('react-redux', () => ({ | ||
...jest.requireActual('react-redux'), | ||
useSelector: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../../core/Engine', () => ({ | ||
context: { | ||
TokenSearchDiscoveryController: { | ||
searchTokens: jest.fn(), | ||
}, | ||
}, | ||
})); | ||
|
||
describe('useTokenSearchDiscovery', () => { | ||
const mockRecentSearches = ['0x123', '0x456']; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
(useSelector as jest.Mock).mockReturnValue(mockRecentSearches); | ||
}); | ||
|
||
it('should return searchTokens function and recent searches', () => { | ||
const { result } = renderHook(() => useTokenSearchDiscovery()); | ||
|
||
expect(result.current.searchTokens).toBeDefined(); | ||
expect(result.current.recentSearches).toEqual(mockRecentSearches); | ||
}); | ||
|
||
it('should call TokenSearchDiscoveryController.searchTokens with correct params', async () => { | ||
const mockSearchParams = { | ||
chainId: '0x1', | ||
query: 'DAI', | ||
limit: '10', | ||
}; | ||
const mockSearchResult = { tokens: [] }; | ||
|
||
( | ||
Engine.context.TokenSearchDiscoveryController.searchTokens as jest.Mock | ||
).mockResolvedValueOnce(mockSearchResult); | ||
|
||
const { result } = renderHook(() => useTokenSearchDiscovery()); | ||
const response = await result.current.searchTokens(mockSearchParams); | ||
|
||
expect( | ||
Engine.context.TokenSearchDiscoveryController.searchTokens, | ||
).toHaveBeenCalledWith(mockSearchParams); | ||
expect(response).toEqual(mockSearchResult); | ||
}); | ||
|
||
it('should handle search errors gracefully', async () => { | ||
const mockError = new Error('Search failed'); | ||
( | ||
Engine.context.TokenSearchDiscoveryController.searchTokens as jest.Mock | ||
).mockRejectedValueOnce(mockError); | ||
|
||
const { result } = renderHook(() => useTokenSearchDiscovery()); | ||
|
||
await expect(result.current.searchTokens({})).rejects.toThrow( | ||
'Search failed', | ||
); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
app/components/hooks/useTokenSearchDiscovery/useTokenSearchDiscovery.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useCallback } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import Engine from '../../../core/Engine'; | ||
import { selectRecentTokenSearches } from '../../../selectors/tokenSearchDiscoveryController'; | ||
import { TokenSearchParams } from '@metamask/token-search-discovery-controller/dist/types.cjs'; | ||
|
||
export const useTokenSearchDiscovery = () => { | ||
const recentSearches = useSelector(selectRecentTokenSearches); | ||
|
||
const searchTokens = useCallback(async (params: TokenSearchParams) => { | ||
const { TokenSearchDiscoveryController } = Engine.context; | ||
return await TokenSearchDiscoveryController.searchTokens(params); | ||
}, []); | ||
|
||
return { | ||
searchTokens, | ||
recentSearches, | ||
}; | ||
}; | ||
|
||
export default useTokenSearchDiscovery; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { createSelector } from 'reselect'; | ||
import { RootState } from '../reducers'; | ||
|
||
const selectTokenSearchDiscoveryControllerState = (state: RootState) => | ||
state.engine.backgroundState.TokenSearchDiscoveryController; | ||
|
||
export const selectRecentTokenSearches = createSelector( | ||
selectTokenSearchDiscoveryControllerState, | ||
(state) => state.recentSearches, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters