1
+ import { act } from '@testing-library/react' ;
1
2
import { runAfterTransition } from '../src/runAfterTransition' ;
2
3
3
4
class MockTransitionEvent extends Event {
@@ -20,15 +21,13 @@ describe('runAfterTransition', () => {
20
21
afterAll ( ( ) => {
21
22
global . TransitionEvent = originalTransitionEvent ;
22
23
} ) ;
23
-
24
+
24
25
beforeEach ( ( ) => {
25
- jest
26
- . spyOn ( window , 'requestAnimationFrame' )
27
- // @ts -expect-error -- mock
28
- . mockImplementation ( ( cb ) => cb ( ) ) ;
26
+ jest . useFakeTimers ( ) ;
29
27
} ) ;
30
28
31
29
afterEach ( ( ) => {
30
+ jest . useRealTimers ( ) ;
32
31
jest . restoreAllMocks ( ) ;
33
32
cleanupElements ( ) ;
34
33
} ) ;
@@ -49,6 +48,7 @@ describe('runAfterTransition', () => {
49
48
it ( 'calls callback immediately when no transition is running' , ( ) => {
50
49
const callback = jest . fn ( ) ;
51
50
runAfterTransition ( callback ) ;
51
+ act ( ( ) => { jest . runOnlyPendingTimers ( ) ; } ) ;
52
52
expect ( callback ) . toHaveBeenCalled ( ) ;
53
53
} ) ;
54
54
@@ -64,7 +64,10 @@ describe('runAfterTransition', () => {
64
64
} )
65
65
) ;
66
66
67
+
67
68
runAfterTransition ( callback ) ;
69
+ act ( ( ) => { jest . runOnlyPendingTimers ( ) ; } ) ;
70
+
68
71
// Callback should not be called immediately since a transition is active.
69
72
expect ( callback ) . not . toHaveBeenCalled ( ) ;
70
73
@@ -91,7 +94,9 @@ describe('runAfterTransition', () => {
91
94
) ;
92
95
93
96
runAfterTransition ( callback1 ) ;
97
+ act ( ( ) => { jest . runOnlyPendingTimers ( ) ; } ) ;
94
98
runAfterTransition ( callback2 ) ;
99
+ act ( ( ) => { jest . runOnlyPendingTimers ( ) ; } ) ;
95
100
// Callbacks should not be called during transition.
96
101
expect ( callback1 ) . not . toHaveBeenCalled ( ) ;
97
102
expect ( callback2 ) . not . toHaveBeenCalled ( ) ;
@@ -123,6 +128,7 @@ describe('runAfterTransition', () => {
123
128
cleanupElements ( ) ;
124
129
125
130
runAfterTransition ( callback ) ;
131
+ act ( ( ) => { jest . runOnlyPendingTimers ( ) ; } ) ;
126
132
127
133
expect ( callback ) . toHaveBeenCalled ( ) ;
128
134
} ) ;
0 commit comments