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 , validatorTypes } from '@data-driven-forms/react-form-renderer' ;
6
6
7
7
import FormTemplate from '../form-template' ;
8
8
import componentMapper from '../component-mapper' ;
9
- import WithDescription from '../with-description' ;
10
9
11
10
describe ( 'component tests' , ( ) => {
12
11
const RendererWrapper = ( { schema = { fields : [ ] } , ...props } ) => (
@@ -73,34 +72,28 @@ describe('component tests', () => {
73
72
} ) ;
74
73
75
74
it ( 'renders correctly' , ( ) => {
76
- const wrapper = mount ( < RendererWrapper schema = { schema } /> ) ;
75
+ render ( < RendererWrapper schema = { schema } /> ) ;
77
76
78
77
if ( component === componentTypes . RADIO ) {
79
- expect ( wrapper . find ( '.bx--radio-button-wrapper' ) ) . toHaveLength ( options . length ) ;
80
- } else if ( component === 'text-field-number' ) {
81
- expect ( wrapper . find ( 'NumberInput' ) ) . toHaveLength ( 1 ) ;
82
- } else {
83
- expect ( wrapper . find ( componentMapper [ component ] ) ) . toHaveLength ( 1 ) ;
84
- expect ( wrapper . find ( 'label' ) . text ( ) . includes ( field . label ) ) . toEqual ( true ) ;
78
+ options . forEach ( ( opt ) => {
79
+ expect ( screen . getByText ( opt . label ) ) . toBeInTheDocument ( ) ;
80
+ } ) ;
85
81
}
82
+
83
+ expect ( screen . getAllByText ( field . label ) ) . toBeTruthy ( ) ;
84
+ expect ( ( ) => screen . getByText ( errorText ) ) . toThrow ( ) ;
86
85
} ) ;
87
86
88
87
it ( 'renders with error' , ( ) => {
89
88
const errorField = {
90
89
...field ,
91
90
validate : [ { type : validatorTypes . REQUIRED } ] ,
92
91
} ;
93
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ errorField ] } } /> ) ;
94
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
92
+ render ( < RendererWrapper schema = { { fields : [ errorField ] } } /> ) ;
95
93
96
- if ( wrapper . find ( '#field-name-error-msg' ) . length ) {
97
- expect ( wrapper . find ( '#field-name-error-msg' ) . text ( ) ) . toEqual ( errorText ) ;
98
- expect ( wrapper . find ( '[invalid=true]' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
99
- }
94
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
100
95
101
- if ( wrapper . find ( '.ddorg__carbon-error-helper-text' ) . length ) {
102
- expect ( wrapper . find ( '.ddorg__carbon-error-helper-text' ) . text ( ) ) . toEqual ( errorText ) ;
103
- }
96
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
104
97
} ) ;
105
98
106
99
if ( component !== 'text-field-number' ) {
@@ -111,31 +104,28 @@ describe('component tests', () => {
111
104
useWarnings : true ,
112
105
validateOnMount : true ,
113
106
} ;
114
- let wrapper ;
115
107
116
108
await act ( async ( ) => {
117
- wrapper = mount ( < RendererWrapper schema = { { fields : [ errorField ] } } /> ) ;
109
+ render (
110
+ < RendererWrapper
111
+ schema = { {
112
+ fields : [ errorField ] ,
113
+ } }
114
+ />
115
+ ) ;
118
116
} ) ;
119
- wrapper . update ( ) ;
120
- wrapper . update ( ) ;
121
-
122
- const helperText = wrapper . find ( '.bx--form__helper-text' ) ;
123
117
124
- if ( helperText . length ) {
125
- expect ( helperText . text ( ) ) . toEqual ( errorText ) ;
126
- } else {
127
- expect ( wrapper . find ( '.bx--form-requirement' ) . last ( ) . text ( ) ) . toEqual ( errorText ) ;
128
- }
118
+ expect ( screen . getAllByText ( errorText ) ) . toBeTruthy ( ) ;
129
119
} ) ;
130
120
131
121
it ( 'renders with helperText' , ( ) => {
132
122
const helpertextField = {
133
123
...field ,
134
124
helperText,
135
125
} ;
136
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ helpertextField ] } } /> ) ;
126
+ render ( < RendererWrapper schema = { { fields : [ helpertextField ] } } /> ) ;
137
127
138
- expect ( wrapper . find ( '.bx--form__helper-text' ) . last ( ) . text ( ) ) . toEqual ( helperText ) ;
128
+ expect ( screen . getByText ( helperText ) ) . toBeInTheDocument ( ) ;
139
129
} ) ;
140
130
141
131
it ( 'renders with description and helperText' , ( ) => {
@@ -144,11 +134,10 @@ describe('component tests', () => {
144
134
description,
145
135
helperText,
146
136
} ;
147
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ descriptionField ] } } /> ) ;
137
+ render ( < RendererWrapper schema = { { fields : [ descriptionField ] } } /> ) ;
148
138
149
- expect ( wrapper . find ( WithDescription ) ) . toHaveLength ( 1 ) ;
150
-
151
- expect ( wrapper . find ( '.bx--form__helper-text' ) . last ( ) . text ( ) ) . toEqual ( helperText ) ;
139
+ expect ( screen . getByText ( helperText ) ) . toBeInTheDocument ( ) ;
140
+ expect ( ( ) => screen . getByText ( description ) ) . toThrow ( ) ;
152
141
} ) ;
153
142
154
143
it ( 'renders with error and helperText' , ( ) => {
@@ -157,19 +146,12 @@ describe('component tests', () => {
157
146
helperText,
158
147
validate : [ { type : validatorTypes . REQUIRED } ] ,
159
148
} ;
160
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ errorFields ] } } /> ) ;
161
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
162
-
163
- if ( wrapper . find ( '#field-name-error-msg' ) . length ) {
164
- expect ( wrapper . find ( '#field-name-error-msg' ) . text ( ) ) . toEqual ( errorText ) ;
165
- expect ( wrapper . find ( '[invalid=true]' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
166
- }
149
+ render ( < RendererWrapper schema = { { fields : [ errorFields ] } } /> ) ;
167
150
168
- if ( wrapper . find ( '.ddorg__carbon-error-helper-text' ) . length ) {
169
- expect ( wrapper . find ( '.ddorg__carbon-error-helper-text' ) . text ( ) ) . toEqual ( errorText ) ;
170
- }
151
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
171
152
172
- expect ( wrapper . find ( '.bx--form__helper-text' ) ) . toHaveLength ( 0 ) ;
153
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
154
+ expect ( ( ) => screen . getByText ( helperText ) ) . toThrow ( ) ;
173
155
} ) ;
174
156
}
175
157
@@ -178,42 +160,39 @@ describe('component tests', () => {
178
160
...field ,
179
161
description,
180
162
} ;
181
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ descriptionField ] } } /> ) ;
163
+ render ( < RendererWrapper schema = { { fields : [ descriptionField ] } } /> ) ;
182
164
183
- expect ( wrapper . find ( WithDescription ) ) . toHaveLength ( 1 ) ;
165
+ expect ( screen . getAllByRole ( 'button' ) [ 0 ] ) . toHaveClass ( 'bx--tooltip__trigger' ) ;
184
166
} ) ;
185
167
186
168
it ( 'renders isDisabled' , ( ) => {
187
169
const disabledField = {
188
170
...field ,
189
171
isDisabled : true ,
172
+ 'aria-label' : field . name ,
173
+ ...( field . type === 'number' && { ariaLabel : field . name } ) ,
190
174
} ;
191
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ disabledField ] } } /> ) ;
175
+ const { container } = render ( < RendererWrapper schema = { { fields : [ disabledField ] } } /> ) ;
192
176
193
- expect ( wrapper . find ( '[disabled=true]' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
177
+ [ ... container . getElementsByTagName ( 'input' ) ] . forEach ( ( el ) => expect ( el ) . toBeDisabled ( ) ) ;
194
178
} ) ;
195
179
196
180
it ( 'renders isRequired' , ( ) => {
197
181
const requiredField = {
198
182
...field ,
199
183
isRequired : true ,
200
184
} ;
201
- const wrapper = mount ( < RendererWrapper schema = { { fields : [ requiredField ] } } /> ) ;
202
- expect ( wrapper . find ( '.ddorg__carbon-component-mapper_is-required' ) . text ( ) ) . toEqual ( '*' ) ;
185
+ render ( < RendererWrapper schema = { { fields : [ requiredField ] } } /> ) ;
186
+
187
+ expect ( screen . getByText ( '*' ) ) . toBeInTheDocument ( ) ;
203
188
} ) ;
204
189
205
190
it ( 'renders with submitError' , ( ) => {
206
- const wrapper = mount ( < RendererWrapper schema = { schema } onSubmit = { ( ) => ( { [ field . name ] : errorText } ) } /> ) ;
207
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
191
+ render ( < RendererWrapper schema = { schema } onSubmit = { ( ) => ( { [ field . name ] : errorText } ) } /> ) ;
208
192
209
- if ( wrapper . find ( '#field-name-error-msg' ) . length ) {
210
- expect ( wrapper . find ( '#field-name-error-msg' ) . text ( ) ) . toEqual ( errorText ) ;
211
- expect ( wrapper . find ( '[invalid=true]' ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
212
- }
193
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
213
194
214
- if ( wrapper . find ( '.ddorg__carbon-error-helper-text' ) . length ) {
215
- expect ( wrapper . find ( '.ddorg__carbon-error-helper-text' ) . text ( ) ) . toEqual ( errorText ) ;
216
- }
195
+ expect ( screen . getByText ( errorText ) ) . toBeInTheDocument ( ) ;
217
196
} ) ;
218
197
} ) ;
219
198
} ) ;
0 commit comments