Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Niloofar/ Convert constants, contract, currency to TS [shared-utiles] #11

Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
27b51d5
refactor: convert constants to TS
Sep 13, 2022
530925e
refactor: convert contract utils to TS
Sep 14, 2022
306e16c
Merge branch 'master' of github.com:binary-com/deriv-app into niloofa…
Sep 14, 2022
6ba3004
fix: symbol type
Sep 14, 2022
263609f
Merge branch 'master' of github.com:binary-com/deriv-app into niloofa…
Sep 14, 2022
c23281d
refactor: convert currency to TS
Sep 14, 2022
7a46c27
Merge branch 'master' of github.com:binary-com/deriv-app into niloofa…
Sep 14, 2022
8d8e468
fix: some types
Sep 14, 2022
c11d117
Merge branch 'master' of github.com:binary-com/deriv-app into niloofa…
Sep 15, 2022
c285695
fix: review comments
Sep 15, 2022
d469388
fix: review comments
Sep 15, 2022
252c9db
fix: review comments
Sep 15, 2022
0e84270
Merge branch 'master' of github.com:binary-com/deriv-app into niloofa…
Sep 15, 2022
769daeb
Merge branch 'master' of github.com:binary-com/deriv-app into niloofa…
Sep 18, 2022
83c319a
fix: review comment
Sep 18, 2022
fb1e71e
fix: review comment
Sep 18, 2022
e2e5037
fix: review comments
Sep 19, 2022
36f245a
Merge branch 'master' of github.com:binary-com/deriv-app into niloofa…
Sep 22, 2022
8376e3d
Merge branch 'components-shared-ts-migration-parent' of github.com:ji…
Sep 29, 2022
621b0de
fix: review comments
Sep 29, 2022
c7ffc94
fix: remove extra packages from shared
Sep 29, 2022
fa69baa
fix: review comment
Oct 2, 2022
f37613e
fix: review comments
Oct 2, 2022
f58cfa9
Merge branch 'components-shared-ts-migration-parent' of github.com:ji…
Oct 3, 2022
13187de
fix: circleci issue
Oct 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ export const getLocalizedBasis = () => ({
* components can be undef or an array containing any of: 'start_date', 'barrier', 'last_digit'
* ['duration', 'amount'] are omitted, as they're available in all contract types
*/
export const getContractTypesConfig = symbol => ({
type TContractTypesConfig = {
title: string;
trade_types: string[];
basis: string[];
components: string[];
barrier_count?: number;
config?: { hide_duration: boolean };
};

type TGetContractTypesConfig = (symbol: string) => Record<string, TContractTypesConfig>;

export const getContractTypesConfig: TGetContractTypesConfig = symbol => ({
rise_fall: {
title: localize('Rise/Fall'),
trade_types: ['CALL', 'PUT'],
Expand Down Expand Up @@ -340,7 +351,8 @@ export const getUnsupportedContracts = () => ({
},
});

export const getSupportedContracts = is_high_low => ({
type TGetSupportedContracts = keyof ReturnType<typeof getSupportedContracts>;
export const getSupportedContracts = (is_high_low?: boolean) => ({
CALL: {
name: is_high_low ? <Localize i18n_default_text='Higher' /> : <Localize i18n_default_text='Rise' />,
position: 'top',
Expand Down Expand Up @@ -399,17 +411,17 @@ export const getSupportedContracts = is_high_low => ({
},
});

export const getContractConfig = is_high_low => ({
export const getContractConfig = (is_high_low: boolean) => ({
...getSupportedContracts(is_high_low),
...getUnsupportedContracts(),
});

export const getContractTypeDisplay = (type, is_high_low = false) => {
return getContractConfig(is_high_low)[type] ? getContractConfig(is_high_low)[type.toUpperCase()].name : '';
export const getContractTypeDisplay = (type: TGetSupportedContracts, is_high_low = false) => {
niloofar-deriv marked this conversation as resolved.
Show resolved Hide resolved
return getContractConfig(is_high_low)[type].name || '';
niloofar-deriv marked this conversation as resolved.
Show resolved Hide resolved
};

export const getContractTypePosition = (type, is_high_low = false) =>
getContractConfig(is_high_low)[type] ? getContractConfig(is_high_low)[type.toUpperCase()].position : 'top';
export const getContractTypePosition = (type: TGetSupportedContracts, is_high_low = false) =>
getContractConfig(is_high_low)[type].position || 'top';

export const isCallPut = trade_type =>
export const isCallPut = (trade_type: 'rise_fall' | 'rise_fall_equal' | 'high_low'): boolean =>
trade_type === 'rise_fall' || trade_type === 'rise_fall_equal' || trade_type === 'high_low';
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { expect } from 'chai';
import * as ContractUtils from '../contract';
import {
TContractInfo,
TDigitsInfo,
TTickItem,
TGetFinalPrice,
TIsEnded,
TGetDisplayStatus,
TIsValidToSell,
} from '../contract-types';

describe('getFinalPrice', () => {
it("should return sell_price as final price when it's available", () => {
const contract_info = {
sell_price: 12345,
bid_price: 0,
};
expect(ContractUtils.getFinalPrice(contract_info)).to.eql(12345);
});
Expand All @@ -17,92 +27,97 @@ describe('getFinalPrice', () => {
});
it('should return bid_price as final price when sell_price is not available and bid_price is available', () => {
const contract_info = {
sell_price: 0,
bid_price: 789,
};
expect(ContractUtils.getFinalPrice(contract_info)).to.eql(789);
});
it('should return 0 as final price when sell_price and bid_price are empty', () => {
const contract_info = {
sell_price: false,
bid_price: false,
sell_price: 0,
bid_price: 0,
};
expect(ContractUtils.getFinalPrice(contract_info)).to.eql(0);
});
});

describe('getIndicativePrice', () => {
it('should return getFinalPrice if it has final price and contract is ended', () => {
const contract_info = {
const contract_info: TGetFinalPrice & TIsEnded = {
sell_price: 12345,
bid_price: 0,
status: 'sold',
};
expect(ContractUtils.getIndicativePrice(contract_info)).to.eql(12345);
});
it("should return null if it doesn't have final price, bid_price and contract is not ended", () => {
const contract_info = {
const contract_info: TGetFinalPrice & TIsEnded = {
status: 'open',
sell_price: 0,
bid_price: 0,
};
expect(ContractUtils.getIndicativePrice(contract_info)).to.eql(null);
});
it("should return bid_price if it doesn't have final price, has bid_price and contract is not ended", () => {
const contract_info = {
const contract_info: TGetFinalPrice & TIsEnded = {
status: 'open',
bid_price: 12345,
sell_price: 0,
};
expect(ContractUtils.getIndicativePrice(contract_info)).to.eql(12345);
});
});

describe('isEnded', () => {
it("should return false when there is status and it's equal to open in contract info", () => {
const contract_info = {
const contract_info: TContractInfo = {
status: 'open',
};
expect(ContractUtils.isEnded(contract_info)).to.eql(false);
});
it("should return true when there is status and it's not equal to open in contract info", () => {
const contract_info = {
const contract_info: TContractInfo = {
status: 'sold',
};
expect(ContractUtils.isEnded(contract_info)).to.eql(true);
});
it('should return true when contract is expired', () => {
const contract_info = {
const contract_info: TContractInfo = {
status: 'open',
is_expired: true,
is_expired: 1,
};
expect(ContractUtils.isEnded(contract_info)).to.eql(true);
});
it('should return true when contract is settleable', () => {
const contract_info = {
const contract_info: TContractInfo = {
status: 'open',
is_expired: false,
is_settleable: true,
is_expired: 0,
is_settleable: 1,
};
expect(ContractUtils.isEnded(contract_info)).to.eql(true);
});
it('should return true when contract is not expired', () => {
const contract_info = {
const contract_info: TContractInfo = {
status: 'open',
is_expired: false,
is_expired: 0,
};
expect(ContractUtils.isEnded(contract_info)).to.eql(false);
});
it('should return true when contract does not have is_settleable, is_expired and status', () => {
const contract_info = {};
const contract_info: TContractInfo = {};
expect(ContractUtils.isEnded(contract_info)).to.eql(false);
});
});

describe('isUserSold', () => {
it("should return true if contract's status is sold", () => {
const contract_info = {
const contract_info: TContractInfo = {
status: 'sold',
};
expect(ContractUtils.isUserSold(contract_info)).to.eql(true);
});
it("should return false if contract's status is not sold", () => {
const contract_info = {
const contract_info: TContractInfo = {
status: 'open',
};
expect(ContractUtils.isUserSold(contract_info)).to.eql(false);
Expand All @@ -111,42 +126,42 @@ describe('isUserSold', () => {

describe('isValidToSell', () => {
it('should return true if contract is not ended and is not sold and contract is valid to_sell', () => {
const contract_info = {
const contract_info: TIsValidToSell = {
status: 'open',
is_valid_to_sell: 1,
};
expect(ContractUtils.isValidToSell(contract_info)).to.eql(true);
});
it('should return false if contract is ended and is sold and contract is valid to sell', () => {
const contract_info = {
const contract_info: TIsValidToSell = {
status: 'sold',
is_valid_to_sell: 1,
};
expect(ContractUtils.isValidToSell(contract_info)).to.eql(false);
});
it('should return false if contract is ended and is not sold and contract is valid to sell', () => {
const contract_info = {
const contract_info: TIsValidToSell = {
status: 'won',
is_valid_to_sell: 1,
};
expect(ContractUtils.isValidToSell(contract_info)).to.eql(false);
});
it('should return false if contract is ended and is sold and contract is not valid to sell', () => {
const contract_info = {
const contract_info: TIsValidToSell = {
status: 'sold',
is_valid_to_sell: 0,
};
expect(ContractUtils.isValidToSell(contract_info)).to.eql(false);
});
it('should return false if contract is ended and is not sold and contract is not valid to sell', () => {
const contract_info = {
const contract_info: TIsValidToSell = {
status: 'won',
is_valid_to_sell: 0,
};
expect(ContractUtils.isValidToSell(contract_info)).to.eql(false);
});
it('should return false if contract is not ended and is not sold and contract is not valid to sell', () => {
const contract_info = {
const contract_info: TIsValidToSell = {
status: 'open',
is_valid_to_sell: 0,
};
Expand All @@ -156,7 +171,7 @@ describe('isValidToSell', () => {

describe('getLastTickFromTickStream', () => {
it('should return the last tick in the tick_stream array', () => {
const tick_stream = [
const tick_stream: TTickItem[] = [
{
tick: 766.53,
epoch: 1000001,
Expand All @@ -172,7 +187,7 @@ describe('getLastTickFromTickStream', () => {
});
});
it('should return an empty object if the tick_stream array is empty', () => {
const tick_stream = [];
const tick_stream: TTickItem[] = [];
expect(ContractUtils.getLastTickFromTickStream(tick_stream)).to.eql({});
});
});
Expand All @@ -189,18 +204,18 @@ describe('isDigitContract', () => {

describe('getDigitInfo', () => {
it('should return an empty object when tick_stream is not in contract_info', () => {
const contract_info = {};
expect(ContractUtils.getDigitInfo({}, contract_info)).to.deep.eql({});
const contract_info: TContractInfo = {};
expect(ContractUtils.getDigitInfo({}, contract_info)).to.deep.eq({});
});
it('should return an empty object if tick_stream data is already in digits_info', () => {
const contract_info = {
const contract_info: TContractInfo = {
entry_tick_time: 1544707342,
entry_tick: 123.99,
current_spot_time: 10000000,
current_spot: 456.99,
exit_tick_time: 10000001,
contract_type: 'DIGITMATCH',
barrier: 9,
barrier: '9',
tick_stream: [
{
tick: 123.456,
Expand All @@ -209,16 +224,16 @@ describe('getDigitInfo', () => {
},
],
};
const digits_info = {
const digits_info: TDigitsInfo = {
1544707344: {
digit: 6,
spot: '123.456',
},
};
expect(ContractUtils.getDigitInfo(digits_info, contract_info)).to.deep.eql({});
expect(ContractUtils.getDigitInfo(digits_info, contract_info)).to.deep.eq({});
});
it('should return a digits_info object with the latest tick_stream array data', () => {
const contract_info = {
const contract_info: TContractInfo = {
tick_stream: [
{
tick: 123.456,
Expand All @@ -232,41 +247,46 @@ describe('getDigitInfo', () => {
},
],
};
const digits_info = {
const digits_info: TDigitsInfo = {
1544707346: {
digit: 3,
spot: '456.993',
},
};
expect(ContractUtils.getDigitInfo({}, contract_info)).to.deep.eql(digits_info);
expect(ContractUtils.getDigitInfo({}, contract_info)).to.deep.eq(digits_info);
});
});

expect('getDisplayStatus', () => {
describe('getDisplayStatus', () => {
it('should return won if contract is ended and profit is more than zero', () => {
const contract_info = {
const contract_info: TGetDisplayStatus = {
status: 'sold',
profit: 100,
buy_price: 0,
bid_price: 100,
};
expect(ContractUtils.getDisplayStatus(contract_info)).to.eql('won');
});
it('should return lost if contract is ended and profit is less than zero', () => {
const contract_info = {
const contract_info: TGetDisplayStatus = {
status: 'sold',
profit: -100,
buy_price: 100,
bid_price: 0,
};
expect(ContractUtils.getDisplayStatus(contract_info)).to.eql('loss');
});
it('should return won if contract is ended and profit is zero', () => {
const contract_info = {
const contract_info: TGetDisplayStatus = {
status: 'sold',
profit: 0,
buy_price: 100,
bid_price: 100,
};
expect(ContractUtils.getDisplayStatus(contract_info)).to.eql('won');
});
it('should return purchased if contract is not ended', () => {
const contract_info = {
const contract_info: TGetDisplayStatus = {
status: 'open',
buy_price: 0,
bid_price: 100,
};
expect(ContractUtils.getDisplayStatus(contract_info)).to.eql('purchased');
});
Expand Down
Loading