forked from binary-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sync with api (binary-com#13754)
Co-authored-by: Wojciech Brygola <wojciech@regentmarkets.com>
- Loading branch information
1 parent
dec0ddd
commit 5905a61
Showing
15 changed files
with
332 additions
and
67 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
packages/api-v2/src/hooks/__tests__/useCountryList.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.