1
1
import React from "react" ;
2
2
import { format } from "date-fns" ;
3
- import DatePicker from "react-datepicker" ;
4
- import Select from "react-select" ;
5
3
6
4
import * as Generator from "../environment/Generator" ;
7
5
import Answer from "../../src/components/Answer" ;
8
6
import Constants from "../../src/constants/Constants" ;
9
- import TypeaheadAnswer from "../../src/components/answer/TypeaheadAnswer" ;
10
- import MaskedInput from "../../src/components/MaskedInput" ;
11
7
import { FormGenContext } from "../../src/contexts/FormGenContext" ;
12
8
import { ConfigurationContext } from "../../src/contexts/ConfigurationContext" ;
13
9
import DefaultInput from "../../src/components/DefaultInput" ;
10
+ import { fireEvent , render , screen , waitFor } from "@testing-library/react" ;
11
+ import "@testing-library/jest-dom" ;
14
12
15
13
describe ( "Answer component" , ( ) => {
16
14
let question ,
@@ -51,13 +49,17 @@ describe("Answer component", () => {
51
49
} ,
52
50
} ;
53
51
inputComponent = DefaultInput ;
52
+ answer = {
53
+ id : Generator . getRandomUri ( ) ,
54
+ } ;
55
+ question [ Constants . HAS_ANSWER ] = [ answer ] ;
54
56
getOptions = jest . fn ( ( ) => [ ] ) ;
55
57
loadFormOptions = jest . fn ( ) ;
56
58
} ) ;
57
59
58
- it ( "renders a Typeahead when layout class is typeahead" , ( ) => {
60
+ test ( "renders a Typeahead when layout class is typeahead" , async ( ) => {
59
61
question [ Constants . LAYOUT_CLASS ] . push ( Constants . LAYOUT . QUESTION_TYPEAHEAD ) ;
60
- const component = mount (
62
+ render (
61
63
< ConfigurationContext . Provider
62
64
value = { {
63
65
componentsOptions,
@@ -71,11 +73,13 @@ describe("Answer component", () => {
71
73
</ ConfigurationContext . Provider >
72
74
) ;
73
75
74
- const typeahead = component . find ( Select ) ;
75
- expect ( typeahead ) . not . toBeNull ( ) ;
76
+ await waitFor ( ( ) => {
77
+ const typeahead = screen . getByRole ( "combobox" ) ;
78
+ expect ( typeahead ) . toBeInTheDocument ( ) ;
79
+ } ) ;
76
80
} ) ;
77
81
78
- it ( "maps answer object value to string label for the typeahead component" , ( ) => {
82
+ it ( "maps answer object value to string label for the typeahead component" , async ( ) => {
79
83
const value = Generator . getRandomUri ( ) ;
80
84
const valueLabel = "masterchief" ;
81
85
const typeAheadOptions = Generator . generateTypeaheadOptions (
@@ -84,11 +88,12 @@ describe("Answer component", () => {
84
88
) ;
85
89
answer = answerWithCodeValue ( value ) ;
86
90
getOptions = jest . fn ( ( ) => typeAheadOptions ) ;
91
+
87
92
question [ Constants . HAS_ANSWER ] = [ answer ] ;
88
93
question [ Constants . LAYOUT_CLASS ] . push ( Constants . LAYOUT . QUESTION_TYPEAHEAD ) ;
89
94
question [ Constants . HAS_OPTIONS_QUERY ] = "SELECT * WHERE {?x ?y ?z. }" ;
90
95
91
- const component = mount (
96
+ render (
92
97
< ConfigurationContext . Provider
93
98
value = { {
94
99
componentsOptions,
@@ -104,20 +109,23 @@ describe("Answer component", () => {
104
109
</ ConfigurationContext . Provider >
105
110
) ;
106
111
107
- waitForComponentToPaint ( component ) ;
108
- const typeahead = component . find ( Answer ) . find ( TypeaheadAnswer ) . find ( Select ) ;
112
+ await waitFor ( async ( ) => {
113
+ const typeahead = screen . getByRole ( "combobox" ) ;
109
114
110
- expect ( typeahead ) . not . toBeNull ( ) ;
115
+ fireEvent . click ( typeahead ) ;
116
+ fireEvent . click ( screen . getByText ( valueLabel ) ) ;
111
117
112
- expect ( typeahead . state ( "value" ) [ 0 ] . name ) . toEqual ( valueLabel ) ;
118
+ expect ( typeahead ) . toBeInTheDocument ( ) ;
119
+ expect ( screen . getByText ( valueLabel ) ) . toBeInTheDocument ( ) ;
120
+ } ) ;
113
121
} ) ;
114
122
115
123
it ( "loads typeahead options when layout class is typeahead and no possible values are specified" , ( ) => {
116
124
question [ Constants . LAYOUT_CLASS ] . push ( Constants . LAYOUT . QUESTION_TYPEAHEAD ) ;
117
125
const query = "SELECT * WHERE { ?x ?y ?z .}" ;
118
126
question [ Constants . HAS_OPTIONS_QUERY ] = query ;
119
127
120
- const component = mount (
128
+ render (
121
129
< ConfigurationContext . Provider
122
130
value = { {
123
131
componentsOptions,
@@ -131,8 +139,6 @@ describe("Answer component", () => {
131
139
</ ConfigurationContext . Provider >
132
140
) ;
133
141
134
- waitForComponentToPaint ( component ) ;
135
-
136
142
expect ( loadFormOptions ) . toHaveBeenCalled ( ) ;
137
143
expect ( loadFormOptions . mock . calls [ 0 ] [ 1 ] ) . toEqual ( query ) ;
138
144
} ) ;
@@ -152,7 +158,7 @@ describe("Answer component", () => {
152
158
answer = answerWithTextValue ( value ) ;
153
159
question [ Constants . HAS_ANSWER ] = [ answer ] ;
154
160
155
- const component = mount (
161
+ render (
156
162
< ConfigurationContext . Provider
157
163
value = { {
158
164
componentsOptions,
@@ -163,11 +169,12 @@ describe("Answer component", () => {
163
169
< Answer answer = { answer } question = { question } onChange = { onChange } /> s
164
170
</ ConfigurationContext . Provider >
165
171
) ;
166
- const input = component . find ( "input" ) ;
172
+
173
+ const input = screen . getByRole ( "textbox" ) ;
167
174
168
175
expect ( input ) . not . toBeNull ( ) ;
169
- expect ( input . props ( ) . type ) . toEqual ( "text" ) ;
170
- expect ( input . props ( ) . value ) . toEqual ( value ) ;
176
+ expect ( input . type ) . toEqual ( "text" ) ;
177
+ expect ( input . value ) . toEqual ( value ) ;
171
178
} ) ;
172
179
173
180
function answerWithTextValue ( value ) {
@@ -189,7 +196,7 @@ describe("Answer component", () => {
189
196
question [ Constants . HAS_ANSWER ] = [ answer ] ;
190
197
question [ Constants . LAYOUT_CLASS ] . push ( Constants . LAYOUT . DATE ) ;
191
198
192
- const component = mount (
199
+ render (
193
200
< ConfigurationContext . Provider
194
201
value = { {
195
202
componentsOptions,
@@ -200,12 +207,12 @@ describe("Answer component", () => {
200
207
< Answer answer = { answer } question = { question } onChange = { onChange } />
201
208
</ ConfigurationContext . Provider >
202
209
) ;
203
- const picker = component . find ( DatePicker ) ;
210
+
211
+ const picker = screen . getByPlaceholderText ( "YYYY-MM-DD" ) ;
204
212
205
213
expect ( picker ) . not . toBeNull ( ) ;
206
- expect ( picker . props ( ) . showTimeSelect ) . toEqual ( false ) ;
207
- expect ( picker . props ( ) . showTimeSelectOnly ) . toEqual ( false ) ;
208
- expect ( picker . props ( ) . selected ) . toEqual ( date ) ;
214
+ expect ( screen . queryByLabelText ( "month 2000-01" ) ) . toBeNull ( ) ;
215
+ expect ( picker . value ) . toEqual ( format ( date , "yyyy-MM-dd" ) ) ;
209
216
} ) ;
210
217
211
218
it ( "renders time picker with answer value when time layout class is specified" , ( ) => {
@@ -216,7 +223,7 @@ describe("Answer component", () => {
216
223
question [ Constants . HAS_ANSWER ] = [ answer ] ;
217
224
question [ Constants . LAYOUT_CLASS ] . push ( Constants . LAYOUT . TIME ) ;
218
225
219
- const component = mount (
226
+ render (
220
227
< ConfigurationContext . Provider
221
228
value = { {
222
229
componentsOptions,
@@ -227,22 +234,23 @@ describe("Answer component", () => {
227
234
< Answer answer = { answer } question = { question } onChange = { onChange } />
228
235
</ ConfigurationContext . Provider >
229
236
) ;
230
- const picker = component . find ( DatePicker ) ;
237
+
238
+ const picker = screen . getByPlaceholderText ( "HH:MM:SS" ) ;
231
239
232
240
expect ( picker ) . not . toBeNull ( ) ;
233
- expect ( picker . props ( ) . showTimeSelect ) . toEqual ( true ) ;
234
- expect ( picker . props ( ) . showTimeSelectOnly ) . toEqual ( true ) ;
235
- expect ( picker . props ( ) . selected ) . toEqual ( new Date ( `0 ${ value } ` ) ) ;
241
+ expect ( screen . queryByText ( "Time" ) ) . toBeNull ( ) ;
242
+ expect ( picker . value ) . toEqual ( value ) ;
236
243
} ) ;
237
244
238
245
it ( "renders datetime picker with answer value when datetime layout class is specified" , ( ) => {
239
246
const date = new Date ( ) ;
247
+ const testDate = format ( date , "yyyy-MM" ) ;
240
248
const value = format ( date , "yyyy-MM-dd HH:mm:ss" ) ;
241
249
answer = answerWithTextValue ( value ) ;
242
250
question [ Constants . HAS_ANSWER ] = [ answer ] ;
243
251
question [ Constants . LAYOUT_CLASS ] . push ( Constants . LAYOUT . DATETIME ) ;
244
252
245
- const component = mount (
253
+ render (
246
254
< ConfigurationContext . Provider
247
255
value = { {
248
256
componentsOptions,
@@ -253,24 +261,27 @@ describe("Answer component", () => {
253
261
< Answer answer = { answer } question = { question } onChange = { onChange } />
254
262
</ ConfigurationContext . Provider >
255
263
) ;
256
- const picker = component . find ( DatePicker ) ;
264
+
265
+ const picker = screen . getByPlaceholderText ( "YYYY-MM-DD HH:MM:SS" ) ;
257
266
258
267
expect ( picker ) . not . toBeNull ( ) ;
259
- expect ( picker . props ( ) . showTimeSelect ) . toEqual ( true ) ;
260
- expect ( picker . props ( ) . showTimeSelectOnly ) . toEqual ( false ) ;
261
- expect ( picker . props ( ) . selected ) . toEqual ( new Date ( value ) ) ;
268
+ expect ( screen . queryByText ( "Time" ) ) . toBeNull ( ) ;
269
+ expect ( screen . queryByLabelText ( "month " + testDate ) ) . toBeNull ( ) ;
270
+ expect ( picker . value ) . toEqual ( value ) ;
262
271
} ) ;
263
272
264
273
it ( "renders datetime picker with answer value when no layout class is specified and numeric answer value is used" , ( ) => {
265
- const value = Number ( new Date ( ) ) ;
274
+ const date = new Date ( ) ;
275
+ const value = Number ( date ) ;
276
+ const testDate = format ( date , "yyyy-MM" ) ;
266
277
answer = {
267
278
"@id" : Generator . getRandomUri ( ) ,
268
279
} ;
269
280
answer [ Constants . HAS_DATA_VALUE ] = value ;
270
281
question [ Constants . HAS_ANSWER ] = [ answer ] ;
271
282
question [ Constants . LAYOUT_CLASS ] . push ( Constants . LAYOUT . DATETIME ) ;
272
283
273
- const component = mount (
284
+ render (
274
285
< ConfigurationContext . Provider
275
286
value = { {
276
287
componentsOptions,
@@ -281,12 +292,15 @@ describe("Answer component", () => {
281
292
< Answer answer = { answer } question = { question } onChange = { onChange } />
282
293
</ ConfigurationContext . Provider >
283
294
) ;
284
- const picker = component . find ( DatePicker ) ;
295
+
296
+ const picker = screen . getByPlaceholderText ( "YYYY-MM-DD HH:MM:SS" ) ;
285
297
286
298
expect ( picker ) . not . toBeNull ( ) ;
287
- expect ( picker . props ( ) . showTimeSelect ) . toEqual ( true ) ;
288
- expect ( picker . props ( ) . showTimeSelectOnly ) . toEqual ( false ) ;
289
- expect ( picker . props ( ) . selected ) . toEqual ( new Date ( value ) ) ;
299
+ expect ( screen . queryByText ( "Time" ) ) . toBeNull ( ) ;
300
+ expect ( screen . queryByLabelText ( "month " + testDate ) ) . toBeNull ( ) ;
301
+ expect ( picker . value ) . toEqual (
302
+ format ( new Date ( value ) , "yyyy-MM-dd HH:mm:ss" )
303
+ ) ;
290
304
} ) ;
291
305
292
306
it ( "renders checkbox with answer value when checkbox layout class is specified" , ( ) => {
@@ -297,7 +311,7 @@ describe("Answer component", () => {
297
311
question [ Constants . HAS_ANSWER ] = [ answer ] ;
298
312
question [ Constants . LAYOUT_CLASS ] . push ( Constants . LAYOUT . CHECKBOX ) ;
299
313
300
- const component = mount (
314
+ render (
301
315
< ConfigurationContext . Provider
302
316
value = { {
303
317
componentsOptions,
@@ -308,11 +322,10 @@ describe("Answer component", () => {
308
322
< Answer answer = { answer } question = { question } onChange = { onChange } />
309
323
</ ConfigurationContext . Provider >
310
324
) ;
311
- const input = component . find ( "input ") ;
325
+ const input = screen . getByRole ( "checkbox ") ;
312
326
313
327
expect ( input ) . toBeDefined ( ) ;
314
- expect ( input . props ( ) . type ) . toEqual ( "checkbox" ) ;
315
- expect ( input . props ( ) . checked ) . toBeTruthy ( ) ;
328
+ expect ( input . checked ) . toBeTruthy ( ) ;
316
329
} ) ;
317
330
318
331
it ( "renders numeric input with answer value when number layout class is specified" , ( ) => {
@@ -324,7 +337,7 @@ describe("Answer component", () => {
324
337
question [ Constants . HAS_ANSWER ] = [ answer ] ;
325
338
question [ Constants . HAS_DATATYPE ] = Constants . XSD . INT ;
326
339
327
- const component = mount (
340
+ render (
328
341
< ConfigurationContext . Provider
329
342
value = { {
330
343
componentsOptions,
@@ -335,11 +348,12 @@ describe("Answer component", () => {
335
348
< Answer answer = { answer } question = { question } onChange = { onChange } />
336
349
</ ConfigurationContext . Provider >
337
350
) ;
338
- const input = component . find ( "input" ) ;
351
+
352
+ // Spinbutton is aria role for input with restricted inputs
353
+ const input = screen . getByRole ( "spinbutton" ) ;
339
354
340
355
expect ( input ) . toBeDefined ( ) ;
341
- expect ( input . props ( ) . type ) . toEqual ( "number" ) ;
342
- expect ( input . props ( ) . value ) . toEqual ( value ) ;
356
+ expect ( Number ( input . value ) ) . toEqual ( value ) ;
343
357
} ) ;
344
358
345
359
it ( "renders textarea for answer with long value" , ( ) => {
@@ -351,7 +365,7 @@ describe("Answer component", () => {
351
365
answer [ Constants . HAS_DATA_VALUE ] = value ;
352
366
question [ Constants . HAS_ANSWER ] = [ answer ] ;
353
367
354
- const component = mount (
368
+ render (
355
369
< ConfigurationContext . Provider
356
370
value = { {
357
371
componentsOptions,
@@ -362,10 +376,10 @@ describe("Answer component", () => {
362
376
< Answer answer = { answer } question = { question } onChange = { onChange } />
363
377
</ ConfigurationContext . Provider >
364
378
) ;
365
- const input = component . find ( "textarea ") ;
379
+ const input = screen . getByRole ( "textbox ") ;
366
380
367
381
expect ( input ) . toBeDefined ( ) ;
368
- expect ( input . props ( ) . value ) . toEqual ( value ) ;
382
+ expect ( input . value ) . toEqual ( value ) ;
369
383
} ) ;
370
384
371
385
it ( "renders masked input for question with masked-input layout class" , ( ) => {
@@ -379,7 +393,7 @@ describe("Answer component", () => {
379
393
question [ Constants . LAYOUT_CLASS ] . push ( Constants . LAYOUT . MASKED_INPUT ) ;
380
394
question [ Constants . INPUT_MASK ] = mask ;
381
395
382
- const component = mount (
396
+ const container = render (
383
397
< ConfigurationContext . Provider
384
398
value = { {
385
399
componentsOptions,
@@ -390,10 +404,10 @@ describe("Answer component", () => {
390
404
< Answer answer = { answer } question = { question } onChange = { onChange } />
391
405
</ ConfigurationContext . Provider >
392
406
) ;
393
- const input = component . find ( MaskedInput ) ;
407
+ const input = screen . getByRole ( "textbox" ) ;
394
408
395
409
expect ( input ) . toBeDefined ( ) ;
396
- expect ( input . props ( ) . value ) . toEqual ( value ) ;
397
- expect ( input . props ( ) . mask ) . toEqual ( mask ) ;
410
+ expect ( input . value ) . toEqual ( value ) ;
411
+ expect ( document . querySelector ( "input" ) . getAttribute ( " mask" ) ) . toEqual ( mask ) ;
398
412
} ) ;
399
413
} ) ;
0 commit comments