1
1
import React from 'react' ;
2
- import { render } from './utils' ;
2
+ import { render , waitFor } from './utils' ;
3
3
4
4
import CSSTransition from '../src/CSSTransition' ;
5
5
import TransitionGroup from '../src/TransitionGroup' ;
@@ -36,8 +36,9 @@ describe('CSSTransition', () => {
36
36
} ) ;
37
37
38
38
describe ( 'entering' , ( ) => {
39
- it ( 'should apply classes at each transition state' , ( done ) => {
39
+ it ( 'should apply classes at each transition state' , async ( ) => {
40
40
let count = 0 ;
41
+ let done = false ;
41
42
const nodeRef = React . createRef ( ) ;
42
43
const { setProps } = render (
43
44
< CSSTransition nodeRef = { nodeRef } timeout = { 10 } classNames = "test" >
@@ -63,12 +64,16 @@ describe('CSSTransition', () => {
63
64
onEntered ( ) {
64
65
expect ( nodeRef . current . className ) . toEqual ( 'test-enter-done' ) ;
65
66
expect ( count ) . toEqual ( 2 ) ;
66
- done ( ) ;
67
+ done = true ;
67
68
} ,
68
69
} ) ;
70
+
71
+ await waitFor ( ( ) => {
72
+ expect ( done ) . toBe ( true ) ;
73
+ } ) ;
69
74
} ) ;
70
75
71
- it ( 'should apply custom classNames names' , ( done ) => {
76
+ it ( 'should apply custom classNames names' , async ( ) => {
72
77
let count = 0 ;
73
78
const nodeRef = React . createRef ( ) ;
74
79
const { setProps } = render (
@@ -102,15 +107,17 @@ describe('CSSTransition', () => {
102
107
103
108
onEntered ( ) {
104
109
expect ( nodeRef . current . className ) . toEqual ( 'custom-super-done' ) ;
105
- expect ( count ) . toEqual ( 2 ) ;
106
- done ( ) ;
107
110
} ,
108
111
} ) ;
112
+
113
+ await waitFor ( ( ) => {
114
+ expect ( count ) . toEqual ( 2 ) ;
115
+ } ) ;
109
116
} ) ;
110
117
} ) ;
111
118
112
119
describe ( 'appearing' , ( ) => {
113
- it ( 'should apply appear classes at each transition state' , ( done ) => {
120
+ it ( 'should apply appear classes at each transition state' , async ( ) => {
114
121
let count = 0 ;
115
122
const nodeRef = React . createRef ( ) ;
116
123
render (
@@ -137,61 +144,68 @@ describe('CSSTransition', () => {
137
144
expect ( nodeRef . current . className ) . toEqual (
138
145
'appear-test-appear-done appear-test-enter-done'
139
146
) ;
140
- expect ( count ) . toEqual ( 2 ) ;
141
- done ( ) ;
142
147
} }
143
148
>
144
149
< div ref = { nodeRef } />
145
150
</ CSSTransition >
146
151
) ;
152
+
153
+ await waitFor ( ( ) => {
154
+ expect ( count ) . toEqual ( 2 ) ;
155
+ } ) ;
147
156
} ) ;
148
157
149
158
it ( 'should lose the "*-appear-done" class after leaving and entering again' , async ( ) => {
150
159
const nodeRef = React . createRef ( ) ;
151
- let handleEntered ;
152
- let onEntered = new Promise ( ( resolve ) => {
153
- handleEntered = resolve ;
154
- } ) ;
155
- let handleExited ;
156
- const onExited = new Promise ( ( resolve ) => {
157
- handleExited = resolve ;
158
- } ) ;
160
+ let entered = false ;
161
+ let exited = false ;
159
162
const { setProps } = render (
160
163
< CSSTransition
161
164
timeout = { 10 }
162
165
nodeRef = { nodeRef }
163
166
classNames = "appear-test"
164
167
in = { true }
165
168
appear = { true }
166
- onEntered = { handleEntered }
169
+ onEntered = { ( ) => {
170
+ entered = true ;
171
+ } }
167
172
>
168
173
< div ref = { nodeRef } />
169
174
</ CSSTransition >
170
175
) ;
171
176
172
- await onEntered ;
177
+ await waitFor ( ( ) => {
178
+ expect ( entered ) . toEqual ( true ) ;
179
+ } ) ;
173
180
setProps ( {
174
181
in : false ,
175
182
onEntered : ( ) => { } ,
176
- onExited : handleExited ,
183
+ onExited : ( ) => {
184
+ exited = true ;
185
+ } ,
177
186
} ) ;
178
187
179
- await onExited ;
180
- expect ( nodeRef . current . className ) . toBe ( 'appear-test-exit-done' ) ;
181
- onEntered = new Promise ( ( resolve ) => {
182
- handleEntered = resolve ;
188
+ await waitFor ( ( ) => {
189
+ expect ( exited ) . toEqual ( true ) ;
183
190
} ) ;
191
+ expect ( nodeRef . current . className ) . toBe ( 'appear-test-exit-done' ) ;
192
+ entered = false ;
184
193
setProps ( {
185
194
in : true ,
186
- onEntered : handleEntered ,
195
+ onEntered : ( ) => {
196
+ entered = true ;
197
+ } ,
187
198
} ) ;
188
199
189
- await onEntered ;
200
+ await waitFor ( ( ) => {
201
+ expect ( entered ) . toEqual ( true ) ;
202
+ } ) ;
190
203
expect ( nodeRef . current . className ) . toBe ( 'appear-test-enter-done' ) ;
191
204
} ) ;
192
205
193
- it ( 'should not add undefined when appearDone is not defined' , ( done ) => {
206
+ it ( 'should not add undefined when appearDone is not defined' , async ( ) => {
194
207
const nodeRef = React . createRef ( ) ;
208
+ let done = false ;
195
209
render (
196
210
< CSSTransition
197
211
timeout = { 10 }
@@ -206,15 +220,19 @@ describe('CSSTransition', () => {
206
220
onEntered = { ( isAppearing ) => {
207
221
expect ( isAppearing ) . toEqual ( true ) ;
208
222
expect ( nodeRef . current . className ) . toEqual ( '' ) ;
209
- done ( ) ;
223
+ done = true ;
210
224
} }
211
225
>
212
226
< div ref = { nodeRef } />
213
227
</ CSSTransition >
214
228
) ;
229
+
230
+ await waitFor ( ( ) => {
231
+ expect ( done ) . toEqual ( true ) ;
232
+ } ) ;
215
233
} ) ;
216
234
217
- it ( 'should not be appearing in normal enter mode' , ( done ) => {
235
+ it ( 'should not be appearing in normal enter mode' , async ( ) => {
218
236
let count = 0 ;
219
237
const nodeRef = React . createRef ( ) ;
220
238
render (
@@ -248,10 +266,12 @@ describe('CSSTransition', () => {
248
266
expect ( nodeRef . current . className ) . toEqual (
249
267
'not-appear-test-enter-done'
250
268
) ;
251
- expect ( count ) . toEqual ( 2 ) ;
252
- done ( ) ;
253
269
} ,
254
270
} ) ;
271
+
272
+ await waitFor ( ( ) => {
273
+ expect ( count ) . toEqual ( 2 ) ;
274
+ } ) ;
255
275
} ) ;
256
276
257
277
it ( 'should not enter the transition states when appear=false' , ( ) => {
@@ -280,7 +300,7 @@ describe('CSSTransition', () => {
280
300
} ) ;
281
301
282
302
describe ( 'exiting' , ( ) => {
283
- it ( 'should apply classes at each transition state' , ( done ) => {
303
+ it ( 'should apply classes at each transition state' , async ( ) => {
284
304
let count = 0 ;
285
305
const nodeRef = React . createRef ( ) ;
286
306
const { setProps } = render (
@@ -306,13 +326,15 @@ describe('CSSTransition', () => {
306
326
307
327
onExited ( ) {
308
328
expect ( nodeRef . current . className ) . toEqual ( 'test-exit-done' ) ;
309
- expect ( count ) . toEqual ( 2 ) ;
310
- done ( ) ;
311
329
} ,
312
330
} ) ;
331
+
332
+ await waitFor ( ( ) => {
333
+ expect ( count ) . toEqual ( 2 ) ;
334
+ } ) ;
313
335
} ) ;
314
336
315
- it ( 'should apply custom classNames names' , ( done ) => {
337
+ it ( 'should apply custom classNames names' , async ( ) => {
316
338
let count = 0 ;
317
339
const nodeRef = React . createRef ( ) ;
318
340
const { setProps } = render (
@@ -347,13 +369,15 @@ describe('CSSTransition', () => {
347
369
348
370
onExited ( ) {
349
371
expect ( nodeRef . current . className ) . toEqual ( 'custom-super-done' ) ;
350
- expect ( count ) . toEqual ( 2 ) ;
351
- done ( ) ;
352
372
} ,
353
373
} ) ;
374
+
375
+ await waitFor ( ( ) => {
376
+ expect ( count ) . toEqual ( 2 ) ;
377
+ } ) ;
354
378
} ) ;
355
379
356
- it ( 'should support empty prefix' , ( done ) => {
380
+ it ( 'should support empty prefix' , async ( ) => {
357
381
let count = 0 ;
358
382
359
383
const nodeRef = React . createRef ( ) ;
@@ -378,10 +402,12 @@ describe('CSSTransition', () => {
378
402
379
403
onExited ( ) {
380
404
expect ( nodeRef . current . className ) . toEqual ( 'exit-done' ) ;
381
- expect ( count ) . toEqual ( 2 ) ;
382
- done ( ) ;
383
405
} ,
384
406
} ) ;
407
+
408
+ await waitFor ( ( ) => {
409
+ expect ( count ) . toEqual ( 2 ) ;
410
+ } ) ;
385
411
} ) ;
386
412
} ) ;
387
413
@@ -423,20 +449,7 @@ describe('CSSTransition', () => {
423
449
< Test direction = "down" text = "foo" nodeRef = { nodeRef . foo } />
424
450
) ;
425
451
426
- const rerender = ( getProps ) =>
427
- new Promise ( ( resolve ) =>
428
- setProps ( {
429
- onEnter : undefined ,
430
- onEntering : undefined ,
431
- onEntered : undefined ,
432
- onExit : undefined ,
433
- onExiting : undefined ,
434
- onExited : undefined ,
435
- ...getProps ( resolve ) ,
436
- } )
437
- ) ;
438
-
439
- await rerender ( ( resolve ) => ( {
452
+ setProps ( {
440
453
direction : 'up' ,
441
454
text : 'bar' ,
442
455
nodeRef : nodeRef . bar ,
@@ -450,11 +463,14 @@ describe('CSSTransition', () => {
450
463
expect ( nodeRef . bar . current . className ) . toEqual (
451
464
'up-enter up-enter-active'
452
465
) ;
453
- resolve ( ) ;
454
466
} ,
455
- } ) ) ;
467
+ } ) ;
456
468
457
- await rerender ( ( resolve ) => ( {
469
+ await waitFor ( ( ) => {
470
+ expect ( count ) . toEqual ( 2 ) ;
471
+ } ) ;
472
+
473
+ setProps ( {
458
474
direction : 'down' ,
459
475
text : 'foo' ,
460
476
nodeRef : nodeRef . foo ,
@@ -468,11 +484,12 @@ describe('CSSTransition', () => {
468
484
onEntered ( ) {
469
485
count ++ ;
470
486
expect ( nodeRef . foo . current . className ) . toEqual ( 'down-enter-done' ) ;
471
- resolve ( ) ;
472
487
} ,
473
- } ) ) ;
488
+ } ) ;
474
489
475
- expect ( count ) . toEqual ( 4 ) ;
490
+ await waitFor ( ( ) => {
491
+ expect ( count ) . toEqual ( 4 ) ;
492
+ } ) ;
476
493
} ) ;
477
494
} ) ;
478
495
} ) ;
0 commit comments