Skip to content

Commit

Permalink
fix: 🐛 fixed stop bot warning inherited from quick-strategy form (#16715
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sandeep-deriv committed Sep 6, 2024
1 parent 65ad9b9 commit ef9d754
Show file tree
Hide file tree
Showing 21 changed files with 234 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,6 @@ jest.mock('@deriv/bot-skeleton', () => ({
},
},
},
config: {
...jest.requireActual('@deriv/bot-skeleton').config,
QUICK_STRATEGY: {
DISABLED: {
SYMBOLS: ['1HZ150V', '1HZ250V'],
SUBMARKETS: ['crash_index', 'non_stable_coin'],
},
DEFAULT: {
symbol: '1HZ100V',
tradetype: 'callput',
durationtype: 't',
size: 2,
unit: 1,
},
},
},
}));

jest.mock('../../../../xml/martingale.xml', () => '');
Expand Down Expand Up @@ -190,9 +174,9 @@ describe('<QuickStrategy />', () => {
const mock_dbot_store = mockDBotStore(mock_store, mock_ws);

beforeEach(() => {
mock_dbot_store?.quick_strategy?.setValue('durationtype', 't');
mock_dbot_store?.quick_strategy?.setSelectedStrategy('MARTINGALE');
mock_dbot_store?.quick_strategy?.setFormVisibility(true);
mock_dbot_store?.server_bot?.setValue('durationtype', 't');
mock_dbot_store?.server_bot?.setSelectedStrategy('MARTINGALE');

wrapper = ({ children }: { children: JSX.Element }) => (
<StoreProvider store={mock_store}>
<DBotStoreProvider ws={mock_ws} mock={mock_dbot_store}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ window.Blockly = {

jest.mock('../config', () => ({
STRATEGIES: {
RSI: {
name: 'RSI',
label: 'RSI',
MARTINGALE: {
name: 'MARTINGALE',
label: 'MARTINGALE',
description: 'test',
fields: [
[
Expand Down Expand Up @@ -197,7 +197,7 @@ jest.mock('../config', () => ({
},
}));

describe('<QuickStrategyForm />', () => {
describe('<AddForm />', () => {
let wrapper: ({ children }: { children: JSX.Element }) => JSX.Element, mock_dbot_store: RootStore | undefined;
const mock_store = mockStore({
ui: {
Expand All @@ -207,7 +207,7 @@ describe('<QuickStrategyForm />', () => {

beforeEach(() => {
mock_dbot_store = mockDBotStore(mock_store, mock_ws);
mock_dbot_store?.quick_strategy?.setSelectedStrategy('RSI');
mock_dbot_store?.server_bot?.setSelectedStrategy('MARTINGALE');
const mockOnSubmit = jest.fn();
const initial_value = {
tradetype: 'callput',
Expand Down Expand Up @@ -247,8 +247,8 @@ describe('<QuickStrategyForm />', () => {
});

it('should render the form with existing duration values and possitive last digit prediction', () => {
mock_dbot_store?.quick_strategy?.setCurrentDurationMinMax(1, 2);
mock_dbot_store?.quick_strategy?.setValue('last_digit_prediction', 5);
mock_dbot_store?.server_bot?.setCurrentDurationMinMax(1, 2);
mock_dbot_store?.server_bot?.setValue('last_digit_prediction', 5);
const { container } = render(<QuickStrategyForm />, {
wrapper,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { useRef, useState } from 'react';
import { Form as FormikForm, Formik } from 'formik';
import * as Yup from 'yup';
import { config as qs_config } from '@deriv/bot-skeleton';
import { MobileFullPageModal, Modal } from '@deriv/components';
import { observer, useStore } from '@deriv/stores';
import { localize } from '@deriv/translations';
import { useDBotStore } from 'Stores/useDBotStore';
import { SERVER_BOT_CONFIG } from '../config';
import DesktopFormWrapper from './form-wrappers/desktop-form-wrapper';
import MobileFormWrapper from './form-wrappers/mobile-form-wrapper';
import LossThresholdWarningDialog from './parts/loss-threshold-warning-dialog';
Expand Down Expand Up @@ -39,13 +39,11 @@ const getErrorMessage = (dir: 'MIN' | 'MAX', value: number, type = 'DEFAULT') =>
};

export const FormikWrapper: React.FC<TFormikWrapper> = observer(({ children, setFormVisibility }) => {
const { quick_strategy, server_bot } = useDBotStore();
const { selected_strategy, form_data, current_duration_min_max, initializeLossThresholdWarningData, setValue } =
quick_strategy;
const { server_bot } = useDBotStore();
const { selected_strategy, current_duration_min_max, initializeLossThresholdWarningData, createBot } = server_bot;
const config: TConfigItem[][] = STRATEGIES[selected_strategy]?.fields;
const [dynamic_schema, setDynamicSchema] = useState(Yup.object().shape({}));
const is_mounted = useRef(true);
const { createBot } = server_bot;

let initial_value: TFormData | null = null;

Expand All @@ -63,15 +61,15 @@ export const FormikWrapper: React.FC<TFormikWrapper> = observer(({ children, set
const data = getSavedValues();
initial_value = {
name: data?.name ?? 'Martingale',
symbol: data?.symbol ?? qs_config.QUICK_STRATEGY.DEFAULT.symbol,
symbol: data?.symbol ?? SERVER_BOT_CONFIG.DEFAULT.symbol,
tradetype: data?.tradetype ?? '',
type: data?.type ?? '',
durationtype: data?.durationtype ?? qs_config.QUICK_STRATEGY.DEFAULT.durationtype,
durationtype: data?.durationtype ?? SERVER_BOT_CONFIG.DEFAULT.durationtype,
duration: data?.duration ?? '1',
stake: data?.stake ?? '1',
loss: data?.loss ?? '',
profit: data?.profit ?? '',
size: data?.size ?? String(qs_config.QUICK_STRATEGY.DEFAULT.size),
size: data?.size ?? String(SERVER_BOT_CONFIG.DEFAULT.size),
max_stake: data?.max_stake ?? 10,
};
return initial_value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { config as qs_config } from '@deriv/bot-skeleton';
import { localize } from '@deriv/translations';
import { MARTINGALE } from '../../../constants/quick-strategies';
import { SERVER_BOT_CONFIG } from '../config';
import { TConfigItem, TStrategies, TValidationItem } from './types';

export const FORM_TABS = [
Expand Down Expand Up @@ -130,7 +130,7 @@ const SIZE: TConfigItem = {
'floor',
{
type: 'min',
value: String(qs_config.QUICK_STRATEGY.DEFAULT.size),
value: String(SERVER_BOT_CONFIG.DEFAULT.size),
getMessage: (min: string | number) =>
localize('The value must be equal or greater than {{ min }}', { min }),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import userEvent from '@testing-library/user-event';
import { mock_ws } from 'Utils/mock';
import RootStore from 'Stores/root-store';
import { DBotStoreProvider, mockDBotStore } from 'Stores/useDBotStore';
import { quick_strategy_content } from '../../../../tutorials/constants';
import DesktopFormWrapper from '../desktop-form-wrapper';

jest.mock('@deriv/bot-skeleton/src/scratch/dbot', () => jest.fn());
Expand Down Expand Up @@ -66,7 +65,6 @@ describe('<DesktopFormWrapper />', () => {
const mock_store = mockStore({});
mock_dbot_store = mockDBotStore(mock_store, mock_ws);
const initial_value = {};
mock_dbot_store?.quick_strategy.setFormVisibility(true);

wrapper = ({ children }: { children: JSX.Element }) => (
<StoreProvider store={mock_store}>
Expand Down Expand Up @@ -101,7 +99,6 @@ describe('<DesktopFormWrapper />', () => {
wrapper,
}
);
expect(mock_dbot_store?.quick_strategy.is_open).toBeTruthy();

const close_button = screen.getByTestId('qs-desktop-close-button');
userEvent.click(close_button);
Expand All @@ -110,19 +107,6 @@ describe('<DesktopFormWrapper />', () => {
expect(onClickClose).toBeCalled();
});

it('should change the selected strategy', () => {
mock_dbot_store?.quick_strategy.setSelectedStrategy(quick_strategy_content[0].qs_name);
render(
<DesktopFormWrapper onClickClose={onClickClose}>
<div>test</div>
</DesktopFormWrapper>,
{
wrapper,
}
);
expect(mock_dbot_store?.quick_strategy.selected_strategy).toBe(quick_strategy_content[0].qs_name);
});

it('should submit the form', async () => {
render(
<DesktopFormWrapper onClickClose={onClickClose}>
Expand All @@ -134,7 +118,6 @@ describe('<DesktopFormWrapper />', () => {
wrapper,
}
);
expect(mock_dbot_store?.quick_strategy.is_open).toBeTruthy();
const submit_button = screen.getByRole('button', { name: 'Add' });
userEvent.click(submit_button);
await waitFor(() => expect(mockOnSubmit).toBeCalled());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ describe('<MobileFormWrapper />', () => {
const mock_store = mockStore({});
mock_dbot_store = mockDBotStore(mock_store, mock_ws);
const initial_value = {};
mock_dbot_store?.quick_strategy.setFormVisibility(true);

wrapper = ({ children }: { children: JSX.Element }) => (
<StoreProvider store={mock_store}>
Expand All @@ -68,7 +67,7 @@ describe('<MobileFormWrapper />', () => {
});

it('should change the selected strategy', () => {
mock_dbot_store?.quick_strategy.setSelectedStrategy('MARTINGALE');
mock_dbot_store?.server_bot.setSelectedStrategy('MARTINGALE');
render(
<MobileFromWrapper>
<div>test</div>
Expand All @@ -77,7 +76,7 @@ describe('<MobileFormWrapper />', () => {
wrapper,
}
);
expect(mock_dbot_store?.quick_strategy.selected_strategy).toBe('MARTINGALE');
expect(mock_dbot_store?.server_bot.selected_strategy).toBe('MARTINGALE');
});

it('should submit the form', async () => {
Expand All @@ -91,7 +90,7 @@ describe('<MobileFormWrapper />', () => {
wrapper,
}
);
expect(mock_dbot_store?.quick_strategy.is_open).toBeTruthy();

const submit_button = screen.getByRole('button', { name: /Add/i });
userEvent.click(submit_button);
await waitFor(() => expect(mockOnSubmit).toBeCalled());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { renderHook } from '@testing-library/react-hooks';
import { mock_ws } from 'Utils/mock';
import RootStore from 'Stores/root-store';
import { DBotStoreProvider, mockDBotStore } from 'Stores/useDBotStore';
import useQsSubmitHandler from '../useQsSubmitHandler';
import useSubmitHandler from '../useSubmitHandler';

jest.mock('@deriv/bot-skeleton/src/scratch/dbot', () => jest.fn());

Expand Down Expand Up @@ -63,19 +63,17 @@ jest.mock('@deriv/bot-skeleton', () => ({

jest.mock('../../../../../xml/martingale_max-stake.xml', () => '');

describe('useQsSubmitHandler hook', () => {
describe('useSubmitHandler hook', () => {
let wrapper: ({ children }: { children: JSX.Element }) => JSX.Element, mock_dbot_store: RootStore | undefined;
const mock_store = mockStore({});

beforeEach(() => {
mock_dbot_store = mockDBotStore(mock_store, mock_ws);
mock_dbot_store = {
...mock_dbot_store,
quick_strategy: {
...mock_dbot_store.quick_strategy,
server_bot: {
...mock_dbot_store.server_bot,
setLossThresholdWarningData: jest.fn(),
onSubmit: jest.fn(),
toggleStopBotDialog: jest.fn(),
},
};
const mockOnSubmit = jest.fn();
Expand All @@ -92,9 +90,9 @@ describe('useQsSubmitHandler hook', () => {
);
});

it('should call useQsSubmitHandler hook', async () => {
it('should call useSubmitHandler hook', async () => {
mock_store.client.is_logged_in = true;
const { result } = renderHook(() => useQsSubmitHandler(), { wrapper });
const { result } = renderHook(() => useSubmitHandler(), { wrapper });
result.current.handleSubmit();

const { handleSubmit, proceedFormSubmission } = result.current;
Expand All @@ -103,49 +101,48 @@ describe('useQsSubmitHandler hook', () => {
expect(typeof proceedFormSubmission).toBe('function');
});

it('useQsSubmitHandler hook should not call setLossThresholdWarningData() when is_logged_in equals false', () => {
it('useSubmitHandler hook should not call setLossThresholdWarningData() when is_logged_in equals false', () => {
mock_store.client.is_logged_in = false;
const { result } = renderHook(() => useQsSubmitHandler(), { wrapper });
const { result } = renderHook(() => useSubmitHandler(), { wrapper });
result.current.handleSubmit();

expect(mock_dbot_store?.quick_strategy.setLossThresholdWarningData).not.toHaveBeenCalled();
expect(mock_dbot_store?.server_bot.setLossThresholdWarningData).not.toHaveBeenCalled();
});

it('useQsSubmitHandler hook should not call setLossThresholdWarningData() when bot is running handle toggleStopBotDialog() and make it false', () => {
it('useSubmitHandler hook should not call setLossThresholdWarningData() when bot is running handle toggleStopBotDialog() and make it false', () => {
mock_dbot_store?.run_panel?.setIsRunning(true);
mock_store.client.is_logged_in = false;
const { result } = renderHook(() => useQsSubmitHandler(), { wrapper });
const { result } = renderHook(() => useSubmitHandler(), { wrapper });
result.current.handleSubmit();

expect(mock_dbot_store?.quick_strategy.is_open).toBeFalsy();
expect(mock_dbot_store?.quick_strategy.setLossThresholdWarningData).not.toHaveBeenCalled();
expect(mock_dbot_store?.server_bot.setLossThresholdWarningData).not.toHaveBeenCalled();
});

it('useQsSubmitHandler hook should not call setLossThresholdWarningData() when the balance not less than loss and loss not bigger than profit', () => {
it('useSubmitHandler hook should not call setLossThresholdWarningData() when the balance not less than loss and loss not bigger than profit', () => {
mock_store.client.balance = undefined;

const { result } = renderHook(() => useQsSubmitHandler(), { wrapper });
const { result } = renderHook(() => useSubmitHandler(), { wrapper });
result.current.handleSubmit();

expect(mock_dbot_store?.quick_strategy.setLossThresholdWarningData).not.toHaveBeenCalled();
expect(mock_dbot_store?.server_bot.setLossThresholdWarningData).not.toHaveBeenCalled();
});

it('useQsSubmitHandler hook should not call setLossThresholdWarningData() when the balance more than loss and loss not bigger than profit', () => {
it('useSubmitHandler hook should not call setLossThresholdWarningData() when the balance more than loss and loss not bigger than profit', () => {
mock_store.client.balance = 100000000;

const { result } = renderHook(() => useQsSubmitHandler(), { wrapper });
const { result } = renderHook(() => useSubmitHandler(), { wrapper });
result.current.handleSubmit();

expect(mock_dbot_store?.quick_strategy.setLossThresholdWarningData).not.toHaveBeenCalled();
expect(mock_dbot_store?.server_bot.setLossThresholdWarningData).not.toHaveBeenCalled();
});

it('useQsSubmitHandler hook should call setLossThresholdWarningData() when is_logged_in equals true and balance is less than loss', () => {
it('useSubmitHandler hook should call setLossThresholdWarningData() when is_logged_in equals true and balance is less than loss', () => {
mock_store.client.is_logged_in = true;
mock_store.client.balance = -1;

const { result } = renderHook(() => useQsSubmitHandler(), { wrapper });
const { result } = renderHook(() => useSubmitHandler(), { wrapper });
result.current.handleSubmit();

expect(mock_dbot_store?.quick_strategy.setLossThresholdWarningData).toHaveBeenCalled();
expect(mock_dbot_store?.server_bot.setLossThresholdWarningData).toHaveBeenCalled();
});
});
Loading

0 comments on commit ef9d754

Please sign in to comment.