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.
test: unit test for useCFDAssets (deriv-com#14285)
- Loading branch information
1 parent
e50404f
commit 0ddf877
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
packages/tradershub/src/hooks/__tests__/useCFDAssets.spec.ts
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,102 @@ | ||
import { useCFDAccountsList } from '@deriv/api-v2'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import useCFDAssets from '../useCFDAssets'; | ||
|
||
jest.mock('@deriv/api-v2', () => ({ | ||
useCFDAccountsList: jest.fn(), | ||
})); | ||
|
||
describe('useCFDAssets', () => { | ||
it('should calculate sample balances correctly', () => { | ||
(useCFDAccountsList as jest.Mock).mockReturnValue({ | ||
data: { | ||
mt5: [ | ||
{ is_virtual: true, converted_balance: 100, landing_company_short: 'maltainvest' }, | ||
{ is_virtual: false, converted_balance: 200, landing_company_short: 'maltainvest' }, | ||
], | ||
dxtrade: [ | ||
{ is_virtual: true, converted_balance: 300 }, | ||
{ is_virtual: false, converted_balance: 400 }, | ||
], | ||
ctrader: [ | ||
{ is_virtual: true, converted_balance: 500 }, | ||
{ is_virtual: false, converted_balance: 600 }, | ||
], | ||
}, | ||
isSuccess: true, | ||
}); | ||
|
||
const { result } = renderHook(() => useCFDAssets('EU')); | ||
|
||
expect(result.current.calculatedDemoBalance).toBe(100); | ||
expect(result.current.calculatedRealBalance).toBe(200); | ||
expect(result.current.isSuccess).toBe(true); | ||
}); | ||
|
||
it('should handle no accounts', () => { | ||
(useCFDAccountsList as jest.Mock).mockReturnValue({ | ||
data: { | ||
mt5: [], | ||
dxtrade: [], | ||
ctrader: [], | ||
}, | ||
isSuccess: true, | ||
}); | ||
|
||
const { result } = renderHook(() => useCFDAssets('EU')); | ||
|
||
expect(result.current.calculatedDemoBalance).toBe(0); | ||
expect(result.current.calculatedRealBalance).toBe(0); | ||
expect(result.current.isSuccess).toBe(true); | ||
}); | ||
|
||
it('should handle API failure', () => { | ||
(useCFDAccountsList as jest.Mock).mockReturnValue({ | ||
data: null, | ||
isSuccess: false, | ||
}); | ||
|
||
const { result } = renderHook(() => useCFDAssets('EU')); | ||
|
||
expect(result.current.calculatedDemoBalance).toBe(0); | ||
expect(result.current.calculatedRealBalance).toBe(0); | ||
expect(result.current.isSuccess).toBe(false); | ||
}); | ||
it('should filter mt5 accounts correctly for EU regulation', () => { | ||
(useCFDAccountsList as jest.Mock).mockReturnValue({ | ||
data: { | ||
mt5: [ | ||
{ is_virtual: true, converted_balance: 100, landing_company_short: 'maltainvest' }, | ||
{ is_virtual: false, converted_balance: 200, landing_company_short: 'maltainvest' }, | ||
{ is_virtual: false, converted_balance: 300, landing_company_short: 'svg' }, | ||
], | ||
dxtrade: [], | ||
ctrader: [], | ||
}, | ||
isSuccess: true, | ||
}); | ||
|
||
const { result } = renderHook(() => useCFDAssets('EU')); | ||
|
||
expect(result.current.calculatedRealBalance).toBe(200); | ||
}); | ||
|
||
it('should filter mt5 accounts correctly for non-EU regulation', () => { | ||
(useCFDAccountsList as jest.Mock).mockReturnValue({ | ||
data: { | ||
mt5: [ | ||
{ is_virtual: true, converted_balance: 100, landing_company_short: 'maltainvest' }, | ||
{ is_virtual: false, converted_balance: 200, landing_company_short: 'maltainvest' }, | ||
{ is_virtual: false, converted_balance: 300, landing_company_short: 'svg' }, | ||
], | ||
dxtrade: [], | ||
ctrader: [], | ||
}, | ||
isSuccess: true, | ||
}); | ||
|
||
const { result } = renderHook(() => useCFDAssets('non-EU')); | ||
|
||
expect(result.current.calculatedRealBalance).toBe(300); | ||
}); | ||
}); |