Skip to content

Commit

Permalink
Kate / Fixes tests cases on master (deriv-com#10812)
Browse files Browse the repository at this point in the history
* fix: failing tests

* fix: types

* remove unnecessary line break packages/trader/src/Stores/Modules/Trading/Constants/__tests__/validation-rules.spec.ts

---------

Co-authored-by: Maryia <103177211+maryia-deriv@users.noreply.github.com>
  • Loading branch information
kate-deriv and maryia-deriv authored Oct 19, 2023
1 parent d4cf332 commit 728ee4f
Showing 1 changed file with 117 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { isTimeValid } from '@deriv/shared';
import { isSessionAvailable } from '../../Helpers/start-date';
import type { TRuleOptions } from 'Utils/Validator/validator';

type TExtendedRuleOptions = TRuleOptions & { condition: (store: TTradeStore) => boolean; message: string };

const mocked_store = {
barrier_count: 2,
contract_start_type: 'test_spot',
Expand All @@ -15,29 +17,24 @@ const mocked_store = {
take_profit: false,
} as unknown as TTradeStore;

const default_mocked_params: [
TTradeStore['barrier_1'],
Partial<TRuleOptions>,
TTradeStore,
Pick<TTradeStore, 'barrier_1' | 'barrier_2'>
] = [
const default_mocked_params: [TTradeStore['barrier_1'], Partial<TRuleOptions>, TTradeStore, TTradeStore] = [
'123',
'test' as unknown as Partial<TRuleOptions>,
mocked_store,
{
barrier_1: '+123',
barrier_2: '-123',
},
} as TTradeStore,
];

const start_time_mocked_params: [TTradeStore['start_time'], Partial<TRuleOptions>, TTradeStore] = [
null,
const start_time_mocked_params: [string, Partial<TRuleOptions>, TTradeStore] = [
null as unknown as string,
'test' as unknown as Partial<TRuleOptions>,
mocked_store,
];

const expiry_time_mocked_params: [TTradeStore['expiry_time'], Partial<TRuleOptions>, TTradeStore] = [
null,
const expiry_time_mocked_params: [string, Partial<TRuleOptions>, TTradeStore] = [
null as unknown as string,
'test' as unknown as Partial<TRuleOptions>,
mocked_store,
];
Expand All @@ -61,14 +58,22 @@ describe('getMultiplierValidationRules', () => {

it('should contain rules for stop_loss', () => {
expect(returned_validation_rules).toHaveProperty('stop_loss');
expect(returned_validation_rules.stop_loss.rules[0][1].message).toBe('Please enter a stop loss amount.');
expect(returned_validation_rules.stop_loss.rules[0][1].condition(mocked_store)).toBe(true);
expect((returned_validation_rules.stop_loss.rules[0][1] as TExtendedRuleOptions).message).toBe(
'Please enter a stop loss amount.'
);
expect((returned_validation_rules.stop_loss.rules[0][1] as TExtendedRuleOptions).condition(mocked_store)).toBe(
true
);
});

it('should contain rules for take_profit', () => {
expect(returned_validation_rules).toHaveProperty('take_profit');
expect(returned_validation_rules.take_profit.rules[0][1].message).toBe('Please enter a take profit amount.');
expect(returned_validation_rules.take_profit.rules[0][1].condition(mocked_store)).toBe(true);
expect((returned_validation_rules.take_profit.rules[0][1] as TExtendedRuleOptions).message).toBe(
'Please enter a take profit amount.'
);
expect(
(returned_validation_rules.take_profit.rules[0][1] as TExtendedRuleOptions).condition(mocked_store)
).toBe(true);
});
});

Expand All @@ -77,27 +82,41 @@ describe('getValidationRules', () => {

it('should contain rules for amount', () => {
expect(returned_validation_rules).toHaveProperty('amount');
expect(returned_validation_rules.amount.rules[0][1].message).toBe('Amount is a required field.');
expect((returned_validation_rules.amount.rules?.[0][1] as TExtendedRuleOptions).message).toBe(
'Amount is a required field.'
);
});

it('should contain rules for barrier_1', () => {
expect(returned_validation_rules).toHaveProperty('barrier_1');
expect(returned_validation_rules.barrier_1.rules[0][1].condition(mocked_store)).toBe(true);
expect(returned_validation_rules.barrier_1.rules[1][1].condition(mocked_store)).toBe(2);
expect(returned_validation_rules.barrier_1.rules[2][1].func(...default_mocked_params)).toBe(true);
expect(
(returned_validation_rules.barrier_1.rules?.[0][1] as TExtendedRuleOptions).condition(mocked_store)
).toBe(true);
expect(
(returned_validation_rules.barrier_1.rules?.[1][1] as TExtendedRuleOptions).condition(mocked_store)
).toBe(true);
expect(
(returned_validation_rules.barrier_1?.rules?.[2][1] as TExtendedRuleOptions).func?.(
...default_mocked_params
)
).toBe(true);

mocked_store.barrier_count = 1;
expect(returned_validation_rules.barrier_1.rules[2][1].func(...default_mocked_params)).toBe(true);
expect(returned_validation_rules.barrier_1.rules[3][1].func(...default_mocked_params)).toBe(true);
expect(
returned_validation_rules.barrier_1.rules[3][1].func(
(returned_validation_rules.barrier_1.rules?.[2][1] as TExtendedRuleOptions).func?.(...default_mocked_params)
).toBe(true);
expect(
(returned_validation_rules.barrier_1.rules?.[3][1] as TExtendedRuleOptions).func?.(...default_mocked_params)
).toBe(true);
expect(
(returned_validation_rules.barrier_1.rules?.[3][1] as TExtendedRuleOptions).func?.(
'123',
'test' as unknown as Partial<TRuleOptions>,
mocked_store,
{
barrier_1: '123',
barrier_2: '123',
}
} as TTradeStore
)
).toBe(true);
});
Expand All @@ -106,15 +125,25 @@ describe('getValidationRules', () => {
mocked_store.barrier_count = 2;

expect(returned_validation_rules).toHaveProperty('barrier_2');
expect(returned_validation_rules.barrier_2.rules[0][1].condition(mocked_store)).toBe(true);
expect(returned_validation_rules.barrier_2.rules[1][1].condition(mocked_store)).toBe(2);
expect(returned_validation_rules.barrier_2.rules[2][1].func(...default_mocked_params)).toBe(false);
expect(returned_validation_rules.barrier_2.rules[3][1].func(...default_mocked_params)).toBe(false);
expect(
(returned_validation_rules.barrier_2.rules?.[0][1] as TExtendedRuleOptions).condition(mocked_store)
).toBe(true);
expect(
(returned_validation_rules.barrier_2.rules?.[1][1] as TExtendedRuleOptions).condition(mocked_store)
).toBe(true);
expect(
(returned_validation_rules.barrier_2.rules?.[2][1] as TExtendedRuleOptions).func?.(...default_mocked_params)
).toBe(false);
expect(
(returned_validation_rules.barrier_2.rules?.[3][1] as TExtendedRuleOptions).func?.(...default_mocked_params)
).toBe(false);
});

it('should contain rules for duration', () => {
expect(returned_validation_rules).toHaveProperty('duration');
expect(returned_validation_rules.duration.rules[0][1].message).toBe('Duration is a required field.');
expect((returned_validation_rules.duration.rules?.[0][1] as TExtendedRuleOptions).message).toBe(
'Duration is a required field.'
);
});

it('should contain rules for start_date', () => {
Expand All @@ -129,35 +158,83 @@ describe('getValidationRules', () => {

it('should contain rules for start_time', () => {
expect(returned_validation_rules).toHaveProperty('start_time');
expect(returned_validation_rules.start_time.rules[0][1].func(...start_time_mocked_params)).toBe(false);
expect(returned_validation_rules.start_time.rules[1][1].func(...start_time_mocked_params)).toBe(false);
expect(returned_validation_rules.start_time.rules[2][1].func(...start_time_mocked_params)).toBe(false);
expect(
(returned_validation_rules.start_time.rules?.[0][1] as TExtendedRuleOptions).func?.(
...start_time_mocked_params
)
).toBe(false);
expect(
(returned_validation_rules.start_time.rules?.[1][1] as TExtendedRuleOptions).func?.(
...start_time_mocked_params
)
).toBe(false);
expect(
(returned_validation_rules.start_time.rules?.[2][1] as TExtendedRuleOptions).func?.(
...start_time_mocked_params
)
).toBe(false);

mocked_store.contract_start_type = 'spot';
expect(returned_validation_rules.start_time.rules[3][1].func(...start_time_mocked_params)).toBe(true);
expect(
(returned_validation_rules.start_time.rules?.[3][1] as TExtendedRuleOptions).func?.(
...start_time_mocked_params
)
).toBe(true);

mocked_store.contract_start_type = 'test_spot';
expect(returned_validation_rules.start_time.rules[3][1].func(...start_time_mocked_params)).toBe(false);
expect(
(returned_validation_rules.start_time.rules?.[3][1] as TExtendedRuleOptions).func?.(
...start_time_mocked_params
)
).toBe(false);

(isTimeValid as jest.Mock).mockReturnValueOnce(true);
(isSessionAvailable as jest.Mock).mockReturnValueOnce(true);
expect(returned_validation_rules.start_time.rules[3][1].func(...start_time_mocked_params)).toBe(true);
expect(
(returned_validation_rules.start_time.rules?.[3][1] as TExtendedRuleOptions).func?.(
...start_time_mocked_params
)
).toBe(true);
});

it('should contain rules for expiry_time', () => {
expect(returned_validation_rules).toHaveProperty('expiry_time');
expect(returned_validation_rules.expiry_time.rules[0][1].func(...expiry_time_mocked_params)).toBe(false);
expect(returned_validation_rules.expiry_time.rules[1][1].func(...expiry_time_mocked_params)).toBe(false);
expect(returned_validation_rules.expiry_time.rules[2][1].func(...expiry_time_mocked_params)).toBe(false);
expect(
(returned_validation_rules.expiry_time.rules?.[0][1] as TExtendedRuleOptions).func?.(
...expiry_time_mocked_params
)
).toBe(false);
expect(
(returned_validation_rules.expiry_time.rules?.[1][1] as TExtendedRuleOptions).func?.(
...expiry_time_mocked_params
)
).toBe(false);
expect(
(returned_validation_rules.expiry_time.rules?.[2][1] as TExtendedRuleOptions).func?.(
...expiry_time_mocked_params
)
).toBe(false);

mocked_store.contract_start_type = 'spot';
expect(returned_validation_rules.expiry_time.rules[3][1].func(...expiry_time_mocked_params)).toBe(true);
expect(
(returned_validation_rules.expiry_time.rules?.[3][1] as TExtendedRuleOptions).func?.(
...expiry_time_mocked_params
)
).toBe(true);

mocked_store.contract_start_type = 'test_spot';
expect(returned_validation_rules.expiry_time.rules[3][1].func(...expiry_time_mocked_params)).toBe(false);
expect(
(returned_validation_rules.expiry_time.rules?.[3][1] as TExtendedRuleOptions).func?.(
...expiry_time_mocked_params
)
).toBe(false);

(isTimeValid as jest.Mock).mockReturnValueOnce(true);
(isSessionAvailable as jest.Mock).mockReturnValueOnce(true);
expect(returned_validation_rules.expiry_time.rules[3][1].func(...expiry_time_mocked_params)).toBe(true);
expect(
(returned_validation_rules.expiry_time.rules?.[3][1] as TExtendedRuleOptions).func?.(
...expiry_time_mocked_params
)
).toBe(true);
});
});

0 comments on commit 728ee4f

Please sign in to comment.