@@ -3,6 +3,8 @@ import { instance, mock } from 'ts-mockito'
3
3
import { fireEvent , render , screen , act , waitForStack , constants } from 'testSrc/helpers'
4
4
5
5
import { RejsonObject } from './RejsonObject'
6
+ import * as tbdUtils from './tbd'
7
+ import * as store from '../hooks/useRejsonStore'
6
8
import { JSONObjectProps , ObjectTypes } from '../interfaces'
7
9
8
10
const EXPAND_OBJECT = 'expand-object'
@@ -284,6 +286,39 @@ describe('JSONObject', () => {
284
286
fireEvent . click ( screen . getByTestId ( EDIT_OBJECT_BTN ) )
285
287
} )
286
288
289
+ fireEvent . input ( screen . getByTestId ( JSON_VALUE ) , {
290
+ target : { value : JSON . stringify ( [ ] ) } ,
291
+ } )
292
+
293
+ await act ( async ( ) => {
294
+ fireEvent . click ( screen . getByTestId ( 'apply-edit-btn' ) )
295
+ } )
296
+
297
+ expect ( onJSONPropertyEdited ) . toBeCalled ( )
298
+ } )
299
+
300
+ it ( 'should apply value for edit with modifications (via ConfirmDialog)' , async ( ) => {
301
+ const fetchVisualisationResults = vi
302
+ . fn ( )
303
+ . mockReturnValue ( Promise . resolve ( { data : { } } ) )
304
+ const onJSONPropertyEdited = vi . fn ( )
305
+
306
+ render (
307
+ < RejsonObject
308
+ { ...instance ( mockedProps ) }
309
+ keyName = "keyName"
310
+ value = { constants . REJSON_DATA }
311
+ isDownloaded
312
+ handleFetchVisualisationResults = { fetchVisualisationResults }
313
+ handleSetRejsonDataAction = { onJSONPropertyEdited }
314
+ onJsonKeyExpandAndCollapse = { vi . fn ( ) }
315
+ /> ,
316
+ )
317
+
318
+ await act ( async ( ) => {
319
+ fireEvent . click ( screen . getByTestId ( EDIT_OBJECT_BTN ) )
320
+ } )
321
+
287
322
fireEvent . input ( screen . getByTestId ( JSON_VALUE ) , {
288
323
target : { value : '{}' } ,
289
324
} )
@@ -292,6 +327,112 @@ describe('JSONObject', () => {
292
327
fireEvent . click ( screen . getByTestId ( 'apply-edit-btn' ) )
293
328
} )
294
329
330
+ expect ( screen . getByText ( 'Duplicate JSON key detected' ) ) . toBeInTheDocument ( )
331
+
332
+ await act ( async ( ) => {
333
+ fireEvent . click ( screen . getByTestId ( 'confirm-btn' ) )
334
+ } )
335
+
295
336
expect ( onJSONPropertyEdited ) . toBeCalled ( )
337
+
338
+ expect (
339
+ screen . queryByText ( 'Duplicate JSON key detected' ) ,
340
+ ) . not . toBeInTheDocument ( )
341
+ } )
342
+
343
+ it ( 'should close ConfirmDialog without calling action when cancel is clicked on edit with modifications' , async ( ) => {
344
+ const fetchVisualisationResults = vi
345
+ . fn ( )
346
+ . mockReturnValue ( Promise . resolve ( { data : { } } ) )
347
+ const onJSONPropertyEdited = vi . fn ( )
348
+
349
+ render (
350
+ < RejsonObject
351
+ { ...instance ( mockedProps ) }
352
+ keyName = "keyName"
353
+ value = { constants . REJSON_DATA }
354
+ isDownloaded
355
+ handleFetchVisualisationResults = { fetchVisualisationResults }
356
+ handleSetRejsonDataAction = { onJSONPropertyEdited }
357
+ onJsonKeyExpandAndCollapse = { vi . fn ( ) }
358
+ /> ,
359
+ )
360
+
361
+ await act ( async ( ) => {
362
+ fireEvent . click ( screen . getByTestId ( EDIT_OBJECT_BTN ) )
363
+ } )
364
+
365
+ fireEvent . input ( screen . getByTestId ( JSON_VALUE ) , {
366
+ target : { value : '{}' } ,
367
+ } )
368
+
369
+ await act ( async ( ) => {
370
+ fireEvent . click ( screen . getByTestId ( 'apply-edit-btn' ) )
371
+ } )
372
+
373
+ expect ( screen . getByText ( 'Duplicate JSON key detected' ) ) . toBeInTheDocument ( )
374
+
375
+ await act ( async ( ) => {
376
+ fireEvent . click ( screen . getByTestId ( 'cancel-btn' ) )
377
+ } )
378
+
379
+ expect ( onJSONPropertyEdited ) . not . toBeCalled ( )
380
+
381
+ expect (
382
+ screen . queryByText ( 'Duplicate JSON key detected' ) ,
383
+ ) . not . toBeInTheDocument ( )
384
+ } )
385
+
386
+ it ( 'should apply new value for existing key after confirming in ConfirmDialog' , async ( ) => {
387
+ vi . spyOn ( tbdUtils , 'checkExistingPath' ) . mockReturnValue ( true )
388
+ vi . spyOn ( store , 'useRejsonStore' ) . mockReturnValue ( {
389
+ fullValue : JSON . stringify ( { existingKey : 1 } ) ,
390
+ } )
391
+
392
+ const onSetRejsonDataAction = vi . fn ( )
393
+
394
+ render (
395
+ < RejsonObject
396
+ { ...instance ( mockedProps ) }
397
+ keyName = "keyName"
398
+ type = { ObjectTypes . Object }
399
+ value = { constants . REJSON_DATA }
400
+ isDownloaded
401
+ handleSetRejsonDataAction = { onSetRejsonDataAction }
402
+ onJsonKeyExpandAndCollapse = { vi . fn ( ) }
403
+ /> ,
404
+ )
405
+
406
+ await act ( async ( ) => {
407
+ fireEvent . click ( screen . getByTestId ( EXPAND_OBJECT ) )
408
+ } )
409
+
410
+ await act ( async ( ) => {
411
+ fireEvent . click ( screen . getByTestId ( 'add-field-btn' ) )
412
+ } )
413
+
414
+ fireEvent . input ( screen . getByTestId ( JSON_KEY ) , {
415
+ target : { value : '"existingKey"' } ,
416
+ } )
417
+
418
+ fireEvent . input ( screen . getByTestId ( JSON_VALUE ) , {
419
+ target : { value : '{}' } ,
420
+ } )
421
+
422
+ await act ( async ( ) => {
423
+ fireEvent . click ( screen . getByTestId ( 'apply-btn' ) )
424
+ } )
425
+
426
+ expect ( screen . getByText ( 'Duplicate JSON key detected' ) ) . toBeInTheDocument ( )
427
+
428
+ await act ( async ( ) => {
429
+ fireEvent . click ( screen . getByTestId ( 'confirm-btn' ) )
430
+ } )
431
+
432
+ expect ( onSetRejsonDataAction ) . toBeCalled ( )
433
+
434
+ expect (
435
+ screen . queryByText ( 'Duplicate JSON key detected' ) ,
436
+ ) . not . toBeInTheDocument ( )
296
437
} )
297
438
} )
0 commit comments