|
| 1 | +import React from 'react'; |
| 2 | +import { APIProvider } from '@deriv/api'; |
| 3 | +import { render, screen, within } from '@testing-library/react'; |
| 4 | +import userEvent from '@testing-library/user-event'; |
| 5 | +import MyProfileBalance from '../MyProfileBalance'; |
| 6 | + |
| 7 | +const wrapper = ({ children }: { children: JSX.Element }) => ( |
| 8 | + <APIProvider> |
| 9 | + <div id='v2_modal_root' /> |
| 10 | + {children} |
| 11 | + </APIProvider> |
| 12 | +); |
| 13 | + |
| 14 | +let mockUseAdvertiserStats = { |
| 15 | + data: { |
| 16 | + balance_available: 50000, |
| 17 | + daily_buy_limit: 500, |
| 18 | + daily_sell_limit: 500, |
| 19 | + dailyAvailableBuyLimit: 10, |
| 20 | + dailyAvailableSellLimit: 10, |
| 21 | + fullName: 'Jane Doe', |
| 22 | + isEligibleForLimitUpgrade: false, |
| 23 | + name: 'Jane', |
| 24 | + show_name: 0, |
| 25 | + }, |
| 26 | + isLoading: false, |
| 27 | +}; |
| 28 | +const mockUseActiveAccount = { |
| 29 | + data: { |
| 30 | + currency: 'USD', |
| 31 | + }, |
| 32 | + isLoading: false, |
| 33 | +}; |
| 34 | + |
| 35 | +jest.mock('@/hooks', () => ({ |
| 36 | + ...jest.requireActual('@/hooks'), |
| 37 | + useAdvertiserStats: jest.fn(() => mockUseAdvertiserStats), |
| 38 | +})); |
| 39 | + |
| 40 | +jest.mock('@deriv/api', () => ({ |
| 41 | + ...jest.requireActual('@deriv/api'), |
| 42 | + useActiveAccount: jest.fn(() => mockUseActiveAccount), |
| 43 | +})); |
| 44 | + |
| 45 | +describe('MyProfileBalance', () => { |
| 46 | + it('should render the correct balance', () => { |
| 47 | + render(<MyProfileBalance />, { wrapper }); |
| 48 | + const availableBalanceNode = screen.getByTestId('dt_p2p_v2_available_balance_amount'); |
| 49 | + expect(within(availableBalanceNode).getByText('50,000.00 USD')).toBeInTheDocument(); |
| 50 | + |
| 51 | + const balanceInfoIcon = screen.getByTestId('dt_p2p_v2_available_balance_icon'); |
| 52 | + userEvent.click(balanceInfoIcon); |
| 53 | + expect(screen.getByTestId('dt_p2p_v2_available_p2p_balance_modal')).toBeInTheDocument(); |
| 54 | + const okButton = screen.getByRole('button', { |
| 55 | + name: 'Ok', |
| 56 | + }); |
| 57 | + userEvent.click(okButton); |
| 58 | + expect(screen.queryByTestId('dt_p2p_v2_available_p2p_balance_modal')).not.toBeInTheDocument(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should render the correct limits', () => { |
| 62 | + mockUseAdvertiserStats = { |
| 63 | + data: { |
| 64 | + ...mockUseAdvertiserStats.data, |
| 65 | + daily_buy_limit: 500, |
| 66 | + daily_sell_limit: 2000, |
| 67 | + dailyAvailableBuyLimit: 100, |
| 68 | + dailyAvailableSellLimit: 600, |
| 69 | + }, |
| 70 | + isLoading: false, |
| 71 | + }; |
| 72 | + render(<MyProfileBalance />, { wrapper }); |
| 73 | + const dailyBuyLimitNode = screen.getByTestId('dt_p2p_v2_profile_balance_daily_buy_limit'); |
| 74 | + expect(within(dailyBuyLimitNode).getByText('500 USD')).toBeInTheDocument(); |
| 75 | + const availableBuyLimitNode = screen.getByTestId('dt_p2p_v2_profile_balance_available_buy_limit'); |
| 76 | + expect(within(availableBuyLimitNode).getByText('100.00 USD')).toBeInTheDocument(); |
| 77 | + |
| 78 | + const dailySellLimitNode = screen.getByTestId('dt_p2p_v2_profile_balance_daily_sell_limit'); |
| 79 | + expect(within(dailySellLimitNode).getByText('2000 USD')).toBeInTheDocument(); |
| 80 | + const dailyAvailableSellLimit = screen.getByTestId('dt_p2p_v2_profile_balance_available_sell_limit'); |
| 81 | + expect(within(dailyAvailableSellLimit).getByText('600.00 USD')).toBeInTheDocument(); |
| 82 | + }); |
| 83 | + it('should render eligibility for daily limit upgrade', () => { |
| 84 | + mockUseAdvertiserStats = { |
| 85 | + data: { |
| 86 | + ...mockUseAdvertiserStats.data, |
| 87 | + isEligibleForLimitUpgrade: true, |
| 88 | + }, |
| 89 | + isLoading: false, |
| 90 | + }; |
| 91 | + render(<MyProfileBalance />, { wrapper }); |
| 92 | + expect(screen.getByTestId('dt_p2p_v2_profile_daily_limit')).toBeInTheDocument(); |
| 93 | + |
| 94 | + const openDailyLimitModalBtn = screen.getByRole('button', { |
| 95 | + name: 'Increase my limits', |
| 96 | + }); |
| 97 | + userEvent.click(openDailyLimitModalBtn); |
| 98 | + const hideDailyLimitBtn = screen.getByRole('button', { |
| 99 | + name: 'No', |
| 100 | + }); |
| 101 | + userEvent.click(hideDailyLimitBtn); |
| 102 | + expect(screen.queryByTestId('dt_p2p_v2_daily_limit_modal')).not.toBeInTheDocument(); |
| 103 | + }); |
| 104 | + it('should render the correct default values', () => { |
| 105 | + mockUseAdvertiserStats = { |
| 106 | + // @ts-expect-error To clear the mocked values and assert the default values |
| 107 | + data: {}, |
| 108 | + isLoading: false, |
| 109 | + }; |
| 110 | + render(<MyProfileBalance />, { wrapper }); |
| 111 | + const availableBalanceNode = screen.getByTestId('dt_p2p_v2_available_balance_amount'); |
| 112 | + expect(within(availableBalanceNode).getByText('0.00 USD')).toBeInTheDocument(); |
| 113 | + const dailyBuyLimitNode = screen.getByTestId('dt_p2p_v2_profile_balance_daily_buy_limit'); |
| 114 | + expect(within(dailyBuyLimitNode).getByText('0.00 USD')).toBeInTheDocument(); |
| 115 | + const availableBuyLimitNode = screen.getByTestId('dt_p2p_v2_profile_balance_available_buy_limit'); |
| 116 | + expect(within(availableBuyLimitNode).getByText('0.00 USD')).toBeInTheDocument(); |
| 117 | + |
| 118 | + const dailySellLimitNode = screen.getByTestId('dt_p2p_v2_profile_balance_daily_sell_limit'); |
| 119 | + expect(within(dailySellLimitNode).getByText('0.00 USD')).toBeInTheDocument(); |
| 120 | + const dailyAvailableSellLimit = screen.getByTestId('dt_p2p_v2_profile_balance_available_sell_limit'); |
| 121 | + expect(within(dailyAvailableSellLimit).getByText('0.00 USD')).toBeInTheDocument(); |
| 122 | + }); |
| 123 | +}); |
0 commit comments