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

[FEQ] P2P-V2 useAdvertiserStats hook test case #13647

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
16d8abd
chore: removed responsive root
adrienne-deriv Nov 17, 2023
65d8aec
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Nov 20, 2023
8c91e7b
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Nov 20, 2023
b5027cb
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Nov 20, 2023
79a6f21
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Nov 21, 2023
7956e94
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Nov 22, 2023
e2d06d0
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Nov 22, 2023
4783835
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Nov 22, 2023
a64e58f
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Nov 22, 2023
1200577
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Nov 23, 2023
0e70ed5
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Nov 23, 2023
f11377b
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Nov 24, 2023
07bf6e5
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Nov 30, 2023
2e25182
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Dec 4, 2023
0d432e7
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Dec 5, 2023
bacb384
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Dec 12, 2023
89f0f91
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Dec 12, 2023
b5a3745
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Dec 13, 2023
93526fa
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Dec 14, 2023
56744da
chore: reverted old changes
adrienne-deriv Dec 14, 2023
eb60b51
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Dec 20, 2023
f2eb3ba
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Jan 3, 2024
c90d747
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Jan 3, 2024
a1ab9d8
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Jan 4, 2024
18030ec
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Jan 5, 2024
1c1fdea
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Jan 9, 2024
7e1de5c
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Jan 11, 2024
cbf6c35
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Jan 17, 2024
aa4223d
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Jan 22, 2024
d9e0e7e
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Jan 24, 2024
97addbc
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Jan 24, 2024
b750467
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Jan 31, 2024
eca7237
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Jan 31, 2024
09782fc
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Feb 1, 2024
62f504d
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Feb 2, 2024
e869a75
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Feb 2, 2024
f39bbae
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Feb 6, 2024
a1c21d9
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Feb 6, 2024
4bd1d01
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Feb 7, 2024
8f966c0
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Feb 8, 2024
251c461
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Feb 8, 2024
6368685
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Feb 8, 2024
1c6db57
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Feb 9, 2024
db9ebcd
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Feb 9, 2024
8ed194f
Merge branch 'master' of github.com:binary-com/deriv-app
adrienne-deriv Feb 19, 2024
6511632
chore: added test cases for advertiser stats hook
adrienne-deriv Feb 19, 2024
4d5211d
chore: use mock return value
adrienne-deriv Feb 19, 2024
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
178 changes: 178 additions & 0 deletions packages/p2p-v2/src/hooks/__tests__/useAdvertiserStats.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import * as React from 'react';
import { APIProvider, p2p, useAuthentication, useSettings } from '@deriv/api';
import { renderHook } from '@testing-library/react-hooks';
import useAdvertiserStats from '../useAdvertiserStats';

const mockUseSettings = useSettings as jest.MockedFunction<typeof useSettings>;
const mockUseAuthentication = useAuthentication as jest.MockedFunction<typeof useAuthentication>;
const mockUseAdvertiserInfo = p2p.advertiser.useGetInfo as jest.MockedFunction<typeof p2p.advertiser.useGetInfo>;

jest.mock('@deriv/api', () => ({
...jest.requireActual('@deriv/api'),
p2p: {
advertiser: {
useGetInfo: jest.fn().mockReturnValue({
data: {
currency: 'USD',
},
isLoading: false,
isSuccess: true,
}),
},
},
useAuthentication: jest.fn().mockReturnValue({
data: {
currency: 'USD',
},
isLoading: false,
isSuccess: true,
}),
useSettings: jest.fn().mockReturnValue({
data: {
currency: 'USD',
},
isLoading: false,
isSuccess: true,
}),
}));

describe('useAdvertiserStats', () => {
test('should not return data when useSettings and useAuthentication is still fetching', () => {
mockUseAuthentication.mockReturnValueOnce({
...mockUseAuthentication,
isSuccess: false,
});
mockUseSettings.mockReturnValueOnce({
...mockUseSettings,
isSuccess: false,
});
mockUseAdvertiserInfo.mockReturnValueOnce({
...mockUseAdvertiserInfo,
isSuccess: false,
});
const wrapper = ({ children }: { children: JSX.Element }) => <APIProvider>{children}</APIProvider>;
const { result } = renderHook(() => useAdvertiserStats(), { wrapper });

expect(result.current.data).toBe(undefined);
});
test('should return the correct information', () => {
const wrapper = ({ children }: { children: JSX.Element }) => <APIProvider>{children}</APIProvider>;
mockUseSettings.mockReturnValueOnce({
data: { first_name: 'Jane', last_name: 'Doe' },
});
mockUseAdvertiserInfo.mockReturnValueOnce({
data: {
buy_orders_count: 10,
created_time: 1698034883,
partner_count: 1,
sell_orders_count: 5,
},
});
const { result } = renderHook(() => useAdvertiserStats(), { wrapper });

expect(result.current.data.fullName).toBe('Jane Doe');
expect(result.current.data.tradePartners).toBe(1);
expect(result.current.data.buyOrdersCount).toBe(10);
expect(result.current.data.sellOrdersCount).toBe(5);
expect(result.current.data.daysSinceJoined).toBe(119);
});
test('should return the correct total count and lifetime', () => {
const wrapper = ({ children }: { children: JSX.Element }) => <APIProvider>{children}</APIProvider>;

mockUseAdvertiserInfo.mockReturnValueOnce({
data: {
buy_orders_amount: 10,
buy_orders_count: 10,
partner_count: 1,
sell_orders_amount: 50,
sell_orders_count: 5,
total_orders_count: 30,
total_turnover: 100,
},
});
const { result } = renderHook(() => useAdvertiserStats(), { wrapper });

expect(result.current.data.totalOrders).toBe(15);
expect(result.current.data.totalOrdersLifetime).toBe(30);
expect(result.current.data.tradeVolume).toBe(60);
expect(result.current.data.tradeVolumeLifetime).toBe(100);
});
test('should return the correct rates and limits', () => {
const wrapper = ({ children }: { children: JSX.Element }) => <APIProvider>{children}</APIProvider>;
mockUseAdvertiserInfo.mockReturnValueOnce({
data: {
buy_completion_rate: 1.4,
daily_buy: 10,
daily_buy_limit: 100,
daily_sell: 40,
daily_sell_limit: 50,
sell_completion_rate: 2.4,
upgradable_daily_limits: 1,
},
});
const { result } = renderHook(() => useAdvertiserStats(), { wrapper });

expect(result.current.data.buyCompletionRate).toBe(1.4);
expect(result.current.data.sellCompletionRate).toBe(2.4);
expect(result.current.data.dailyAvailableBuyLimit).toBe(90);
expect(result.current.data.dailyAvailableSellLimit).toBe(10);
expect(result.current.data.isEligibleForLimitUpgrade).toBe(true);
});
test('should return the correct buy/release times', () => {
const wrapper = ({ children }: { children: JSX.Element }) => <APIProvider>{children}</APIProvider>;
mockUseAdvertiserInfo.mockReturnValueOnce({
data: {
buy_time_avg: 150,
release_time_avg: 40,
},
});
const { result } = renderHook(() => useAdvertiserStats(), { wrapper });

expect(result.current.data.averagePayTime).toBe(3);
expect(result.current.data.averageReleaseTime).toBe(1);
});
test('should return the correct verification statuses', () => {
const wrapper = ({ children }: { children: JSX.Element }) => <APIProvider>{children}</APIProvider>;
mockUseAdvertiserInfo.mockReturnValueOnce({
data: {
full_verification: false,
is_approved: false,
},
});
mockUseAuthentication.mockReturnValueOnce({
data: {
document: {
status: 'verified',
},
identity: {
status: 'pending',
},
},
});
const { result } = renderHook(() => useAdvertiserStats(), { wrapper });

expect(result.current.data.isAddressVerified).toBe(true);
expect(result.current.data.isIdentityVerified).toBe(false);

mockUseAdvertiserInfo.mockReturnValueOnce({
data: {
full_verification: true,
is_approved: true,
},
});
mockUseAuthentication.mockReturnValueOnce({
data: {
document: {
status: 'verified',
},
identity: {
status: 'pending',
},
},
});
const { result: verifiedResult } = renderHook(() => useAdvertiserStats(), { wrapper });

expect(verifiedResult.current.data.isAddressVerified).toBe(true);
expect(verifiedResult.current.data.isIdentityVerified).toBe(true);
});
});
10 changes: 7 additions & 3 deletions packages/p2p-v2/src/hooks/useAdvertiserStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ const useAdvertiserStats = (advertiserId?: string) => {
),

/** The advertiser's full name */
fullName: (settings?.first_name || '') + (settings?.last_name || ''),
fullName: `${settings?.first_name || ''} ${settings?.last_name || ''}`,

/** Checks if the advertiser has completed proof of address verification */
isAddressVerified: isAdvertiser ? data?.full_verification : authenticationStatus?.document?.status,
isAddressVerified: isAdvertiser
? data?.full_verification
: authenticationStatus?.document?.status === 'verified',

/** Checks if the user is already an advertiser */
isAdvertiser,
Expand All @@ -68,7 +70,9 @@ const useAdvertiserStats = (advertiserId?: string) => {
isEligibleForLimitUpgrade: Boolean(data?.upgradable_daily_limits),

/** Checks if the advertiser has completed proof of identity verification */
isIdentityVerified: isAdvertiser ? data?.full_verification : authenticationStatus?.identity?.status,
isIdentityVerified: isAdvertiser
? data?.full_verification
: authenticationStatus?.identity?.status === 'verified',

/** The percentage of completed orders out of total orders as a seller within the past 30 days. */
sellCompletionRate: data?.sell_completion_rate || 0,
Expand Down