Skip to content

Commit 97cacf2

Browse files
committed
add tests
1 parent 04b4e61 commit 97cacf2

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-1
lines changed

src/webviews/src/modules/key-details/components/rejson-details/rejson-object/RejsonObject.spec.tsx

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { instance, mock } from 'ts-mockito'
33
import { fireEvent, render, screen, act, waitForStack, constants } from 'testSrc/helpers'
44

55
import { RejsonObject } from './RejsonObject'
6+
import * as tbdUtils from './tbd'
7+
import * as store from '../hooks/useRejsonStore'
68
import { JSONObjectProps, ObjectTypes } from '../interfaces'
79

810
const EXPAND_OBJECT = 'expand-object'
@@ -284,6 +286,39 @@ describe('JSONObject', () => {
284286
fireEvent.click(screen.getByTestId(EDIT_OBJECT_BTN))
285287
})
286288

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+
287322
fireEvent.input(screen.getByTestId(JSON_VALUE), {
288323
target: { value: '{}' },
289324
})
@@ -292,6 +327,112 @@ describe('JSONObject', () => {
292327
fireEvent.click(screen.getByTestId('apply-edit-btn'))
293328
})
294329

330+
expect(screen.getByText('Duplicate JSON key detected')).toBeInTheDocument()
331+
332+
await act(async () => {
333+
fireEvent.click(screen.getByTestId('confirm-btn'))
334+
})
335+
295336
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()
296437
})
297438
})

src/webviews/src/modules/key-details/components/rejson-details/rejson-object/RejsonObject.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export const RejsonObject = React.memo((props: JSONObjectProps) => {
103103

104104
if (isKeyExisting) {
105105
setConfirmDialogAction(() => () => {
106+
setIsConfirmVisible(false)
106107
setAddNewKeyValuePair(false)
107108

108109
if (updatedPath) {
@@ -121,8 +122,9 @@ export const RejsonObject = React.memo((props: JSONObjectProps) => {
121122
}
122123

123124
const handleUpdateValueFormSubmit = (updatedValue: string) => {
124-
if (hasModifications(value, updatedValue)) {
125+
if (hasModifications(value, JSONParse(updatedValue))) {
125126
setConfirmDialogAction(() => () => {
127+
setIsConfirmVisible(false)
126128
setEditEntireObject(false)
127129
handleSetRejsonDataAction(selectedKey, path, updatedValue as string)
128130
})

0 commit comments

Comments
 (0)