|
| 1 | +import React from 'react'; |
| 2 | +import { useHistory } from 'react-router-dom'; |
| 3 | +import { useMutation } from '@deriv/api'; |
| 4 | +import { act, fireEvent, render, screen } from '@testing-library/react'; |
| 5 | +import ResetBalance from '../ResetBalance'; |
| 6 | + |
| 7 | +jest.mock('@deriv/api', () => ({ |
| 8 | + useMutation: jest.fn(() => ({ |
| 9 | + isSuccess: false, |
| 10 | + mutate: jest.fn(), |
| 11 | + })), |
| 12 | +})); |
| 13 | + |
| 14 | +jest.mock('react-router-dom', () => ({ |
| 15 | + useHistory: jest.fn(() => ({ |
| 16 | + push: jest.fn(), |
| 17 | + })), |
| 18 | +})); |
| 19 | + |
| 20 | +describe('ResetBalance', () => { |
| 21 | + beforeEach(() => { |
| 22 | + jest.clearAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should render with initial state', () => { |
| 26 | + render(<ResetBalance />); |
| 27 | + |
| 28 | + expect(screen.getByRole('button', { name: 'Reset balance' })).toBeInTheDocument(); |
| 29 | + expect(screen.getByText('Reset your virtual balance to 10,000.00 USD.')).toBeInTheDocument(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should render success state', () => { |
| 33 | + (useMutation as jest.Mock).mockReturnValueOnce({ |
| 34 | + isSuccess: true, |
| 35 | + mutate: jest.fn(), |
| 36 | + }); |
| 37 | + |
| 38 | + render(<ResetBalance />); |
| 39 | + |
| 40 | + expect(screen.getByText('Success')).toBeInTheDocument(); |
| 41 | + expect(screen.getByText('Your balance has been reset to 10,000.00 USD.')).toBeInTheDocument(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should trigger resetBalance function on button click', async () => { |
| 45 | + const { mutate } = useMutation('topup_virtual'); |
| 46 | + (useMutation as jest.Mock).mockReturnValueOnce({ |
| 47 | + isSuccess: false, |
| 48 | + mutate, |
| 49 | + }); |
| 50 | + |
| 51 | + const { push } = useHistory(); |
| 52 | + (useHistory as jest.Mock).mockReturnValueOnce({ |
| 53 | + push, |
| 54 | + }); |
| 55 | + |
| 56 | + render(<ResetBalance />); |
| 57 | + |
| 58 | + const resetBalanceButton = screen.getByRole('button', { name: 'Reset balance' }); |
| 59 | + fireEvent.click(resetBalanceButton); |
| 60 | + |
| 61 | + await act(async () => { |
| 62 | + expect(mutate).toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should redirect to transfer page on button click when reset is successful', async () => { |
| 67 | + (useMutation as jest.Mock).mockReturnValueOnce({ |
| 68 | + isSuccess: true, |
| 69 | + mutate: jest.fn(), |
| 70 | + }); |
| 71 | + |
| 72 | + const { push } = useHistory(); |
| 73 | + (useHistory as jest.Mock).mockReturnValueOnce({ |
| 74 | + push, |
| 75 | + }); |
| 76 | + |
| 77 | + render(<ResetBalance />); |
| 78 | + |
| 79 | + const transferFundsButton = screen.getByRole('button', { name: 'Transfer funds' }); |
| 80 | + fireEvent.click(transferFundsButton); |
| 81 | + |
| 82 | + expect(push).toHaveBeenCalledWith('/wallets/cashier/transfer'); |
| 83 | + }); |
| 84 | +}); |
0 commit comments