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,17 +144,21 @@ 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
- it ( 'should lose the "*-appear-done" class after leaving and entering again' , ( done ) => {
158
+ it ( 'should lose the "*-appear-done" class after leaving and entering again' , async ( ) => {
150
159
const nodeRef = React . createRef ( ) ;
160
+ let entered = false ;
161
+ let exited = false ;
151
162
const { setProps } = render (
152
163
< CSSTransition
153
164
timeout = { 10 }
@@ -156,31 +167,45 @@ describe('CSSTransition', () => {
156
167
in = { true }
157
168
appear = { true }
158
169
onEntered = { ( ) => {
159
- setProps ( {
160
- in : false ,
161
- onEntered : ( ) => { } ,
162
- onExited : ( ) => {
163
- expect ( nodeRef . current . className ) . toBe ( 'appear-test-exit-done' ) ;
164
- setProps ( {
165
- in : true ,
166
- onEntered : ( ) => {
167
- expect ( nodeRef . current . className ) . toBe (
168
- 'appear-test-enter-done'
169
- ) ;
170
- done ( ) ;
171
- } ,
172
- } ) ;
173
- } ,
174
- } ) ;
170
+ entered = true ;
175
171
} }
176
172
>
177
173
< div ref = { nodeRef } />
178
174
</ CSSTransition >
179
175
) ;
176
+
177
+ await waitFor ( ( ) => {
178
+ expect ( entered ) . toEqual ( true ) ;
179
+ } ) ;
180
+ setProps ( {
181
+ in : false ,
182
+ onEntered : ( ) => { } ,
183
+ onExited : ( ) => {
184
+ exited = true ;
185
+ } ,
186
+ } ) ;
187
+
188
+ await waitFor ( ( ) => {
189
+ expect ( exited ) . toEqual ( true ) ;
190
+ } ) ;
191
+ expect ( nodeRef . current . className ) . toBe ( 'appear-test-exit-done' ) ;
192
+ entered = false ;
193
+ setProps ( {
194
+ in : true ,
195
+ onEntered : ( ) => {
196
+ entered = true ;
197
+ } ,
198
+ } ) ;
199
+
200
+ await waitFor ( ( ) => {
201
+ expect ( entered ) . toEqual ( true ) ;
202
+ } ) ;
203
+ expect ( nodeRef . current . className ) . toBe ( 'appear-test-enter-done' ) ;
180
204
} ) ;
181
205
182
- it ( 'should not add undefined when appearDone is not defined' , ( done ) => {
206
+ it ( 'should not add undefined when appearDone is not defined' , async ( ) => {
183
207
const nodeRef = React . createRef ( ) ;
208
+ let done = false ;
184
209
render (
185
210
< CSSTransition
186
211
timeout = { 10 }
@@ -195,15 +220,19 @@ describe('CSSTransition', () => {
195
220
onEntered = { ( isAppearing ) => {
196
221
expect ( isAppearing ) . toEqual ( true ) ;
197
222
expect ( nodeRef . current . className ) . toEqual ( '' ) ;
198
- done ( ) ;
223
+ done = true ;
199
224
} }
200
225
>
201
226
< div ref = { nodeRef } />
202
227
</ CSSTransition >
203
228
) ;
229
+
230
+ await waitFor ( ( ) => {
231
+ expect ( done ) . toEqual ( true ) ;
232
+ } ) ;
204
233
} ) ;
205
234
206
- it ( 'should not be appearing in normal enter mode' , ( done ) => {
235
+ it ( 'should not be appearing in normal enter mode' , async ( ) => {
207
236
let count = 0 ;
208
237
const nodeRef = React . createRef ( ) ;
209
238
render (
@@ -237,10 +266,12 @@ describe('CSSTransition', () => {
237
266
expect ( nodeRef . current . className ) . toEqual (
238
267
'not-appear-test-enter-done'
239
268
) ;
240
- expect ( count ) . toEqual ( 2 ) ;
241
- done ( ) ;
242
269
} ,
243
270
} ) ;
271
+
272
+ await waitFor ( ( ) => {
273
+ expect ( count ) . toEqual ( 2 ) ;
274
+ } ) ;
244
275
} ) ;
245
276
246
277
it ( 'should not enter the transition states when appear=false' , ( ) => {
@@ -269,7 +300,7 @@ describe('CSSTransition', () => {
269
300
} ) ;
270
301
271
302
describe ( 'exiting' , ( ) => {
272
- it ( 'should apply classes at each transition state' , ( done ) => {
303
+ it ( 'should apply classes at each transition state' , async ( ) => {
273
304
let count = 0 ;
274
305
const nodeRef = React . createRef ( ) ;
275
306
const { setProps } = render (
@@ -295,13 +326,15 @@ describe('CSSTransition', () => {
295
326
296
327
onExited ( ) {
297
328
expect ( nodeRef . current . className ) . toEqual ( 'test-exit-done' ) ;
298
- expect ( count ) . toEqual ( 2 ) ;
299
- done ( ) ;
300
329
} ,
301
330
} ) ;
331
+
332
+ await waitFor ( ( ) => {
333
+ expect ( count ) . toEqual ( 2 ) ;
334
+ } ) ;
302
335
} ) ;
303
336
304
- it ( 'should apply custom classNames names' , ( done ) => {
337
+ it ( 'should apply custom classNames names' , async ( ) => {
305
338
let count = 0 ;
306
339
const nodeRef = React . createRef ( ) ;
307
340
const { setProps } = render (
@@ -336,13 +369,15 @@ describe('CSSTransition', () => {
336
369
337
370
onExited ( ) {
338
371
expect ( nodeRef . current . className ) . toEqual ( 'custom-super-done' ) ;
339
- expect ( count ) . toEqual ( 2 ) ;
340
- done ( ) ;
341
372
} ,
342
373
} ) ;
374
+
375
+ await waitFor ( ( ) => {
376
+ expect ( count ) . toEqual ( 2 ) ;
377
+ } ) ;
343
378
} ) ;
344
379
345
- it ( 'should support empty prefix' , ( done ) => {
380
+ it ( 'should support empty prefix' , async ( ) => {
346
381
let count = 0 ;
347
382
348
383
const nodeRef = React . createRef ( ) ;
@@ -367,10 +402,12 @@ describe('CSSTransition', () => {
367
402
368
403
onExited ( ) {
369
404
expect ( nodeRef . current . className ) . toEqual ( 'exit-done' ) ;
370
- expect ( count ) . toEqual ( 2 ) ;
371
- done ( ) ;
372
405
} ,
373
406
} ) ;
407
+
408
+ await waitFor ( ( ) => {
409
+ expect ( count ) . toEqual ( 2 ) ;
410
+ } ) ;
374
411
} ) ;
375
412
} ) ;
376
413
@@ -412,20 +449,7 @@ describe('CSSTransition', () => {
412
449
< Test direction = "down" text = "foo" nodeRef = { nodeRef . foo } />
413
450
) ;
414
451
415
- const rerender = ( getProps ) =>
416
- new Promise ( ( resolve ) =>
417
- setProps ( {
418
- onEnter : undefined ,
419
- onEntering : undefined ,
420
- onEntered : undefined ,
421
- onExit : undefined ,
422
- onExiting : undefined ,
423
- onExited : undefined ,
424
- ...getProps ( resolve ) ,
425
- } )
426
- ) ;
427
-
428
- await rerender ( ( resolve ) => ( {
452
+ setProps ( {
429
453
direction : 'up' ,
430
454
text : 'bar' ,
431
455
nodeRef : nodeRef . bar ,
@@ -439,11 +463,14 @@ describe('CSSTransition', () => {
439
463
expect ( nodeRef . bar . current . className ) . toEqual (
440
464
'up-enter up-enter-active'
441
465
) ;
442
- resolve ( ) ;
443
466
} ,
444
- } ) ) ;
467
+ } ) ;
468
+
469
+ await waitFor ( ( ) => {
470
+ expect ( count ) . toEqual ( 2 ) ;
471
+ } ) ;
445
472
446
- await rerender ( ( resolve ) => ( {
473
+ setProps ( {
447
474
direction : 'down' ,
448
475
text : 'foo' ,
449
476
nodeRef : nodeRef . foo ,
@@ -457,11 +484,12 @@ describe('CSSTransition', () => {
457
484
onEntered ( ) {
458
485
count ++ ;
459
486
expect ( nodeRef . foo . current . className ) . toEqual ( 'down-enter-done' ) ;
460
- resolve ( ) ;
461
487
} ,
462
- } ) ) ;
488
+ } ) ;
463
489
464
- expect ( count ) . toEqual ( 4 ) ;
490
+ await waitFor ( ( ) => {
491
+ expect ( count ) . toEqual ( 4 ) ;
492
+ } ) ;
465
493
} ) ;
466
494
} ) ;
467
495
} ) ;
0 commit comments