1
1
import React from 'react' ;
2
- import { render , screen } from '@testing-library/react' ;
3
- import userEvent from '@testing-library/user-event' ;
2
+
4
3
import { CONTRACT_TYPES , TRADE_TYPES } from '@deriv/shared' ;
5
4
import { mockStore } from '@deriv/stores' ;
5
+ import { render , screen } from '@testing-library/react' ;
6
+ import userEvent from '@testing-library/user-event' ;
7
+
8
+ import { useDtraderQuery } from 'AppV2/Hooks/useDtraderQuery' ;
6
9
import ModulesProvider from 'Stores/Providers/modules-providers' ;
10
+
7
11
import TraderProviders from '../../../../../trader-providers' ;
8
12
import Stake from '../stake' ;
9
13
@@ -29,6 +33,24 @@ jest.mock('AppV2/Hooks/useContractsForCompany', () => ({
29
33
} ,
30
34
} ) ) ,
31
35
} ) ) ;
36
+ jest . mock ( '@deriv/shared' , ( ) => ( {
37
+ ...jest . requireActual ( '@deriv/shared' ) ,
38
+ WS : {
39
+ send : jest . fn ( ) ,
40
+ authorized : {
41
+ send : jest . fn ( ) ,
42
+ } ,
43
+ } ,
44
+ } ) ) ;
45
+ jest . mock ( 'AppV2/Hooks/useDtraderQuery' , ( ) => ( {
46
+ ...jest . requireActual ( 'AppV2/Hooks/useDtraderQuery' ) ,
47
+ useDtraderQuery : jest . fn ( ( ) => ( {
48
+ data : {
49
+ proposal : { } ,
50
+ error : { } ,
51
+ } ,
52
+ } ) ) ,
53
+ } ) ) ;
32
54
33
55
describe ( 'Stake' , ( ) => {
34
56
let default_mock_store : ReturnType < typeof mockStore > ;
@@ -45,14 +67,12 @@ describe('Stake', () => {
45
67
currency : 'USD' ,
46
68
proposal_info : {
47
69
[ CONTRACT_TYPES . CALL ] : {
48
- id : '53e8cb91-8c13-60a3-289f-778e8386367c' ,
49
70
has_error : false ,
50
71
message :
51
72
'Win payout if Volatility 100 (1s) Index is strictly higher than entry spot at 5 minutes after contract start time.' ,
52
73
payout : 19.55 ,
53
74
} ,
54
75
[ CONTRACT_TYPES . PUT ] : {
55
- id : '2b5dd806-7505-8af7-1bbb-5e24ac48bbbc' ,
56
76
has_error : false ,
57
77
message :
58
78
'Win payout if Volatility 100 (1s) Index is strictly lower than entry spot at 5 minutes after contract start time.' ,
@@ -63,14 +83,11 @@ describe('Stake', () => {
63
83
[ CONTRACT_TYPES . CALL ] : 'Higher' ,
64
84
[ CONTRACT_TYPES . PUT ] : 'Lower' ,
65
85
} ,
66
- validation_errors : { amount : [ ] } ,
86
+ trade_type_tab : 'CALL' ,
67
87
validation_params : {
68
88
[ CONTRACT_TYPES . CALL ] : { payout : { max : '50000.00' } } ,
69
89
[ CONTRACT_TYPES . PUT ] : { payout : { max : '50000.00' } } ,
70
90
} ,
71
- v2_params_initial_values : {
72
- stake : 10 ,
73
- } ,
74
91
} ,
75
92
} ,
76
93
} ) )
@@ -84,15 +101,6 @@ describe('Stake', () => {
84
101
</ TraderProviders >
85
102
) ;
86
103
87
- it ( 'switches basis to stake if it is different' , ( ) => {
88
- default_mock_store . modules . trade . basis = 'payout' ;
89
- render ( < MockedStake /> ) ;
90
-
91
- expect ( default_mock_store . modules . trade . onChange ) . toHaveBeenCalledWith ( {
92
- target : { name : 'basis' , value : 'stake' } ,
93
- } ) ;
94
- } ) ;
95
-
96
104
it ( 'renders trade param with "Stake" label and input with a value equal to the current stake amount value' , ( ) => {
97
105
render ( < MockedStake /> ) ;
98
106
const { amount, currency } = default_mock_store . modules . trade ;
@@ -109,16 +117,19 @@ describe('Stake', () => {
109
117
110
118
expect ( screen . getByTestId ( 'dt-actionsheet-overlay' ) ) . toBeInTheDocument ( ) ;
111
119
expect ( screen . getByPlaceholderText ( input_placeholder ) ) . toBeInTheDocument ( ) ;
112
- expect ( screen . getAllByText ( / p a y o u t / i) ) . toHaveLength ( 3 ) ;
120
+ expect ( screen . getAllByText ( / p a y o u t / i) ) . toHaveLength ( 2 ) ;
113
121
expect ( screen . getByRole ( 'button' , { name : save_button_label } ) ) . toBeInTheDocument ( ) ;
114
122
} ) ;
115
123
116
- it ( 'calls onChange when stake input changes ' , async ( ) => {
124
+ it ( 'calls onChange if user clicks on Save ' , async ( ) => {
117
125
render ( < MockedStake /> ) ;
126
+
118
127
await userEvent . click ( screen . getByText ( stake_param_label ) ) ;
119
- await userEvent . type ( screen . getByPlaceholderText ( input_placeholder ) , '0' ) ;
128
+ await userEvent . type ( screen . getByPlaceholderText ( input_placeholder ) , '10' ) ;
129
+ await userEvent . click ( screen . getByRole ( 'button' , { name : save_button_label } ) ) ;
130
+
120
131
expect ( default_mock_store . modules . trade . onChange ) . toHaveBeenCalledWith ( {
121
- target : { name : 'amount' , value : '100' } ,
132
+ target : { name : 'amount' , value : 10 } ,
122
133
} ) ;
123
134
} ) ;
124
135
@@ -157,14 +168,12 @@ describe('Stake', () => {
157
168
is_multiplier : true ,
158
169
proposal_info : {
159
170
[ CONTRACT_TYPES . MULTIPLIER . UP ] : {
160
- id : '3b09df15-b0b7-70a8-15c9-fad8e2bda5ef' ,
161
171
has_error : false ,
162
172
message :
163
173
"If you select 'Up', your total profit/loss will be the percentage increase in Volatility 100 (1s) Index, multiplied by 1000, minus commissions." ,
164
174
payout : 0 ,
165
175
} ,
166
176
[ CONTRACT_TYPES . MULTIPLIER . DOWN ] : {
167
- id : '873af3a9-a0da-5486-d1f9-fce8206e6ba2' ,
168
177
has_error : false ,
169
178
message :
170
179
"If you select 'Down', your total profit/loss will be the percentage decrease in Volatility 100 (1s) Index, multiplied by 1000, minus commissions." ,
@@ -202,84 +211,25 @@ describe('Stake', () => {
202
211
expect ( screen . getByText ( 'Commission' ) ) . toBeInTheDocument ( ) ;
203
212
} ) ;
204
213
205
- it ( 'calls setV2ParamsInitialValues if v2_params_initial_values.stake !== amount on mount and on Save button click if no error' , async ( ) => {
206
- default_mock_store . modules . trade . amount = '30' ;
207
- render ( < MockedStake /> ) ;
208
- await userEvent . click ( screen . getByText ( stake_param_label ) ) ;
209
- await userEvent . type ( screen . getByPlaceholderText ( input_placeholder ) , '0' ) ;
210
-
211
- expect ( default_mock_store . modules . trade . setV2ParamsInitialValues ) . toHaveBeenCalledTimes ( 2 ) ;
212
-
213
- await userEvent . click ( screen . getByRole ( 'button' , { name : save_button_label } ) ) ;
214
- expect ( default_mock_store . modules . trade . setV2ParamsInitialValues ) . toHaveBeenCalledTimes ( 3 ) ;
215
- } ) ;
216
-
217
- it ( 'calls onChange on component mount if v2_params_initial_values.stake is not equal to amount' , ( ) => {
218
- default_mock_store . modules . trade . amount = '30' ;
219
- render ( < MockedStake /> ) ;
220
- expect ( default_mock_store . modules . trade . onChange ) . toHaveBeenCalledWith ( {
221
- target : { name : 'amount' , value : 10 } ,
222
- } ) ;
223
- } ) ;
224
-
225
214
it ( 'shows error in case of a validation error if input is non-empty' , async ( ) => {
226
215
const error_text = "Please enter a stake amount that's at least 0.35." ;
216
+ default_mock_store . modules . trade . contract_type = TRADE_TYPES . HIGH_LOW ;
217
+ default_mock_store . modules . trade . trade_type_tab = 'CALL' ;
227
218
default_mock_store . modules . trade . proposal_info = {
228
- PUT : { id : '' , has_error : true , message : error_text } ,
229
- CALL : { id : '' , has_error : true , message : error_text } ,
219
+ CALL : { has_error : true , message : error_text , error_field : 'amount' } ,
230
220
} ;
231
- default_mock_store . modules . trade . validation_errors . amount = [ error_text ] ;
232
- default_mock_store . modules . trade . amount = 0 ;
233
-
234
- render ( < MockedStake /> ) ;
235
- await userEvent . click ( screen . getByText ( stake_param_label ) ) ;
236
- expect ( screen . getByText ( error_text ) ) . toBeInTheDocument ( ) ;
237
- expect ( screen . getAllByText ( '- USD' ) ) . toHaveLength ( 2 ) ;
238
- } ) ;
239
-
240
- it ( 'shows max payout error with the least current payout when both of the 2 contract types exceed max payout' , async ( ) => {
241
- const error_text_rise = 'Minimum stake of 0.35 and maximum payout of 50000.00. Current payout is 50631.97.' ;
242
- const error_text_fall = 'Minimum stake of 0.35 and maximum payout of 50000.00. Current payout is 50513.21.' ;
243
- default_mock_store . modules . trade . proposal_info = {
244
- CALL : { id : '' , has_error : true , message : error_text_rise , error_field : 'amount' } ,
245
- PUT : { id : '' , has_error : true , message : error_text_fall , error_field : 'amount' } ,
246
- } ;
247
- default_mock_store . modules . trade . validation_errors . amount = [ error_text_fall ] ;
248
- default_mock_store . modules . trade . amount = '26500' ;
249
-
250
- render ( < MockedStake /> ) ;
251
- await userEvent . click ( screen . getByText ( stake_param_label ) ) ;
252
-
253
- expect ( screen . getByText ( error_text_fall ) ) . toBeInTheDocument ( ) ;
254
- expect ( screen . queryByText ( '- USD' ) ) . not . toBeInTheDocument ( ) ;
255
- } ) ;
256
221
257
- it ( 'does not show max payout error if one of the 2 contract types satisfies max payout' , async ( ) => {
258
- const error_text_rise = 'Minimum stake of 0.35 and maximum payout of 50000.00. Current payout is 50058.77.' ;
259
- const success_text_fall =
260
- 'Win payout if Volatility 100 (1s) Index is strictly lower than entry spot at 5 minutes after contract start time.' ;
261
- default_mock_store . modules . trade . proposal_info = {
262
- CALL : { id : '' , has_error : true , message : error_text_rise } ,
263
- PUT : {
264
- id : 'b608baf2-ba5d-00e0-8035-9af5c0769664' ,
265
- has_error : false ,
266
- message : success_text_fall ,
267
- payout : 49942.7 ,
222
+ ( useDtraderQuery as jest . Mock ) . mockReturnValue ( {
223
+ data : {
224
+ error : { has_error : true , message : error_text , details : { error_field : 'amount' } } ,
225
+ proposal : { } ,
268
226
} ,
269
- } ;
270
- default_mock_store . modules . trade . amount = '26200' ;
271
-
272
- render ( < MockedStake /> ) ;
273
- await userEvent . click ( screen . getByText ( stake_param_label ) ) ;
274
-
275
- expect ( screen . queryByText ( error_text_rise ) ) . not . toBeInTheDocument ( ) ;
276
- } ) ;
277
-
278
- it ( 'sets default stake if available_contract_types object contains it' , ( ) => {
279
- default_mock_store . modules . trade . contract_type = TRADE_TYPES . VANILLA . CALL ;
227
+ } ) ;
280
228
render ( < MockedStake /> ) ;
281
229
282
- expect ( default_mock_store . modules . trade . setDefaultStake ) . toHaveBeenCalledWith ( 10 ) ;
230
+ await userEvent . click ( screen . getByText ( stake_param_label ) ) ;
231
+ expect ( screen . getByText ( error_text ) ) . toBeInTheDocument ( ) ;
232
+ expect ( screen . getByText ( '- USD' ) ) . toBeInTheDocument ( ) ;
283
233
} ) ;
284
234
285
235
it ( 'disables trade param if is_market_closed == true' , ( ) => {
0 commit comments