Skip to content

Commit 03101ea

Browse files
add tests
1 parent 24cf0cb commit 03101ea

File tree

12 files changed

+214
-131
lines changed

12 files changed

+214
-131
lines changed

redisinsight/ui/src/pages/browser/modules/key-details/components/rejson-details/RejsonDetailsWrapper.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { RejsonDetailsWrapper, Props } from './RejsonDetailsWrapper'
55

66
const mockedProps = mock<Props>()
77

8-
describe('ReJSONDetailsWrapper', () => {
8+
describe('RejsonDetailsWrapper', () => {
99
it('should render', () => {
1010
expect(render(<RejsonDetailsWrapper {...instance(mockedProps)} />)).toBeTruthy()
1111
})

redisinsight/ui/src/pages/browser/modules/key-details/components/rejson-details/rejson-array/RejsonArray.spec.tsx

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import React from 'react'
22
import { instance, mock } from 'ts-mockito'
33
import { act, fireEvent, render, screen } from 'uiSrc/utils/test-utils'
4-
import { fetchVisualisationResults } from 'uiSrc/slices/browser/rejson'
5-
import JSONArray, { Props } from './JSONArray'
4+
import {
5+
JSONArrayProps
6+
} from 'uiSrc/pages/browser/modules/key-details/components/rejson-details/interfaces'
7+
import RejsonArray from './RejsonArray'
68

79
const EXPAND_ARRAY = 'expand-array'
810
const JSON_ARRAY_DOT = '.jsonValue'
911
const JSON_ARRAY = 'json-value'
1012
const BTN_EDIT_FIELD = 'btn-edit-field'
1113

12-
const mockedProps = mock<Props>()
14+
const mockedProps = mock<JSONArrayProps>()
1315

1416
const mockedDownloadedSimpleArray = [1, 2, 3]
1517

@@ -35,7 +37,7 @@ jest.mock('uiSrc/slices/browser/rejson', () => ({
3537

3638
describe('JSONArray', () => {
3739
it('should render simple JSON', () => {
38-
expect(render(<JSONArray
40+
expect(render(<RejsonArray
3941
{...instance(mockedProps)}
4042
keyName="keyName"
4143
shouldRejsonDataBeDownloaded={false}
@@ -44,12 +46,12 @@ describe('JSONArray', () => {
4446
})
4547

4648
it('should expand simple downloaded JSON', async () => {
47-
const { container } = render(<JSONArray
49+
const { container } = render(<RejsonArray
4850
{...instance(mockedProps)}
4951
keyName="keyName"
5052
value={mockedDownloadedSimpleArray}
5153
shouldRejsonDataBeDownloaded={false}
52-
onJSONKeyExpandAndCollapse={jest.fn()}
54+
onJsonKeyExpandAndCollapse={jest.fn()}
5355
/>)
5456

5557
await act(async () => {
@@ -60,12 +62,12 @@ describe('JSONArray', () => {
6062
})
6163

6264
it('should render and expand downloaded JSON with objects', async () => {
63-
const { container } = render(<JSONArray
65+
const { container } = render(<RejsonArray
6466
{...instance(mockedProps)}
6567
keyName="keyName"
6668
value={mockedDownloadedArrayWithObjects}
6769
shouldRejsonDataBeDownloaded={false}
68-
onJSONKeyExpandAndCollapse={jest.fn()}
70+
onJsonKeyExpandAndCollapse={jest.fn()}
6971
/>)
7072

7173
await act(async () => {
@@ -80,12 +82,12 @@ describe('JSONArray', () => {
8082
})
8183

8284
it('should render and expand downloaded JSON with arrays', async () => {
83-
const { container } = render(<JSONArray
85+
const { container } = render(<RejsonArray
8486
{...instance(mockedProps)}
8587
keyName="keyName"
8688
value={mockedDownloadedArrayWithArrays}
8789
shouldRejsonDataBeDownloaded={false}
88-
onJSONKeyExpandAndCollapse={jest.fn()}
90+
onJsonKeyExpandAndCollapse={jest.fn()}
8991
/>)
9092

9193
await act(async () => {
@@ -100,15 +102,16 @@ describe('JSONArray', () => {
100102
})
101103

102104
it('should render simple not downloaded JSON', async () => {
103-
fetchVisualisationResults.mockImplementation(() => jest.fn().mockReturnValue(
105+
const fetchVisualisationResults = jest.fn().mockReturnValue(
104106
Promise.resolve({ data: mockedDownloadedSimpleArray })
105-
))
106-
const { container } = render(<JSONArray
107+
)
108+
const { container } = render(<RejsonArray
107109
{...instance(mockedProps)}
108110
keyName="keyName"
109111
value={mockedDownloadedSimpleArray}
110112
shouldRejsonDataBeDownloaded
111-
onJSONKeyExpandAndCollapse={jest.fn()}
113+
onJsonKeyExpandAndCollapse={jest.fn()}
114+
handleFetchVisualisationResults={fetchVisualisationResults}
112115
/>)
113116

114117
await act(async () => {
@@ -119,15 +122,16 @@ describe('JSONArray', () => {
119122
})
120123

121124
it('should render not downloaded JSON with arrays', async () => {
122-
fetchVisualisationResults.mockImplementation(() => jest.fn().mockReturnValue(
125+
const fetchVisualisationResults = jest.fn().mockReturnValue(
123126
Promise.resolve({ data: mockedDownloadedArrayWithArrays })
124-
))
125-
const { container } = render(<JSONArray
127+
)
128+
const { container } = render(<RejsonArray
126129
{...instance(mockedProps)}
127130
keyName="keyName"
128131
value={mockedDownloadedSimpleArray}
129132
shouldRejsonDataBeDownloaded
130-
onJSONKeyExpandAndCollapse={jest.fn()}
133+
onJsonKeyExpandAndCollapse={jest.fn()}
134+
handleFetchVisualisationResults={fetchVisualisationResults}
131135
/>)
132136

133137
await act(async () => {
@@ -138,15 +142,16 @@ describe('JSONArray', () => {
138142
})
139143

140144
it('should render not downloaded JSON with objects', async () => {
141-
fetchVisualisationResults.mockImplementation(() => jest.fn().mockReturnValue(
145+
const fetchVisualisationResults = jest.fn().mockReturnValue(
142146
Promise.resolve({ data: mockedDownloadedArrayWithObjects })
143-
))
144-
const { container } = render(<JSONArray
147+
)
148+
const { container } = render(<RejsonArray
145149
{...instance(mockedProps)}
146150
keyName="keyName"
147151
value={mockedDownloadedSimpleArray}
148152
shouldRejsonDataBeDownloaded
149-
onJSONKeyExpandAndCollapse={jest.fn()}
153+
onJsonKeyExpandAndCollapse={jest.fn()}
154+
handleFetchVisualisationResults={fetchVisualisationResults}
150155
/>)
151156

152157
await act(async () => {
@@ -157,12 +162,12 @@ describe('JSONArray', () => {
157162
})
158163

159164
it('should render inline editor to add', async () => {
160-
render(<JSONArray
165+
render(<RejsonArray
161166
{...instance(mockedProps)}
162167
keyName="keyName"
163168
value={mockedDownloadedSimpleArray}
164169
shouldRejsonDataBeDownloaded={false}
165-
onJSONKeyExpandAndCollapse={jest.fn()}
170+
onJsonKeyExpandAndCollapse={jest.fn()}
166171
/>)
167172

168173
await act(async () => {
@@ -177,15 +182,16 @@ describe('JSONArray', () => {
177182
})
178183

179184
it('should not be able to add value with wrong json', async () => {
180-
const onJSONPropertyAdded = jest.fn()
181-
render(<JSONArray
185+
const handleSubmitJsonUpdateValue = jest.fn()
186+
const handleAppendRejsonArrayItemAction = jest.fn()
187+
render(<RejsonArray
182188
{...instance(mockedProps)}
183189
keyName="keyName"
184190
value={mockedDownloadedSimpleArray}
185191
shouldRejsonDataBeDownloaded={false}
186-
onJSONKeyExpandAndCollapse={jest.fn()}
187-
handleSubmitJsonUpdateValue={jest.fn()}
188-
onJSONPropertyAdded={onJSONPropertyAdded}
192+
onJsonKeyExpandAndCollapse={jest.fn()}
193+
handleSubmitJsonUpdateValue={handleSubmitJsonUpdateValue}
194+
handleAppendRejsonArrayItemAction={handleAppendRejsonArrayItemAction}
189195
/>)
190196

191197
await act(async () => {
@@ -204,19 +210,21 @@ describe('JSONArray', () => {
204210
await fireEvent.click(screen.getByTestId('apply-btn'))
205211
})
206212

207-
expect(onJSONPropertyAdded).not.toBeCalled()
213+
expect(handleSubmitJsonUpdateValue).not.toBeCalled()
214+
expect(handleAppendRejsonArrayItemAction).not.toBeCalled()
208215
})
209216

210217
it('should apply proper value to add element in array', async () => {
211-
const onJSONPropertyAdded = jest.fn()
212-
render(<JSONArray
218+
const handleSubmitJsonUpdateValue = jest.fn()
219+
const handleAppendRejsonArrayItemAction = jest.fn()
220+
render(<RejsonArray
213221
{...instance(mockedProps)}
214222
keyName="keyName"
215223
value={mockedDownloadedSimpleArray}
216224
shouldRejsonDataBeDownloaded={false}
217-
onJSONKeyExpandAndCollapse={jest.fn()}
218-
handleSubmitJsonUpdateValue={jest.fn()}
219-
onJSONPropertyAdded={onJSONPropertyAdded}
225+
onJsonKeyExpandAndCollapse={jest.fn()}
226+
handleSubmitJsonUpdateValue={handleSubmitJsonUpdateValue}
227+
handleAppendRejsonArrayItemAction={handleAppendRejsonArrayItemAction}
220228
/>)
221229

222230
await act(async () => {
@@ -235,16 +243,17 @@ describe('JSONArray', () => {
235243
await fireEvent.click(screen.getByTestId('apply-btn'))
236244
})
237245

238-
expect(onJSONPropertyAdded).toBeCalled()
246+
expect(handleSubmitJsonUpdateValue).toBeCalled()
247+
expect(handleAppendRejsonArrayItemAction).toBeCalled()
239248
})
240249

241250
it('should render inline editor to edit value', async () => {
242-
render(<JSONArray
251+
render(<RejsonArray
243252
{...instance(mockedProps)}
244253
keyName="keyName"
245254
value={mockedDownloadedSimpleArray}
246255
shouldRejsonDataBeDownloaded={false}
247-
onJSONKeyExpandAndCollapse={jest.fn()}
256+
onJsonKeyExpandAndCollapse={jest.fn()}
248257
/>)
249258

250259
await act(async () => {
@@ -255,35 +264,34 @@ describe('JSONArray', () => {
255264
})
256265

257266
it('should change value when editing', async () => {
258-
render(<JSONArray
267+
render(<RejsonArray
259268
{...instance(mockedProps)}
260269
keyName="keyName"
261270
value={mockedDownloadedSimpleArray}
262271
shouldRejsonDataBeDownloaded={false}
263-
onJSONKeyExpandAndCollapse={jest.fn()}
272+
onJsonKeyExpandAndCollapse={jest.fn()}
264273
/>)
265274

266275
await act(async () => {
267276
await fireEvent.click(screen.getByTestId(BTN_EDIT_FIELD))
268277
})
269278

270279
fireEvent.change(screen.getByTestId(JSON_ARRAY), {
271-
target: { value: '{}' }
280+
target: { value: '[]' }
272281
})
273282

274-
expect(screen.getByTestId(JSON_ARRAY)).toHaveValue('{}')
283+
expect(screen.getByTestId(JSON_ARRAY)).toHaveValue('[]')
275284
})
276285

277286
it('should not be apply wrong value for edit', async () => {
278287
const onJSONPropertyEdited = jest.fn()
279-
render(<JSONArray
288+
render(<RejsonArray
280289
{...instance(mockedProps)}
281290
keyName="keyName"
282291
value={mockedDownloadedSimpleArray}
283292
shouldRejsonDataBeDownloaded={false}
284-
onJSONKeyExpandAndCollapse={jest.fn()}
293+
onJsonKeyExpandAndCollapse={jest.fn()}
285294
handleSubmitJsonUpdateValue={jest.fn()}
286-
onJSONPropertyEdited={onJSONPropertyEdited}
287295
/>)
288296

289297
await act(async () => {
@@ -303,14 +311,13 @@ describe('JSONArray', () => {
303311

304312
it('should apply proper value for edit', async () => {
305313
const onJSONPropertyEdited = jest.fn()
306-
render(<JSONArray
314+
render(<RejsonArray
307315
{...instance(mockedProps)}
308316
keyName="keyName"
309317
value={mockedDownloadedSimpleArray}
310318
shouldRejsonDataBeDownloaded={false}
311-
onJSONKeyExpandAndCollapse={jest.fn()}
319+
onJsonKeyExpandAndCollapse={jest.fn()}
312320
handleSubmitJsonUpdateValue={jest.fn()}
313-
onJSONPropertyEdited={onJSONPropertyEdited}
314321
/>)
315322

316323
await act(async () => {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react'
2+
import { instance, mock } from 'ts-mockito'
3+
import { fireEvent, render, screen } from 'uiSrc/utils/test-utils'
4+
import { AddItemFieldAction, Props } from './AddItemFieldAction'
5+
6+
const mockedProps = mock<Props>()
7+
8+
describe('AddItemFieldAction', () => {
9+
it('renders correctly with object type', () => {
10+
render(<AddItemFieldAction {...instance(mockedProps)} />)
11+
12+
expect(screen.getByTestId('add-field-btn')).toBeInTheDocument()
13+
expect(screen.getByLabelText('Add field')).toBeInTheDocument()
14+
})
15+
16+
it('triggers onClickSetKVPair when the button is clicked', () => {
17+
const onClickSetKVPair = jest.fn()
18+
render(<AddItemFieldAction
19+
{...instance(mockedProps)}
20+
onClickSetKVPair={onClickSetKVPair}
21+
/>)
22+
23+
fireEvent.click(screen.getByTestId('add-field-btn'))
24+
expect(onClickSetKVPair).toHaveBeenCalled()
25+
})
26+
})
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React from 'react'
2+
import { instance, mock } from 'ts-mockito'
3+
import { fireEvent, render, screen } from 'uiSrc/utils/test-utils'
4+
import { EditEntireItemAction, Props } from './EditEntireItemAction'
5+
6+
const mockedProps = mock<Props>()
7+
8+
const valueOfEntireItem = 'Sample JSON'
9+
10+
describe('EditEntireItemAction', () => {
11+
it('renders correctly with provided props', () => {
12+
render(<EditEntireItemAction
13+
{...instance(mockedProps)}
14+
valueOfEntireItem={valueOfEntireItem}
15+
/>)
16+
17+
expect(screen.getByTestId('json-value')).toBeInTheDocument()
18+
expect(screen.getByTestId('json-value')).toHaveValue(valueOfEntireItem)
19+
})
20+
21+
it('triggers handleUpdateValueFormSubmit when the form is submitted', () => {
22+
const handleUpdateValueFormSubmit = jest.fn()
23+
render(<EditEntireItemAction
24+
{...instance(mockedProps)}
25+
handleUpdateValueFormSubmit={handleUpdateValueFormSubmit}
26+
/>)
27+
28+
fireEvent.submit(screen.getByTestId('json-entire-form'))
29+
expect(handleUpdateValueFormSubmit).toHaveBeenCalled()
30+
})
31+
32+
it('triggers setEditEntireItem and setError when the cancel button is clicked', () => {
33+
const setEditEntireItem = jest.fn()
34+
const setError = jest.fn()
35+
const { getByLabelText } = render(<EditEntireItemAction
36+
{...instance(mockedProps)}
37+
setEditEntireItem={setEditEntireItem}
38+
setError={setError}
39+
/>)
40+
fireEvent.click(getByLabelText('Cancel add'))
41+
42+
expect(setEditEntireItem).toHaveBeenCalledWith(false)
43+
expect(setError).toHaveBeenCalledWith(null)
44+
})
45+
})

redisinsight/ui/src/pages/browser/modules/key-details/components/rejson-details/rejson-details-actions/edit-entire-item-action/EditEntireItemAction.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const EditEntireItemAction = ({
4646
component="form"
4747
className="relative"
4848
onSubmit={(e) => handleUpdateValueFormSubmit(e)}
49+
data-testid="json-entire-form"
4950
noValidate
5051
>
5152
<EuiFlexItem grow component="span">

0 commit comments

Comments
 (0)