forked from deriv-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.
Merge pull request #76 from akmal-deriv/f-apply-new-api
Akmal / feat: add useContractsForCompanyHook
- Loading branch information
Showing
7 changed files
with
234 additions
and
9 deletions.
There are no files selected for viewing
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
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
100 changes: 100 additions & 0 deletions
100
packages/trader/src/AppV2/Hooks/__tests__/useContractsForCompany.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,100 @@ | ||
import React from 'react'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { mockStore } from '@deriv/stores'; | ||
import TraderProviders from '../../../trader-providers'; | ||
import { WS, getContractCategoriesConfig, getContractTypesConfig } from '@deriv/shared'; | ||
import useContractsForCompany from '../useContractsForCompany'; | ||
import { waitFor } from '@testing-library/react'; | ||
|
||
jest.mock('@deriv/shared', () => ({ | ||
...jest.requireActual('@deriv/shared'), | ||
getContractCategoriesConfig: jest.fn(), | ||
getContractTypesConfig: jest.fn(), | ||
WS: { | ||
contractsForCompany: jest.fn(), | ||
}, | ||
})); | ||
|
||
describe('useContractsForCompany', () => { | ||
let mocked_store: ReturnType<typeof mockStore>; | ||
|
||
const wrapper = ({ children }: { children: JSX.Element }) => ( | ||
<TraderProviders store={mocked_store}>{children}</TraderProviders> | ||
); | ||
|
||
beforeEach(() => { | ||
mocked_store = { | ||
...mockStore({}), | ||
client: { | ||
...mockStore({}).client, | ||
landing_company_shortcode: 'maltainvest', | ||
}, | ||
modules: { | ||
trade: { | ||
setContractTypesListV2: jest.fn(), | ||
}, | ||
}, | ||
}; | ||
|
||
(getContractCategoriesConfig as jest.Mock).mockReturnValue({ | ||
category_1: { categories: ['type_1'] }, | ||
category_2: { categories: ['type_2'] }, | ||
}); | ||
|
||
(getContractTypesConfig as jest.Mock).mockReturnValue({ | ||
type_1: { trade_types: ['type_1'], title: 'Type 1', barrier_count: 0 }, | ||
type_2: { trade_types: ['type_2'], title: 'Type 2', barrier_count: 1 }, | ||
}); | ||
|
||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should fetch and set contract types for the company successfully', async () => { | ||
WS.contractsForCompany.mockResolvedValue({ | ||
contracts_for_company: { | ||
available: [{ contract_type: 'type_1' }, { contract_type: 'type_2' }], | ||
}, | ||
}); | ||
|
||
const { result } = renderHook(() => useContractsForCompany(), { wrapper }); | ||
|
||
await waitFor(() => { | ||
expect(result.current.contract_types_list).toEqual({ | ||
category_1: { categories: [{ value: 'type_1', text: 'Type 1' }] }, | ||
category_2: { categories: [{ value: 'type_2', text: 'Type 2' }] }, | ||
}); | ||
expect(mocked_store.modules.trade.setContractTypesListV2).toHaveBeenCalledWith({ | ||
category_1: { categories: [{ value: 'type_1', text: 'Type 1' }] }, | ||
category_2: { categories: [{ value: 'type_2', text: 'Type 2' }] }, | ||
}); | ||
}); | ||
}); | ||
|
||
it('should handle API errors gracefully', async () => { | ||
WS.contractsForCompany.mockResolvedValue({ | ||
error: { message: 'Some error' }, | ||
}); | ||
|
||
const { result } = renderHook(() => useContractsForCompany(), { wrapper }); | ||
|
||
await waitFor(() => { | ||
expect(result.current.contract_types_list).toEqual([]); | ||
expect(mocked_store.modules.trade.setContractTypesListV2).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('should not set unsupported contract types', async () => { | ||
WS.contractsForCompany.mockResolvedValue({ | ||
contracts_for_company: { | ||
available: [{ contract_type: 'unsupported_type' }], | ||
}, | ||
}); | ||
|
||
const { result } = renderHook(() => useContractsForCompany(), { wrapper }); | ||
|
||
await waitFor(() => { | ||
expect(result.current.contract_types_list).toEqual([]); | ||
expect(mocked_store.modules.trade.setContractTypesListV2).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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,87 @@ | ||
import React from 'react'; | ||
import { useTraderStore } from 'Stores/useTraderStores'; | ||
import { useStore } from '@deriv/stores'; | ||
import { cloneObject, getContractCategoriesConfig, getContractTypesConfig, WS } from '@deriv/shared'; | ||
|
||
type TContractType = { | ||
text?: string; | ||
value: string; | ||
}; | ||
|
||
export type TContractTypesList = { | ||
[key: string]: { | ||
name: string; | ||
categories: DeepRequired<TContractType[]>; | ||
}; | ||
}; | ||
|
||
const useContractsForCompany = () => { | ||
const [contract_types_list, setContractTypesList] = React.useState<TContractTypesList | []>([]); | ||
const { setContractTypesListV2 } = useTraderStore(); | ||
const { client } = useStore(); | ||
const { landing_company_shortcode } = client; | ||
const contract_categories = getContractCategoriesConfig(); | ||
const available_categories = cloneObject(contract_categories); | ||
const contract_types = getContractTypesConfig(); | ||
let available_contract_types: ReturnType<typeof getContractTypesConfig> = {}; | ||
|
||
const fetchContractForCompany = React.useCallback(async () => { | ||
let response; | ||
|
||
const request = { | ||
landing_company: landing_company_shortcode, | ||
}; | ||
|
||
try { | ||
response = await WS.contractsForCompany(request); | ||
const { contracts_for_company = [], error } = response; | ||
available_contract_types = {}; | ||
|
||
if (error) { | ||
console.error(error); | ||
} else if (contracts_for_company?.available.length) { | ||
contracts_for_company.available.forEach((contract: any) => { | ||
const type = Object.keys(contract_types).find( | ||
key => | ||
contract_types[key].trade_types.indexOf(contract.contract_type) !== -1 && | ||
(contract.contract_type !== 'PUT' || contract_types[key].barrier_count === 1) // To distinguish betweeen Rise/Fall & Higher/Lower | ||
); | ||
|
||
if (!type) return; // ignore unsupported contract types | ||
|
||
if (!available_contract_types[type]) { | ||
// extend contract_categories to include what is needed to create the contract list | ||
const sub_cats = | ||
available_categories[ | ||
Object.keys(available_categories).find( | ||
key => available_categories[key].categories.indexOf(type) !== -1 | ||
) ?? '' | ||
].categories; | ||
|
||
if (!sub_cats) return; | ||
|
||
sub_cats[(sub_cats as string[]).indexOf(type)] = { | ||
value: type, | ||
text: contract_types[type].title, | ||
}; | ||
|
||
available_contract_types[type] = cloneObject(contract_types[type]); | ||
} | ||
}); | ||
|
||
setContractTypesListV2(available_categories); | ||
setContractTypesList(available_categories); | ||
} | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}, [setContractTypesListV2]); | ||
|
||
React.useEffect(() => { | ||
fetchContractForCompany(); | ||
}, [fetchContractForCompany]); | ||
|
||
return { contract_types_list }; | ||
}; | ||
|
||
export default useContractsForCompany; |
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