1
1
import React from 'react' ;
2
- import { mount } from 'enzyme ' ;
3
- import { act } from 'react-dom/test-utils ' ;
4
- import MultipleChoiceListCommon from '@data-driven-forms/common/multiple-choice-list' ;
2
+ import { render , screen , act } from '@testing-library/react ' ;
3
+ import userEvent from '@testing-library/user-event ' ;
4
+
5
5
import { FormRenderer , componentTypes , validatorTypes } from '@data-driven-forms/react-form-renderer' ;
6
6
import Checkbox from '../checkbox' ;
7
7
8
8
import RenderWithProvider from '../../../../__mocks__/with-provider' ;
9
9
import FormTemplate from '../form-template' ;
10
10
import componentMapper from '../component-mapper' ;
11
- import { Radio , Dropdown } from 'semantic-ui-react' ;
12
- import HelperText from '../helper-text/helper-text' ;
13
11
14
12
const RendererWrapper = ( { schema = { fields : [ ] } , ...props } ) => (
15
13
< FormRenderer
@@ -69,28 +67,28 @@ describe('formFields', () => {
69
67
} ) ;
70
68
71
69
it ( 'renders correctly' , ( ) => {
72
- const wrapper = mount ( < RendererWrapper schema = { schema } /> ) ;
70
+ render ( < RendererWrapper schema = { schema } /> ) ;
73
71
74
72
if ( component === componentTypes . RADIO ) {
75
- expect ( wrapper . find ( Radio ) ) . toHaveLength ( options . length ) ;
76
- } else {
77
- expect ( wrapper . find ( componentMapper [ component ] ) ) . toHaveLength ( 1 ) ;
73
+ options . forEach ( ( opt ) => {
74
+ expect ( screen . getByText ( opt . label ) ) . toBeInTheDocument ( ) ;
75
+ } ) ;
78
76
}
79
77
80
- expect ( wrapper . find ( 'label' ) . first ( ) . text ( ) ) . toEqual ( field . label ) ;
81
- expect ( wrapper . find ( '.ui.pointing.prompt.label' ) ) . toHaveLength ( 0 ) ;
82
- expect ( wrapper . find ( HelperText ) ) . toHaveLength ( 0 ) ;
83
- expect ( wrapper . find ( '.required.field' ) ) . toHaveLength ( 0 ) ;
78
+ expect ( screen . getAllByText ( field . label ) ) . toBeTruthy ( ) ;
79
+ expect ( ( ) => screen . getByText ( errorText ) ) . toThrow ( ) ;
84
80
} ) ;
85
81
86
82
it ( 'renders with error' , ( ) => {
87
83
const errorField = {
88
84
...field ,
89
85
validate : [ { type : validatorTypes . REQUIRED } ] ,
90
86
} ;
91
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ errorField ] } } /> ) ;
92
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
93
- expect ( wrapper . find ( '.ui.pointing.prompt.label' ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
87
+ render ( < RendererWrapper schema = { { fields : [ errorField ] } } /> ) ;
88
+
89
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
90
+
91
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
94
92
} ) ;
95
93
96
94
it ( 'renders with warning' , async ( ) => {
@@ -100,24 +98,27 @@ describe('formFields', () => {
100
98
useWarnings : true ,
101
99
validateOnMount : true ,
102
100
} ;
103
- let wrapper ;
104
-
105
101
await act ( async ( ) => {
106
- wrapper = mount ( < RendererWrapper schema = { { fields : [ errorField ] } } /> ) ;
102
+ render (
103
+ < RendererWrapper
104
+ schema = { {
105
+ fields : [ errorField ] ,
106
+ } }
107
+ />
108
+ ) ;
107
109
} ) ;
108
- wrapper . update ( ) ;
109
110
110
- expect ( wrapper . find ( HelperText ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
111
+ expect ( screen . getAllByText ( errorText ) ) . toBeTruthy ( ) ;
111
112
} ) ;
112
113
113
114
it ( 'renders with helperText' , ( ) => {
114
115
const helpertextField = {
115
116
...field ,
116
117
helperText,
117
118
} ;
118
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ helpertextField ] } } /> ) ;
119
+ render ( < RendererWrapper schema = { { fields : [ helpertextField ] } } /> ) ;
119
120
120
- expect ( wrapper . find ( HelperText ) . last ( ) . text ( ) ) . toEqual ( helperText ) ;
121
+ expect ( screen . getByText ( helperText ) ) . toBeInTheDocument ( ) ;
121
122
} ) ;
122
123
123
124
it ( 'renders with error and helperText' , ( ) => {
@@ -126,63 +127,56 @@ describe('formFields', () => {
126
127
helperText,
127
128
validate : [ { type : validatorTypes . REQUIRED } ] ,
128
129
} ;
129
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ errorFields ] } } /> ) ;
130
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
130
+ render ( < RendererWrapper schema = { { fields : [ errorFields ] } } /> ) ;
131
131
132
- expect ( wrapper . find ( '.ui.pointing.prompt.label' ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
133
- expect ( wrapper . find ( HelperText ) . last ( ) . text ( ) ) . toEqual ( helperText ) ;
132
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
133
+
134
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
135
+ expect ( screen . getByText ( helperText ) ) . toBeInTheDocument ( ) ;
134
136
} ) ;
135
137
136
138
it ( 'renders isRequired' , ( ) => {
137
139
const requiredField = {
138
140
...field ,
139
141
isRequired : true ,
140
142
} ;
141
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ requiredField ] } } /> ) ;
142
- if ( component === componentTypes . TEXTAREA ) {
143
- expect ( wrapper . find ( '.required.field' ) ) . toHaveLength ( 2 ) ;
144
- } else {
145
- expect ( wrapper . find ( '.required.field' ) ) . toHaveLength ( 1 ) ;
146
- }
143
+ render ( < RendererWrapper schema = { { fields : [ requiredField ] } } /> ) ;
144
+
145
+ expect ( screen . getByText ( field . label ) . closest ( '.required' ) ) . toBeInTheDocument ( ) ;
147
146
} ) ;
148
147
149
148
it ( 'renders isDisabled' , ( ) => {
150
149
const disabledField = {
151
150
...field ,
152
151
isDisabled : true ,
153
152
} ;
154
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ disabledField ] } } /> ) ;
155
-
156
- if ( component === componentTypes . TEXTAREA ) {
157
- expect ( wrapper . find ( 'textarea' ) . first ( ) . props ( ) . disabled ) . toEqual ( true ) ;
158
- } else if ( component === componentTypes . SELECT ) {
159
- expect ( wrapper . find ( Dropdown ) . first ( ) . prop ( 'disabled' ) ) . toEqual ( true ) ;
160
- } else {
161
- expect ( wrapper . find ( 'input' ) . first ( ) . props ( ) . disabled ) . toEqual ( true ) ;
162
- }
153
+ const { container } = render ( < RendererWrapper schema = { { fields : [ disabledField ] } } /> ) ;
154
+
155
+ [ ...container . getElementsByTagName ( 'input' ) ] . forEach ( ( el ) => expect ( el ) . toBeDisabled ( ) ) ;
163
156
} ) ;
164
157
165
158
it ( 'renders isReadOnly' , ( ) => {
166
159
const disabledField = {
167
160
...field ,
168
161
isReadOnly : true ,
169
162
} ;
170
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ disabledField ] } } /> ) ;
171
-
172
- if ( component === componentTypes . TEXTAREA ) {
173
- expect ( wrapper . find ( 'textarea' ) . first ( ) . props ( ) . readOnly ) . toEqual ( true ) ;
174
- } else if ( component === componentTypes . SELECT ) {
175
- /**SUIR select does not have read only prop */
176
- expect ( true ) ;
177
- } else {
178
- expect ( wrapper . find ( 'input' ) . first ( ) . props ( ) . readOnly ) . toEqual ( true ) ;
179
- }
163
+ const { container } = render ( < RendererWrapper schema = { { fields : [ disabledField ] } } /> ) ;
164
+
165
+ [ ...container . getElementsByTagName ( 'input' ) ] . forEach ( ( el ) => {
166
+ try {
167
+ expect ( el ) . toBeDisabled ( ) ;
168
+ } catch {
169
+ expect ( el ) . toHaveAttribute ( 'readonly' , '' ) ;
170
+ }
171
+ } ) ;
180
172
} ) ;
181
173
182
174
it ( 'renders with submitError' , ( ) => {
183
- const wrapper = mount ( < RendererWrapper schema = { schema } onSubmit = { ( ) => ( { [ field . name ] : errorText } ) } /> ) ;
184
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
185
- expect ( wrapper . find ( '.ui.pointing.prompt.label' ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
175
+ render ( < RendererWrapper schema = { schema } onSubmit = { ( ) => ( { [ field . name ] : errorText } ) } /> ) ;
176
+
177
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
178
+
179
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
186
180
} ) ;
187
181
} ) ;
188
182
} ) ;
@@ -203,16 +197,15 @@ describe('formFields', () => {
203
197
} ) ;
204
198
205
199
it ( 'renders correctly' , ( ) => {
206
- const wrapper = mount (
200
+ render (
207
201
< RenderWithProvider >
208
202
< Checkbox { ...initialProps } />
209
203
</ RenderWithProvider >
210
204
) ;
211
205
212
- expect ( wrapper . find ( MultipleChoiceListCommon ) ) . toHaveLength ( 1 ) ;
213
- expect ( wrapper . find ( '.ui.pointing.prompt.label' ) ) . toHaveLength ( 0 ) ;
214
- expect ( wrapper . find ( HelperText ) ) . toHaveLength ( 0 ) ;
215
- expect ( wrapper . find ( '.required.field' ) ) . toHaveLength ( 0 ) ;
206
+ expect ( screen . getByText ( 'Cat' ) ) . toBeInTheDocument ( ) ;
207
+ expect ( screen . getByText ( 'Dog' ) ) . toBeInTheDocument ( ) ;
208
+ expect ( screen . getByText ( 'Hamster' ) ) . toBeInTheDocument ( ) ;
216
209
} ) ;
217
210
218
211
it ( 'renders with error' , ( ) => {
@@ -225,10 +218,11 @@ describe('formFields', () => {
225
218
} ,
226
219
] ,
227
220
} ;
228
- const wrapper = mount ( < RendererWrapper schema = { schema } /> ) ;
229
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
221
+ render ( < RendererWrapper schema = { schema } /> ) ;
222
+
223
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
230
224
231
- expect ( wrapper . find ( '.ui.pointing.prompt.label' ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
225
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
232
226
} ) ;
233
227
234
228
it ( 'renders with helperText' , ( ) => {
@@ -241,9 +235,9 @@ describe('formFields', () => {
241
235
} ,
242
236
] ,
243
237
} ;
244
- const wrapper = mount ( < RendererWrapper schema = { schema } /> ) ;
238
+ render ( < RendererWrapper schema = { schema } /> ) ;
245
239
246
- expect ( wrapper . find ( HelperText ) . last ( ) . text ( ) ) . toEqual ( helperText ) ;
240
+ expect ( screen . getByText ( helperText ) ) . toBeInTheDocument ( ) ;
247
241
} ) ;
248
242
249
243
it ( 'renders with error and helperText' , ( ) => {
@@ -257,12 +251,12 @@ describe('formFields', () => {
257
251
} ,
258
252
] ,
259
253
} ;
260
- const wrapper = mount ( < RendererWrapper schema = { schema } /> ) ;
261
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
254
+ render ( < RendererWrapper schema = { schema } /> ) ;
262
255
263
- expect ( wrapper . find ( '.ui.pointing.prompt.label' ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
256
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
264
257
265
- expect ( wrapper . find ( HelperText ) . last ( ) . text ( ) ) . toEqual ( helperText ) ;
258
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
259
+ expect ( screen . getByText ( helperText ) ) . toBeInTheDocument ( ) ;
266
260
} ) ;
267
261
} ) ;
268
262
} ) ;
0 commit comments