1
1
import React from 'react'
2
2
import { instance , mock } from 'ts-mockito'
3
3
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'
6
8
7
9
const EXPAND_ARRAY = 'expand-array'
8
10
const JSON_ARRAY_DOT = '.jsonValue'
9
11
const JSON_ARRAY = 'json-value'
10
12
const BTN_EDIT_FIELD = 'btn-edit-field'
11
13
12
- const mockedProps = mock < Props > ( )
14
+ const mockedProps = mock < JSONArrayProps > ( )
13
15
14
16
const mockedDownloadedSimpleArray = [ 1 , 2 , 3 ]
15
17
@@ -35,7 +37,7 @@ jest.mock('uiSrc/slices/browser/rejson', () => ({
35
37
36
38
describe ( 'JSONArray' , ( ) => {
37
39
it ( 'should render simple JSON' , ( ) => {
38
- expect ( render ( < JSONArray
40
+ expect ( render ( < RejsonArray
39
41
{ ...instance ( mockedProps ) }
40
42
keyName = "keyName"
41
43
shouldRejsonDataBeDownloaded = { false }
@@ -44,12 +46,12 @@ describe('JSONArray', () => {
44
46
} )
45
47
46
48
it ( 'should expand simple downloaded JSON' , async ( ) => {
47
- const { container } = render ( < JSONArray
49
+ const { container } = render ( < RejsonArray
48
50
{ ...instance ( mockedProps ) }
49
51
keyName = "keyName"
50
52
value = { mockedDownloadedSimpleArray }
51
53
shouldRejsonDataBeDownloaded = { false }
52
- onJSONKeyExpandAndCollapse = { jest . fn ( ) }
54
+ onJsonKeyExpandAndCollapse = { jest . fn ( ) }
53
55
/> )
54
56
55
57
await act ( async ( ) => {
@@ -60,12 +62,12 @@ describe('JSONArray', () => {
60
62
} )
61
63
62
64
it ( 'should render and expand downloaded JSON with objects' , async ( ) => {
63
- const { container } = render ( < JSONArray
65
+ const { container } = render ( < RejsonArray
64
66
{ ...instance ( mockedProps ) }
65
67
keyName = "keyName"
66
68
value = { mockedDownloadedArrayWithObjects }
67
69
shouldRejsonDataBeDownloaded = { false }
68
- onJSONKeyExpandAndCollapse = { jest . fn ( ) }
70
+ onJsonKeyExpandAndCollapse = { jest . fn ( ) }
69
71
/> )
70
72
71
73
await act ( async ( ) => {
@@ -80,12 +82,12 @@ describe('JSONArray', () => {
80
82
} )
81
83
82
84
it ( 'should render and expand downloaded JSON with arrays' , async ( ) => {
83
- const { container } = render ( < JSONArray
85
+ const { container } = render ( < RejsonArray
84
86
{ ...instance ( mockedProps ) }
85
87
keyName = "keyName"
86
88
value = { mockedDownloadedArrayWithArrays }
87
89
shouldRejsonDataBeDownloaded = { false }
88
- onJSONKeyExpandAndCollapse = { jest . fn ( ) }
90
+ onJsonKeyExpandAndCollapse = { jest . fn ( ) }
89
91
/> )
90
92
91
93
await act ( async ( ) => {
@@ -100,15 +102,16 @@ describe('JSONArray', () => {
100
102
} )
101
103
102
104
it ( 'should render simple not downloaded JSON' , async ( ) => {
103
- fetchVisualisationResults . mockImplementation ( ( ) => jest . fn ( ) . mockReturnValue (
105
+ const fetchVisualisationResults = jest . fn ( ) . mockReturnValue (
104
106
Promise . resolve ( { data : mockedDownloadedSimpleArray } )
105
- ) )
106
- const { container } = render ( < JSONArray
107
+ )
108
+ const { container } = render ( < RejsonArray
107
109
{ ...instance ( mockedProps ) }
108
110
keyName = "keyName"
109
111
value = { mockedDownloadedSimpleArray }
110
112
shouldRejsonDataBeDownloaded
111
- onJSONKeyExpandAndCollapse = { jest . fn ( ) }
113
+ onJsonKeyExpandAndCollapse = { jest . fn ( ) }
114
+ handleFetchVisualisationResults = { fetchVisualisationResults }
112
115
/> )
113
116
114
117
await act ( async ( ) => {
@@ -119,15 +122,16 @@ describe('JSONArray', () => {
119
122
} )
120
123
121
124
it ( 'should render not downloaded JSON with arrays' , async ( ) => {
122
- fetchVisualisationResults . mockImplementation ( ( ) => jest . fn ( ) . mockReturnValue (
125
+ const fetchVisualisationResults = jest . fn ( ) . mockReturnValue (
123
126
Promise . resolve ( { data : mockedDownloadedArrayWithArrays } )
124
- ) )
125
- const { container } = render ( < JSONArray
127
+ )
128
+ const { container } = render ( < RejsonArray
126
129
{ ...instance ( mockedProps ) }
127
130
keyName = "keyName"
128
131
value = { mockedDownloadedSimpleArray }
129
132
shouldRejsonDataBeDownloaded
130
- onJSONKeyExpandAndCollapse = { jest . fn ( ) }
133
+ onJsonKeyExpandAndCollapse = { jest . fn ( ) }
134
+ handleFetchVisualisationResults = { fetchVisualisationResults }
131
135
/> )
132
136
133
137
await act ( async ( ) => {
@@ -138,15 +142,16 @@ describe('JSONArray', () => {
138
142
} )
139
143
140
144
it ( 'should render not downloaded JSON with objects' , async ( ) => {
141
- fetchVisualisationResults . mockImplementation ( ( ) => jest . fn ( ) . mockReturnValue (
145
+ const fetchVisualisationResults = jest . fn ( ) . mockReturnValue (
142
146
Promise . resolve ( { data : mockedDownloadedArrayWithObjects } )
143
- ) )
144
- const { container } = render ( < JSONArray
147
+ )
148
+ const { container } = render ( < RejsonArray
145
149
{ ...instance ( mockedProps ) }
146
150
keyName = "keyName"
147
151
value = { mockedDownloadedSimpleArray }
148
152
shouldRejsonDataBeDownloaded
149
- onJSONKeyExpandAndCollapse = { jest . fn ( ) }
153
+ onJsonKeyExpandAndCollapse = { jest . fn ( ) }
154
+ handleFetchVisualisationResults = { fetchVisualisationResults }
150
155
/> )
151
156
152
157
await act ( async ( ) => {
@@ -157,12 +162,12 @@ describe('JSONArray', () => {
157
162
} )
158
163
159
164
it ( 'should render inline editor to add' , async ( ) => {
160
- render ( < JSONArray
165
+ render ( < RejsonArray
161
166
{ ...instance ( mockedProps ) }
162
167
keyName = "keyName"
163
168
value = { mockedDownloadedSimpleArray }
164
169
shouldRejsonDataBeDownloaded = { false }
165
- onJSONKeyExpandAndCollapse = { jest . fn ( ) }
170
+ onJsonKeyExpandAndCollapse = { jest . fn ( ) }
166
171
/> )
167
172
168
173
await act ( async ( ) => {
@@ -177,15 +182,16 @@ describe('JSONArray', () => {
177
182
} )
178
183
179
184
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
182
188
{ ...instance ( mockedProps ) }
183
189
keyName = "keyName"
184
190
value = { mockedDownloadedSimpleArray }
185
191
shouldRejsonDataBeDownloaded = { false }
186
- onJSONKeyExpandAndCollapse = { jest . fn ( ) }
187
- handleSubmitJsonUpdateValue = { jest . fn ( ) }
188
- onJSONPropertyAdded = { onJSONPropertyAdded }
192
+ onJsonKeyExpandAndCollapse = { jest . fn ( ) }
193
+ handleSubmitJsonUpdateValue = { handleSubmitJsonUpdateValue }
194
+ handleAppendRejsonArrayItemAction = { handleAppendRejsonArrayItemAction }
189
195
/> )
190
196
191
197
await act ( async ( ) => {
@@ -204,19 +210,21 @@ describe('JSONArray', () => {
204
210
await fireEvent . click ( screen . getByTestId ( 'apply-btn' ) )
205
211
} )
206
212
207
- expect ( onJSONPropertyAdded ) . not . toBeCalled ( )
213
+ expect ( handleSubmitJsonUpdateValue ) . not . toBeCalled ( )
214
+ expect ( handleAppendRejsonArrayItemAction ) . not . toBeCalled ( )
208
215
} )
209
216
210
217
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
213
221
{ ...instance ( mockedProps ) }
214
222
keyName = "keyName"
215
223
value = { mockedDownloadedSimpleArray }
216
224
shouldRejsonDataBeDownloaded = { false }
217
- onJSONKeyExpandAndCollapse = { jest . fn ( ) }
218
- handleSubmitJsonUpdateValue = { jest . fn ( ) }
219
- onJSONPropertyAdded = { onJSONPropertyAdded }
225
+ onJsonKeyExpandAndCollapse = { jest . fn ( ) }
226
+ handleSubmitJsonUpdateValue = { handleSubmitJsonUpdateValue }
227
+ handleAppendRejsonArrayItemAction = { handleAppendRejsonArrayItemAction }
220
228
/> )
221
229
222
230
await act ( async ( ) => {
@@ -235,16 +243,17 @@ describe('JSONArray', () => {
235
243
await fireEvent . click ( screen . getByTestId ( 'apply-btn' ) )
236
244
} )
237
245
238
- expect ( onJSONPropertyAdded ) . toBeCalled ( )
246
+ expect ( handleSubmitJsonUpdateValue ) . toBeCalled ( )
247
+ expect ( handleAppendRejsonArrayItemAction ) . toBeCalled ( )
239
248
} )
240
249
241
250
it ( 'should render inline editor to edit value' , async ( ) => {
242
- render ( < JSONArray
251
+ render ( < RejsonArray
243
252
{ ...instance ( mockedProps ) }
244
253
keyName = "keyName"
245
254
value = { mockedDownloadedSimpleArray }
246
255
shouldRejsonDataBeDownloaded = { false }
247
- onJSONKeyExpandAndCollapse = { jest . fn ( ) }
256
+ onJsonKeyExpandAndCollapse = { jest . fn ( ) }
248
257
/> )
249
258
250
259
await act ( async ( ) => {
@@ -255,35 +264,34 @@ describe('JSONArray', () => {
255
264
} )
256
265
257
266
it ( 'should change value when editing' , async ( ) => {
258
- render ( < JSONArray
267
+ render ( < RejsonArray
259
268
{ ...instance ( mockedProps ) }
260
269
keyName = "keyName"
261
270
value = { mockedDownloadedSimpleArray }
262
271
shouldRejsonDataBeDownloaded = { false }
263
- onJSONKeyExpandAndCollapse = { jest . fn ( ) }
272
+ onJsonKeyExpandAndCollapse = { jest . fn ( ) }
264
273
/> )
265
274
266
275
await act ( async ( ) => {
267
276
await fireEvent . click ( screen . getByTestId ( BTN_EDIT_FIELD ) )
268
277
} )
269
278
270
279
fireEvent . change ( screen . getByTestId ( JSON_ARRAY ) , {
271
- target : { value : '{} ' }
280
+ target : { value : '[] ' }
272
281
} )
273
282
274
- expect ( screen . getByTestId ( JSON_ARRAY ) ) . toHaveValue ( '{} ' )
283
+ expect ( screen . getByTestId ( JSON_ARRAY ) ) . toHaveValue ( '[] ' )
275
284
} )
276
285
277
286
it ( 'should not be apply wrong value for edit' , async ( ) => {
278
287
const onJSONPropertyEdited = jest . fn ( )
279
- render ( < JSONArray
288
+ render ( < RejsonArray
280
289
{ ...instance ( mockedProps ) }
281
290
keyName = "keyName"
282
291
value = { mockedDownloadedSimpleArray }
283
292
shouldRejsonDataBeDownloaded = { false }
284
- onJSONKeyExpandAndCollapse = { jest . fn ( ) }
293
+ onJsonKeyExpandAndCollapse = { jest . fn ( ) }
285
294
handleSubmitJsonUpdateValue = { jest . fn ( ) }
286
- onJSONPropertyEdited = { onJSONPropertyEdited }
287
295
/> )
288
296
289
297
await act ( async ( ) => {
@@ -303,14 +311,13 @@ describe('JSONArray', () => {
303
311
304
312
it ( 'should apply proper value for edit' , async ( ) => {
305
313
const onJSONPropertyEdited = jest . fn ( )
306
- render ( < JSONArray
314
+ render ( < RejsonArray
307
315
{ ...instance ( mockedProps ) }
308
316
keyName = "keyName"
309
317
value = { mockedDownloadedSimpleArray }
310
318
shouldRejsonDataBeDownloaded = { false }
311
- onJSONKeyExpandAndCollapse = { jest . fn ( ) }
319
+ onJsonKeyExpandAndCollapse = { jest . fn ( ) }
312
320
handleSubmitJsonUpdateValue = { jest . fn ( ) }
313
- onJSONPropertyEdited = { onJSONPropertyEdited }
314
321
/> )
315
322
316
323
await act ( async ( ) => {
0 commit comments