-
-
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.
test(ramp): add
useCryptoCurrencies
tests (#9580)
- Loading branch information
Showing
1 changed file
with
357 additions
and
0 deletions.
There are no files selected for viewing
357 changes: 357 additions & 0 deletions
357
app/components/UI/Ramp/hooks/useCryptoCurrencies.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,357 @@ | ||
import { NATIVE_ADDRESS } from '../../../../constants/on-ramp'; | ||
import { renderHookWithProvider } from '../../../../util/test/renderWithProvider'; | ||
import { RampSDK } from '../sdk'; | ||
import useCryptoCurrencies from './useCryptoCurrencies'; | ||
import useSDKMethod from './useSDKMethod'; | ||
|
||
type DeepPartial<BaseType> = { | ||
[key in keyof BaseType]?: DeepPartial<BaseType[key]>; | ||
}; | ||
|
||
const mockuseRampSDKInitialValues: DeepPartial<RampSDK> = { | ||
selectedRegion: { id: 'test-region-id' }, | ||
selectedPaymentMethodId: 'test-payment-method-id', | ||
selectedFiatCurrencyId: 'test-fiat-currency-id', | ||
selectedAsset: null, | ||
setSelectedAsset: jest.fn(), | ||
selectedChainId: '1', | ||
isBuy: true, | ||
}; | ||
|
||
let mockUseRampSDKValues: DeepPartial<RampSDK> = { | ||
...mockuseRampSDKInitialValues, | ||
}; | ||
|
||
jest.mock('../sdk', () => ({ | ||
useRampSDK: () => mockUseRampSDKValues, | ||
})); | ||
|
||
jest.mock('./useSDKMethod'); | ||
|
||
describe('useCryptoCurrencies', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockUseRampSDKValues = { | ||
...mockuseRampSDKInitialValues, | ||
}; | ||
}); | ||
|
||
it('calls useSDKMethod with the correct parameters for buy', () => { | ||
(useSDKMethod as jest.Mock).mockReturnValue([ | ||
{ | ||
data: [], | ||
error: null, | ||
isFetching: false, | ||
}, | ||
jest.fn(), | ||
]); | ||
|
||
renderHookWithProvider(() => useCryptoCurrencies()); | ||
|
||
expect(useSDKMethod).toHaveBeenCalledWith( | ||
'getCryptoCurrencies', | ||
'test-region-id', | ||
'test-payment-method-id', | ||
'test-fiat-currency-id', | ||
); | ||
}); | ||
|
||
it('calls useSDKMethod with the correct parameters for sell', () => { | ||
mockUseRampSDKValues.isBuy = false; | ||
(useSDKMethod as jest.Mock).mockReturnValue([ | ||
{ | ||
data: [], | ||
error: null, | ||
isFetching: false, | ||
}, | ||
jest.fn(), | ||
]); | ||
|
||
renderHookWithProvider(() => useCryptoCurrencies()); | ||
|
||
expect(useSDKMethod).toHaveBeenCalledWith( | ||
'getSellCryptoCurrencies', | ||
'test-region-id', | ||
'test-payment-method-id', | ||
'test-fiat-currency-id', | ||
); | ||
}); | ||
|
||
it('returns loading state', () => { | ||
const mockQueryGetCryptoCurrencies = jest.fn(); | ||
(useSDKMethod as jest.Mock).mockReturnValue([ | ||
{ | ||
data: [], | ||
error: null, | ||
isFetching: true, | ||
}, | ||
mockQueryGetCryptoCurrencies, | ||
]); | ||
|
||
const { result } = renderHookWithProvider(() => useCryptoCurrencies()); | ||
|
||
expect(result.current).toEqual({ | ||
cryptoCurrencies: null, | ||
errorCryptoCurrencies: null, | ||
isFetchingCryptoCurrencies: true, | ||
queryGetCryptoCurrencies: mockQueryGetCryptoCurrencies, | ||
}); | ||
}); | ||
|
||
it('returns error state', () => { | ||
const mockQueryGetCryptoCurrencies = jest.fn(); | ||
(useSDKMethod as jest.Mock).mockReturnValue([ | ||
{ | ||
data: null, | ||
error: 'test error message', | ||
isFetching: false, | ||
}, | ||
mockQueryGetCryptoCurrencies, | ||
]); | ||
|
||
const { result } = renderHookWithProvider(() => useCryptoCurrencies()); | ||
|
||
expect(result.current).toEqual({ | ||
cryptoCurrencies: null, | ||
errorCryptoCurrencies: 'test error message', | ||
isFetchingCryptoCurrencies: false, | ||
queryGetCryptoCurrencies: mockQueryGetCryptoCurrencies, | ||
}); | ||
}); | ||
|
||
it('filters list by selectedChainId', () => { | ||
const mockQueryGetCryptoCurrencies = jest.fn(); | ||
(useSDKMethod as jest.Mock).mockReturnValue([ | ||
{ | ||
data: [ | ||
{ network: { chainId: '1' }, address: 'test-address-1' }, | ||
{ network: { chainId: '2' }, address: 'test-address-2' }, | ||
], | ||
error: null, | ||
isFetching: false, | ||
}, | ||
mockQueryGetCryptoCurrencies, | ||
]); | ||
|
||
mockUseRampSDKValues.selectedChainId = '2'; | ||
|
||
const { result, rerender } = renderHookWithProvider(() => | ||
useCryptoCurrencies(), | ||
); | ||
|
||
expect(result.current).toEqual({ | ||
cryptoCurrencies: [ | ||
{ network: { chainId: '2' }, address: 'test-address-2' }, | ||
], | ||
errorCryptoCurrencies: null, | ||
isFetchingCryptoCurrencies: false, | ||
queryGetCryptoCurrencies: mockQueryGetCryptoCurrencies, | ||
}); | ||
|
||
mockUseRampSDKValues.selectedChainId = '1'; | ||
rerender({}); | ||
expect(result.current).toEqual({ | ||
cryptoCurrencies: [ | ||
{ network: { chainId: '1' }, address: 'test-address-1' }, | ||
], | ||
errorCryptoCurrencies: null, | ||
isFetchingCryptoCurrencies: false, | ||
queryGetCryptoCurrencies: mockQueryGetCryptoCurrencies, | ||
}); | ||
}); | ||
|
||
it('does not call setSelectedAsset if current selection is available', () => { | ||
const mockQueryGetCryptoCurrencies = jest.fn(); | ||
(useSDKMethod as jest.Mock).mockReturnValue([ | ||
{ | ||
data: [ | ||
{ network: { chainId: '1' }, address: 'test-address-1' }, | ||
{ network: { chainId: '1' }, address: 'test-address-2' }, | ||
], | ||
error: null, | ||
isFetching: false, | ||
}, | ||
mockQueryGetCryptoCurrencies, | ||
]); | ||
|
||
mockUseRampSDKValues.selectedAsset = { | ||
network: { chainId: '1' }, | ||
address: 'test-address-2', | ||
}; | ||
|
||
const { result } = renderHookWithProvider(() => useCryptoCurrencies()); | ||
|
||
expect(mockUseRampSDKValues.setSelectedAsset).not.toHaveBeenCalled(); | ||
expect(result.current).toEqual({ | ||
cryptoCurrencies: [ | ||
{ network: { chainId: '1' }, address: 'test-address-1' }, | ||
{ network: { chainId: '1' }, address: 'test-address-2' }, | ||
], | ||
errorCryptoCurrencies: null, | ||
isFetchingCryptoCurrencies: false, | ||
queryGetCryptoCurrencies: mockQueryGetCryptoCurrencies, | ||
}); | ||
}); | ||
|
||
it('selects the native crypto currency if available and current selection is null', () => { | ||
const mockQueryGetCryptoCurrencies = jest.fn(); | ||
(useSDKMethod as jest.Mock).mockReturnValue([ | ||
{ | ||
data: [ | ||
{ network: { chainId: '1' }, address: 'test-address-1' }, | ||
{ network: { chainId: '1' }, address: 'test-address-2' }, | ||
{ network: { chainId: '1' }, address: NATIVE_ADDRESS }, | ||
], | ||
error: null, | ||
isFetching: false, | ||
}, | ||
mockQueryGetCryptoCurrencies, | ||
]); | ||
|
||
const { result } = renderHookWithProvider(() => useCryptoCurrencies()); | ||
|
||
expect(mockUseRampSDKValues.setSelectedAsset).toHaveBeenCalledWith({ | ||
network: { chainId: '1' }, | ||
address: NATIVE_ADDRESS, | ||
}); | ||
expect(result.current).toEqual({ | ||
cryptoCurrencies: [ | ||
{ network: { chainId: '1' }, address: 'test-address-1' }, | ||
{ network: { chainId: '1' }, address: 'test-address-2' }, | ||
{ network: { chainId: '1' }, address: NATIVE_ADDRESS }, | ||
], | ||
errorCryptoCurrencies: null, | ||
isFetchingCryptoCurrencies: false, | ||
queryGetCryptoCurrencies: mockQueryGetCryptoCurrencies, | ||
}); | ||
}); | ||
|
||
it('selects the native crypto currency if available and current selection is not available', () => { | ||
const mockQueryGetCryptoCurrencies = jest.fn(); | ||
(useSDKMethod as jest.Mock).mockReturnValue([ | ||
{ | ||
data: [ | ||
{ network: { chainId: '1' }, address: 'test-address-1' }, | ||
{ network: { chainId: '1' }, address: 'test-address-2' }, | ||
{ network: { chainId: '1' }, address: NATIVE_ADDRESS }, | ||
], | ||
error: null, | ||
isFetching: false, | ||
}, | ||
mockQueryGetCryptoCurrencies, | ||
]); | ||
|
||
mockUseRampSDKValues.selectedAsset = { | ||
network: { chainId: '1' }, | ||
address: 'test-address-3', | ||
}; | ||
|
||
const { result } = renderHookWithProvider(() => useCryptoCurrencies()); | ||
|
||
expect(mockUseRampSDKValues.setSelectedAsset).toHaveBeenCalledWith({ | ||
network: { chainId: '1' }, | ||
address: NATIVE_ADDRESS, | ||
}); | ||
expect(result.current).toEqual({ | ||
cryptoCurrencies: [ | ||
{ network: { chainId: '1' }, address: 'test-address-1' }, | ||
{ network: { chainId: '1' }, address: 'test-address-2' }, | ||
{ network: { chainId: '1' }, address: NATIVE_ADDRESS }, | ||
], | ||
errorCryptoCurrencies: null, | ||
isFetchingCryptoCurrencies: false, | ||
queryGetCryptoCurrencies: mockQueryGetCryptoCurrencies, | ||
}); | ||
}); | ||
|
||
it('selects the first available crypto currency if native is not available and current selection is null', () => { | ||
const mockQueryGetCryptoCurrencies = jest.fn(); | ||
(useSDKMethod as jest.Mock).mockReturnValue([ | ||
{ | ||
data: [ | ||
{ network: { chainId: '1' }, address: 'test-address-1' }, | ||
{ network: { chainId: '1' }, address: 'test-address-2' }, | ||
], | ||
error: null, | ||
isFetching: false, | ||
}, | ||
mockQueryGetCryptoCurrencies, | ||
]); | ||
|
||
const { result } = renderHookWithProvider(() => useCryptoCurrencies()); | ||
|
||
expect(mockUseRampSDKValues.setSelectedAsset).toHaveBeenCalledWith({ | ||
network: { chainId: '1' }, | ||
address: 'test-address-1', | ||
}); | ||
expect(result.current).toEqual({ | ||
cryptoCurrencies: [ | ||
{ network: { chainId: '1' }, address: 'test-address-1' }, | ||
{ network: { chainId: '1' }, address: 'test-address-2' }, | ||
], | ||
errorCryptoCurrencies: null, | ||
isFetchingCryptoCurrencies: false, | ||
queryGetCryptoCurrencies: mockQueryGetCryptoCurrencies, | ||
}); | ||
}); | ||
|
||
it('selects the first available crypto currency if native is not available and current selection is not found', () => { | ||
const mockQueryGetCryptoCurrencies = jest.fn(); | ||
(useSDKMethod as jest.Mock).mockReturnValue([ | ||
{ | ||
data: [ | ||
{ network: { chainId: '1' }, address: 'test-address-1' }, | ||
{ network: { chainId: '1' }, address: 'test-address-2' }, | ||
], | ||
error: null, | ||
isFetching: false, | ||
}, | ||
mockQueryGetCryptoCurrencies, | ||
]); | ||
|
||
mockUseRampSDKValues.selectedAsset = { | ||
network: { chainId: '1' }, | ||
address: 'test-address-3', | ||
}; | ||
|
||
const { result } = renderHookWithProvider(() => useCryptoCurrencies()); | ||
|
||
expect(mockUseRampSDKValues.setSelectedAsset).toHaveBeenCalledWith({ | ||
network: { chainId: '1' }, | ||
address: 'test-address-1', | ||
}); | ||
expect(result.current).toEqual({ | ||
cryptoCurrencies: [ | ||
{ network: { chainId: '1' }, address: 'test-address-1' }, | ||
{ network: { chainId: '1' }, address: 'test-address-2' }, | ||
], | ||
errorCryptoCurrencies: null, | ||
isFetchingCryptoCurrencies: false, | ||
queryGetCryptoCurrencies: mockQueryGetCryptoCurrencies, | ||
}); | ||
}); | ||
|
||
it('sets selectedAsset to undefined if no crypto currencies are available', () => { | ||
const mockQueryGetCryptoCurrencies = jest.fn(); | ||
(useSDKMethod as jest.Mock).mockReturnValue([ | ||
{ | ||
data: [], | ||
error: null, | ||
isFetching: false, | ||
}, | ||
mockQueryGetCryptoCurrencies, | ||
]); | ||
|
||
const { result } = renderHookWithProvider(() => useCryptoCurrencies()); | ||
|
||
expect(mockUseRampSDKValues.setSelectedAsset).toHaveBeenCalledWith( | ||
undefined, | ||
); | ||
expect(result.current).toEqual({ | ||
cryptoCurrencies: [], | ||
errorCryptoCurrencies: null, | ||
isFetchingCryptoCurrencies: false, | ||
queryGetCryptoCurrencies: mockQueryGetCryptoCurrencies, | ||
}); | ||
}); | ||
}); |