-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
1 parent
ab43b86
commit 883070c
Showing
6 changed files
with
219 additions
and
137 deletions.
There are no files selected for viewing
67 changes: 0 additions & 67 deletions
67
ui/pages/confirmations/components/confirm/info/hooks/use-selected-token.test.ts
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
ui/pages/confirmations/components/confirm/info/hooks/use-selected-token.ts
This file was deleted.
Oops, something went wrong.
120 changes: 120 additions & 0 deletions
120
ui/pages/confirmations/components/confirm/info/hooks/use-token-values.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,120 @@ | ||
import { TransactionMeta } from '@metamask/transaction-controller'; | ||
import { genUnapprovedTokenTransferConfirmation } from '../../../../../../../test/data/confirmations/token-transfer'; | ||
import mockState from '../../../../../../../test/data/mock-state.json'; | ||
import { renderHookWithProvider } from '../../../../../../../test/lib/render-helpers'; | ||
// import useTokenExchangeRate from '../../../../../../components/app/currency-input/hooks/useTokenExchangeRate'; | ||
import { Numeric } from '../../../../../../../shared/modules/Numeric'; | ||
import useTokenExchangeRate from '../../../../../../components/app/currency-input/hooks/useTokenExchangeRate'; | ||
import { useTokenTracker } from '../../../../../../hooks/useTokenTracker'; | ||
import { useTokenValues } from './use-token-values'; | ||
|
||
jest.mock( | ||
'../../../../../../components/app/currency-input/hooks/useTokenExchangeRate', | ||
() => jest.fn(), | ||
); | ||
|
||
jest.mock('../../../../../../hooks/useTokenTracker', () => ({ | ||
...jest.requireActual('../../../../../../hooks/useTokenTracker'), | ||
useTokenTracker: jest.fn(), | ||
})); | ||
|
||
describe('useTokenValues', () => { | ||
const useTokenExchangeRateMock = jest.mocked(useTokenExchangeRate); | ||
const useTokenTrackerMock = jest.mocked(useTokenTracker); | ||
|
||
const TEST_SELECTED_TOKEN = { | ||
address: 'address', | ||
decimals: 18, | ||
symbol: 'symbol', | ||
iconUrl: 'iconUrl', | ||
image: 'image', | ||
}; | ||
|
||
it('returns native and fiat balances', async () => { | ||
(useTokenTrackerMock as jest.Mock).mockResolvedValue({ | ||
tokensWithBalances: [ | ||
{ | ||
address: '0x076146c765189d51be3160a2140cf80bfc73ad68', | ||
balance: '1000000000000000000', | ||
decimals: 18, | ||
}, | ||
], | ||
}); | ||
|
||
(useTokenExchangeRateMock as jest.Mock).mockResolvedValue( | ||
new Numeric(1, 10), | ||
); | ||
|
||
const transactionMeta = genUnapprovedTokenTransferConfirmation( | ||
{}, | ||
) as TransactionMeta; | ||
|
||
const { result, waitForNextUpdate } = renderHookWithProvider( | ||
() => useTokenValues(transactionMeta, TEST_SELECTED_TOKEN), | ||
mockState, | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual({ | ||
fiatDisplayValue: '$1.00', | ||
tokenBalance: '1', | ||
}); | ||
}); | ||
|
||
it('returns undefined native and fiat balances if no token with balances is returned', async () => { | ||
(useTokenTrackerMock as jest.Mock).mockResolvedValue({ | ||
tokensWithBalances: [], | ||
}); | ||
|
||
(useTokenExchangeRateMock as jest.Mock).mockResolvedValue( | ||
new Numeric(1, 10), | ||
); | ||
|
||
const transactionMeta = genUnapprovedTokenTransferConfirmation( | ||
{}, | ||
) as TransactionMeta; | ||
|
||
const { result, waitForNextUpdate } = renderHookWithProvider( | ||
() => useTokenValues(transactionMeta, TEST_SELECTED_TOKEN), | ||
mockState, | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual({ | ||
fiatDisplayValue: undefined, | ||
tokenBalance: undefined, | ||
}); | ||
}); | ||
|
||
it('returns undefined fiat balance if no token rate is returned', async () => { | ||
(useTokenTrackerMock as jest.Mock).mockResolvedValue({ | ||
tokensWithBalances: [ | ||
{ | ||
address: '0x076146c765189d51be3160a2140cf80bfc73ad68', | ||
balance: '1000000000000000000', | ||
decimals: 18, | ||
}, | ||
], | ||
}); | ||
|
||
(useTokenExchangeRateMock as jest.Mock).mockResolvedValue(null); | ||
|
||
const transactionMeta = genUnapprovedTokenTransferConfirmation( | ||
{}, | ||
) as TransactionMeta; | ||
|
||
const { result, waitForNextUpdate } = renderHookWithProvider( | ||
() => useTokenValues(transactionMeta, TEST_SELECTED_TOKEN), | ||
mockState, | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual({ | ||
fiatDisplayValue: undefined, | ||
tokenBalance: '1', | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.