Skip to content

Commit

Permalink
Merge pull request #100 from thisyahlen-deriv/thisyahlen/exchange-rates
Browse files Browse the repository at this point in the history
chore: add exchange rates to total assets
  • Loading branch information
thisyahlen-deriv authored Apr 17, 2024
2 parents b8f776e + d4a1712 commit 4be47eb
Show file tree
Hide file tree
Showing 11 changed files with 358 additions and 58 deletions.
102 changes: 98 additions & 4 deletions src/hooks/__tests__/useCtraderAccountsList.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useTradingPlatformAccounts } from '@deriv-com/api-hooks';
import { useSubscribe, useTradingPlatformAccounts } from '@deriv-com/api-hooks';
import { renderHook } from '@testing-library/react';

import { useCtraderAccountsList } from '../useCtraderAccountsList';

jest.mock('@deriv-com/api-hooks', () => ({
useTradingPlatformAccounts: jest.fn(),
useSubscribe: jest.fn(),
}));

jest.mock('@deriv-com/utils', () => ({
Expand All @@ -24,17 +25,67 @@ describe('useCtraderAccountsList hook', () => {
];
(useTradingPlatformAccounts as jest.Mock).mockReturnValue({ data: mockData, rest: {} });

(useSubscribe as jest.Mock).mockImplementation((_, cb) => {
if (typeof cb === 'function') {
cb({
data: {
trading_platform_accounts: [
{
login: 'MT5ID12345',
account_type: 'demo',
balance: 1000,
currency: 'USD',
},
],
},
});
}
return { data: {} };
});

const { result } = renderHook(() => useCtraderAccountsList());

expect(result.current.data).toEqual([
{ ...mockData[0], platform: 'ctrader', is_virtual: true, loginid: '1', display_balance: '1000 USD USD' },
{ ...mockData[1], platform: 'ctrader', is_virtual: false, loginid: '2', display_balance: '2000 EUR EUR' },
{
...mockData[0],
platform: 'ctrader',
is_virtual: true,
loginid: '1',
display_balance: '1000 USD USD',
convertedBalance: 1000,
},
{
...mockData[1],
platform: 'ctrader',
is_virtual: false,
loginid: '2',
display_balance: '2000 EUR EUR',
convertedBalance: 2000,
},
]);
});

it('should return undefined when there is no data', () => {
(useTradingPlatformAccounts as jest.Mock).mockReturnValue({ data: undefined, rest: {} });

(useSubscribe as jest.Mock).mockImplementation((_, cb) => {
if (typeof cb === 'function') {
cb({
data: {
trading_platform_accounts: [
{
login: 'MT5ID12345',
account_type: 'demo',
balance: 1000,
currency: 'USD',
},
],
},
});
}
return { data: {} };
});

const { result } = renderHook(() => useCtraderAccountsList());

expect(result.current.data).toBeUndefined();
Expand All @@ -43,6 +94,24 @@ describe('useCtraderAccountsList hook', () => {
it('should return isPending as true when fetching data', () => {
(useTradingPlatformAccounts as jest.Mock).mockReturnValue({ data: undefined, isPending: true, rest: {} });

(useSubscribe as jest.Mock).mockImplementation((_, cb) => {
if (typeof cb === 'function') {
cb({
data: {
trading_platform_accounts: [
{
login: 'MT5ID12345',
account_type: 'demo',
balance: 1000,
currency: 'USD',
},
],
},
});
}
return { data: {} };
});

const { result } = renderHook(() => useCtraderAccountsList());

expect(result.current.isPending).toBeTruthy();
Expand All @@ -53,10 +122,35 @@ describe('useCtraderAccountsList hook', () => {
const mockData = [{ account_id: '1', account_type: 'demo', currency: 'USD', platform: 'ctrader' }];
(useTradingPlatformAccounts as jest.Mock).mockReturnValue({ data: mockData, rest: {} });

(useSubscribe as jest.Mock).mockImplementation((_, cb) => {
if (typeof cb === 'function') {
cb({
data: {
trading_platform_accounts: [
{
login: 'MT5ID12345',
account_type: 'demo',
balance: 1000,
currency: 'USD',
},
],
},
});
}
return { data: {} };
});

const { result } = renderHook(() => useCtraderAccountsList());

expect(result.current.data).toEqual([
{ ...mockData[0], platform: 'ctrader', is_virtual: true, loginid: '1', display_balance: '0 USD USD' },
{
...mockData[0],
platform: 'ctrader',
is_virtual: true,
loginid: '1',
display_balance: '0 USD USD',
convertedBalance: 0,
},
]);
});
});
51 changes: 43 additions & 8 deletions src/hooks/__tests__/useDxtradeAccountsList.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { useTradingPlatformAccounts } from '@deriv-com/api-hooks';
import { useSubscribe, useTradingPlatformAccounts } from '@deriv-com/api-hooks';
import { CurrencyConstants, FormatUtils } from '@deriv-com/utils';
import { renderHook } from '@testing-library/react';

import { useDxtradeAccountsList } from '../useDxtradeAccountsList';

jest.mock('@deriv-com/api-hooks');
jest.mock('@deriv-com/api-hooks', () => ({
useTradingPlatformAccounts: jest.fn(),
useSubscribe: jest.fn(),
}));

describe('useDxtradeAccountsList', () => {
it('returns modified accounts data', () => {
Expand All @@ -15,18 +18,30 @@ describe('useDxtradeAccountsList', () => {
currency: 'USD',
platform: 'dxtrade',
},
{
account_type: 'real',
balance: 2000,
currency: 'EUR',
platform: 'dxtrade',
},
];

(useTradingPlatformAccounts as jest.Mock).mockReturnValue({
data: mockData,
});

(useSubscribe as jest.Mock).mockImplementation((_, cb) => {
if (typeof cb === 'function') {
cb({
data: {
trading_platform_accounts: [
{
login: 'MT5ID12345',
account_type: 'demo',
balance: 1000,
currency: 'USD',
},
],
},
});
}
return { data: {} };
});

const { result } = renderHook(() => useDxtradeAccountsList());

const expectedData = mockData.map(account => ({
Expand All @@ -36,6 +51,7 @@ describe('useDxtradeAccountsList', () => {
display_balance: `${FormatUtils.formatMoney(account.balance, {
currency: account.currency as CurrencyConstants.Currency,
})} ${account.currency}`,
convertedBalance: 1000,
}));

expect(result.current.data).toEqual(expectedData);
Expand All @@ -59,13 +75,32 @@ describe('useDxtradeAccountsList', () => {
data: mockData,
});

(useSubscribe as jest.Mock).mockImplementation((_, cb) => {
if (typeof cb === 'function') {
cb({
data: {
trading_platform_accounts: [
{
login: 'MT5ID12345',
account_type: 'demo',
balance: 1000,
currency: 'USD',
},
],
},
});
}
return { data: {} };
});

const { result } = renderHook(() => useDxtradeAccountsList());

const expectedData = mockData.map(account => ({
...account,
platform: 'dxtrade',
is_virtual: account.account_type === 'demo',
display_balance: `0.00 ${account.currency}`,
convertedBalance: 0,
}));

expect(result.current.data).toEqual(expectedData);
Expand Down
60 changes: 59 additions & 1 deletion src/hooks/__tests__/useMT5AccountsList.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMt5LoginList } from '@deriv-com/api-hooks';
import { useMt5LoginList, useSubscribe } from '@deriv-com/api-hooks';
import { CurrencyConstants, FormatUtils } from '@deriv-com/utils';
import { renderHook } from '@testing-library/react';

Expand All @@ -7,6 +7,7 @@ import { useMT5AccountsList } from '../useMT5AccountsList';

jest.mock('@deriv-com/api-hooks', () => ({
useMt5LoginList: jest.fn(),
useSubscribe: jest.fn(),
}));

jest.mock('../useCurrencyConfig', () => ({
Expand Down Expand Up @@ -34,6 +35,24 @@ describe('useMT5AccountsList', () => {
getConfig: () => mockConfig,
});

(useSubscribe as jest.Mock).mockImplementation((_, cb) => {
if (typeof cb === 'function') {
cb({
data: {
mt5_login_list: [
{
login: 'MT5ID12345',
account_type: 'demo',
balance: 1000,
currency: 'USD',
},
],
},
});
}
return { data: {} };
});

const { result } = renderHook(() => useMT5AccountsList());

expect(result.current.data).toEqual([
Expand All @@ -47,6 +66,7 @@ describe('useMT5AccountsList', () => {
display_balance: `${FormatUtils.formatMoney(mockData[0].balance, {
currency: mockData[0].currency as CurrencyConstants.Currency,
})} ${mockData[0].currency}`,
convertedBalance: 1000,
},
]);
});
Expand All @@ -68,6 +88,24 @@ describe('useMT5AccountsList', () => {
getConfig: () => undefined,
});

(useSubscribe as jest.Mock).mockImplementation((_, cb) => {
if (typeof cb === 'function') {
cb({
data: {
mt5_login_list: [
{
login: 'MT5ID12345',
account_type: 'demo',
balance: 1000,
currency: 'USD',
},
],
},
});
}
return { data: {} };
});

const { result } = renderHook(() => useMT5AccountsList());

expect(result.current.data).toEqual([
Expand All @@ -81,6 +119,7 @@ describe('useMT5AccountsList', () => {
display_balance: `${FormatUtils.formatMoney(mockData[0].balance, {
currency: mockData[0].currency as CurrencyConstants.Currency | undefined,
})} ${mockData[0].currency}`,
convertedBalance: 1000,
},
]);
});
Expand All @@ -104,6 +143,24 @@ describe('useMT5AccountsList', () => {
getConfig: () => mockConfig,
});

(useSubscribe as jest.Mock).mockImplementation((_, cb) => {
if (typeof cb === 'function') {
cb({
data: {
mt5_login_list: [
{
login: 'MT5ID12345',
account_type: 'demo',
balance: 1000,
currency: 'USD',
},
],
},
});
}
return { data: {} };
});

const { result } = renderHook(() => useMT5AccountsList());

expect(result.current.data).toEqual([
Expand All @@ -117,6 +174,7 @@ describe('useMT5AccountsList', () => {
display_balance: `${FormatUtils.formatMoney(0, {
currency: mockData[0].currency as CurrencyConstants.Currency,
})} ${mockData[0].currency}`,
convertedBalance: 0,
},
]);
});
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ export { usePlatformAssets } from './usePlatformAssets';
export { useCFDAssets } from './useCFDAssets';
export { useTotalAssets } from './useTotalAssets';
export { useCtraderServiceToken } from './useCtraderServiceToken';
export { useExchangeRates } from './useExchangeRates';
Loading

0 comments on commit 4be47eb

Please sign in to comment.