1
1
import React from 'react' ;
2
- import { mount } from 'enzyme ' ;
3
- import { act } from 'react-dom/test-utils ' ;
2
+ import { render , screen , act } 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/form-template' ;
7
7
import componentMapper from '../component-mapper/component-mapper' ;
8
8
import { validatorTypes } from '@data-driven-forms/react-form-renderer' ;
9
- import FormGroupWrapper from '../form-group/form-group' ;
10
9
11
10
describe ( 'formFields generated tests' , ( ) => {
12
11
const RendererWrapper = ( { schema = { fields : [ ] } , ...props } ) => (
@@ -67,30 +66,28 @@ 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 ( '.bp3-radio' ) ) . toHaveLength ( options . length ) ;
74
- } else if ( component === componentTypes . SWITCH ) {
75
- expect ( wrapper . find ( '.bp3-switch' ) . first ( ) . text ( ) . includes ( field . label ) ) . toEqual ( true ) ;
76
- } else {
77
- expect ( wrapper . find ( componentMapper [ component ] ) ) . toHaveLength ( 1 ) ;
78
- expect ( wrapper . find ( 'label' ) . text ( ) . includes ( field . label ) ) . toEqual ( true ) ;
72
+ options . forEach ( ( opt ) => {
73
+ expect ( screen . getByText ( opt . label ) ) . toBeInTheDocument ( ) ;
74
+ } ) ;
79
75
}
80
76
81
- expect ( wrapper . find ( FormGroupWrapper ) ) . toHaveLength ( 1 ) ;
82
- expect ( wrapper . find ( '.bp3-form-helper-text' ) ) . toHaveLength ( 0 ) ;
77
+ expect ( screen . getAllByText ( field . label ) ) . toBeTruthy ( ) ;
78
+ expect ( ( ) => screen . getByText ( errorText ) ) . toThrow ( ) ;
83
79
} ) ;
84
80
85
81
it ( 'renders with error' , ( ) => {
86
82
const errorField = {
87
83
...field ,
88
84
validate : [ { type : validatorTypes . REQUIRED } ] ,
89
85
} ;
90
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ errorField ] } } /> ) ;
91
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
92
- expect ( wrapper . find ( '.bp3-form-helper-text' ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
93
- expect ( wrapper . find ( '.bp3-intent-danger' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
86
+ render ( < RendererWrapper schema = { { fields : [ errorField ] } } /> ) ;
87
+
88
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
89
+
90
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
94
91
} ) ;
95
92
96
93
it ( 'renders with warning' , async ( ) => {
@@ -100,35 +97,38 @@ describe('formFields generated tests', () => {
100
97
useWarnings : true ,
101
98
validateOnMount : true ,
102
99
} ;
103
- let wrapper ;
104
100
105
101
await act ( async ( ) => {
106
- wrapper = mount ( < RendererWrapper schema = { { fields : [ errorField ] } } /> ) ;
102
+ render (
103
+ < RendererWrapper
104
+ schema = { {
105
+ fields : [ errorField , { name : 'error-reset-touched' , component : componentTypes . TEXT_FIELD , validate : [ { type : 'required' } ] } ] ,
106
+ } }
107
+ />
108
+ ) ;
107
109
} ) ;
108
- wrapper . update ( ) ;
109
110
110
- expect ( wrapper . find ( '.bp3-form-helper-text' ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
111
- expect ( wrapper . find ( '.bp3-intent-warning' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
111
+ expect ( screen . getAllByText ( errorText ) ) . toBeTruthy ( ) ;
112
112
} ) ;
113
113
114
114
it ( 'renders with helperText' , ( ) => {
115
115
const helpertextField = {
116
116
...field ,
117
117
helperText,
118
118
} ;
119
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ helpertextField ] } } /> ) ;
119
+ render ( < RendererWrapper schema = { { fields : [ helpertextField ] } } /> ) ;
120
120
121
- expect ( wrapper . find ( '.bp3-form-helper-text' ) . last ( ) . text ( ) ) . toEqual ( helperText ) ;
121
+ expect ( screen . getByText ( helperText ) ) . toBeInTheDocument ( ) ;
122
122
} ) ;
123
123
124
124
it ( 'renders with description' , ( ) => {
125
125
const descriptionField = {
126
126
...field ,
127
127
description,
128
128
} ;
129
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ descriptionField ] } } /> ) ;
129
+ render ( < RendererWrapper schema = { { fields : [ descriptionField ] } } /> ) ;
130
130
131
- expect ( wrapper . find ( '.bp3-form-helper-text' ) . last ( ) . text ( ) ) . toEqual ( description ) ;
131
+ expect ( screen . getByText ( description ) ) . toBeInTheDocument ( ) ;
132
132
} ) ;
133
133
134
134
it ( 'renders with description and helperText' , ( ) => {
@@ -137,9 +137,10 @@ describe('formFields generated tests', () => {
137
137
description,
138
138
helperText,
139
139
} ;
140
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ descriptionField ] } } /> ) ;
140
+ render ( < RendererWrapper schema = { { fields : [ descriptionField ] } } /> ) ;
141
141
142
- expect ( wrapper . find ( '.bp3-form-helper-text' ) . last ( ) . text ( ) ) . toEqual ( helperText ) ;
142
+ expect ( screen . getByText ( helperText ) ) . toBeInTheDocument ( ) ;
143
+ expect ( ( ) => screen . getByText ( description ) ) . toThrow ( ) ;
143
144
} ) ;
144
145
145
146
it ( 'renders with error and helperText' , ( ) => {
@@ -148,63 +149,68 @@ describe('formFields generated tests', () => {
148
149
helperText,
149
150
validate : [ { type : validatorTypes . REQUIRED } ] ,
150
151
} ;
151
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ errorFields ] } } /> ) ;
152
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
152
+ render ( < RendererWrapper schema = { { fields : [ errorFields ] } } /> ) ;
153
+
154
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
153
155
154
- expect ( wrapper . find ( '.bp3-form-helper-text' ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
156
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
157
+ expect ( ( ) => screen . getByText ( helperText ) ) . toThrow ( ) ;
155
158
} ) ;
156
159
157
160
it ( 'renders isRequired' , ( ) => {
158
161
const requiredField = {
159
162
...field ,
160
163
isRequired : true ,
161
164
} ;
162
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ requiredField ] } } /> ) ;
165
+ render ( < RendererWrapper schema = { { fields : [ requiredField ] } } /> ) ;
163
166
164
- expect ( wrapper . find ( '.bp3-text-muted' ) . last ( ) . text ( ) ) . toEqual ( '(required)' ) ;
167
+ expect ( screen . getByText ( '(required)' ) ) . toBeInTheDocument ( ) ;
165
168
} ) ;
166
169
167
170
it ( 'renders isDisabled' , ( ) => {
168
171
const disabledField = {
169
172
...field ,
170
173
isDisabled : true ,
174
+ 'aria-label' : field . name ,
171
175
} ;
172
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ disabledField ] } } /> ) ;
176
+ const { container } = render ( < RendererWrapper schema = { { fields : [ disabledField ] } } /> ) ;
173
177
174
- if ( component === componentTypes . TEXTAREA ) {
175
- expect ( wrapper . find ( 'textarea' ) . first ( ) . props ( ) . disabled ) . toEqual ( true ) ;
178
+ if ( component === componentTypes . RADIO ) {
179
+ options . forEach ( ( opt ) => {
180
+ expect ( screen . getByText ( opt . label ) ) . toHaveClass ( 'bp3-disabled' ) ;
181
+ } ) ;
182
+ } else if ( [ componentTypes . SLIDER , componentTypes . DATE_PICKER , componentTypes . TIME_PICKER , componentTypes . SELECT ] . includes ( component ) ) {
183
+ expect ( container . getElementsByClassName ( 'bp3-disabled' ) ) . toHaveLength ( 1 ) ;
176
184
} else {
177
- if ( ! wrapper . find ( '.bp3-disabled' ) ) {
178
- expect ( wrapper . find ( 'input' ) . first ( ) . props ( ) . disabled ) . toEqual ( true ) ;
179
- } else {
180
- expect ( wrapper . find ( '.bp3-disabled' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
181
- }
185
+ expect ( screen . getAllByLabelText ( field . name ) [ 0 ] ) . toBeDisabled ( ) ;
182
186
}
183
187
} ) ;
184
188
185
189
it ( 'renders isReadOnly' , ( ) => {
186
190
const disabledField = {
187
191
...field ,
188
192
isReadOnly : true ,
193
+ 'aria-label' : field . name ,
189
194
} ;
190
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ disabledField ] } } /> ) ;
195
+ const { container } = render ( < RendererWrapper schema = { { fields : [ disabledField ] } } /> ) ;
191
196
192
- if ( component === componentTypes . TEXTAREA ) {
193
- expect ( wrapper . find ( 'textarea' ) . first ( ) . props ( ) . disabled ) . toEqual ( true ) ;
197
+ if ( component === componentTypes . RADIO ) {
198
+ options . forEach ( ( opt ) => {
199
+ expect ( screen . getByText ( opt . label ) ) . toHaveClass ( 'bp3-disabled' ) ;
200
+ } ) ;
201
+ } else if ( [ componentTypes . SLIDER , componentTypes . DATE_PICKER , componentTypes . TIME_PICKER , componentTypes . SELECT ] . includes ( component ) ) {
202
+ expect ( container . getElementsByClassName ( 'bp3-disabled' ) ) . toHaveLength ( 1 ) ;
194
203
} else {
195
- if ( ! wrapper . find ( '.bp3-disabled' ) ) {
196
- expect ( wrapper . find ( 'input' ) . first ( ) . props ( ) . disabled ) . toEqual ( true ) ;
197
- } else {
198
- expect ( wrapper . find ( '.bp3-disabled' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
199
- }
204
+ expect ( screen . getAllByLabelText ( field . name ) [ 0 ] ) . toBeDisabled ( ) ;
200
205
}
201
206
} ) ;
202
207
203
208
it ( 'renders with submit error' , ( ) => {
204
- const wrapper = mount ( < RendererWrapper schema = { schema } onSubmit = { ( ) => ( { [ field . name ] : errorText } ) } /> ) ;
205
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
206
- expect ( wrapper . find ( '.bp3-form-helper-text' ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
207
- expect ( wrapper . find ( '.bp3-intent-danger' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
209
+ render ( < RendererWrapper schema = { schema } onSubmit = { ( ) => ( { [ field . name ] : errorText } ) } /> ) ;
210
+
211
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
212
+
213
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
208
214
} ) ;
209
215
} ) ;
210
216
} ) ;
0 commit comments