Skip to content

Commit

Permalink
feat: sync with api (binary-com#13754)
Browse files Browse the repository at this point in the history
Co-authored-by: Wojciech Brygola <wojciech@regentmarkets.com>
  • Loading branch information
wojciech-deriv and wojciech-deriv committed Feb 26, 2024
1 parent dec0ddd commit 5905a61
Show file tree
Hide file tree
Showing 15 changed files with 332 additions and 67 deletions.
56 changes: 56 additions & 0 deletions packages/api-v2/src/hooks/__tests__/useCountryList.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { renderHook } from '@testing-library/react-hooks';
import useQuery from '../../useQuery';
import useClientCountry from '../useClientCountry';

jest.mock('../../useQuery');

const mockUseQuery = useQuery as jest.MockedFunction<typeof useQuery<'website_status'>>;

describe('useClientCountry', () => {
it('should return an undefined', () => {
// @ts-expect-error need to come up with a way to mock the return type of useFetch
mockUseQuery.mockReturnValue({
data: {
website_status: undefined,
},
});
const { result } = renderHook(() => useClientCountry());

expect(result.current.data).toBeUndefined();
});

it('should return Indonesia country code', () => {
// @ts-expect-error need to come up with a way to mock the return type of useFetch
mockUseQuery.mockReturnValue({
data: {
website_status: {
api_call_limits: {
max_proposal_subscription: {
applies_to: '',
max: 0,
},
max_requestes_general: {
applies_to: '',
hourly: 0,
minutely: 0,
},
max_requests_outcome: {
applies_to: '',
hourly: 0,
minutely: 0,
},
max_requests_pricing: {
applies_to: '',
hourly: 0,
minutely: 0,
},
},
currencies_config: {},
clients_country: 'id',
},
},
});
const { result } = renderHook(() => useClientCountry());
expect(result.current.data).toBe('id');
});
});
2 changes: 2 additions & 0 deletions packages/api-v2/src/hooks/__tests__/useKycAuthStatus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import useQuery from '../../useQuery';

jest.mock('../../useQuery');

jest.mock('../useAuthorize', () => jest.fn(() => ({ isSuccess: true })));

describe('useKycAuthStatus', () => {
afterEach(() => {
jest.clearAllMocks();
Expand Down
129 changes: 129 additions & 0 deletions packages/api-v2/src/hooks/__tests__/useOnfido.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { act, renderHook } from '@testing-library/react-hooks';
import useOnfido from '../useOnfido';
import { useSettings, useResidenceList, useOnfidoServiceToken, useOnfidoNotificationEvent } from '..';
import { fireEvent } from '@testing-library/react';

jest.mock('../useSettings');
jest.mock('../useResidenceList');
jest.mock('../useOnfidoServiceToken');
jest.mock('../useOnfidoNotificationEvent');

const mockSettings = { country_code: 'US' };
const mockResidenceList = [
{
value: 'US',
identity: {
services: {
onfido: {
is_country_supported: true,
documents_supported: {
passport: { display_name: 'Passport' },
driving_licence: { display_name: 'Driving Licence' },
},
},
},
},
},
];

const mockServiceToken = { token: 'mock_token' };

describe('useOnfido', () => {
it('should return the necessary data', async () => {
const mockSubmitDocuments = jest.fn();

(useSettings as jest.Mock).mockReturnValue({ data: mockSettings });
(useResidenceList as jest.Mock).mockReturnValue({ data: mockResidenceList });
(useOnfidoServiceToken as jest.Mock).mockReturnValue({ data: mockServiceToken, isLoading: false, error: null });
(useOnfidoNotificationEvent as jest.Mock).mockReturnValue({ mutate: mockSubmitDocuments });

const { result, waitForNextUpdate } = renderHook(() => useOnfido('US'));

// eslint-disable-next-line testing-library/no-node-access
const scriptNode = document.getElementById('onfido_sdk');
expect(scriptNode).not.toBeNull();

// Manually trigger the load event
act(() => {
fireEvent.load(scriptNode as HTMLElement);
});

await waitForNextUpdate();

// Assert that the necessary data is returned
expect(result.current.isOnfidoInitialized).toBe(true);
expect(result.current.isServiceTokenLoading).toBe(false);
expect(result.current.serviceTokenError).toBeNull();
expect(result.current.onfidoInitializationError).toBeNull();
expect(result.current.data.onfidoRef.current).not.toBeNull();
expect(result.current.data.onfidoContainerId).toBeDefined();
expect(result.current.data.hasSubmitted).toBe(false);

// Assert that the necessary functions are called
expect(mockSubmitDocuments).not.toHaveBeenCalled(); // submitDocuments should not be called yet
});

it('should initialize Onfido with correct values', async () => {
const mockSubmitDocuments = jest.fn();

(useSettings as jest.Mock).mockReturnValue({ data: mockSettings });
(useResidenceList as jest.Mock).mockReturnValue({ data: mockResidenceList });
(useOnfidoServiceToken as jest.Mock).mockReturnValue({ data: mockServiceToken, isLoading: false, error: null });
(useOnfidoNotificationEvent as jest.Mock).mockReturnValue({ mutate: mockSubmitDocuments });

const mockOnfidoInit = jest.fn();
window.Onfido.init = mockOnfidoInit;

const { waitForNextUpdate } = renderHook(() => useOnfido());

// eslint-disable-next-line testing-library/no-node-access
const scriptNode = document.getElementById('onfido_sdk');
expect(scriptNode).not.toBeNull();

// Manually trigger the load event
act(() => {
fireEvent.load(scriptNode as HTMLElement);
});

await waitForNextUpdate();

expect(mockOnfidoInit).toHaveBeenLastCalledWith(
expect.objectContaining({
token: 'mock_token',
useModal: false,
useMemoryHistory: true,
steps: [
{
type: 'document',
options: {
documentTypes: {
passport: true,
driving_licence: { country: 'USA' },
national_identity_card: false,
},
hideCountrySelection: true,
},
},
'face',
],
})
);
});

it('should return error if Onfido initialization throws error', async () => {
(useSettings as jest.Mock).mockReturnValue({ data: mockSettings });
(useResidenceList as jest.Mock).mockReturnValue({ data: mockResidenceList });
(useOnfidoServiceToken as jest.Mock).mockReturnValue({ data: mockServiceToken, isLoading: false, error: null });
(useOnfidoNotificationEvent as jest.Mock).mockReturnValue({ mutate: jest.fn() });

const onfidoError = new Error('Failed to initialize Onfido');
const mockOnfidoInit = jest.fn(() => {
throw onfidoError;
});
window.Onfido.init = mockOnfidoInit;

const { result } = renderHook(() => useOnfido('US'));

expect(result.current.onfidoInitializationError).toEqual(onfidoError);
});
});
1 change: 1 addition & 0 deletions packages/api-v2/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ export { default as useResetVirtualBalance } from './useResetVirtualBalance';
export { default as useExchangeRates } from './useExchangeRates';
export { default as useIsDIELEnabled } from './useIsDIELEnabled';
export { default as useKycAuthStatus } from './useKycAuthStatus';
export { default as useClientCountry } from './useClientCountry';
20 changes: 20 additions & 0 deletions packages/api-v2/src/hooks/useClientCountry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useMemo } from 'react';
import useQuery from '../useQuery';

/** A custom hook that gets the client country. */
const useClientCountry = () => {
const { data, ...website_status_rest } = useQuery('website_status');

/** Modify the client country. */
const modified_client_country = useMemo(() => {
return data?.website_status?.clients_country;
}, [data]);

return {
/** The client's country */
data: modified_client_country,
...website_status_rest,
};
};

export default useClientCountry;
8 changes: 5 additions & 3 deletions packages/api-v2/src/hooks/useGetAccountStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ const useGetAccountStatus = () => {
const modified_account_status = useMemo(() => {
if (!get_account_status_data?.get_account_status) return;

const { prompt_client_to_authenticate, p2p_status } = get_account_status_data.get_account_status;

return {
...get_account_status_data.get_account_status,
/** Indicates whether the client should be prompted to authenticate their account. */
should_prompt_client_to_authenticate: Boolean(
get_account_status_data.get_account_status.prompt_client_to_authenticate
),
should_prompt_client_to_authenticate: Boolean(prompt_client_to_authenticate),
/** Indicates whether the client is a P2P user. */
is_p2p_user: Boolean(p2p_status !== 'none' && p2p_status !== 'perm_ban'),
};
}, [get_account_status_data?.get_account_status]);

Expand Down
4 changes: 2 additions & 2 deletions packages/api-v2/src/hooks/useJurisdictionStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const useJurisdictionStatus = () => {
}
};

const pendingStatuses = ['poa_pending', 'verification_pending'];
const isStatusPending = pendingStatuses.includes(mt5_account_status ?? '');
const pendingStatus = ['verification_pending'];
const isStatusPending = pendingStatus.includes(mt5_account_status ?? '');

const status = {
is_failed: false,
Expand Down
4 changes: 3 additions & 1 deletion packages/api-v2/src/hooks/useKycAuthStatus.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { TSocketRequestPayload } from '../../types';
import useQuery from '../useQuery';
import useAuthorize from './useAuthorize';

type TKycAuthStatusPayload = TSocketRequestPayload<'kyc_auth_status'>['payload'];

/** Custom hook that returns Proof of Identity (POI) and Proof of Address (POA) authentication status details. */
const useKycAuthStatus = (payload: TKycAuthStatusPayload) => {
const { isSuccess } = useAuthorize();
const { data, ...kyc_auth_status_rest } = useQuery('kyc_auth_status', {
payload,
options: { enabled: isSuccess },
});

return {
/** The KYC auth status */
kyc_auth_status: data?.kyc_auth_status,
Expand Down
Loading

0 comments on commit 5905a61

Please sign in to comment.