1
1
import * as React from 'react' ;
2
- import {
3
- findByText ,
4
- fireEvent ,
5
- render ,
6
- screen ,
7
- waitFor ,
8
- } from '@testing-library/react' ;
2
+ import { fireEvent , render , screen , waitFor } from '@testing-library/react' ;
9
3
import {
10
4
required ,
11
5
testDataProvider ,
@@ -73,7 +67,7 @@ describe('<SelectInput />', () => {
73
67
) . toEqual ( 'rea' ) ;
74
68
} ) ;
75
69
76
- it ( 'should render disable choices marked so' , ( ) => {
70
+ it ( 'should render disabled choices marked so' , ( ) => {
77
71
render (
78
72
< AdminContext dataProvider = { testDataProvider ( ) } >
79
73
< SimpleForm onSubmit = { jest . fn ( ) } >
@@ -98,6 +92,34 @@ describe('<SelectInput />', () => {
98
92
screen . getByText ( 'React' ) . getAttribute ( 'aria-disabled' )
99
93
) . toEqual ( 'true' ) ;
100
94
} ) ;
95
+
96
+ it ( 'should include an empty option by default' , ( ) => {
97
+ render (
98
+ < AdminContext dataProvider = { testDataProvider ( ) } >
99
+ < SimpleForm onSubmit = { jest . fn ( ) } >
100
+ < SelectInput { ...defaultProps } />
101
+ </ SimpleForm >
102
+ </ AdminContext >
103
+ ) ;
104
+ fireEvent . mouseDown (
105
+ screen . getByLabelText ( 'resources.posts.fields.language' )
106
+ ) ;
107
+ expect ( screen . queryAllByRole ( 'option' ) ) . toHaveLength ( 3 ) ;
108
+ } ) ;
109
+
110
+ it ( 'should not include an empty option if the field is required' , ( ) => {
111
+ render (
112
+ < AdminContext dataProvider = { testDataProvider ( ) } >
113
+ < SimpleForm onSubmit = { jest . fn ( ) } >
114
+ < SelectInput { ...defaultProps } validate = { required ( ) } />
115
+ </ SimpleForm >
116
+ </ AdminContext >
117
+ ) ;
118
+ fireEvent . mouseDown (
119
+ screen . getByLabelText ( 'resources.posts.fields.language *' )
120
+ ) ;
121
+ expect ( screen . queryAllByRole ( 'option' ) ) . toHaveLength ( 2 ) ;
122
+ } ) ;
101
123
} ) ;
102
124
103
125
describe ( 'emptyText' , ( ) => {
@@ -375,12 +397,16 @@ describe('<SelectInput />', () => {
375
397
defaultValues = { { language : 'ang' } }
376
398
onSubmit = { jest . fn ( ) }
377
399
>
378
- < SelectInput { ...defaultProps } validate = { required ( ) } />
400
+ < SelectInput
401
+ { ...defaultProps }
402
+ helperText = "helperText"
403
+ validate = { ( ) => 'error' }
404
+ />
379
405
</ SimpleForm >
380
406
</ AdminContext >
381
407
) ;
382
- const error = screen . queryAllByText ( 'ra.validation.required ') ;
383
- expect ( error . length ) . toEqual ( 0 ) ;
408
+ screen . getByText ( 'helperText ') ;
409
+ expect ( screen . queryAllByText ( 'error' ) ) . toHaveLength ( 0 ) ;
384
410
} ) ;
385
411
386
412
it ( 'should not be displayed if field has been touched but is valid' , ( ) => {
@@ -391,18 +417,21 @@ describe('<SelectInput />', () => {
391
417
mode = "onBlur"
392
418
onSubmit = { jest . fn ( ) }
393
419
>
394
- < SelectInput { ...defaultProps } validate = { required ( ) } />
420
+ < SelectInput
421
+ { ...defaultProps }
422
+ helperText = "helperText"
423
+ validate = { ( ) => undefined }
424
+ />
395
425
</ SimpleForm >
396
426
</ AdminContext >
397
427
) ;
398
428
const input = screen . getByLabelText (
399
- 'resources.posts.fields.language * '
429
+ 'resources.posts.fields.language'
400
430
) ;
401
431
input . focus ( ) ;
402
432
input . blur ( ) ;
403
433
404
- const error = screen . queryAllByText ( 'ra.validation.required' ) ;
405
- expect ( error . length ) . toEqual ( 0 ) ;
434
+ screen . getByText ( 'helperText' ) ;
406
435
} ) ;
407
436
408
437
it ( 'should be displayed if field has been touched and is invalid' , async ( ) => {
@@ -411,27 +440,25 @@ describe('<SelectInput />', () => {
411
440
< SimpleForm mode = "onChange" onSubmit = { jest . fn ( ) } >
412
441
< SelectInput
413
442
{ ...defaultProps }
443
+ helperText = "helperText"
414
444
emptyText = "Empty"
415
- validate = { required ( ) }
445
+ validate = { ( ) => 'error' }
416
446
/>
417
447
</ SimpleForm >
418
448
</ AdminContext >
419
449
) ;
420
450
421
451
const select = screen . getByLabelText (
422
- 'resources.posts.fields.language * '
452
+ 'resources.posts.fields.language'
423
453
) ;
424
454
fireEvent . mouseDown ( select ) ;
425
455
426
456
const optionAngular = screen . getByText ( 'Angular' ) ;
427
457
fireEvent . click ( optionAngular ) ;
458
+ select . blur ( ) ;
428
459
429
- const optionEmpty = screen . getByText ( 'Empty' ) ;
430
- fireEvent . click ( optionEmpty ) ;
431
-
432
- await waitFor ( ( ) => {
433
- expect ( screen . queryByText ( 'ra.validation.required' ) ) ;
434
- } ) ;
460
+ await screen . findByText ( 'error' ) ;
461
+ expect ( screen . queryAllByText ( 'helperText' ) ) . toHaveLength ( 0 ) ;
435
462
} ) ;
436
463
} ) ;
437
464
0 commit comments