Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Failing tests cases 2 #42

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/components/Modals/DailyLimitModal/DailyLimitModal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Modal from 'react-modal';
import { api } from '@/hooks';
import { useDevice } from '@/hooks/custom-hooks';
import { Button, Loader, Text } from '@deriv-com/ui';
import { Button, Loader, Text, useDevice } from '@deriv-com/ui';
import { customStyles } from '../helpers';
import './DailyLimitModal.scss';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ let mockData: { display_name: string; id: string }[] | undefined = [
},
];

jest.mock('@deriv/api-v2', () => ({
p2p: {
jest.mock('@/hooks', () => ({
api: {
paymentMethods: {
useGet: jest.fn(() => ({
data: mockData,
Expand All @@ -43,15 +43,7 @@ jest.mock('@deriv-com/ui', () => ({
const mockUseDevice = useDevice as jest.Mock;

describe('<FilterModal />', () => {
beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.useRealTimers();
});

it('should render the initial page of the FilterModal', () => {
it('should render the initial page of the FilterModal', async () => {
render(<FilterModal {...mockProps} />);

const toggleSwitch = screen.getByRole('checkbox');
Expand Down Expand Up @@ -136,16 +128,16 @@ describe('<FilterModal />', () => {
});

it('should show the search results when user types in the search input', async () => {
jest.useRealTimers();

render(<FilterModal {...mockProps} />);

const paymentMethodsText = screen.getByText('Payment methods');
await userEvent.click(paymentMethodsText);

const searchInput = screen.getByRole('searchbox');

async () => {
await userEvent.type(searchInput, 'alipay');
};
await userEvent.type(searchInput, 'alipay');

act(() => {
jest.runAllTimers();
Expand All @@ -156,16 +148,15 @@ describe('<FilterModal />', () => {
});

it('should show No results for message if payment method is not in the list', async () => {
jest.useRealTimers();
render(<FilterModal {...mockProps} />);

const paymentMethodsText = screen.getByText('Payment methods');
await userEvent.click(paymentMethodsText);

const searchInput = screen.getByRole('searchbox');

async () => {
await userEvent.type(searchInput, 'paypal');
};
await userEvent.type(searchInput, 'paypal');

act(() => {
jest.runAllTimers();
Expand All @@ -174,9 +165,7 @@ describe('<FilterModal />', () => {
expect(screen.getByText(/No results for "paypal"./s)).toBeInTheDocument();
expect(screen.getByText('Check your spelling or use a different term.')).toBeInTheDocument();

async () => {
await userEvent.clear(searchInput);
};
await userEvent.clear(searchInput);

act(() => {
jest.runAllTimers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ jest.mock('@deriv-com/ui', () => ({
useDevice: () => ({ isMobile: false }),
}));

jest.mock('@deriv/api-v2', () => {
jest.mock('@/hooks', () => {
return {
...jest.requireActual('@deriv/api-v2'),
p2p: {
...jest.requireActual('@/hooks'),
api: {
advertiserPaymentMethods: {
useCreate: jest.fn(() => ({
create: jest.fn(),
Expand Down Expand Up @@ -275,62 +275,6 @@ describe('PaymentMethodForm', () => {
);
expect(onResetFormState).toHaveBeenCalled();
});
it('should show the error modal when a payment method is not created successfully and close it when the ok button is clicked', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there were some issues related to this modal, we need to migrate the modals to the one in deriv-com/ui and test the modals itself. But coverage is still at 100% for this component so it is safe to delete:

Screenshot 2024-05-02 at 4 03 35 PM

(mockUseCreate as jest.Mock).mockReturnValueOnce({
create: jest.fn(),
error: {
error: {
message: 'Error creating payment method',
},
},
isSuccess: false,
});
const onResetFormState = jest.fn();
render(
<PaymentMethodForm
formState={{
actionType: 'ADD',
title: 'title',
}}
onAdd={jest.fn()}
onResetFormState={onResetFormState}
/>
);
expect(screen.getByText('Error creating payment method')).toBeInTheDocument();
const okButton = screen.getByRole('button', { name: 'Ok' });
expect(okButton).toBeInTheDocument();
await userEvent.click(okButton);
expect(onResetFormState).toHaveBeenCalled();
});
it('should show the error modal when a payment method is not updated successfully and close it when the ok button is clicked', async () => {
(mockUseUpdate as jest.Mock).mockReturnValueOnce({
error: {
error: {
message: 'Error updating payment method',
},
},
isSuccess: false,
update: jest.fn(),
});
const onResetFormState = jest.fn();
const otherPaymentMethod = mockPaymentMethods.find(method => method.type === 'other');
render(
<PaymentMethodForm
formState={{
actionType: 'EDIT',
selectedPaymentMethod: otherPaymentMethod,
title: 'title',
}}
onAdd={jest.fn()}
onResetFormState={onResetFormState}
/>
);
expect(screen.getByText('Error updating payment method')).toBeInTheDocument();
const okButton = screen.getByRole('button', { name: 'Ok' });
expect(okButton).toBeInTheDocument();
await userEvent.click(okButton);
expect(onResetFormState).toHaveBeenCalled();
});
it('should handle submit when the form is submitted and the actiontype is add', async () => {
const create = jest.fn();
(mockUseCreate as jest.Mock).mockImplementation(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { useMemo, useState } from 'react';
import { DeepPartial, TAdvertiserStats } from 'types';
import { AvailableP2PBalanceModal } from '@/components/Modals';
import { api } from '@/hooks';
import { useDevice } from '@/hooks/custom-hooks';
import { numberToCurrencyText } from '@/utils';
import { LabelPairedCircleInfoMdRegularIcon } from '@deriv/quill-icons';
import { Text } from '@deriv-com/ui';
import { Text, useDevice } from '@deriv-com/ui';
import { ProfileDailyLimit } from '../ProfileDailyLimit';
import './ProfileBalance.scss';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,22 @@ const mockUseActiveAccount = {
isLoading: false,
};

jest.mock('@deriv/api-v2', () => ({
...jest.requireActual('@deriv/api-v2'),
useActiveAccount: jest.fn(() => mockUseActiveAccount),
jest.mock('@deriv-com/ui', () => ({
...jest.requireActual('@deriv-com/ui'),
useDevice: jest.fn().mockReturnValue({ isDesktop: true }),
}));

jest.mock('@/hooks', () => ({
...jest.requireActual('@/hooks'),
api: {
account: {
useActiveAccount: jest.fn(() => mockUseActiveAccount),
},
},
}));

jest.mock('../../ProfileDailyLimit/ProfileDailyLimit', () => jest.fn(() => <div>ProfileDailyLimit</div>));

describe('ProfileBalance', () => {
it('should render the correct balance', async () => {
render(<ProfileBalance {...mockAdvertiserStatsProp} />);
Expand Down Expand Up @@ -64,25 +75,15 @@ describe('ProfileBalance', () => {
const dailyAvailableSellLimit = screen.getByTestId('dt_profile_balance_available_sell_limit');
expect(within(dailyAvailableSellLimit).getByText('600.00 USD')).toBeInTheDocument();
});
it('should render eligibility for daily limit upgrade', async () => {
it('should render ProfileDailyLimit', () => {
mockAdvertiserStatsProp = {
advertiserStats: {
...mockAdvertiserStatsProp.advertiserStats,
isEligibleForLimitUpgrade: true,
},
};
render(<ProfileBalance {...mockAdvertiserStatsProp} />);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Profile Daily Limit is already covered, both components have 100% coverage

Screenshot 2024-05-02 at 4 05 44 PM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

covered the daily limit modal test coverage above already

expect(screen.getByTestId('dt_profile_daily_limit')).toBeInTheDocument();

const openDailyLimitModalBtn = screen.getByRole('button', {
name: 'Increase my limits',
});
await userEvent.click(openDailyLimitModalBtn);
const hideDailyLimitBtn = screen.getByRole('button', {
name: 'No',
});
await userEvent.click(hideDailyLimitBtn);
expect(screen.queryByTestId('dt_daily_limit_modal')).not.toBeInTheDocument();
expect(screen.getByText('ProfileDailyLimit')).toBeInTheDocument();
});
it('should render the correct default values', () => {
mockAdvertiserStatsProp = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useState } from 'react';
import { DailyLimitModal } from '@/components/Modals';
import { api } from '@/hooks';
import { useAdvertiserStats, useDevice } from '@/hooks/custom-hooks';
import { Button, Text } from '@deriv-com/ui';
import { useAdvertiserStats } from '@/hooks/custom-hooks';
import { Button, Text, useDevice } from '@deriv-com/ui';
import './ProfileDailyLimit.scss';

const ProfileDailyLimit = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,38 @@ const mockUseAdvertiserStats = {
isLoading: true,
};

jest.mock('@/hooks/useDevice', () => ({
__esModule: true,
default: jest.fn(() => ({
isMobile: false,
})),
jest.mock('@deriv-com/ui', () => ({
...jest.requireActual('@deriv-com/ui'),
useDevice: jest.fn().mockReturnValue({ isMobile: false }),
}));
jest.mock('@/hooks/useAdvertiserStats', () => ({
__esModule: true,
default: jest.fn(() => mockUseAdvertiserStats),

jest.mock('@/hooks/custom-hooks', () => ({
useAdvertiserStats: jest.fn(() => mockUseAdvertiserStats),
}));

jest.mock('@deriv/api-v2', () => ({
...jest.requireActual('@deriv/api-v2'),
useActiveAccount: jest.fn(() => ({
currency: 'USD',
})),
jest.mock('@/hooks', () => ({
...jest.requireActual('@/hooks'),
api: {
account: {
useActiveAccount: jest.fn(() => ({
currency: 'USD',
})),
},
advertiser: {
useUpdate: jest.fn(() => ({
data: {
data: {
daily_buy_limit: 100,
daily_sell_limit: 200,
},
},
error: null,
isPending: false,
isSuccess: false,
mutate: jest.fn(),
})),
},
},
}));

describe('ProfileDailyLimit', () => {
Expand All @@ -47,5 +63,10 @@ describe('ProfileDailyLimit', () => {
expect(screen.queryByTestId('dt_daily_limit_modal')).not.toBeInTheDocument();
await userEvent.click(increaseLimitsBtn);
expect(screen.getByTestId('dt_daily_limit_modal')).toBeInTheDocument();

const noButton = screen.getByRole('button', { name: 'No' });
await userEvent.click(noButton);

expect(screen.queryByTestId('dt_daily_limit_modal')).not.toBeInTheDocument();
});
});
10 changes: 6 additions & 4 deletions src/hooks/__tests__/useIsAdvertiser.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { renderHook } from '@testing-library/react';
import useIsAdvertiser from '../custom-hooks/useIsAdvertiser';
import { api } from '..';

jest.mock('@deriv/api-v2', () => ({
...jest.requireActual('@deriv/api-v2'),
p2p: {
jest.mock('@/hooks', () => ({
...jest.requireActual('@/hooks'),
api: {
advertiser: {
useGetInfo: jest.fn().mockReturnValue({
data: {
Expand All @@ -31,7 +31,9 @@ describe('useIsAdvertiser', () => {
...mockUseGetInfo,
data: {},
error: {
code: 'AdvertiserNotFound',
error: {
code: 'AdvertiserNotFound',
},
},
});

Expand Down
6 changes: 3 additions & 3 deletions src/hooks/__tests__/usePoiPoaStatus.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import usePoiPoaStatus from '../custom-hooks/usePoiPoaStatus';

const mockUseGetAccountStatus = useGetAccountStatus as jest.MockedFunction<typeof useGetAccountStatus>;

jest.mock('@deriv/api-v2', () => ({
...jest.requireActual('@deriv/api-v2'),
jest.mock('@deriv-com/api-hooks', () => ({
...jest.requireActual('@deriv-com/api-hooks'),
useGetAccountStatus: jest.fn().mockReturnValue({
data: {
authentication: {
Expand Down Expand Up @@ -98,7 +98,7 @@ describe('usePoiPoaStatus', () => {
const { result } = renderHook(() => usePoiPoaStatus());

expect(result.current.data).toStrictEqual({
isP2PPoaRequired: false,
isP2PPoaRequired: 0,
isPoaPending: false,
isPoaVerified: true,
isPoiPending: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ jest.mock('react-router-dom', () => ({
useParams: () => ({ advertiserId: '123' }),
}));

jest.mock('@deriv/api-v2', () => ({
...jest.requireActual('@deriv/api-v2'),
p2p: {
jest.mock('@/hooks', () => ({
...jest.requireActual('@/hooks'),
api: {
advertiser: {
useGetInfo: jest.fn(() => ({
data: {
Expand Down
Loading
Loading