1
1
import React from 'react' ;
2
- import { mount } from 'enzyme ' ;
3
- import { act } from 'react-dom/test-utils ' ;
2
+ import { act , render , screen } from '@testing-library/react ' ;
3
+ import userEvent from '@testing-library/user-event ' ;
4
4
5
5
import { FormRenderer , componentTypes } from '@data-driven-forms/react-form-renderer' ;
6
6
import FormTemplate from '../form-template' ;
7
7
import componentMapper from '../component-mapper' ;
8
8
import { validatorTypes } from '@data-driven-forms/react-form-renderer' ;
9
- import FormGroupWrapper from '../form-group' ;
10
9
11
10
describe ( 'formFields generated tests' , ( ) => {
12
11
const RendererWrapper = ( { schema = { fields : [ ] } , ...props } ) => (
@@ -67,31 +66,29 @@ describe('formFields generated tests', () => {
67
66
} ) ;
68
67
69
68
it ( 'renders correctly' , ( ) => {
70
- const wrapper = mount ( < RendererWrapper schema = { schema } /> ) ;
69
+ render ( < RendererWrapper schema = { schema } /> ) ;
71
70
72
71
if ( component === componentTypes . RADIO ) {
73
- expect ( wrapper . find ( '.ant-radio-wrapper' ) ) . toHaveLength ( options . length ) ;
74
- } else {
75
- expect ( wrapper . find ( componentMapper [ component ] ) ) . toHaveLength ( 1 ) ;
76
- expect ( wrapper . find ( 'label' ) . text ( ) . includes ( field . label ) ) . toEqual ( true ) ;
72
+ options . forEach ( ( option ) => {
73
+ expect ( screen . getByText ( option . label ) ) . toBeInTheDocument ( ) ;
74
+ } ) ;
77
75
}
78
76
79
- expect ( wrapper . find ( FormGroupWrapper ) ) . toHaveLength ( 1 ) ;
80
- expect ( wrapper . find ( '.ant-form-item-explain' ) ) . toHaveLength ( 0 ) ;
77
+ expect ( screen . getByText ( field . label ) ) . toBeInTheDocument ( ) ;
81
78
} ) ;
82
79
83
80
it ( 'renders with error' , async ( ) => {
84
81
const errorField = {
85
82
...field ,
86
83
validate : [ { type : validatorTypes . REQUIRED } ] ,
87
84
} ;
88
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ errorField ] } } /> ) ;
85
+ render ( < RendererWrapper schema = { { fields : [ errorField ] } } /> ) ;
86
+
89
87
await act ( async ( ) => {
90
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
88
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
91
89
} ) ;
92
- wrapper . update ( ) ;
93
- expect ( wrapper . find ( '.ant-form-item-explain' ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
94
- expect ( wrapper . find ( '.ant-form-item-has-error' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
90
+
91
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
95
92
} ) ;
96
93
97
94
it ( 'renders with warning' , async ( ) => {
@@ -101,15 +98,12 @@ describe('formFields generated tests', () => {
101
98
useWarnings : true ,
102
99
validateOnMount : true ,
103
100
} ;
104
- let wrapper ;
105
101
106
102
await act ( async ( ) => {
107
- wrapper = mount ( < RendererWrapper schema = { { fields : [ errorField ] } } /> ) ;
103
+ render ( < RendererWrapper schema = { { fields : [ errorField ] } } /> ) ;
108
104
} ) ;
109
- wrapper . update ( ) ;
110
105
111
- expect ( wrapper . find ( '.ant-form-item-explain' ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
112
- expect ( wrapper . find ( '.ant-form-item-has-warning' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
106
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
113
107
} ) ;
114
108
115
109
it ( 'renders with error and validateOnMount' , async ( ) => {
@@ -118,33 +112,33 @@ describe('formFields generated tests', () => {
118
112
validate : [ { type : validatorTypes . REQUIRED } ] ,
119
113
validateOnMount : true ,
120
114
} ;
121
- let wrapper ;
122
115
await act ( async ( ) => {
123
- wrapper = mount ( < RendererWrapper schema = { { fields : [ errorField ] } } /> ) ;
116
+ render ( < RendererWrapper schema = { { fields : [ errorField ] } } /> ) ;
124
117
} ) ;
125
- wrapper . update ( ) ;
126
- expect ( wrapper . find ( '.ant-form-item-explain' ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
127
- expect ( wrapper . find ( '.ant-form-item-has-error' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
118
+
119
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
128
120
} ) ;
129
121
130
122
it ( 'renders with helperText' , ( ) => {
131
123
const helpertextField = {
132
124
...field ,
133
125
helperText,
134
126
} ;
135
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ helpertextField ] } } /> ) ;
136
127
137
- expect ( wrapper . find ( '.ant-form-item-explain' ) . last ( ) . text ( ) ) . toEqual ( helperText ) ;
128
+ render ( < RendererWrapper schema = { { fields : [ helpertextField ] } } /> ) ;
129
+
130
+ expect ( screen . getByText ( helperText ) ) . toBeInTheDocument ( ) ;
138
131
} ) ;
139
132
140
133
it ( 'renders with description' , ( ) => {
141
134
const descriptionField = {
142
135
...field ,
143
136
description,
144
137
} ;
145
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ descriptionField ] } } /> ) ;
146
138
147
- expect ( wrapper . find ( '.ant-form-item-explain' ) . last ( ) . text ( ) ) . toEqual ( description ) ;
139
+ render ( < RendererWrapper schema = { { fields : [ descriptionField ] } } /> ) ;
140
+
141
+ expect ( screen . getByText ( description ) ) . toBeInTheDocument ( ) ;
148
142
} ) ;
149
143
150
144
it ( 'renders with description and helperText' , ( ) => {
@@ -153,9 +147,10 @@ describe('formFields generated tests', () => {
153
147
description,
154
148
helperText,
155
149
} ;
156
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ descriptionField ] } } /> ) ;
157
150
158
- expect ( wrapper . find ( '.ant-form-item-explain' ) . last ( ) . text ( ) ) . toEqual ( helperText ) ;
151
+ render ( < RendererWrapper schema = { { fields : [ descriptionField ] } } /> ) ;
152
+
153
+ expect ( screen . getByText ( helperText ) ) . toBeInTheDocument ( ) ;
159
154
} ) ;
160
155
161
156
it ( 'renders with error and helperText' , async ( ) => {
@@ -164,81 +159,85 @@ describe('formFields generated tests', () => {
164
159
helperText,
165
160
validate : [ { type : validatorTypes . REQUIRED } ] ,
166
161
} ;
167
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ errorFields ] } } /> ) ;
162
+
163
+ render ( < RendererWrapper schema = { { fields : [ errorFields ] } } /> ) ;
164
+
168
165
await act ( async ( ) => {
169
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
166
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
170
167
} ) ;
171
- wrapper . update ( ) ;
172
168
173
- expect ( wrapper . find ( '.ant-form-item-explain' ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
169
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
174
170
} ) ;
175
171
176
172
it ( 'renders isRequired' , ( ) => {
177
173
const requiredField = {
178
174
...field ,
179
175
isRequired : true ,
180
176
} ;
181
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ requiredField ] } } /> ) ;
177
+
178
+ render ( < RendererWrapper schema = { { fields : [ requiredField ] } } /> ) ;
182
179
183
180
if ( component === componentTypes . CHECKBOX ) {
184
- expect ( wrapper . find ( '.ddorg__ant-component-mapper_is-required' ) . text ( ) ) . toEqual ( '*' ) ;
181
+ expect ( screen . getByText ( '*' ) ) . toBeInTheDocument ( ) ;
185
182
} else {
186
- expect ( wrapper . find ( '. ant-form-item-required') ) . toHaveLength ( 1 ) ;
183
+ expect ( screen . getByText ( field . label ) ) . toHaveClass ( ' ant-form-item-required') ;
187
184
}
188
185
} ) ;
189
186
190
187
it ( 'renders isDisabled' , ( ) => {
188
+ const labelText = 'field' ;
189
+
191
190
const disabledField = {
192
191
...field ,
193
192
isDisabled : true ,
193
+ 'aria-label' : labelText ,
194
194
} ;
195
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ disabledField ] } } /> ) ;
196
195
197
- if ( component === componentTypes . TEXTAREA ) {
198
- expect ( wrapper . find ( 'textarea' ) . first ( ) . props ( ) . disabled ) . toEqual ( true ) ;
196
+ const { container } = render ( < RendererWrapper schema = { { fields : [ disabledField ] } } /> ) ;
197
+
198
+ if ( component === componentTypes . SLIDER ) {
199
+ expect ( screen . getByRole ( 'slider' ) ) . toHaveAttribute ( 'aria-disabled' , 'true' ) ;
199
200
} else if ( component === componentTypes . RADIO ) {
200
- expect ( wrapper . find ( '.ant-radio-disabled' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
201
- } else if ( component === componentTypes . CHECKBOX ) {
202
- expect ( wrapper . find ( '.ant-checkbox-disabled' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
203
- } else if ( component === componentTypes . SWITCH ) {
204
- expect ( wrapper . find ( '.ant-switch-disabled' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
205
- } else if ( component === componentTypes . SLIDER ) {
206
- expect ( wrapper . find ( '.ant-slider-disabled' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
201
+ expect ( container . getElementsByTagName ( 'input' ) [ 0 ] . disabled ) . toEqual ( true ) ;
202
+ } else if ( component === componentTypes . SELECT ) {
203
+ expect ( screen . getAllByLabelText ( labelText ) [ 0 ] ) . toHaveClass ( 'ant-select-disabled' ) ;
207
204
} else {
208
- expect ( wrapper . find ( 'input' ) . first ( ) . props ( ) . disabled ) . toEqual ( true ) ;
205
+ expect ( screen . getAllByLabelText ( labelText ) [ 0 ] ) . toBeDisabled ( ) ;
209
206
}
210
207
} ) ;
211
208
212
209
it ( 'renders isReadOnly' , ( ) => {
210
+ const labelText = 'field' ;
211
+
213
212
const disabledField = {
214
213
...field ,
215
214
isReadOnly : true ,
215
+ 'aria-label' : labelText ,
216
216
} ;
217
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ disabledField ] } } /> ) ;
218
217
219
- if ( component === componentTypes . TEXTAREA ) {
220
- expect ( wrapper . find ( 'textarea' ) . first ( ) . props ( ) . readOnly ) . toEqual ( true ) ;
218
+ const { container } = render ( < RendererWrapper schema = { { fields : [ disabledField ] } } /> ) ;
219
+
220
+ if ( component === componentTypes . SLIDER ) {
221
+ expect ( screen . getByRole ( 'slider' ) ) . toHaveAttribute ( 'aria-disabled' , 'true' ) ;
221
222
} else if ( component === componentTypes . RADIO ) {
222
- expect ( wrapper . find ( '.ant-radio-disabled' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
223
- } else if ( component === componentTypes . CHECKBOX ) {
224
- expect ( wrapper . find ( '.ant-checkbox-disabled' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
225
- } else if ( component === componentTypes . SWITCH ) {
226
- expect ( wrapper . find ( '.ant-switch-disabled' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
227
- } else if ( component === componentTypes . SLIDER ) {
228
- expect ( wrapper . find ( '.ant-slider-disabled' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
223
+ expect ( container . getElementsByTagName ( 'input' ) [ 0 ] . disabled ) . toEqual ( true ) ;
224
+ } else if ( component === componentTypes . SELECT ) {
225
+ expect ( screen . getAllByLabelText ( labelText ) [ 0 ] ) . toHaveClass ( 'ant-select-disabled' ) ;
226
+ } else if ( component === componentTypes . TEXTAREA || componentTypes . TEXT_FIELD === component ) {
227
+ expect ( screen . getAllByLabelText ( labelText ) [ 0 ] ) . toHaveAttribute ( 'readonly' , '' ) ;
229
228
} else {
230
- expect ( wrapper . find ( 'input' ) . first ( ) . props ( ) . readOnly ) . toEqual ( true ) ;
229
+ expect ( screen . getAllByLabelText ( labelText ) [ 0 ] ) . toBeDisabled ( ) ;
231
230
}
232
231
} ) ;
233
232
234
233
it ( 'renders with submitError' , async ( ) => {
235
- const wrapper = mount ( < RendererWrapper schema = { schema } onSubmit = { ( ) => ( { [ field . name ] : errorText } ) } /> ) ;
234
+ render ( < RendererWrapper schema = { schema } onSubmit = { ( ) => ( { [ field . name ] : errorText } ) } /> ) ;
235
+
236
236
await act ( async ( ) => {
237
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
237
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
238
238
} ) ;
239
- wrapper . update ( ) ;
240
- expect ( wrapper . find ( '.ant-form-item-explain' ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
241
- expect ( wrapper . find ( '.ant-form-item-has-error' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
239
+
240
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
242
241
} ) ;
243
242
} ) ;
244
243
} ) ;
0 commit comments