Skip to content

Commit 6e76bc6

Browse files
committed
Merge branch 'master' of github.com:deriv-com/p2p into shayan/FEQ-2389/implement-auto-login-to-p2p
2 parents 79055c6 + e44acd3 commit 6e76bc6

File tree

13 files changed

+166
-86
lines changed

13 files changed

+166
-86
lines changed

src/components/CopyAdForm/CopyAdForm.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Controller, FormProvider, NonUndefined, useForm } from 'react-hook-form';
22
import { TCountryListItem, TCurrency, THooks } from 'types';
33
import { RATE_TYPE } from '@/constants';
4-
import { api, useFloatingRate } from '@/hooks';
4+
import { useFloatingRate } from '@/hooks';
55
import { AdFormInput } from '@/pages/my-ads/components/AdFormInput';
66
import { formatTime, getValidationRules, restrictDecimalPlace } from '@/utils';
7+
import { useP2PCountryList } from '@deriv-com/api-hooks';
78
import { Localize, useTranslations } from '@deriv-com/translations';
89
import { InlineMessage, Text, useDevice } from '@deriv-com/ui';
910
import { FloatingRate } from '../FloatingRate';
@@ -62,7 +63,9 @@ const CopyAdForm = ({ formValues, isModalOpen, onClickCancel, onFormSubmit, ...r
6263
mode: 'all',
6364
});
6465

65-
const { data: countryList = {} as TCountryListItem } = api.countryList.useGet();
66+
const { data: countryList = {} as TCountryListItem } = useP2PCountryList({
67+
refetchOnWindowFocus: false,
68+
});
6669
const {
6770
control,
6871
formState: { isValid },

src/components/CopyAdForm/__tests__/CopyAdForm.spec.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,11 @@ jest.mock('@/hooks/custom-hooks', () => {
6161
};
6262
});
6363

64-
jest.mock('@/hooks', () => ({
65-
...jest.requireActual('@/hooks'),
66-
api: {
67-
countryList: {
68-
useGet: jest.fn(() => ({ data: mockCountryList })),
69-
},
70-
},
64+
jest.mock('@deriv-com/api-hooks', () => ({
65+
...jest.requireActual('@deriv-com/api-hooks'),
66+
useP2PCountryList: jest.fn(() => ({
67+
data: mockCountryList,
68+
})),
7169
}));
7270

7371
describe('CopyAdForm', () => {

src/hooks/api/country/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/hooks/api/country/p2p-country-list/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/hooks/api/country/p2p-country-list/useCountryList.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/hooks/api/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ export * from './advert';
33
export * from './advertiser';
44
export * from './chat';
55
export * from './counterparty';
6-
export * from './country';
76
export * from './order';
87
export * from './order-dispute';
98
export * from './order-review';
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { useP2POrderReview } from '@deriv-com/api-hooks';
2+
import { renderHook } from '@testing-library/react';
3+
import useOrderReview from '../useOrderReview';
4+
5+
jest.mock('@deriv-com/api-hooks', () => ({
6+
...jest.requireActual('@deriv-com/api-hooks'),
7+
useP2POrderReview: jest.fn().mockReturnValue({
8+
data: {},
9+
mutate: jest.fn(),
10+
}),
11+
}));
12+
13+
const mockUseP2POrderReview = useP2POrderReview as jest.MockedFunction<typeof useP2POrderReview>;
14+
15+
const mockFn = jest.fn();
16+
jest.mock('../../../useInvalidateQuery', () => ({
17+
__esModule: true,
18+
default: jest.fn(() => mockFn),
19+
}));
20+
21+
describe('useOrderReview', () => {
22+
it('should return undefined if data is not available', () => {
23+
// @ts-expect-error - useQuery return values not specified
24+
mockUseP2POrderReview.mockReturnValue({
25+
data: undefined,
26+
mutate: jest.fn(),
27+
});
28+
29+
const { result } = renderHook(() => useOrderReview());
30+
31+
expect(result.current.data).toBeUndefined();
32+
});
33+
34+
it('should call invalidate when onSuccess is triggered', async () => {
35+
const { result } = renderHook(() => useOrderReview());
36+
37+
await result.current.mutate({ order_id: '1234', rating: 4, recommended: 1 });
38+
39+
// @ts-expect-error - onSuccess prop not defined
40+
const { onSuccess } = mockUseP2POrderReview.mock.calls[0][0];
41+
onSuccess();
42+
expect(mockFn).toHaveBeenCalledWith('p2p_order_list');
43+
});
44+
45+
it('should return the correct data when data is available', () => {
46+
const mockData = {
47+
advertiser_id: 'CR1234',
48+
created_time: 123456,
49+
order_id: '1234',
50+
rating: 4,
51+
recommended: 1 as 0 | 1,
52+
};
53+
54+
// @ts-expect-error - useQuery return values not specified
55+
mockUseP2POrderReview.mockReturnValue({
56+
data: mockData,
57+
mutate: jest.fn(),
58+
});
59+
const { result } = renderHook(() => useOrderReview());
60+
61+
expect(result.current.data).toEqual(mockData);
62+
});
63+
});

src/hooks/api/order-review/p2p-order-review/useOrderReview.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useMemo } from 'react';
1+
import { useCallback } from 'react';
22
import { useP2POrderReview } from '@deriv-com/api-hooks';
33
import useInvalidateQuery from '../../useInvalidateQuery';
44

@@ -34,23 +34,9 @@ const useOrderReview = () => {
3434
[_mutate]
3535
);
3636

37-
const modified_data = useMemo(() => {
38-
const p2p_order_review = data;
39-
40-
if (!p2p_order_review) return undefined;
41-
42-
return {
43-
...p2p_order_review,
44-
// Flag to check if the advertiser has not been recommended yet
45-
has_not_been_recommended: p2p_order_review.recommended === null,
46-
// Flag to check if the advertiser is recommended
47-
is_recommended: Boolean(p2p_order_review.recommended),
48-
};
49-
}, [data]);
50-
5137
return {
5238
/** Data returned after a review was created for the order */
53-
data: modified_data,
39+
data,
5440
/** mutate function to create a review for a specified order */
5541
mutate,
5642
...rest,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { act, renderHook, waitFor } from '@testing-library/react';
2+
import useCopyToClipboard from '../useCopyToClipboard';
3+
4+
const mockCopyFunction = async (text: string) => {
5+
return text === 'error' ? Promise.reject() : Promise.resolve();
6+
};
7+
8+
const mockCopyToClipboardData = [
9+
'',
10+
async (text: string) => {
11+
return mockCopyFunction(text);
12+
},
13+
];
14+
15+
jest.mock('usehooks-ts', () => ({
16+
useCopyToClipboard: jest.fn(() => mockCopyToClipboardData),
17+
}));
18+
19+
describe('useCopyToClipboard', () => {
20+
it('should return default values', () => {
21+
const { result } = renderHook(() => useCopyToClipboard());
22+
expect(result.current).toEqual([false, expect.any(Function), expect.any(Function)]);
23+
});
24+
25+
it('should set isCopied to true on successful copy', async () => {
26+
const { result } = renderHook(() => useCopyToClipboard());
27+
const [, copyToClipboard] = result.current;
28+
29+
act(() => {
30+
copyToClipboard('Test text').catch(() => {});
31+
});
32+
33+
await waitFor(() => {
34+
expect(result.current[0]).toBe(true);
35+
});
36+
});
37+
38+
it('should set isCopied to false on failed copy', async () => {
39+
const { result } = renderHook(() => useCopyToClipboard());
40+
const [, copyToClipboard] = result.current;
41+
42+
act(() => {
43+
copyToClipboard('error');
44+
});
45+
46+
await waitFor(() => {
47+
expect(result.current[0]).toBe(false);
48+
});
49+
});
50+
});

src/hooks/custom-hooks/useCopyToClipboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const useCopyToClipboard = (): [boolean, copyFn, setterFn] => {
1010

1111
const copyToClipboard = async (text: string) => {
1212
try {
13-
copy(text);
13+
await copy(text);
1414
setIsCopied(true);
1515
return true;
1616
} catch (error) {

src/pages/my-ads/screens/CreateEditAd/CreateEditAd.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AdCancelCreateEditModal, AdCreateEditErrorModal, AdCreateEditSuccessMod
66
import { MY_ADS_URL, RATE_TYPE } from '@/constants';
77
import { api } from '@/hooks';
88
import { useFloatingRate, useModalManager, useQueryString } from '@/hooks/custom-hooks';
9+
import { useP2PCountryList } from '@deriv-com/api-hooks';
910
import { Loader } from '@deriv-com/ui';
1011
import { LocalStorageConstants, LocalStorageUtils } from '@deriv-com/utils';
1112
import { AdWizard } from '../../components';
@@ -46,7 +47,9 @@ const CreateEditAd = () => {
4647
const { data: advertInfo, isLoading } = api.advert.useGet({ id: advertId ?? undefined }, !!advertId, false);
4748
const isEdit = !!advertId;
4849
const { hideModal, isModalOpenFor, showModal } = useModalManager({ shouldReinitializeModals: false });
49-
const { data: countryList = {} as TCountryListItem } = api.countryList.useGet();
50+
const { data: countryList = {} as TCountryListItem } = useP2PCountryList({
51+
refetchOnWindowFocus: false,
52+
});
5053
const { data: paymentMethodList = [] } = api.paymentMethods.useGet();
5154
const { floatRateOffsetLimitString, rateType } = useFloatingRate();
5255
const { data: activeAccount } = api.account.useActiveAccount();

src/pages/my-ads/screens/CreateEditAd/__tests__/CreateEditAd.spec.tsx

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -82,38 +82,6 @@ jest.mock('@/hooks', () => ({
8282
},
8383
})),
8484
},
85-
countryList: {
86-
useGet: () => ({
87-
data: {
88-
af: {
89-
country_name: 'Afghanistan',
90-
cross_border_ads_enabled: 1,
91-
fixed_rate_adverts: 'enabled',
92-
float_rate_adverts: 'disabled',
93-
float_rate_offset_limit: 10,
94-
local_currency: 'AFN',
95-
payment_methods: {
96-
alipay: {
97-
display_name: 'Alipay',
98-
fields: {
99-
account: {
100-
display_name: 'Alipay ID',
101-
required: 1,
102-
type: 'text',
103-
},
104-
instructions: {
105-
display_name: 'Instructions',
106-
required: 0,
107-
type: 'memo',
108-
},
109-
},
110-
type: 'ewallet',
111-
},
112-
},
113-
},
114-
},
115-
}),
116-
},
11785
paymentMethods: {
11886
useGet: () => ({
11987
data: [
@@ -184,6 +152,40 @@ jest.mock('@/hooks', () => ({
184152
},
185153
}));
186154

155+
jest.mock('@deriv-com/api-hooks', () => ({
156+
...jest.requireActual('@deriv-com/api-hooks'),
157+
useP2PCountryList: jest.fn(() => ({
158+
data: {
159+
af: {
160+
country_name: 'Afghanistan',
161+
cross_border_ads_enabled: 1,
162+
fixed_rate_adverts: 'enabled',
163+
float_rate_adverts: 'disabled',
164+
float_rate_offset_limit: 10,
165+
local_currency: 'AFN',
166+
payment_methods: {
167+
alipay: {
168+
display_name: 'Alipay',
169+
fields: {
170+
account: {
171+
display_name: 'Alipay ID',
172+
required: 1,
173+
type: 'text',
174+
},
175+
instructions: {
176+
display_name: 'Instructions',
177+
required: 0,
178+
type: 'memo',
179+
},
180+
},
181+
type: 'ewallet',
182+
},
183+
},
184+
},
185+
},
186+
})),
187+
}));
188+
187189
jest.mock('@/hooks/custom-hooks', () => {
188190
const modalManager = {
189191
hideModal: jest.fn(),

types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { AD_CONDITION_TYPES, ERROR_CODES } from '@/constants';
44
import { api } from '@/hooks';
55
import { useSendbirdServiceToken } from '@/hooks/api/account';
66
import { useAdvertiserStats, useSendbird } from '@/hooks/custom-hooks';
7-
import { useExchangeRates } from '@deriv-com/api-hooks';
7+
import { useExchangeRates, useP2PCountryList } from '@deriv-com/api-hooks';
88
import { useTranslations } from '@deriv-com/translations';
99
import { Text } from '@deriv-com/ui';
1010
import { CurrencyConstants } from '@deriv-com/utils';
@@ -93,7 +93,7 @@ export namespace THooks {
9393
export type Get = NonNullable<ReturnType<typeof api.settings.useSettings>['data']>;
9494
}
9595
export namespace Country {
96-
export type Get = NonNullable<ReturnType<typeof api.countryList.useGet>['data']>;
96+
export type Get = NonNullable<ReturnType<typeof useP2PCountryList>['data']>;
9797
}
9898
}
9999
export type TOrders = NonNullable<ReturnType<typeof api.order.useGetList>['data']>;

0 commit comments

Comments
 (0)