1
+ import { act } from '@testing-library/react' ;
1
2
import { runAfterTransition } from '../src/runAfterTransition' ;
2
3
3
4
class MockTransitionEvent extends Event {
@@ -22,10 +23,7 @@ describe('runAfterTransition', () => {
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 ( ( ) => {
@@ -49,6 +47,7 @@ describe('runAfterTransition', () => {
49
47
it ( 'calls callback immediately when no transition is running' , ( ) => {
50
48
const callback = jest . fn ( ) ;
51
49
runAfterTransition ( callback ) ;
50
+ act ( ( ) => { jest . runOnlyPendingTimers ( ) ; } ) ;
52
51
expect ( callback ) . toHaveBeenCalled ( ) ;
53
52
} ) ;
54
53
@@ -64,7 +63,10 @@ describe('runAfterTransition', () => {
64
63
} )
65
64
) ;
66
65
66
+
67
67
runAfterTransition ( callback ) ;
68
+ act ( ( ) => { jest . runOnlyPendingTimers ( ) ; } ) ;
69
+
68
70
// Callback should not be called immediately since a transition is active.
69
71
expect ( callback ) . not . toHaveBeenCalled ( ) ;
70
72
@@ -91,7 +93,9 @@ describe('runAfterTransition', () => {
91
93
) ;
92
94
93
95
runAfterTransition ( callback1 ) ;
96
+ act ( ( ) => { jest . runOnlyPendingTimers ( ) ; } ) ;
94
97
runAfterTransition ( callback2 ) ;
98
+ act ( ( ) => { jest . runOnlyPendingTimers ( ) ; } ) ;
95
99
// Callbacks should not be called during transition.
96
100
expect ( callback1 ) . not . toHaveBeenCalled ( ) ;
97
101
expect ( callback2 ) . not . toHaveBeenCalled ( ) ;
@@ -123,6 +127,7 @@ describe('runAfterTransition', () => {
123
127
cleanupElements ( ) ;
124
128
125
129
runAfterTransition ( callback ) ;
130
+ act ( ( ) => { jest . runOnlyPendingTimers ( ) ; } ) ;
126
131
127
132
expect ( callback ) . toHaveBeenCalled ( ) ;
128
133
} ) ;
0 commit comments