1
1
import React from 'react' ;
2
2
import { mount } from 'enzyme' ;
3
+ import { act } from 'react-dom/test-utils' ;
3
4
4
5
import FormTemplate from '../../../../../__mocks__/mock-form-template' ;
5
6
import componentTypes from '../../../dist/cjs/component-types' ;
@@ -18,6 +19,8 @@ describe('condition test', () => {
18
19
let wrapper ;
19
20
20
21
beforeEach ( ( ) => {
22
+ jest . useFakeTimers ( ) ;
23
+
21
24
onSubmit = jest . fn ( ) ;
22
25
23
26
initialProps = {
@@ -67,7 +70,7 @@ describe('condition test', () => {
67
70
expect ( wrapper . find ( 'input' ) ) . toHaveLength ( 1 ) ;
68
71
} ) ;
69
72
70
- it ( 'sets value when condition is fulfill' , ( ) => {
73
+ it ( 'sets value when condition is fulfill' , async ( ) => {
71
74
schema = {
72
75
fields : [
73
76
{
@@ -90,24 +93,34 @@ describe('condition test', () => {
90
93
]
91
94
} ;
92
95
93
- wrapper = mount ( < FormRenderer { ...initialProps } schema = { schema } /> ) ;
96
+ await act ( async ( ) => {
97
+ wrapper = mount ( < FormRenderer { ...initialProps } schema = { schema } /> ) ;
98
+ } ) ;
99
+ wrapper . update ( ) ;
94
100
95
101
expect ( wrapper . find ( 'input' ) ) . toHaveLength ( 1 ) ;
96
102
97
- wrapper . find ( 'input' ) . simulate ( 'change' , { target : { value : 'show' } } ) ;
103
+ await act ( async ( ) => {
104
+ wrapper . find ( 'input' ) . simulate ( 'change' , { target : { value : 'show' } } ) ;
105
+ jest . advanceTimersByTime ( 1 ) ;
106
+ } ) ;
98
107
wrapper . update ( ) ;
99
108
100
109
expect ( wrapper . find ( 'input' ) ) . toHaveLength ( 2 ) ;
101
110
102
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
111
+ await act ( async ( ) => {
112
+ wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
113
+ jest . advanceTimersByTime ( 1 ) ;
114
+ } ) ;
115
+ wrapper . update ( ) ;
103
116
104
117
expect ( onSubmit ) . toHaveBeenCalledWith ( {
105
118
'field-1' : 'show' ,
106
119
'field-2' : 'someValue'
107
120
} ) ;
108
121
} ) ;
109
122
110
- it ( 'sets value when condition is fulfill - initialValues' , ( ) => {
123
+ it ( 'sets value when condition is fulfill - initialValues' , async ( ) => {
111
124
schema = {
112
125
fields : [
113
126
{
@@ -130,19 +143,26 @@ describe('condition test', () => {
130
143
]
131
144
} ;
132
145
133
- wrapper = mount ( < FormRenderer { ...initialProps } schema = { schema } initialValues = { { 'field-1' : 'show' } } /> ) ;
146
+ await act ( async ( ) => {
147
+ wrapper = mount ( < FormRenderer { ...initialProps } schema = { schema } initialValues = { { 'field-1' : 'show' } } /> ) ;
148
+ jest . advanceTimersByTime ( 1 ) ;
149
+ } ) ;
150
+ wrapper . update ( ) ;
134
151
135
152
expect ( wrapper . find ( 'input' ) ) . toHaveLength ( 2 ) ;
136
153
137
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
154
+ await act ( async ( ) => {
155
+ wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
156
+ } ) ;
157
+ wrapper . update ( ) ;
138
158
139
159
expect ( onSubmit ) . toHaveBeenCalledWith ( {
140
160
'field-1' : 'show' ,
141
161
'field-2' : 'someValue'
142
162
} ) ;
143
163
} ) ;
144
164
145
- it ( 'sets value when condition is fulfill on reset' , ( ) => {
165
+ it ( 'sets value when condition is fulfill on reset' , async ( ) => {
146
166
schema = {
147
167
fields : [
148
168
{
@@ -165,41 +185,58 @@ describe('condition test', () => {
165
185
]
166
186
} ;
167
187
168
- wrapper = mount ( < FormRenderer { ...initialProps } schema = { schema } initialValues = { { 'field-1' : 'show' } } /> ) ;
188
+ await act ( async ( ) => {
189
+ wrapper = mount ( < FormRenderer { ...initialProps } schema = { schema } initialValues = { { 'field-1' : 'show' } } /> ) ;
190
+ jest . advanceTimersByTime ( 1 ) ;
191
+ } ) ;
192
+ wrapper . update ( ) ;
169
193
170
194
expect ( wrapper . find ( 'input' ) ) . toHaveLength ( 2 ) ;
171
195
172
- wrapper
173
- . find ( 'input' )
174
- . first ( )
175
- . simulate ( 'change' , { target : { value : 'dontshow' } } ) ;
196
+ await act ( async ( ) => {
197
+ wrapper
198
+ . find ( 'input' )
199
+ . first ( )
200
+ . simulate ( 'change' , { target : { value : 'dontshow' } } ) ;
201
+ jest . advanceTimersByTime ( 1 ) ;
202
+ } ) ;
176
203
wrapper . update ( ) ;
177
204
178
205
expect ( wrapper . find ( 'input' ) ) . toHaveLength ( 1 ) ;
179
206
180
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
207
+ await act ( async ( ) => {
208
+ wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
209
+ } ) ;
210
+ wrapper . update ( ) ;
181
211
182
212
expect ( onSubmit ) . toHaveBeenCalledWith ( {
183
213
'field-1' : 'dontshow' ,
184
214
'field-2' : 'someValue'
185
215
} ) ;
186
-
187
- //Reset
188
- wrapper
189
- . find ( 'button' )
190
- . at ( 1 )
191
- . simulate ( 'click' ) ;
216
+ onSubmit . mockClear ( ) ;
217
+
218
+ await act ( async ( ) => {
219
+ //Reset
220
+ wrapper
221
+ . find ( 'button' )
222
+ . at ( 1 )
223
+ . simulate ( 'click' ) ;
224
+ jest . advanceTimersByTime ( 1 ) ;
225
+ } ) ;
192
226
wrapper . update ( ) ;
193
227
194
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
228
+ await act ( async ( ) => {
229
+ wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
230
+ } ) ;
231
+ wrapper . update ( ) ;
195
232
196
233
expect ( onSubmit ) . toHaveBeenCalledWith ( {
197
234
'field-1' : 'show' ,
198
235
'field-2' : 'someValue'
199
236
} ) ;
200
237
} ) ;
201
238
202
- it ( 'sets value when condition is fulfill - sequence' , ( ) => {
239
+ it ( 'sets value when condition is fulfill - sequence' , async ( ) => {
203
240
schema = {
204
241
fields : [
205
242
{
@@ -245,14 +282,22 @@ describe('condition test', () => {
245
282
]
246
283
} ;
247
284
248
- wrapper = mount ( < FormRenderer { ...initialProps } schema = { schema } /> ) ;
285
+ await act ( async ( ) => {
286
+ wrapper = mount ( < FormRenderer { ...initialProps } schema = { schema } /> ) ;
287
+ } ) ;
249
288
250
289
expect ( wrapper . find ( 'input' ) ) . toHaveLength ( 1 ) ;
251
290
252
- wrapper . find ( 'input' ) . simulate ( 'change' , { target : { value : 'show' } } ) ;
291
+ await act ( async ( ) => {
292
+ wrapper . find ( 'input' ) . simulate ( 'change' , { target : { value : 'show' } } ) ;
293
+ jest . advanceTimersByTime ( 1 ) ;
294
+ } ) ;
253
295
wrapper . update ( ) ;
254
296
255
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
297
+ await act ( async ( ) => {
298
+ wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
299
+ } ) ;
300
+ wrapper . update ( ) ;
256
301
257
302
expect ( onSubmit ) . toHaveBeenCalledWith ( {
258
303
'field-1' : 'show' ,
0 commit comments