forked from deriv-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GRWT-2395 / Kate / [Dtrader -V2] Risk management changes (deriv-com#1…
…7567) * refactor: reset the stop loss value on trade type change * feat: create error observer component for trade params error and a helper hook * refactor: tak * refactor: add tests for new component * refactor: hook * fix: independent tp and sl validation * refactor: add tests * chore: remove unused code * fix: add date start field to the observes * refactor: rename componnets and params
- Loading branch information
1 parent
b9603d4
commit 291993e
Showing
13 changed files
with
346 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...es/trader/src/AppV2/Components/TradeErrorSnackbar/__tests__/trade-error-snackbar.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { mockStore } from '@deriv/stores'; | ||
import { useSnackbar } from '@deriv-com/quill-ui'; | ||
import TraderProviders from '../../../../trader-providers'; | ||
import TradeErrorSnackbar from '../trade-error-snackbar'; | ||
import { CONTRACT_TYPES, TRADE_TYPES } from '@deriv/shared'; | ||
|
||
jest.mock('@deriv-com/quill-ui', () => ({ | ||
...jest.requireActual('@deriv-com/quill-ui'), | ||
useSnackbar: jest.fn(), | ||
})); | ||
|
||
describe('TradeErrorSnackbar', () => { | ||
let default_mock_store: ReturnType<typeof mockStore>, | ||
default_mock_props: React.ComponentProps<typeof TradeErrorSnackbar>; | ||
let mockAddSnackbar = jest.fn(); | ||
|
||
beforeEach(() => { | ||
default_mock_store = mockStore({ | ||
client: { is_logged_in: true }, | ||
modules: { | ||
trade: { | ||
contract_type: TRADE_TYPES.TURBOS.LONG, | ||
proposal_info: { | ||
TURBOSLONG: { | ||
has_error: true, | ||
has_error_details: false, | ||
error_code: 'ContractBuyValidationError', | ||
error_field: 'take_profit', | ||
message: 'Enter an amount equal to or lower than 1701.11.', | ||
}, | ||
}, | ||
validation_errors: { | ||
amount: [], | ||
barrier_1: [], | ||
barrier_2: [], | ||
duration: [], | ||
start_date: [], | ||
start_time: [], | ||
stop_loss: [], | ||
take_profit: [], | ||
expiry_date: [], | ||
expiry_time: [], | ||
}, | ||
trade_type_tab: CONTRACT_TYPES.TURBOS.LONG, | ||
trade_types: { | ||
[CONTRACT_TYPES.TURBOS.LONG]: 'Turbos Long', | ||
}, | ||
}, | ||
}, | ||
}); | ||
default_mock_props = { error_fields: ['take_profit', 'stop_loss'], should_show_snackbar: true }; | ||
mockAddSnackbar = jest.fn(); | ||
(useSnackbar as jest.Mock).mockReturnValue({ addSnackbar: mockAddSnackbar }); | ||
}); | ||
|
||
const mockTradeErrorSnackbar = () => { | ||
return ( | ||
<TraderProviders store={default_mock_store}> | ||
<TradeErrorSnackbar {...default_mock_props} /> | ||
</TraderProviders> | ||
); | ||
}; | ||
|
||
it('calls useSnackbar if error field in proposal matches the passed error_fields', () => { | ||
render(mockTradeErrorSnackbar()); | ||
|
||
expect(mockAddSnackbar).toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls useSnackbar if error field in proposal matches the passed error_fields even if user is log out', () => { | ||
default_mock_store.client.is_logged_in = false; | ||
render(mockTradeErrorSnackbar()); | ||
|
||
expect(mockAddSnackbar).toHaveBeenCalled(); | ||
}); | ||
|
||
it('does not call useSnackbar if error field in proposal does not matches the passed error_fields', () => { | ||
default_mock_store.modules.trade.proposal_info = { | ||
TURBOSLONG: { | ||
has_error: true, | ||
has_error_details: false, | ||
error_code: 'ContractBuyValidationError', | ||
error_field: 'new_trade_param', | ||
message: 'Enter an amount equal to or lower than 1701.11.', | ||
}, | ||
}; | ||
render(mockTradeErrorSnackbar()); | ||
|
||
expect(mockAddSnackbar).not.toHaveBeenCalled(); | ||
}); | ||
}); |
3 changes: 3 additions & 0 deletions
3
packages/trader/src/AppV2/Components/TradeErrorSnackbar/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import TradeErrorSnackbar from './trade-error-snackbar'; | ||
|
||
export default TradeErrorSnackbar; |
36 changes: 36 additions & 0 deletions
36
packages/trader/src/AppV2/Components/TradeErrorSnackbar/trade-error-snackbar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react'; | ||
import { observer, useStore } from '@deriv/stores'; | ||
import { SnackbarController, useSnackbar } from '@deriv-com/quill-ui'; | ||
import useTradeError, { TErrorFields } from '../../Hooks/useTradeError'; | ||
|
||
const TradeErrorSnackbar = observer( | ||
({ error_fields, should_show_snackbar }: { error_fields: TErrorFields[]; should_show_snackbar?: boolean }) => { | ||
const { | ||
client: { is_logged_in }, | ||
} = useStore(); | ||
const { addSnackbar } = useSnackbar(); | ||
const { is_error_matching_field: has_error, message } = useTradeError({ | ||
error_fields, // array with BE error_fields, for which we will track errors. | ||
}); | ||
|
||
React.useEffect(() => { | ||
if (has_error && should_show_snackbar) { | ||
addSnackbar({ | ||
message, | ||
status: 'fail', | ||
hasCloseButton: true, | ||
hasFixedHeight: false, | ||
style: { | ||
marginBottom: is_logged_in ? '48px' : '-8px', | ||
width: 'calc(100% - var(--core-spacing-800)', | ||
}, | ||
}); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [has_error, should_show_snackbar]); | ||
|
||
return <SnackbarController />; | ||
} | ||
); | ||
|
||
export default TradeErrorSnackbar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.