Skip to content

Commit 9bb5179

Browse files
authored
[GRWT]fix: add limit to tp and sl field same as prod behavior (deriv-com#17734)
* fix: add limit to tp and sl field same as prod behavior * fix: added test case for the changes * fix: fix the scnario for mobile * fix: detect decimal keys for mobile * test: add console log * fix: add regex to identify the decimal * fix: code to debug * fix: handled the decimal in mobile for tp and al * fix: added onKeyPress to detect period key * fix: handle numeric key condition for andriod phones
1 parent c91fc7d commit 9bb5179

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

packages/trader/src/AppV2/Components/TradeParameters/RiskManagement/__tests__/take-profit-and-stop-loss-input.spec.tsx

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { render, screen } from '@testing-library/react';
2+
import { fireEvent, render, screen } from '@testing-library/react';
33
import userEvent from '@testing-library/user-event';
44
import { mockStore } from '@deriv/stores';
55
import ModulesProvider from 'Stores/Providers/modules-providers';
@@ -90,46 +90,56 @@ describe('TakeProfitAndStopLossInput', () => {
9090
expect(screen.getByText(accu_content)).toBeInTheDocument();
9191
});
9292

93-
it('should call focusAndOpenKeyboard, when ToggleSwitch is switched to true.', () => {
93+
it('should call focusAndOpenKeyboard, when ToggleSwitch is switched to true.', async () => {
9494
const mockFocusAndOpenKeyboard = jest.spyOn(utils, 'focusAndOpenKeyboard');
9595
mockTakeProfitAndStopLossInput();
9696

9797
const toggle_switcher = screen.getAllByRole('button')[0];
98-
userEvent.click(toggle_switcher);
98+
await userEvent.click(toggle_switcher);
9999
expect(mockFocusAndOpenKeyboard).toBeCalledTimes(1);
100100
});
101101

102-
it('should call focusAndOpenKeyboard, when user clicks on Take Profit overlay.', () => {
102+
it('should call focusAndOpenKeyboard, when user clicks on Take Profit overlay.', async () => {
103103
const mockFocusAndOpenKeyboard = jest.spyOn(utils, 'focusAndOpenKeyboard');
104104
mockTakeProfitAndStopLossInput();
105105

106106
const take_profit_overlay = screen.getByTestId('dt_take_profit_overlay');
107-
userEvent.click(take_profit_overlay);
107+
await userEvent.click(take_profit_overlay);
108108

109109
expect(mockFocusAndOpenKeyboard).toBeCalledTimes(1);
110110
});
111111

112-
it('should render take profit overlay if ToggleSwitch was switched to false', () => {
112+
it('should render take profit overlay if ToggleSwitch was switched to false', async () => {
113113
default_mock_store.modules.trade.has_take_profit = true;
114114
default_mock_store.modules.trade.take_profit = '5';
115115
mockTakeProfitAndStopLossInput();
116116

117117
expect(screen.queryByTestId('dt_take_profit_overlay')).not.toBeInTheDocument();
118118
const toggle_switcher = screen.getAllByRole('button')[0];
119-
userEvent.click(toggle_switcher);
119+
await userEvent.click(toggle_switcher);
120120

121121
expect(screen.getByTestId('dt_take_profit_overlay')).toBeInTheDocument();
122122
});
123123

124-
it('should call onChangeMultiple when user click on Save button, if there are no API errors', () => {
124+
it('should call onChangeMultiple when user click on Save button, if there are no API errors', async () => {
125125
default_mock_store.modules.trade.has_take_profit = true;
126126
default_mock_store.modules.trade.take_profit = '5';
127127
mockTakeProfitAndStopLossInput();
128128

129-
userEvent.type(screen.getByTestId(tp_data_testid), '2');
129+
await userEvent.type(screen.getByTestId(tp_data_testid), '2');
130130
const save_button = screen.getByText('Save');
131-
userEvent.click(save_button);
131+
await userEvent.click(save_button);
132132

133133
expect(default_mock_store.modules.trade.onChangeMultiple).toBeCalled();
134134
});
135+
136+
it('should have max length of 10 for take profit input when currency is USD', async () => {
137+
default_mock_store.modules.trade.has_take_profit = true;
138+
default_mock_store.modules.trade.take_profit = '5';
139+
mockTakeProfitAndStopLossInput();
140+
const input_field = screen.getByTestId(tp_data_testid);
141+
await userEvent.type(input_field, '12345678901');
142+
143+
expect(input_field).toHaveValue('5123456789');
144+
});
135145
});

packages/trader/src/AppV2/Components/TradeParameters/RiskManagement/take-profit-and-stop-loss-input.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const TakeProfitAndStopLossInput = ({
7171
const [new_input_value, setNewInputValue] = React.useState(is_take_profit_input ? take_profit : stop_loss);
7272
const [error_text, setErrorText] = React.useState('');
7373
const [fe_error_text, setFEErrorText] = React.useState(initial_error_text ?? message ?? '');
74+
const [max_length, setMaxLength] = React.useState(10);
7475

7576
// Refs for handling focusing and bluring input
7677
const input_ref = React.useRef<HTMLInputElement>(null);
@@ -283,6 +284,17 @@ const TakeProfitAndStopLossInput = ({
283284
unitLeft={currency_display_code}
284285
variant='fill'
285286
value={new_input_value ?? ''}
287+
onBeforeInput={(e: React.FormEvent<HTMLInputElement>) => {
288+
if (
289+
['.', ','].includes((e.nativeEvent as InputEvent)?.data ?? '') &&
290+
(new_input_value?.length ?? 0) <= 10
291+
) {
292+
setMaxLength(decimals ? 11 + decimals : 10);
293+
} else if (!new_input_value?.includes('.')) {
294+
setMaxLength(10);
295+
}
296+
}}
297+
maxLength={max_length}
286298
/>
287299
{!is_enabled && (
288300
<button

0 commit comments

Comments
 (0)