From fce46a35771eb9ea65dfd5254dd373a4c8e350f1 Mon Sep 17 00:00:00 2001 From: vetalcore Date: Thu, 28 Nov 2024 15:14:14 +0200 Subject: [PATCH] fix: [lw-11830]: fix unit tests --- .../__tests__/useSelectedCoins.test.ts | 35 +++++++++++++++++++ .../Form/CoinInput/__tests__/util.test.ts | 25 ++++++++++--- .../Form/CoinInput/useSelectedCoins.tsx | 2 +- .../components/Form/__tests__/Form.test.tsx | 25 +++++++------ .../features/send-transaction/helpers.ts | 2 +- 5 files changed, 72 insertions(+), 17 deletions(-) diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/__tests__/useSelectedCoins.test.ts b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/__tests__/useSelectedCoins.test.ts index 9aee39a5b0..752fd88848 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/__tests__/useSelectedCoins.test.ts +++ b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/__tests__/useSelectedCoins.test.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable no-magic-numbers */ const mockCoinStateSelector = { uiOutputs: [], @@ -10,6 +11,7 @@ const mockUseCurrencyStore = jest.fn().mockReturnValue({ fiatCurrency: { code: ' const mockUseWalletStore = jest.fn().mockReturnValue({ walletUI: { cardanoCoin: { id: '1', name: 'Cardano', decimals: 6, symbol: 'ADA' }, appMode: 'popup' } }); +const mockUseRewardAccountsData = jest.fn().mockReturnValue({ lockedStakeRewards: 0 }); const mockUseCoinStateSelector = jest.fn().mockReturnValue(mockCoinStateSelector); const mockUseBuiltTxState = jest.fn().mockReturnValue({ builtTxData: { error: undefined } }); const mockUseAddressState = jest.fn().mockReturnValue({ address: undefined }); @@ -41,6 +43,12 @@ jest.mock('@stores', (): typeof Stores => ({ ...jest.requireActual('@stores'), useWalletStore: mockUseWalletStore })); + +jest.mock('@src/views/browser-view/features/staking/hooks', () => ({ + ...jest.requireActual('@src/views/browser-view/features/staking/hooks'), + useRewardAccountsData: mockUseRewardAccountsData +})); + jest.mock('../../../../store', (): typeof SendTransactionStore => ({ ...jest.requireActual('../../../../store'), useCoinStateSelector: mockUseCoinStateSelector, @@ -191,6 +199,33 @@ describe('useSelectedCoin', () => { expect(result.current.selectedCoins).toHaveLength(1); expect(result.current.selectedCoins[0].coin).toEqual({ id: '1', ticker: 'ADA', balance: 'Balance: 1.00M' }); }); + test('gets coin properties from walletUI cardanoCoin in store with compacts coin balance and locked rewards', () => { + mockUseRewardAccountsData.mockReturnValueOnce({ + lockedStakeRewards: '10000000000' + }); + + mockUseCoinStateSelector.mockReturnValueOnce({ + ...mockCoinStateSelector, + uiOutputs: [{ id: '1', value: '100' }] + }); + const props: UseSelectedCoinsProps = { + assetBalances: new Map(), + assets: new Map(), + bundleId: 'bundleId', + coinBalance: '1010000000000', + spendableCoin: BigInt(100) + }; + const { result } = renderUseSelectedCoins(props); + + expect(result.current.selectedCoins).toHaveLength(1); + expect(result.current.selectedCoins[0].coin).toEqual({ + id: '1', + ticker: 'ADA', + balance: 'Balance: 1.01M', + availableBalance: 'Available Balance: 1.00M', + lockedStakeRewards: 'Locked Stake Rewards: 10,000.00' + }); + }); test('converts coin value to fiat and set decimals from walletUI cardanoCoin', () => { mockUseCoinStateSelector.mockReturnValueOnce({ diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/__tests__/util.test.ts b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/__tests__/util.test.ts index 5f5266178e..356f82f97f 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/__tests__/util.test.ts +++ b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/__tests__/util.test.ts @@ -35,7 +35,10 @@ describe('CoinInput util', () => { max: '1', hasMaxBtn: false, hasReachedMaxAmount: false, - allowFloat: true + allowFloat: true, + hasReachedMaxAvailableAmount: true, + lockedStakeRewards: '0.00', + totalADA: '0.00' }); }); test('returns 0 for max and true for hasReachedMaxAmount when spendable coins is 0', () => { @@ -44,7 +47,10 @@ describe('CoinInput util', () => { max: '0', hasMaxBtn: true, hasReachedMaxAmount: true, - allowFloat: true + allowFloat: true, + hasReachedMaxAvailableAmount: false, + lockedStakeRewards: '0.00', + totalADA: '1.00' }); }); test('returns formatted balance as availableADA, and the spendable coin in ADA as max when there is no spending', () => { @@ -53,7 +59,10 @@ describe('CoinInput util', () => { max: '10', hasMaxBtn: true, hasReachedMaxAmount: false, - allowFloat: true + allowFloat: true, + hasReachedMaxAvailableAmount: false, + lockedStakeRewards: '0.00', + totalADA: '20.00' }); }); test('returns the calculated max amount when there is less spent coin than spendable coin', () => { @@ -62,7 +71,10 @@ describe('CoinInput util', () => { max: '7', hasMaxBtn: true, hasReachedMaxAmount: false, - allowFloat: true + allowFloat: true, + hasReachedMaxAvailableAmount: false, + lockedStakeRewards: '0.00', + totalADA: '20.00' }); }); test('returns max amount as 0 and hasReachedMaxAmount as true when there is more spent coin than spendable coin', () => { @@ -71,7 +83,10 @@ describe('CoinInput util', () => { max: '0', hasMaxBtn: true, hasReachedMaxAmount: true, - allowFloat: true + allowFloat: true, + hasReachedMaxAvailableAmount: false, + lockedStakeRewards: '0.00', + totalADA: '20.00' }); }); }); diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/useSelectedCoins.tsx b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/useSelectedCoins.tsx index facd4a640a..8271f8d938 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/useSelectedCoins.tsx +++ b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/useSelectedCoins.tsx @@ -209,7 +209,7 @@ export const useSelectedCoins = ({ id: cardanoCoin.id, ticker: cardanoCoin.symbol, balance: t('send.balanceAmount', { amount: compactNumberWithUnit(totalADA) }), - ...(lockedStakeRewards && { + ...(rewardAcountsData.lockedStakeRewards && { availableBalance: t('send.availableBalanceAmount', { amount: compactNumberWithUnit(availableADA) }), diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/__tests__/Form.test.tsx b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/__tests__/Form.test.tsx index c525ea612e..63fd192084 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/__tests__/Form.test.tsx +++ b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/__tests__/Form.test.tsx @@ -15,6 +15,7 @@ import * as CurrencyProvider from '@providers/currency'; const mockUseOutputs = jest.fn(); const mockGetBackgroundStorage = jest.fn(); const mockUseMaxAda = jest.fn().mockReturnValue(BigInt(100)); +const mockUseRewardAccountsData = jest.fn().mockReturnValue({ lockedStakeRewards: 0 }); const mockUseAddressState = jest.fn((_row: string) => ({ address: '', handle: '', @@ -95,6 +96,11 @@ jest.mock('@providers/currency', (): typeof CurrencyProvider => ({ useCurrencyStore: mockUseCurrencyStore })); +jest.mock('@src/views/browser-view/features/staking/hooks', () => ({ + ...jest.requireActual('@src/views/browser-view/features/staking/hooks'), + useRewardAccountsData: mockUseRewardAccountsData +})); + const setNewOutput = jest.fn(); mockUseOutputs.mockReturnValue({ setNewOutput, @@ -122,16 +128,15 @@ const backgroundService = { const getWrapper = ({ backgroundService }: { backgroundService?: BackgroundServiceAPIProviderProps['value'] }) => - ({ children }: { children: React.ReactNode }) => - ( - - - - {children} - - - - ); + ({ children }: { children: React.ReactNode }) => ( + + + + {children} + + + + ); const mockProps: Props = { assets: new Map(), diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/helpers.ts b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/helpers.ts index cfd4fec4bb..6b5a5384de 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/helpers.ts +++ b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/helpers.ts @@ -93,7 +93,7 @@ export const hasReachedMaxAmountAda = ({ }): boolean => tokensUsed[cardanoCoin.id] && balance ? new BigNumber(tokensUsed[cardanoCoin.id])[exceed ? 'gt' : 'gte']( - Wallet.util.lovelacesToAdaString((balance + availableRewards).toString()) + Wallet.util.lovelacesToAdaString((BigInt(balance) + BigInt(availableRewards)).toString()) ) : false;