Skip to content

Commit

Permalink
fix: [lw-11830]: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vetalcore committed Nov 28, 2024
1 parent 37201e4 commit fce46a3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable no-magic-numbers */
const mockCoinStateSelector = {
uiOutputs: [],
Expand All @@ -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 });
Expand Down Expand Up @@ -41,6 +43,12 @@ jest.mock('@stores', (): typeof Stores => ({
...jest.requireActual<typeof Stores>('@stores'),
useWalletStore: mockUseWalletStore
}));

jest.mock('@src/views/browser-view/features/staking/hooks', () => ({
...jest.requireActual<any>('@src/views/browser-view/features/staking/hooks'),
useRewardAccountsData: mockUseRewardAccountsData
}));

jest.mock('../../../../store', (): typeof SendTransactionStore => ({
...jest.requireActual<typeof SendTransactionStore>('../../../../store'),
useCoinStateSelector: mockUseCoinStateSelector,
Expand Down Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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'
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand Down Expand Up @@ -95,6 +96,11 @@ jest.mock('@providers/currency', (): typeof CurrencyProvider => ({
useCurrencyStore: mockUseCurrencyStore
}));

jest.mock('@src/views/browser-view/features/staking/hooks', () => ({
...jest.requireActual<any>('@src/views/browser-view/features/staking/hooks'),
useRewardAccountsData: mockUseRewardAccountsData
}));

const setNewOutput = jest.fn();
mockUseOutputs.mockReturnValue({
setNewOutput,
Expand Down Expand Up @@ -122,16 +128,15 @@ const backgroundService = {

const getWrapper =
({ backgroundService }: { backgroundService?: BackgroundServiceAPIProviderProps['value'] }) =>
({ children }: { children: React.ReactNode }) =>
(
<BackgroundServiceAPIProvider value={backgroundService}>
<DatabaseProvider>
<PostHogClientProvider postHogCustomClient={postHogClientMocks as any}>
<AnalyticsProvider analyticsDisabled>{children}</AnalyticsProvider>
</PostHogClientProvider>
</DatabaseProvider>
</BackgroundServiceAPIProvider>
);
({ children }: { children: React.ReactNode }) => (

Check failure on line 131 in apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/__tests__/Form.test.tsx

View workflow job for this annotation

GitHub Actions / Release package

Insert `⏎···`
<BackgroundServiceAPIProvider value={backgroundService}>

Check failure on line 132 in apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/__tests__/Form.test.tsx

View workflow job for this annotation

GitHub Actions / Release package

Insert `··`
<DatabaseProvider>

Check failure on line 133 in apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/__tests__/Form.test.tsx

View workflow job for this annotation

GitHub Actions / Release package

Insert `··`
<PostHogClientProvider postHogCustomClient={postHogClientMocks as any}>

Check failure on line 134 in apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/__tests__/Form.test.tsx

View workflow job for this annotation

GitHub Actions / Release package

Insert `··`
<AnalyticsProvider analyticsDisabled>{children}</AnalyticsProvider>

Check failure on line 135 in apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/__tests__/Form.test.tsx

View workflow job for this annotation

GitHub Actions / Release package

Insert `··`
</PostHogClientProvider>

Check failure on line 136 in apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/__tests__/Form.test.tsx

View workflow job for this annotation

GitHub Actions / Release package

Insert `··`
</DatabaseProvider>

Check failure on line 137 in apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/__tests__/Form.test.tsx

View workflow job for this annotation

GitHub Actions / Release package

Replace `······` with `········`
</BackgroundServiceAPIProvider>

Check failure on line 138 in apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/__tests__/Form.test.tsx

View workflow job for this annotation

GitHub Actions / Release package

Insert `··`
);

Check failure on line 139 in apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/__tests__/Form.test.tsx

View workflow job for this annotation

GitHub Actions / Release package

Insert `··`

const mockProps: Props = {
assets: new Map(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit fce46a3

Please sign in to comment.